Search in sources :

Example 36 with NotFoundException

use of javassist.NotFoundException in project pinpoint by naver.

the class InterceptorTest method interceptor.

//    @Deprecated
@Test
public void interceptor() throws NotFoundException, CannotCompileException, IllegalAccessException, InstantiationException, IOException, ClassNotFoundException, NoSuchMethodException {
    AroundInterceptor aroundInterceptor = new AroundInterceptor() {

        @Override
        public void before(Object target, Object[] args) {
            logger.info("BEFORE target:" + target + " args:" + Arrays.toString(args));
        }

        @Override
        public void after(Object target, Object[] args, Object result, Throwable throwable) {
            logger.info("AFTER target: " + target + " args:" + Arrays.toString(args) + " result:" + result + " throwable:" + throwable);
        }
    };
    int interceptorId = INTERCEPTOR_REGISTRY_ADAPTOR.addInterceptor(aroundInterceptor);
    final ClassPool classPool = new ClassPool(true);
    CtClass throwable = classPool.get(Throwable.class.getName());
    CtClass ctClass = classPool.get("com.navercorp.pinpoint.profiler.interceptor.JavaAssistTestObject");
    final CtMethod hello = ctClass.getMethod("hello", "(Ljava/lang/String;)Ljava/lang/String;");
    logger.debug("longName:{}", hello.getLongName());
    logger.debug("name:{}", hello.getName());
    String interceptorClassName = AroundInterceptor.class.getName();
    CtClass interceptor = classPool.get(interceptorClassName);
    hello.addLocalVariable("interceptor", interceptor);
    CtClass object = classPool.get(Object.class.getName());
    hello.addLocalVariable("result", object);
    //        hello.insertBefore("{ System.out.println(\"BEFORE\"); }");
    hello.insertBefore("{" + "interceptor = (" + interceptorClassName + ") " + InterceptorRegistry.class.getName() + ".getInterceptor(" + interceptorId + ");" + "interceptor.before(this, $args);" + "}");
    //        hello.addCatch("{" +
    ////            " interceptor.after(ctx);"+
    ////           " AroundInterceptor a = (AroundInterceptor) " + InterceptorRegistry.class.getName() + ".getStaticInterceptor(\"a\");"+
    //                " throw $e;" +
    //                "}", throwable);
    //        hello.insertAfter("{" +
    //                "interceptor.after(this,  $args, ($w)$_, null); " +
    //                "}");
    //       hello.setBody(generatedAroundInterceptor("TestObject", "hello"));
    //       hello.setBody("{ System.out.println(\"ddd\");  }", ClassMap map );
    //       hello.insertBefore(" System.out.println(\" BEFORE +  \");");
    //       hello.insertAfter(" System.out.println($_);");
    //       hello.insertAfter(" System.out.println($r);");
    //       hello.insertAfter(" System.out.println($w);");
    //       hello.insertAfter(" System.out.println($sig);");
    //       hello.insertAfter(" System.out.println($type);");
    //       hello.insertAfter(" System.out.println($class);");
    //       hello.instrument(new ExprEditor() {
    //         public void edit(MethodCall m)
    //         throws CannotCompileException
    //         {
    //             try {
    //                 System.out.println("method call" + m.getMethod().getName());
    //             } catch (NotFoundException e) {
    //                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    //             }
    //             String code = generatedAroundInterceptor("TestObject", "hello");
    //             m.replace(code);
    //         }
    //         });
    //        hello.addCatch("System.out.println(\"catch\"); throw $e;", throwable);
    //       hello.setName("__hello");
    //       CtMethod method = CtNewMethod.make("public void hello() { try {__hello(); } catch(Throwable th){throw th;}}", ctClass);
    //         CtMethod method = CtNewMethod.make("public void hello() { System.out.println(\"ddd\"); } catch(Throwable th){throw th;}}", ctClass);
    //       ctClass.addMethod(method);
    //        ctClass.freeze();
    //       ctClass.writeFile("./debug");
    //       ctClass.debugWriteFile("./debug");
    Loader loader = LoaderUtils.createLoader(classPool);
    loader.delegateLoadingOf("com.navercorp.pinpoint.bootstrap.");
    Class aClass = loader.loadClass(ctClass.getName());
    Object testObject = aClass.newInstance();
    Method helloMethod = testObject.getClass().getDeclaredMethod("hello", String.class);
    try {
        helloMethod.invoke(testObject, "hello~~");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
//       o.hello();
}
Also used : ClassPool(javassist.ClassPool) Loader(javassist.Loader) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) NotFoundException(javassist.NotFoundException) CtClass(javassist.CtClass) InterceptorRegistry(com.navercorp.pinpoint.bootstrap.interceptor.registry.InterceptorRegistry) AroundInterceptor(com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 37 with NotFoundException

use of javassist.NotFoundException in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormsHelperStubber method createStub.

protected static void createStub() {
    ClassPool classPool = ClassPool.getDefault();
    CtClass ctClass;
    try {
        ctClass = classPool.get(CLASS_NAME);
        //indicates the class has already been stubbed and loaded
        if (ctClass.isFrozen()) {
            return;
        }
        //to remove any dependencies on impl classes.
        for (CtMethod ctMethod : ctClass.getDeclaredMethods()) {
            ctMethod.setBody(null);
        }
        //remove the error causing static field declaration
        ctClass.removeField(ctClass.getDeclaredField(ERROR_FIELD));
        //remove the static initilizer block calling new on impl class.
        ctClass.removeConstructor(ctClass.getClassInitializer());
        //load the stubbed class
        ctClass.toClass();
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 38 with NotFoundException

use of javassist.NotFoundException in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

public static String[] getParameterNames(Class<?> clazz, Method method) {
    CtMethod ctMethod;
    try {
        ctMethod = CtClassUtil.getMethod(clazz, method);
    } catch (NotFoundException e) {
        Throwable error = e.getCause();
        if (error instanceof RuntimeException) {
            throw (RuntimeException) error;
        }
        if (error instanceof Exception) {
            throw new RuntimeException(error.getMessage(), error);
        }
        throw new RuntimeException(e.getMessage(), e);
    }
    String[] names;
    try {
        names = getParameterNames(ctMethod);
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // System.err.println("getParameterNames methodName:" + method.toGenericString() + " names:" + StringUtils.join(names, ", "));
    return names;
}
Also used : NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod) NotFoundException(javassist.NotFoundException)

Example 39 with NotFoundException

use of javassist.NotFoundException in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

public static String[] getParameterNames(Class<?> clazz, Method method) {
    CtMethod ctMethod;
    try {
        ctMethod = CtClassUtil.getMethod(clazz, method);
    } catch (NotFoundException e) {
        Throwable error = e.getCause();
        if (error instanceof RuntimeException) {
            throw (RuntimeException) error;
        }
        if (error instanceof Exception) {
            throw new RuntimeException(error.getMessage(), error);
        }
        throw new RuntimeException(e.getMessage(), e);
    }
    String[] names;
    try {
        names = getParameterNames(ctMethod);
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // System.err.println("getParameterNames methodName:" + method.toGenericString() + " names:" + StringUtils.join(names, ", "));
    return names;
}
Also used : NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod) NotFoundException(javassist.NotFoundException)

Aggregations

NotFoundException (javassist.NotFoundException)39 CtClass (javassist.CtClass)28 CannotCompileException (javassist.CannotCompileException)20 CtMethod (javassist.CtMethod)18 ClassPool (javassist.ClassPool)14 IOException (java.io.IOException)6 CtField (javassist.CtField)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 BadBytecode (javassist.bytecode.BadBytecode)4 CodeIterator (javassist.bytecode.CodeIterator)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 CtBehavior (javassist.CtBehavior)2 CtConstructor (javassist.CtConstructor)2