Search in sources :

Example 91 with CtClass

use of javassist.CtClass in project activejdbc by javalite.

the class Instrumentation method instrument.

public void instrument() {
    if (outputDirectory == null) {
        throw new RuntimeException("Property 'outputDirectory' must be provided");
    }
    try {
        System.out.println("**************************** START INSTRUMENTATION ****************************");
        System.out.println("Directory: " + outputDirectory);
        InstrumentationModelFinder mf = new InstrumentationModelFinder();
        File target = new File(outputDirectory);
        mf.processDirectoryPath(target);
        ModelInstrumentation mi = new ModelInstrumentation();
        for (CtClass clazz : mf.getModels()) {
            byte[] bytecode = mi.instrument(clazz);
            String fileName = getFullFilePath(clazz);
            FileOutputStream fout = new FileOutputStream(fileName);
            fout.write(bytecode);
            fout.flush();
            fout.close();
            System.out.println("Instrumented class: " + fileName);
        }
        generateModelsFile(mf.getModels(), target);
        System.out.println("**************************** END INSTRUMENTATION ****************************");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CtClass(javassist.CtClass) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URISyntaxException(java.net.URISyntaxException) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 92 with CtClass

use of javassist.CtClass in project activejdbc by javalite.

the class JavaAgent method premain.

@SuppressWarnings("unchecked")
public static void premain(String args, java.lang.instrument.Instrumentation inst) {
    Instrumentation.log("You are using dynamic instrumentation...");
    try {
        modelFinder = new InstrumentationModelFinder();
        modelInstrumentation = new ModelInstrumentation();
        // calling this via reflection because we do not want AJ dependency on instrumentation project
        Class finderClass = Class.forName("org.javalite.activejdbc.ModelFinder");
        modelFound = finderClass.getDeclaredMethod("modelFound", String.class);
    } catch (Exception e) {
        throw new InstrumentationException(e);
    }
    inst.addTransformer(new ClassFileTransformer() {

        @Override
        public synchronized byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
            try {
                CtClass clazz = modelFinder.getClazz(className.replace('/', '.'));
                if (modelFinder.isModel(clazz)) {
                    if (!loaders.contains(loader) && loader instanceof URLClassLoader) {
                        scanLoader(loader);
                        loaders.add(loader);
                        List<CtClass> models = modelFinder.getModels();
                        for (CtClass ctClass : models) {
                            modelFound.invoke(null, ctClass.getName());
                        }
                    }
                    byte[] bytecode = modelInstrumentation.instrument(clazz);
                    Instrumentation.log("Instrumented model: " + clazz.getName());
                    return bytecode;
                } else {
                    return null;
                }
            } catch (Exception e) {
                throw new InstrumentationException(e);
            }
        }
    });
}
Also used : ProtectionDomain(java.security.ProtectionDomain) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CtClass(javassist.CtClass) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) CtClass(javassist.CtClass) List(java.util.List) Arrays.asList(java.util.Arrays.asList)

Example 93 with CtClass

use of javassist.CtClass in project powermock by powermock.

the class InstrumentMockTransformerTest method should_ignore_call_to_synthetic_field_when_instrument_call_to_method.

@Test
public void should_ignore_call_to_synthetic_field_when_instrument_call_to_method() throws Throwable {
    final ClassPool classPool = new ClassPool(true);
    CtClass ctClass = prepareClassesForFieldTests(classPool);
    mockTransformerChain.transform(wrap(ctClass));
    runTestWithNewClassLoader(classPool, ShouldIgnoreCallToSyntheticField.class.getName());
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) Test(org.junit.Test)

Example 94 with CtClass

use of javassist.CtClass in project powermock by powermock.

the class InstrumentMockTransformerTest method prepareClassesForFieldTests.

private CtClass prepareClassesForFieldTests(ClassPool classPool) throws NotFoundException, CannotCompileException {
    CtClass ctClass = classPool.getCtClass(SuperClassWithObjectMethod.class.getName());
    addSyntheticField(classPool, ctClass);
    insertCallSyntheticField(ctClass);
    return ctClass;
}
Also used : CtClass(javassist.CtClass) SuperClassWithObjectMethod(powermock.test.support.MainMockTransformerTestSupport.SuperClassWithObjectMethod)

Example 95 with CtClass

use of javassist.CtClass in project powermock by powermock.

the class MethodsMockTransformerTest method should_ignore_call_to_synthetic_non_bridge_methods.

@Test
public void should_ignore_call_to_synthetic_non_bridge_methods() throws Throwable {
    final ClassPool classPool = new ClassPool(true);
    CtClass ctClass = prepareClassesForTest(classPool, "syntheticMethodIsCalled = true;");
    final Class<?> clazz = loadWithMockClassLoader(ctClass);
    final Object instance = WhiteboxImpl.newInstance(clazz);
    clazz.getMethod("doSomething", Object.class).invoke(instance, new Object());
    assertThat(methodCalls()).isNot(registered().forMethod(SYNTHETIC_METHOD_NAME));
    assertThat(WhiteboxImpl.getInternalState(clazz, "syntheticMethodIsCalled")).isEqualTo(true);
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) Test(org.junit.Test)

Aggregations

CtClass (javassist.CtClass)271 CtMethod (javassist.CtMethod)96 ClassPool (javassist.ClassPool)93 NotFoundException (javassist.NotFoundException)85 Test (org.junit.Test)63 CannotCompileException (javassist.CannotCompileException)62 CtField (javassist.CtField)53 IOException (java.io.IOException)35 CtConstructor (javassist.CtConstructor)26 Method (java.lang.reflect.Method)17 LoaderClassPath (javassist.LoaderClassPath)16 ClassFile (javassist.bytecode.ClassFile)14 ArrayList (java.util.ArrayList)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)11 ConstPool (javassist.bytecode.ConstPool)11 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)8 Collectors (java.util.stream.Collectors)8 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)7