Search in sources :

Example 1 with ExprEditor

use of javassist.expr.ExprEditor in project pinpoint by naver.

the class JavaAssistInterceptorTest method afterCatch.

@Test
public void afterCatch() throws NotFoundException, CannotCompileException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ClassNotFoundException {
    ClassPool pool = new ClassPool(true);
    Loader loader = getLoader(pool);
    CtClass ctClass = pool.get("com.navercorp.pinpoint.test.javasssit.mock.TestObject");
    CtClass object = pool.get("java.lang.String");
    logger.debug("target:{}", ctClass);
    CtMethod callA = ctClass.getDeclaredMethod("callA", null);
    logger.debug("callA:{}", callA);
    //      callA.addLocalVariable("__test", object);
    final String before = "{ java.lang.Throwable __throwable = null; java.lang.String __test = \"abc\"; System.out.println(\"BEFORE\" + __test);";
    //      callA.insertBefore();
    //      callA.insertAfter("System.out.println(\"AFTER\" + __test);");
    //      final String AFTER =  "finally {System.out.println(\"AFTER\" + __test);}}";
    final String after = "}";
    //      callA.addCatch();
    //      callA.addCatch("System.out.println(\"AFTER\");", pool.get("java.lang.Throwable"));
    callA.instrument(new ExprEditor() {

        @Override
        public void edit(MethodCall m) throws CannotCompileException {
            logger.debug("edit class{}", m.getClassName());
            try {
                logger.debug("edit method:{}", m.getMethod().toString());
            } catch (NotFoundException e) {
                logger.warn("getMethod() fail. Caused:{}", e.getMessage(), e);
            }
            logger.debug(m.getMethodName());
            m.replace(before + " try {$_ = $proceed($$); System.out.println(\"end---\"+ $_);} catch (java.lang.Throwable ex) { __throwable = ex; System.out.println(\"catch\"); } " + after);
        }
    });
    Class aClass = loader.loadClass(ctClass.getName());
    java.lang.reflect.Method callA1 = aClass.getMethod("callA");
    Object target = aClass.newInstance();
    Object result = callA1.invoke(target);
    logger.debug("result:{}", result);
}
Also used : MethodCall(javassist.expr.MethodCall) ExprEditor(javassist.expr.ExprEditor) Test(org.junit.Test)

Example 2 with ExprEditor

use of javassist.expr.ExprEditor in project javassist-maven-plugin by icon-Systemhaus-GmbH.

the class MethodCallClassTransformer method applyTransformations.

@Override
public void applyTransformations(final CtClass classToTransform) throws JavassistBuildException {
    if (null == classToTransform) {
        return;
    }
    try {
        classToTransform.instrument(new ExprEditor() {

            @Override
            public void edit(final MethodCall method) throws CannotCompileException {
                final String statement = getStatement(method.getClassName(), method.getMethodName());
                if (statement != null) {
                    try {
                        method.replace(statement);
                    } catch (final CannotCompileException e) {
                        throw new CannotCompileException(String.format("Compile statement '%1$s' FAILED with: %2$s", statement, e.getMessage()), e);
                    }
                }
            }
        });
        // insert internal introspection state field
        final CtField introspectedField = new CtField(CtClass.booleanType, ALREADY_INTROSPECTED_FIELD_NAME, classToTransform);
        introspectedField.setModifiers(AccessFlag.PUBLIC | AccessFlag.STATIC | AccessFlag.FINAL);
        classToTransform.addField(introspectedField, Initializer.constant(true));
    } catch (CannotCompileException e) {
        throw new JavassistBuildException(e);
    }
}
Also used : CtField(javassist.CtField) ExprEditor(javassist.expr.ExprEditor) JavassistBuildException(javassist.build.JavassistBuildException) CannotCompileException(javassist.CannotCompileException) MethodCall(javassist.expr.MethodCall)

Example 3 with ExprEditor

use of javassist.expr.ExprEditor in project reflections by ronmamo.

the class MemberUsageScanner method scanMember.

private void scanMember(CtBehavior member, List<Map.Entry<String, String>> entries) throws CannotCompileException {
    // key contains this$/val$ means local field/parameter closure
    final String key = member.getDeclaringClass().getName() + "." + member.getMethodInfo().getName() + "(" + parameterNames(member.getMethodInfo()) + // + " #" + member.getMethodInfo().getLineNumber(0)
    ")";
    member.instrument(new ExprEditor() {

        @Override
        public void edit(NewExpr e) {
            try {
                add(entries, e.getConstructor().getDeclaringClass().getName() + "." + "<init>" + "(" + parameterNames(e.getConstructor().getMethodInfo()) + ")", key + " #" + e.getLineNumber());
            } catch (NotFoundException e1) {
                throw new ReflectionsException("Could not find new instance usage in " + key, e1);
            }
        }

        @Override
        public void edit(MethodCall m) {
            try {
                add(entries, m.getMethod().getDeclaringClass().getName() + "." + m.getMethodName() + "(" + parameterNames(m.getMethod().getMethodInfo()) + ")", key + " #" + m.getLineNumber());
            } catch (NotFoundException e) {
                throw new ReflectionsException("Could not find member " + m.getClassName() + " in " + key, e);
            }
        }

        @Override
        public void edit(ConstructorCall c) {
            try {
                add(entries, c.getConstructor().getDeclaringClass().getName() + "." + "<init>" + "(" + parameterNames(c.getConstructor().getMethodInfo()) + ")", key + " #" + c.getLineNumber());
            } catch (NotFoundException e) {
                throw new ReflectionsException("Could not find member " + c.getClassName() + " in " + key, e);
            }
        }

        @Override
        public void edit(FieldAccess f) {
            try {
                add(entries, f.getField().getDeclaringClass().getName() + "." + f.getFieldName(), key + " #" + f.getLineNumber());
            } catch (NotFoundException e) {
                throw new ReflectionsException("Could not find member " + f.getFieldName() + " in " + key, e);
            }
        }
    });
}
Also used : ReflectionsException(org.reflections.ReflectionsException) ExprEditor(javassist.expr.ExprEditor) NewExpr(javassist.expr.NewExpr) NotFoundException(javassist.NotFoundException) ConstructorCall(javassist.expr.ConstructorCall) FieldAccess(javassist.expr.FieldAccess) MethodCall(javassist.expr.MethodCall)

Aggregations

ExprEditor (javassist.expr.ExprEditor)3 MethodCall (javassist.expr.MethodCall)3 CannotCompileException (javassist.CannotCompileException)1 CtField (javassist.CtField)1 NotFoundException (javassist.NotFoundException)1 JavassistBuildException (javassist.build.JavassistBuildException)1 ConstructorCall (javassist.expr.ConstructorCall)1 FieldAccess (javassist.expr.FieldAccess)1 NewExpr (javassist.expr.NewExpr)1 Test (org.junit.Test)1 ReflectionsException (org.reflections.ReflectionsException)1