Search in sources :

Example 6 with CtClass

use of javassist.CtClass in project hibernate-orm by hibernate.

the class EnhancerTestUtils method enhanceAndDecompile.

/**
	 * method that performs the enhancement of a class
	 * also checks the signature of enhanced entities methods using 'javap' decompiler
	 */
public static Class<?> enhanceAndDecompile(Class<?> classToEnhance, ClassLoader cl) throws Exception {
    CtClass entityCtClass = generateCtClassForAnEntity(classToEnhance);
    byte[] original = entityCtClass.toBytecode();
    byte[] enhanced = Environment.getBytecodeProvider().getEnhancer(new EnhancerTestContext()).enhance(entityCtClass.getName(), original);
    assertFalse("entity was not enhanced", enhanced == null);
    log.infof("enhanced entity [%s]", entityCtClass.getName());
    ClassPool cp = new ClassPool(false);
    cp.appendClassPath(new LoaderClassPath(cl));
    CtClass enhancedCtClass = cp.makeClass(new ByteArrayInputStream(enhanced));
    enhancedCtClass.debugWriteFile(workingDir);
    DecompileUtils.decompileDumpedClass(workingDir, classToEnhance.getName());
    Class<?> enhancedClass = enhancedCtClass.toClass(cl, EnhancerTestUtils.class.getProtectionDomain());
    assertNotNull(enhancedClass);
    return enhancedClass;
}
Also used : CtClass(javassist.CtClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Example 7 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavassistClass method getNestedClasses.

@Override
public List<InstrumentClass> getNestedClasses(ClassFilter filter) {
    List<InstrumentClass> list = new ArrayList<InstrumentClass>();
    CtClass[] nestedClasses;
    try {
        nestedClasses = ctClass.getNestedClasses();
    } catch (NotFoundException ex) {
        return list;
    }
    if (nestedClasses == null || nestedClasses.length == 0) {
        return list;
    }
    for (CtClass nested : nestedClasses) {
        final InstrumentClass clazz = new JavassistClass(objectBinderFactory, pluginContext, interceptorRegistryBinder, apiMetaDataService, classLoader, nested);
        if (filter.accept(clazz)) {
            list.add(clazz);
        }
    }
    return list;
}
Also used : CtClass(javassist.CtClass) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException)

Example 8 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavassistClass method weave.

@Override
public void weave(String adviceClassName) throws InstrumentException {
    pluginContext.injectClass(classLoader, adviceClassName);
    CtClass adviceClass;
    try {
        adviceClass = getCtClass(adviceClassName);
    } catch (NotFoundException e) {
        throw new NotFoundInstrumentException(adviceClassName + " not found. Caused:" + e.getMessage(), e);
    }
    try {
        AspectWeaverClass weaverClass = new AspectWeaverClass();
        weaverClass.weaving(ctClass, adviceClass);
    } catch (CannotCompileException e) {
        throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
    } catch (NotFoundException e) {
        throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) AspectWeaverClass(com.navercorp.pinpoint.profiler.instrument.aspect.AspectWeaverClass)

Example 9 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavassistClass method addGetter.

@Override
public void addGetter(String getterTypeName, String fieldName) throws InstrumentException {
    try {
        Class<?> getterType = pluginContext.injectClass(classLoader, getterTypeName);
        GetterAnalyzer getterAnalyzer = new GetterAnalyzer();
        GetterDetails getterDetails = getterAnalyzer.analyze(getterType);
        CtField field = ctClass.getField(fieldName);
        String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(getterDetails.getFieldType().getName());
        if (!field.getType().getName().equals(fieldTypeName)) {
            throw new IllegalArgumentException("Return type of the getter is different with the field type. getterMethod: " + getterDetails.getGetter() + ", fieldType: " + field.getType().getName());
        }
        CtMethod getterMethod = CtNewMethod.getter(getterDetails.getGetter().getName(), field);
        if (getterMethod.getDeclaringClass() != ctClass) {
            getterMethod = CtNewMethod.copy(getterMethod, ctClass, null);
        }
        ctClass.addMethod(getterMethod);
        CtClass ctInterface = getCtClass(getterTypeName);
        ctClass.addInterface(ctInterface);
    } catch (Exception e) {
        throw new InstrumentException("Failed to add getter: " + getterTypeName, e);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) GetterDetails(com.navercorp.pinpoint.profiler.instrument.GetterAnalyzer.GetterDetails) CtMethod(javassist.CtMethod) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException)

Example 10 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavassistClass method addSetter.

@Override
public void addSetter(String setterTypeName, String fieldName, boolean removeFinalFlag) throws InstrumentException {
    try {
        Class<?> setterType = pluginContext.injectClass(classLoader, setterTypeName);
        SetterAnalyzer setterAnalyzer = new SetterAnalyzer();
        SetterDetails setterDetails = setterAnalyzer.analyze(setterType);
        CtField field = ctClass.getField(fieldName);
        String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(setterDetails.getFieldType().getName());
        if (!field.getType().getName().equals(fieldTypeName)) {
            throw new IllegalArgumentException("Argument type of the setter is different with the field type. setterMethod: " + setterDetails.getSetter() + ", fieldType: " + field.getType().getName());
        }
        final int originalModifiers = field.getModifiers();
        if (Modifier.isStatic(originalModifiers)) {
            throw new IllegalArgumentException("Cannot add setter to static fields. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
        }
        boolean finalRemoved = false;
        if (Modifier.isFinal(originalModifiers)) {
            if (!removeFinalFlag) {
                throw new IllegalArgumentException("Cannot add setter to final field. setterMethod: " + setterDetails.getSetter().getName() + ", fieldName: " + fieldName);
            } else {
                final int modifiersWithFinalRemoved = Modifier.clear(originalModifiers, Modifier.FINAL);
                field.setModifiers(modifiersWithFinalRemoved);
                finalRemoved = true;
            }
        }
        try {
            CtMethod setterMethod = CtNewMethod.setter(setterDetails.getSetter().getName(), field);
            if (setterMethod.getDeclaringClass() != ctClass) {
                setterMethod = CtNewMethod.copy(setterMethod, ctClass, null);
            }
            ctClass.addMethod(setterMethod);
            CtClass ctInterface = getCtClass(setterTypeName);
            ctClass.addInterface(ctInterface);
        } catch (Exception e) {
            if (finalRemoved) {
                field.setModifiers(originalModifiers);
            }
            throw e;
        }
    } catch (Exception e) {
        throw new InstrumentException("Failed to add setter: " + setterTypeName, e);
    }
}
Also used : CtClass(javassist.CtClass) SetterDetails(com.navercorp.pinpoint.profiler.instrument.SetterAnalyzer.SetterDetails) CtField(javassist.CtField) CtMethod(javassist.CtMethod) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException)

Aggregations

CtClass (javassist.CtClass)113 CtMethod (javassist.CtMethod)42 NotFoundException (javassist.NotFoundException)33 ClassPool (javassist.ClassPool)32 Test (org.junit.Test)32 CannotCompileException (javassist.CannotCompileException)22 CtField (javassist.CtField)20 IOException (java.io.IOException)10 CtConstructor (javassist.CtConstructor)8 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 CtMethodJavaWriter (com.github.stephanenicolas.afterburner.inserts.CtMethodJavaWriter)4 FileNotFoundException (java.io.FileNotFoundException)4 LoaderClassPath (javassist.LoaderClassPath)4 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)4 CodeAttribute (javassist.bytecode.CodeAttribute)4 ConstPool (javassist.bytecode.ConstPool)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 NamedClassPool (com.navercorp.pinpoint.profiler.instrument.classpool.NamedClassPool)3