/** * This function instantiates an invariant class by using the <type>(PptSlice) constructor. * * @param theClass the invariant class to be instantiated * @param sl the PptSlice representing the variables about which an invariant is determined * @return an instance of the class in theClass if one can be constructed, else throw a * RuntimeException */ private static Invariant instantiateClass(Class<? extends Invariant> theClass, PptSlice sl) { try { Method get_proto = theClass.getMethod("get_proto", new Class<?>[] {}); Invariant proto = (/*@Prototype*/ Invariant) get_proto.invoke(null, new Object[] {}); Invariant inv = proto.instantiate(sl); return (inv); } catch (Exception e) { e.printStackTrace(System.out); throw new RuntimeException( "Error while instantiating invariant " + theClass.getName() + ": " + e.toString()); } }
/** * Initializes the fields of this class based on the first two lines of a case which include the * class name and parameter types. * * @return true is end of file is reached. */ private static boolean initFields(LineNumberReader commands, boolean generatingCommands) { results = new StringBuffer(); String className = getNextRealLine(commands); // End of file reached if (className == null) return true; // Load the class from file Class<? extends Invariant> classToTest = asInvClass(getClass(className)); try { classToTest.getField("dkconfig_enabled"); // Enable if needs to be done InvariantAddAndCheckTester.config.apply(className + ".enabled", "true"); } catch (NoSuchFieldException e) { // Otherwise do nothing } if (generatingCommands) { results.append(className + lineSep); } // Instantiate variables to be used as the names in the // invariants, variables are labeled a,b,c and so on as they // appear String typeString = getNextRealLine(commands); types = getTypes(typeString); VarInfo[] vars = getVarInfos(classToTest, types); PptSlice sl = createSlice(vars, daikon.test.Common.makePptTopLevel("Test:::OBJECT", vars)); // Create an actual instance of the class invariantToTest = instantiateClass(classToTest, sl); addModified = getAddModified(invariantToTest.getClass()); checkModified = getCheckModified(invariantToTest.getClass()); outputProducer = getOutputProducer(invariantToTest.getClass()); assert getArity(invariantToTest.getClass()) == types.length; if (generatingCommands) { results.append(typeString + lineSep); } return false; }
/** * This function returns the check_modified method from the class type provided. * * @param theClass the class in which to find the check_modified method * @return the check_modified method if it exists * @throws RuntimeException if check_modified does not exist. */ private static Method getCheckModified(Class<? extends Invariant> theClass) { Method[] methods = theClass.getMethods(); Method currentMethod; for (int i = 0; i < methods.length; i++) { currentMethod = methods[i]; if (currentMethod.getName().lastIndexOf("check_modified") != -1) { // Method should be called check_modified return currentMethod; } } throw new RuntimeException("Cannot find check_modified method"); }
/** * @return the method of invariant named by theClass that produces a String representation of * the invariant. */ private static Method getOutputProducer(Class<? extends Invariant> theClass) { Method[] methods = theClass.getMethods(); Method currentMethod; for (int i = 0; i < methods.length; i++) { currentMethod = methods[i]; // Method should be called format_using if (currentMethod.getName().lastIndexOf("format_using") != -1) { return currentMethod; } } throw new RuntimeException("Cannot find format_using method"); }
/** @param loader The SplitterLoader used to load the compiled source. Must not be null. */ public void load(SplitterLoader loader) { Class tempClass = loader.load_Class(className, directory + className + ".class"); if (tempClass != null) { try { splitter = (Splitter) tempClass.newInstance(); } catch (ClassFormatError ce) { ce.printStackTrace(System.out); } catch (InstantiationException ie) { ie.printStackTrace(System.out); } catch (IllegalAccessException iae) { iae.printStackTrace(System.out); } DummyInvariant dummy = new DummyInvariant(null); dummy.setFormats( daikonFormat, javaFormat, escFormat, simplifyFormat, ioaFormat, jmlFormat, dbcFormat, dummyDesired); splitter.makeDummyInvariant(dummy); errorMessage = "Splitter exists " + this.toString(); exists = true; } else { errorMessage = "No class data for " + this.toString() + ", to be loaded from " + directory + className + ".class"; exists = false; } }