Search in sources :

Example 11 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavassistMethod method addLocalVariable.

private void addLocalVariable(String name, Class<?> type) throws CannotCompileException, NotFoundException {
    final String interceptorClassName = type.getName();
    final CtClass interceptorCtClass = behavior.getDeclaringClass().getClassPool().get(interceptorClassName);
    behavior.addLocalVariable(name, interceptorCtClass);
}
Also used : CtClass(javassist.CtClass)

Example 12 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavassistMethod method addAfterInterceptor.

private void addAfterInterceptor(InterceptorDefinition interceptorDefinition, int interceptorId, boolean localVarsInitialized, int originalCodeOffset) throws NotFoundException, CannotCompileException {
    final Class<?> interceptorClass = interceptorDefinition.getInterceptorClass();
    final CaptureType captureType = interceptorDefinition.getCaptureType();
    if (!isAfterInterceptor(captureType)) {
        return;
    }
    final Method interceptorMethod = interceptorDefinition.getAfterMethod();
    if (interceptorMethod == null) {
        if (isDebug) {
            logger.debug("Skip adding after interceptor because the interceptor doesn't have after method: {}", interceptorClass.getName());
        }
        return;
    }
    InvokeAfterCodeGenerator catchGenerator = new InvokeAfterCodeGenerator(interceptorId, interceptorDefinition, declaringClass, this, apiMetaDataService, localVarsInitialized, true);
    String catchCode = catchGenerator.generate();
    if (isDebug) {
        logger.debug("addAfterInterceptor catch behavior:{} code:{}", behavior.getLongName(), catchCode);
    }
    CtClass throwable = behavior.getDeclaringClass().getClassPool().get("java.lang.Throwable");
    insertCatch(originalCodeOffset, catchCode, throwable, "$e");
    InvokeAfterCodeGenerator afterGenerator = new InvokeAfterCodeGenerator(interceptorId, interceptorDefinition, declaringClass, this, apiMetaDataService, localVarsInitialized, false);
    final String afterCode = afterGenerator.generate();
    if (isDebug) {
        logger.debug("addAfterInterceptor after behavior:{} code:{}", behavior.getLongName(), afterCode);
    }
    behavior.insertAfter(afterCode);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method)

Example 13 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavaAssistTest method innerClass.

@Test
public void innerClass() throws NotFoundException {
    CtClass testClass = pool.get("com.navercorp.pinpoint.test.javasssit.TestClass");
    logger.debug(testClass.toString());
    CtClass[] nestedClasses = testClass.getNestedClasses();
    for (CtClass nested : nestedClasses) {
        logger.debug("nestedClass:{}", nested);
    }
    CtClass innerClass = pool.get("com.navercorp.pinpoint.test.javasssit.TestClass$InnerClass");
    logger.debug("{}", innerClass);
    CtClass class1 = pool.get("com.navercorp.pinpoint.test.javasssit.TestClass$1");
    logger.debug("{}", class1);
}
Also used : CtClass(javassist.CtClass) Test(org.junit.Test)

Example 14 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class TestBootstrapClass method test.

@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, CannotCompileException {
    URLClassLoader classLoader = new URLClassLoader(new URL[] {});
    LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader);
    ClassPool cp = new ClassPool();
    cp.appendClassPath(loaderClassPath);
    CtClass ctClass = cp.makeClass(TEST_CLASS_NAME);
    byte[] bytes = ctClass.toBytecode();
    logger.debug(classLoader.getClass().getName());
    Class<?> aClass = BytecodeUtils.defineClass(classLoader, TEST_CLASS_NAME, bytes);
    logger.debug("{}", aClass.getName());
}
Also used : CtClass(javassist.CtClass) URLClassLoader(java.net.URLClassLoader) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) Test(org.junit.Test)

Example 15 with CtClass

use of javassist.CtClass in project pinpoint by naver.

the class JavaAssistTest method genericTest.

@Test
public void genericTest() throws NotFoundException {
    CtClass testClass = pool.get("com.navercorp.pinpoint.test.javasssit.TestClass");
    //        CtMethod setb = testClass.getMethod("setb");
    CtMethod[] declaredMethods = testClass.getDeclaredMethods();
    for (CtMethod declaredMethod : declaredMethods) {
        logger.debug(declaredMethod.toString());
        logger.debug(declaredMethod.getGenericSignature());
        logger.debug(declaredMethod.getSignature());
        logger.debug("paramTypes:{}", Arrays.toString(declaredMethod.getParameterTypes()));
        logger.debug(declaredMethod.getMethodInfo2().getDescriptor());
        logger.debug(declaredMethod.getMethodInfo().getDescriptor());
    //            logger.debug(declaredMethod.());
    }
    CtMethod setb = testClass.getDeclaredMethod("setA", new CtClass[] { pool.get("int") });
    logger.debug(setb.toString());
    CtMethod setStringArray = testClass.getDeclaredMethod("setStringArray", new CtClass[] { pool.get("java.lang.String[]") });
    logger.debug(setStringArray.toString());
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

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