//
// 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.conformanceChecker.Inconsistency;
import static java.lang.reflect.Modifier.*;
import gov.nasa.jpf.conformanceChecker.Inconsistency.InconsistencyType;
import gov.nasa.jpf.conformanceChecker.comparators.FieldInfoComparator;
import gov.nasa.jpf.conformanceChecker.reporters.Reporter;
import gov.nasa.jpf.conformanceChecker.util.MaybeVersion;
import gov.nasa.jpf.jvm.FieldInfo;
import java.util.Arrays;
import java.util.List;
import javax.naming.NoInitialContextException;
/**
* Abstract class that provides common functionalities to
* ClassStateChecker and ObjectStateChecker
*
* @author Matteo Ceccarello <matteo.ceccarello AT gmail.com>
*
*/
public abstract class StateChecker extends Checker {
/**
* The mask to be used in comparisons with {@link FieldInfoComparator}
*/
private static final int mask = ~( PUBLIC | PROTECTED | PRIVATE | FINAL | TRANSIENT | VOLATILE );
public StateChecker() {
super();
}
public StateChecker(List<Reporter> reporters, MemberLevel level) {
super(reporters, level);
}
public StateChecker(List<Reporter> reporters) {
super(reporters);
}
/**
* Compares the two arrays in linear time. To be tested. Still requires n log n for sorting
*/
protected boolean compareArraysFast(FieldInfo[] modelFields, FieldInfo[] stdFields, MaybeVersion version) {
boolean noInconsistencies = true;
FieldInfoComparator cmp = new FieldInfoComparator(mask);
Arrays.sort(modelFields, cmp);
Arrays.sort(stdFields, cmp);
for(int i=0, j=0; i<modelFields.length && j<stdFields.length;) {
int result = cmp.compare(modelFields[i], stdFields[j]);
if(result == 0){
i++;
j++;
} else if (result < 0) { // modelField < stdField
signal(new Inconsistency(
getInconsistencyType(),
version,
modelFields[i].getClassInfo(),
modelFields[i],
"This field is present in the model class but not in the " +
"library version."));
i++;
noInconsistencies = false;
} else { // modelField > stdField
signal(new Inconsistency(
getInconsistencyType(),
version,
stdFields[j].getClassInfo(),
stdFields[j],
"This field is present in the library version of the class but " +
"not in the model."));
j++;
noInconsistencies = false;
}
}
return noInconsistencies;
}
protected boolean compareArrays(FieldInfo[] modelFields, FieldInfo[] stdFields, MaybeVersion version) {
boolean noInconsistencies = true;
FieldInfoComparator cmp = new FieldInfoComparator(mask);
Arrays.sort(modelFields, cmp);
Arrays.sort(stdFields, cmp);
for (FieldInfo f : stdFields) {
if (Arrays.binarySearch(modelFields, f, cmp) < 0) {
signal(new Inconsistency(
getInconsistencyType(),
version,
f.getClassInfo(),
f,
"This field is present in the library version of the class but " +
"not in the model."));
}
}
for (FieldInfo f : modelFields) {
if (Arrays.binarySearch(stdFields, f, cmp) < 0) {
signal(new Inconsistency(
getInconsistencyType(),
version,
f.getClassInfo(),
f,
"This field is present in the model class but not in the " +
"library version."));
}
}
return noInconsistencies;
}
protected abstract InconsistencyType getInconsistencyType();
}