Search in sources :

Example 1 with CtBehavior

use of javassist.CtBehavior in project ratpack by ratpack.

the class DescribingHandlers method describeTo.

public static void describeTo(Handler handler, StringBuilder stringBuilder) {
    Class<? extends Handler> clazz = handler.getClass();
    if (clazz.isAnonymousClass()) {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctClass;
        try {
            ctClass = pool.get(clazz.getName());
            CtBehavior[] behaviors = ctClass.getDeclaredBehaviors();
            List<CtBehavior> withLineNumber = Arrays.asList(behaviors).stream().filter(input -> input.getMethodInfo().getLineNumber(0) > 0).sorted((o1, o2) -> Integer.valueOf(o1.getMethodInfo().getLineNumber(0)).compareTo(o2.getMethodInfo().getLineNumber(0))).collect(Collectors.toList());
            if (!withLineNumber.isEmpty()) {
                CtBehavior method = withLineNumber.get(0);
                int lineNumber = method.getMethodInfo().getLineNumber(0);
                ClassFile classFile = ctClass.getClassFile();
                String sourceFile = classFile.getSourceFile();
                if (lineNumber != -1 && sourceFile != null) {
                    stringBuilder.append("anonymous class ").append(clazz.getName()).append(" at approximately line ").append(lineNumber).append(" of ").append(sourceFile);
                    return;
                }
            }
        } catch (NotFoundException ignore) {
        // fall through
        }
    }
    stringBuilder.append(clazz.getName());
}
Also used : CtBehavior(javassist.CtBehavior) Arrays(java.util.Arrays) List(java.util.List) ClassFile(javassist.bytecode.ClassFile) Handler(ratpack.handling.Handler) NotFoundException(javassist.NotFoundException) CtBehavior(javassist.CtBehavior) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException)

Example 2 with CtBehavior

use of javassist.CtBehavior in project pinpoint by naver.

the class JavassistClass method hasEnclosingMethod.

@Override
public boolean hasEnclosingMethod(String methodName, String... parameterTypes) {
    CtBehavior behavior;
    try {
        behavior = ctClass.getEnclosingBehavior();
    } catch (NotFoundException ignored) {
        return false;
    }
    if (behavior == null) {
        return false;
    }
    final MethodInfo methodInfo = behavior.getMethodInfo2();
    if (!methodInfo.getName().equals(methodName)) {
        return false;
    }
    final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypes);
    if (methodInfo.getDescriptor().startsWith(jvmSignature)) {
        return true;
    }
    return false;
}
Also used : CtBehavior(javassist.CtBehavior) NotFoundException(javassist.NotFoundException) MethodInfo(javassist.bytecode.MethodInfo)

Aggregations

CtBehavior (javassist.CtBehavior)2 NotFoundException (javassist.NotFoundException)2 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 ClassFile (javassist.bytecode.ClassFile)1 MethodInfo (javassist.bytecode.MethodInfo)1 Handler (ratpack.handling.Handler)1