Search in sources :

Example 41 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 42 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 43 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 44 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)

Example 45 with CtClass

use of javassist.CtClass in project voltdb by VoltDB.

the class JavassistTypeParameterMatcherGenerator method generate.

public static TypeParameterMatcher generate(Class<?> type, ClassLoader classLoader) {
    final String typeName = typeName(type);
    final String className = "io.netty.util.internal.__matchers__." + typeName + "Matcher";
    try {
        try {
            return (TypeParameterMatcher) Class.forName(className, true, classLoader).newInstance();
        } catch (Exception e) {
        // Not defined in the specified class loader.
        }
        CtClass c = classPool.getAndRename(NoOpTypeParameterMatcher.class.getName(), className);
        c.setModifiers(c.getModifiers() | Modifier.FINAL);
        c.getDeclaredMethod("match").setBody("{ return $1 instanceof " + typeName + "; }");
        byte[] byteCode = c.toBytecode();
        c.detach();
        Method method = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
        method.setAccessible(true);
        Class<?> generated = (Class<?>) method.invoke(classLoader, className, byteCode, 0, byteCode.length);
        if (type != Object.class) {
            logger.debug("Generated: {}", generated.getName());
        } else {
        // Object.class is only used when checking if Javassist is available.
        }
        return (TypeParameterMatcher) generated.newInstance();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CtClass(javassist.CtClass) CtClass(javassist.CtClass) Method(java.lang.reflect.Method) NotFoundException(javassist.NotFoundException)

Aggregations

CtClass (javassist.CtClass)121 CtMethod (javassist.CtMethod)46 ClassPool (javassist.ClassPool)37 NotFoundException (javassist.NotFoundException)35 Test (org.junit.Test)32 CannotCompileException (javassist.CannotCompileException)23 CtField (javassist.CtField)20 IOException (java.io.IOException)11 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