/** * Check if we can instrument the given class * * @param className * a {@link java.lang.String} object. * @return a boolean. */ public static boolean checkIfCanInstrument(String className) { return RuntimeInstrumentation.checkIfCanInstrument(className); }
public EvoClassLoader() { this(new RuntimeInstrumentation()); }
@Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { String classWithDots = className.replace('/', '.'); if(!active || !RuntimeInstrumentation.checkIfCanInstrument(classWithDots) || classWithDots.startsWith(PackageInfo.getEvoSuitePackage())){ return classfileBuffer; } else { //ClassResetter.getInstance().setClassLoader(loader); ClassReader reader = new ClassReader(classfileBuffer); synchronized(instrumentedClasses){ instrumentedClasses.add(classWithDots); } logger.debug("Going to instrument: "+classWithDots); if(instrumenter.isAlreadyInstrumented(new ClassReader(classfileBuffer))) { logger.debug("Skipping transformation of {} as it is already instrumented", classWithDots); return classfileBuffer; } return instrumenter.transformBytes(loader, className, reader, false); // TODO: Need to set skip instrumentation for test class } }
@After public void reset(){ RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true); }
public void setRetransformingMode(boolean on){ instrumenter.setRetransformingMode(on); }
byte[] byteBuffer = instrumentation.transformBytes(this, className, new ClassReader(is), shouldSkip); createPackageDefinition(fullyQualifiedTargetClass);
if(RuntimeInstrumentation.getAvoidInstrumentingShadedClasses()){ list.addAll(Arrays.asList(new String[]{
private void testConfirmNumberExternal() throws Exception{ assertEquals(IssueWithNumber.RESULT, IssueWithNumber.getResult()); RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); EvoClassLoader loader = new EvoClassLoader(); loader.skipInstrumentation(IssueWithNumber.class.getName()); org.evosuite.runtime.Runtime.getInstance().resetRuntime(); Class<?> klass = loader.loadClass(IssueWithNumber.class.getName()); Method m = klass.getDeclaredMethod("getResult"); String res = (String) m.invoke(null); assertEquals(IssueWithNumber.RESULT, res); }
@Test public void testSUTThatIsPartOfEvoSuiteDependencies() { //Chosen dependency might be among the shaded ones RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(false); EvoSuite evosuite = new EvoSuite(); String targetClass = DependencyLibrary.class.getCanonicalName(); Properties.TARGET_CLASS = targetClass; Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.LINE}; 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); Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001); } }
/** {@inheritDoc} */ @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { if ((opcode == Opcodes.PUTSTATIC || opcode == Opcodes.GETSTATIC) && !(className.equals(owner) && methodName.equals("<clinit>")) && !(className.equals(owner) && methodName.equals(ClassResetter.STATIC_RESET))) { String classNameWithDots = owner.replace('/', '.'); if (RuntimeInstrumentation.checkIfCanInstrument(classNameWithDots)) { String executionTracerClassName = ExecutionTracer.class.getName().replace('.', '/'); String executionTracerDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class), Type.getType(String.class)); super.visitLdcInsn(classNameWithDots); super.visitLdcInsn(name); if (opcode == Opcodes.PUTSTATIC) super.visitMethodInsn(INVOKESTATIC, executionTracerClassName, PASSED_PUT_STATIC, executionTracerDescriptor, false); else super.visitMethodInsn(INVOKESTATIC, executionTracerClassName, PASSED_GET_STATIC, executionTracerDescriptor, false); } } super.visitFieldInsn(opcode, owner, name, desc); } }
public TransformerForTests(){ active = false; instrumenter = new RuntimeInstrumentation(); instrumentedClasses = new LinkedHashSet<>(); }
RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true);
/** {@inheritDoc} */ @Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (!RuntimeInstrumentation.checkIfCanInstrument(name)) { Class<?> result = findLoadedClass(name); if (result != null) { return result; } result = classLoader.loadClass(name); return result; } Class<?> result = classes.get(name); if (result != null) { return result; } else { logger.info("Seeing class for first time: " + name); Class<?> instrumentedClass = instrumentClass(name); return instrumentedClass; } }
@After public void resetStaticVariables() { RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(false); RuntimeSettings.applyUIDTransformation = false; TestCaseExecutor.getInstance().newObservers(); TestGenerationContext.getInstance().resetContext(); ClassReInitializer.resetSingleton(); System.setProperties(currentProperties); Properties.getInstance().resetToDefaults(); ExceptionCoverageFactory.getGoals().clear(); Archive.getArchiveInstance().reset(); }
/** * {@inheritDoc} */ @Override public Class<?> loadClass(String name) throws ClassNotFoundException { if ("<evosuite>".equals(name)) throw new ClassNotFoundException(); //first check if already loaded if (!RuntimeInstrumentation.checkIfCanInstrument(name)) { Class<?> result = findLoadedClass(name); if (result != null) { return result; } result = classLoader.loadClass(name); return result; } Class<?> result = classes.get(name); if (result != null) { return result; } else { logger.info("Seeing class for first time: " + name); Class<?> instrumentedClass = instrumentClass(name); return instrumentedClass; } }
@Test public void testPackageLevel_differentPackage_instrumentation_public() throws Exception{ TestCase tc = new DefaultTestCase(); RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true); ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite(); InstrumentingClassLoader loader = new InstrumentingClassLoader(); Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePublicLevel"); VariableReference ref = new VariableReferenceImpl(tc, example); FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, new GenericClass(example)); tc.addStatement(mockStmt); execute(tc); }
if (!RuntimeInstrumentation.checkIfCanInstrument(name)) { Class<?> result = findLoadedClass(name); if (result != null) {
if (!checkIfCanInstrument(classNameWithDots)) { throw new IllegalArgumentException("Should not transform a shared class (" + classNameWithDots + ")! Load by parent (JVM) classloader.");