Search in sources :

Example 41 with NotFoundException

use of javassist.NotFoundException in project atlas by alibaba.

the class CodeInjectByJavassist method addField.

private static void addField(ClassPool pool, CtClass cc, String key, String value) throws NotFoundException, CannotCompileException {
    if (StringUtils.isNotBlank(value)) {
        try {
            cc.removeField(cc.getField(key));
        } catch (NotFoundException notfoundException) {
        }
        logger.info("inject key " + key + "->" + value);
        CtClass ctClass = pool.get("java.lang.String");
        CtField ctField = new CtField(ctClass, key, cc);
        ctField.setModifiers(Modifier.STATIC | Modifier.PUBLIC);
        CtField.Initializer initializer = CtField.Initializer.constant(value);
        cc.addField(ctField, initializer);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) NotFoundException(javassist.NotFoundException)

Example 42 with NotFoundException

use of javassist.NotFoundException in project ratpack by ratpack.

the class ClosureBackedHandler method describeTo.

@Override
public void describeTo(StringBuilder stringBuilder) {
    ClosureUtil.SourceInfo sourceInfo = ClosureUtil.getSourceInfo(invoker.getClosure());
    if (sourceInfo == null) {
        ClassPool pool = ClassPool.getDefault();
        try {
            CtClass ctClass = pool.get(invoker.getClosure().getClass().getName());
            CtMethod ctMethod = ctClass.getDeclaredMethod("doCall");
            int lineNumber = ctMethod.getMethodInfo().getLineNumber(0);
            ClassFile classFile = ctClass.getClassFile();
            String sourceFile = classFile.getSourceFile();
            if (lineNumber != -1 && sourceFile != null) {
                stringBuilder.append("closure at line ").append(lineNumber).append(" of ").append(sourceFile);
            } else {
                stringBuilder.append("closure ").append(invoker.getClosure().getClass().getName());
            }
        } catch (NotFoundException e) {
            stringBuilder.append(invoker.getClosure().getClass().getName());
        }
    } else {
        stringBuilder.append("closure at line ").append(sourceInfo.getLineNumber()).append(" of ").append(sourceInfo.getUri());
    }
}
Also used : CtClass(javassist.CtClass) ClassFile(javassist.bytecode.ClassFile) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) ClosureUtil(ratpack.groovy.internal.ClosureUtil) CtMethod(javassist.CtMethod)

Example 43 with NotFoundException

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

Example 44 with NotFoundException

use of javassist.NotFoundException in project pinpoint by naver.

the class JavassistClass method addDelegatorMethod.

@Override
public InstrumentMethod addDelegatorMethod(String methodName, String... paramTypes) throws InstrumentException {
    if (getCtMethod0(ctClass, methodName, paramTypes) != null) {
        throw new InstrumentException(getName() + "already have method(" + methodName + ").");
    }
    try {
        final CtClass superClass = ctClass.getSuperclass();
        CtMethod superMethod = getCtMethod0(superClass, methodName, paramTypes);
        if (superMethod == null) {
            throw new NotFoundInstrumentException(methodName + Arrays.toString(paramTypes) + " is not found in " + superClass.getName());
        }
        CtMethod delegatorMethod = CtNewMethod.delegator(superMethod, ctClass);
        ctClass.addMethod(delegatorMethod);
        return new JavassistMethod(objectBinderFactory, pluginContext, interceptorRegistryBinder, apiMetaDataService, this, delegatorMethod);
    } catch (NotFoundException ex) {
        throw new InstrumentException(getName() + "don't have super class(" + getSuperClass() + "). Cause:" + ex.getMessage(), ex);
    } catch (CannotCompileException ex) {
        throw new InstrumentException(methodName + " addDelegatorMethod fail. Cause:" + ex.getMessage(), ex);
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 45 with NotFoundException

use of javassist.NotFoundException in project pinpoint by naver.

the class LegacyProfilerPluginClassInjector method loadFromOtherClassLoader.

private Class<?> loadFromOtherClassLoader(ClassPool pool, ClassLoader classLoader, String className) throws NotFoundException, IOException, CannotCompileException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Class<?> c = null;
    try {
        c = classLoader.loadClass(className);
    } catch (ClassNotFoundException ignore) {
    }
    if (c != null) {
        return c;
    }
    CtClass ct = pool.get(className);
    if (ct == null) {
        throw new NotFoundException(className);
    }
    CtClass superClass = ct.getSuperclass();
    if (superClass != null) {
        loadFromOtherClassLoader(pool, classLoader, superClass.getName());
    }
    CtClass[] interfaces = ct.getInterfaces();
    for (CtClass i : interfaces) {
        loadFromOtherClassLoader(pool, classLoader, i.getName());
    }
    Collection<String> refs = ct.getRefClasses();
    for (String ref : refs) {
        try {
            loadFromOtherClassLoader(pool, classLoader, ref);
        } catch (NotFoundException e) {
            logger.warn("Skip a referenced class because of NotFoundException : ", e);
        }
    }
    byte[] bytes = ct.toBytecode();
    return (Class<?>) DEFINE_CLASS.invoke(classLoader, ct.getName(), bytes, 0, bytes.length);
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CtClass(javassist.CtClass)

Aggregations

NotFoundException (javassist.NotFoundException)90 CtClass (javassist.CtClass)64 CannotCompileException (javassist.CannotCompileException)42 CtMethod (javassist.CtMethod)36 ClassPool (javassist.ClassPool)32 IOException (java.io.IOException)19 CtField (javassist.CtField)16 FileNotFoundException (java.io.FileNotFoundException)9 CtConstructor (javassist.CtConstructor)9 File (java.io.File)7 Method (java.lang.reflect.Method)7 ClassFile (javassist.bytecode.ClassFile)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 BadBytecode (javassist.bytecode.BadBytecode)6 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 MethodUsage (com.github.javaparser.resolution.MethodUsage)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)4 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)4 Context (com.github.javaparser.symbolsolver.core.resolution.Context)4