//
// Copyright (C) 2012 United States Government as represented by the
// Administrator of the National Aeronautics and Space Administration
// (NASA). All Rights Reserved.
//
// This software is distributed under the NASA Open Source Agreement
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
// directory tree for the complete NOSA document.
//
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
//
package gov.nasa.jpf.conformanceChecker.checkers;
import gov.nasa.jpf.JPF;
import gov.nasa.jpf.conformanceChecker.Inconsistency;
import gov.nasa.jpf.conformanceChecker.Inconsistency.InconsistencyType;
import gov.nasa.jpf.conformanceChecker.comparators.MangledMethodInfoComparator;
import gov.nasa.jpf.conformanceChecker.comparators.MethodInfoComparator;
import gov.nasa.jpf.conformanceChecker.providers.ModelClassProvider;
import gov.nasa.jpf.conformanceChecker.providers.NativePeerProvider;
import gov.nasa.jpf.conformanceChecker.providers.ProviderFactory;
import gov.nasa.jpf.conformanceChecker.providers.StandardClassProvider;
import gov.nasa.jpf.conformanceChecker.reporters.Reporter;
import gov.nasa.jpf.conformanceChecker.reporters.SimpleCmdLineReporter;
import gov.nasa.jpf.conformanceChecker.util.MaybeVersion;
import gov.nasa.jpf.jvm.ClassInfo;
import gov.nasa.jpf.jvm.MethodInfo;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
/**
* Cheks for missing native peers inconsistencies.
*
* @see InconsistencyType#MISSING_NATIVE_PEER
*
* @author Matteo Ceccarello <matteo.ceccarello AT gmail.com>
*
*/
public class MissingNativePeerChecker extends NativePeerChecker {
NativePeerProvider provider;
public MissingNativePeerChecker(List<Reporter> reporters, MemberLevel level) {
super(reporters, level);
this.provider = ProviderFactory.getNativePeerProvider();
}
public MissingNativePeerChecker(List<Reporter> reporters) {
super(reporters);
this.provider = ProviderFactory.getNativePeerProvider();
}
public MissingNativePeerChecker() {
super();
this.provider = ProviderFactory.getNativePeerProvider();
}
@Override
public boolean check(ClassInfo cls) {
boolean noInconsistencies = true;
ClassInfo peer = provider.loadNativePeerClass(cls);
MethodInfo[] peerMethods = getSortedPeerMethods(peer);
// in the comparison during binary search, the key is in the
// second position, hence the constructor has false as parameter
MangledMethodInfoComparator comp =
new MangledMethodInfoComparator(false);
for (MethodInfo method : cls.getDeclaredMethodInfos()) {
if (Modifier.isNative(method.getModifiers())) {
if (Arrays.binarySearch(peerMethods, method, comp) < 0) {
noInconsistencies = false;
signal(new Inconsistency(
InconsistencyType.ORPHAN_NATIVE_PEER,
MaybeVersion.nothing(),
cls,
method,
"The native peer is missing for this method."));
}
}
}
return noInconsistencies;
}
public static void main(String[] args) {
ProviderFactory.init(JPF.createConfig(args));
ClassInfo cls = new ModelClassProvider().loadClassInfo("java.lang.System");
MissingNativePeerChecker mnpc = new MissingNativePeerChecker();
SimpleCmdLineReporter reporter = new SimpleCmdLineReporter(JPF.createConfig(args));
mnpc.registerReporter(reporter);
System.out.println(mnpc.check(cls));
}
}