Search in sources :

Example 26 with CannotCompileException

use of javassist.CannotCompileException in project pinpoint by naver.

the class JavassistMethod method insertBeforeConstructor.

private int insertBeforeConstructor(int pos, String src) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    CodeIterator iterator = ca.iterator();
    Bytecode b = new Bytecode(behavior.getMethodInfo().getConstPool(), ca.getMaxStack(), ca.getMaxLocals());
    b.setStackDepth(ca.getMaxStack());
    Javac jv = new Javac(b, cc);
    try {
        jv.recordParams(behavior.getParameterTypes(), false);
        jv.recordLocalVariables(ca, 0);
        jv.compileStmnt(src);
        ca.setMaxStack(b.getMaxStack());
        ca.setMaxLocals(b.getMaxLocals());
        iterator.skipConstructor();
        if (pos != -1) {
            iterator.insertEx(pos, b.get());
        } else {
            pos = iterator.insertEx(b.get());
        }
        iterator.insert(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        return pos + b.length();
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) CtClass(javassist.CtClass) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 27 with CannotCompileException

use of javassist.CannotCompileException in project pinpoint by naver.

the class JavassistMethod method insertBeforeMethod.

private int insertBeforeMethod(int pos, String src) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    if (ca == null)
        throw new CannotCompileException("no method body");
    CodeIterator iterator = ca.iterator();
    Javac jv = new Javac(cc);
    try {
        int nvars = jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.recordParamNames(ca, nvars);
        jv.recordLocalVariables(ca, 0);
        jv.recordType(getReturnType0());
        jv.compileStmnt(src);
        Bytecode b = jv.getBytecode();
        int stack = b.getMaxStack();
        int locals = b.getMaxLocals();
        if (stack > ca.getMaxStack())
            ca.setMaxStack(stack);
        if (locals > ca.getMaxLocals())
            ca.setMaxLocals(locals);
        if (pos != -1) {
            iterator.insertEx(pos, b.get());
        } else {
            pos = iterator.insertEx(b.get());
        }
        iterator.insert(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
        return pos + b.length();
    } catch (NotFoundException e) {
        throw new CannotCompileException(e);
    } catch (CompileError e) {
        throw new CannotCompileException(e);
    } catch (BadBytecode e) {
        throw new CannotCompileException(e);
    }
}
Also used : CompileError(javassist.compiler.CompileError) CtClass(javassist.CtClass) Javac(javassist.compiler.Javac) CodeAttribute(javassist.bytecode.CodeAttribute) CodeIterator(javassist.bytecode.CodeIterator) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) CannotCompileException(javassist.CannotCompileException) BadBytecode(javassist.bytecode.BadBytecode)

Example 28 with CannotCompileException

use of javassist.CannotCompileException 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 29 with CannotCompileException

use of javassist.CannotCompileException 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)

Aggregations

CannotCompileException (javassist.CannotCompileException)29 NotFoundException (javassist.NotFoundException)20 CtClass (javassist.CtClass)18 CtMethod (javassist.CtMethod)16 ClassPool (javassist.ClassPool)10 IOException (java.io.IOException)8 CtField (javassist.CtField)8 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 File (java.io.File)3 CtConstructor (javassist.CtConstructor)3 BadBytecode (javassist.bytecode.BadBytecode)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CodeIterator (javassist.bytecode.CodeIterator)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 AsyncProvider (com.google.gwt.inject.client.AsyncProvider)1 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1