/** * Returns list of inspector methods in a given class. * An inspector is a cheap-pure method with no arguments. * * @param className A class name */ public static List<String> getInspectors(String className) { List<String> pureMethods = CheapPurityAnalyzer.getInstance().getPureMethods(className); List<String> inspectors = new ArrayList<>(); for (String pm : pureMethods) { if ((Type.getArgumentTypes(pm.substring(pm.indexOf('('))).length == 0) && ! (pm.substring(0, pm.indexOf("(")).equals("<clinit>"))) inspectors.add(pm); } return inspectors; } }
private boolean isPure(MethodEntry entry) { Stack<MethodEntry> emptyStack = new Stack<MethodEntry>(); return isPure(entry, emptyStack); }
private boolean isPure(MethodEntry entry, Stack<MethodEntry> callStack) { if (isCached(entry)) { return getCacheValue(entry); } else { boolean isPure = isPure0(entry, callStack); addCacheValue(entry, isPure); return isPure; } }
@Override public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { String targetClassName = owner.replace('/', '.'); if (targetClassName.equals(org.evosuite.runtime.Random.class.getCanonicalName()) || !BytecodeInstrumentation.checkIfEvoSuitePackage(targetClassName)) { //Only ignore EvoSuite callbacks if (opcode == Opcodes.INVOKESTATIC) { this.purityAnalyzer.addStaticCall(classNameWithDots, methodName, descriptor, targetClassName, name, desc); } else if (opcode == Opcodes.INVOKEVIRTUAL) { this.purityAnalyzer.addVirtualCall(classNameWithDots, methodName, descriptor, targetClassName, name, desc); } else if (opcode == Opcodes.INVOKEINTERFACE) { this.purityAnalyzer.addInterfaceCall(classNameWithDots, methodName, descriptor, targetClassName, name, desc); } else if (opcode == Opcodes.INVOKESPECIAL) { this.purityAnalyzer.addSpecialCall(classNameWithDots, methodName, descriptor, targetClassName, name, desc); } } super.visitMethodInsn(opcode, owner, name, desc, itf); } }
if (!CheapPurityAnalyzer.getInstance().isPure(method)) { return false;
/** {@inheritDoc} */ @Override public MethodVisitor visitMethod(int methodAccess, String name, String descriptor, String signature, String[] exceptions) { if (visitingInterface == true) { purityAnalyzer.addInterfaceMethod(className.replace('/', '.'), name, descriptor); } else { purityAnalyzer.addMethod(className.replace('/', '.'), name, descriptor); if ((methodAccess & Opcodes.ACC_ABSTRACT) != Opcodes.ACC_ABSTRACT) { purityAnalyzer.addMethodWithBody(className.replace('/', '.'), name, descriptor); } else { // The declaration of this method is abstract. So // there is no method body for this method in this class } } MethodVisitor mv = super.visitMethod(methodAccess, name, descriptor, signature, exceptions); PurityAnalysisMethodVisitor purityAnalysisMethodVisitor = new PurityAnalysisMethodVisitor( className, name, descriptor, mv, purityAnalyzer); MethodEntry methodEntry = new MethodEntry(className, name, descriptor); this.method_adapters.put(methodEntry, purityAnalysisMethodVisitor); return purityAnalysisMethodVisitor; }
CheapPurityAnalyzer purityAnalyzer = CheapPurityAnalyzer.getInstance(); cv = new PurityAnalysisClassVisitor(cv, className, purityAnalyzer);
public void addStaticCall(String sourceClassName, String sourceMethodName, String sourceDescriptor, String targetClassName, String targetMethodName, String targetDescriptor) { addCall(staticCalls, sourceClassName, sourceMethodName, sourceDescriptor, targetClassName, targetMethodName, targetDescriptor); }
String methodName = "equals"; String descriptor = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class)); CheapPurityAnalyzer cheapPurityAnalyzer = CheapPurityAnalyzer.getInstance(); if (!cheapPurityAnalyzer.isPure(className, methodName, descriptor)) continue; //Don't compare using impure equals(Object) methods
public void addSpecialCall(String sourceClassName, String sourceMethodName, String sourceDescriptor, String targetClassName, String targetMethodName, String targetDescriptor) { addCall(specialCalls, sourceClassName, sourceMethodName, sourceDescriptor, targetClassName, targetMethodName, targetDescriptor); }
if (!CheapPurityAnalyzer.getInstance().isPure(method)) { cluster.addModifier(new GenericClass(clazz), genericMethod);
public List<String> getPureMethods(String className) { ArrayList<String> list = new ArrayList<>(); for (MethodEntry m : methodEntries) { if (m.className.equals(className) && isPure(m) && m.methodName != ClassResetter.STATIC_RESET) { list.add(m.methodName + m.descriptor); } } return list; }
public void addInterfaceCall(String sourceClassName, String sourceMethodName, String sourceDescriptor, String targetClassName, String targetMethodName, String targetDescriptor) { addCall(interfaceCalls, sourceClassName, sourceMethodName, sourceDescriptor, targetClassName, targetMethodName, targetDescriptor); }
cluster.addModifier(new GenericClass(clazz), genericMethod); } else { if (!CheapPurityAnalyzer.getInstance().isPure(method)) { cluster.addModifier(new GenericClass(clazz), genericMethod);
private boolean checkAnyCallImpure(Set<MethodEntry> calls, MethodEntry entry, Stack<MethodEntry> callStack) { for (MethodEntry callMethodEntry : calls) { if (!callStack.contains(callMethodEntry)) { Stack<MethodEntry> copyOfStack = new Stack<MethodEntry>(); copyOfStack.addAll(callStack); copyOfStack.add(entry); if (!isPure(callMethodEntry, copyOfStack)) { return true; } } } return false; }
public void addVirtualCall(String sourceClassName, String sourceMethodName, String sourceDescriptor, String targetClassName, String targetMethodName, String targetDescriptor) { addCall(virtualCalls, sourceClassName, sourceMethodName, sourceDescriptor, targetClassName, targetMethodName, targetDescriptor); }
@Test public void testRandom() { EvoSuite evosuite = new EvoSuite(); String targetClass = RandomUser.class.getCanonicalName(); Properties.TARGET_CLASS = targetClass; Properties.OUTPUT_VARIABLES=""+RuntimeVariable.HadUnstableTests; String[] command = new String[] { "-generateSuite", "-class", targetClass }; Object result = evosuite.parseCommandLine(command); GeneticAlgorithm<?> ga = getGAFromResult(result); TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual(); System.out.println("EvolvedTestSuite:\n" + best); CheapPurityAnalyzer purityAnalyzer = CheapPurityAnalyzer.getInstance(); String descriptor = Type.getMethodDescriptor(Type.INT_TYPE); boolean nextInt = purityAnalyzer.isPure(targetClass, "nextInt", descriptor); assertFalse(nextInt); boolean randomNextInt = purityAnalyzer.isPure(java.util.Random.class.getCanonicalName(), "nextInt", descriptor); assertFalse(randomNextInt); boolean secureRandomNextInt = purityAnalyzer.isPure(java.security.SecureRandom.class.getCanonicalName(), "nextInt", descriptor); assertFalse(secureRandomNextInt ); Map<String, OutputVariable<?>> map = DebugStatisticsBackend.getLatestWritten(); Assert.assertNotNull(map); OutputVariable unstable = map.get(RuntimeVariable.HadUnstableTests.toString()); Assert.assertNotNull(unstable); Assert.assertEquals(Boolean.FALSE, unstable.getValue()); }
/** * Returns if the method is cheap-pure. * * @param className The declaring class name * @param methodName The method name * @param descriptor The method descriptor * @return true if the method is cheap-pure, false otherwise */ public boolean isPure(String className, String methodName, String descriptor) { MethodEntry entry = new MethodEntry(className, methodName, descriptor); return isPure(entry); }
CheapPurityAnalyzer purityAnalyzer = CheapPurityAnalyzer.getInstance(); boolean greaterthanZeroIsPure = purityAnalyzer.isPure(targetClass, "greaterThanZero", descriptor); assertTrue(greaterthanZeroIsPure); boolean notPureGreaterthanZeroIsPure = purityAnalyzer.isPure( targetClass, "notPureGreaterThanZero", descriptor); assertFalse(notPureGreaterthanZeroIsPure); boolean notPureCreationOfObjectIsPure = purityAnalyzer.isPure( targetClass, "notPureCreationOfObject", descriptor); assertFalse(notPureCreationOfObjectIsPure); boolean pureCreationOfObjectIsPure = purityAnalyzer.isPure(targetClass, "pureCreationOfObject", descriptor); assertFalse(pureCreationOfObjectIsPure); boolean superPureCall = purityAnalyzer.isPure(targetClass, "superPureCall", descriptor); assertTrue(superPureCall); boolean notPureGreaterThanZero = purityAnalyzer.isPure( AbstractInspector.class.getCanonicalName(), "notPureGreaterThanZero", descriptor); assertFalse(notPureGreaterThanZero); boolean superNotPureCall = purityAnalyzer.isPure(targetClass, "superNotPureCall", descriptor);
/** * Returns if a Method is <code>cheap-pure</code> * @param method * @return true if the method is cheap-pure, otherwise false. */ public boolean isPure(java.lang.reflect.Method method) { // Using getName rather than getCanonicalName because that's what // the inheritancetree also uses String className = method.getDeclaringClass().getName(); if (MockList.isAMockClass(className)) { className = method.getDeclaringClass().getSuperclass().getName(); } String methodName = method.getName(); String descriptor = Type.getMethodDescriptor(method); MethodEntry entry = new MethodEntry(className, methodName, descriptor); boolean isPureValue = isPure(entry); return isPureValue; }