Search in sources :

Example 46 with CtClass

use of javassist.CtClass in project jvm-profiler by uber-common.

the class JavaAgentFileTransformer method transformImpl.

private byte[] transformImpl(ClassLoader loader, String className, byte[] classfileBuffer) {
    if (durationProfilingFilter.isEmpty() && argumentFilterProfilingFilter.isEmpty()) {
        return null;
    }
    String normalizedClassName = className.replaceAll("/", ".");
    logger.debug("Checking class for transform: " + normalizedClassName);
    if (!durationProfilingFilter.matchClass(normalizedClassName) && !argumentFilterProfilingFilter.matchClass(normalizedClassName)) {
        return null;
    }
    byte[] byteCode;
    logger.info("Transforming class: " + normalizedClassName);
    try {
        ClassPool classPool = new ClassPool();
        classPool.appendClassPath(new LoaderClassPath(loader));
        final CtClass ctClass;
        try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(classfileBuffer)) {
            ctClass = classPool.makeClass(byteArrayInputStream);
        }
        CtMethod[] ctMethods = ctClass.getDeclaredMethods();
        for (CtMethod ctMethod : ctMethods) {
            boolean enableDurationProfiling = durationProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
            List<Integer> enableArgumentProfiler = argumentFilterProfilingFilter.matchMethod(ctClass.getName(), ctMethod.getName());
            transformMethod(normalizedClassName, ctMethod, enableDurationProfiling, enableArgumentProfiler);
        }
        byteCode = ctClass.toBytecode();
        ctClass.detach();
    } catch (Throwable ex) {
        ex.printStackTrace();
        logger.warn("Failed to transform class: " + normalizedClassName, ex);
        byteCode = null;
    }
    return byteCode;
}
Also used : CtClass(javassist.CtClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) CtMethod(javassist.CtMethod)

Example 47 with CtClass

use of javassist.CtClass in project uavstack by uavorg.

the class AbstractAdaptor method inject.

protected byte[] inject(String className, String[] importPackages, AdaptorProcessor... p) {
    try {
        CtClass cc = pool.get(className);
        if (importPackages != null) {
            for (String ip : importPackages) {
                pool.importPackage(ip);
            }
        }
        for (AdaptorProcessor ap : p) {
            CtMethod mthd = null;
            try {
                if (ap.getParamNum() == -1) {
                    mthd = cc.getDeclaredMethod(ap.getMethodName());
                } else {
                    CtMethod[] mlist = cc.getDeclaredMethods();
                    for (CtMethod m : mlist) {
                        if (m.getParameterTypes().length == ap.getParamNum() && m.getName().equals(ap.getMethodName())) {
                            mthd = m;
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                continue;
            }
            if (mthd == null) {
                continue;
            }
            ap.process(mthd);
        }
        byte[] code = cc.toBytecode();
        cc.detach();
        return code;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException)

Example 48 with CtClass

use of javassist.CtClass in project uavstack by uavorg.

the class AbstractAdaptor method addLocalVar.

protected void addLocalVar(CtMethod m, String varName, String varClassName) {
    try {
        CtClass cc = pool.get(varClassName);
        m.addLocalVariable(varName, cc);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CtClass(javassist.CtClass) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException)

Example 49 with CtClass

use of javassist.CtClass in project uavstack by uavorg.

the class TtlAdaptor method getCtClass.

private CtClass getCtClass(byte[] classFileBuffer, ClassLoader classLoader) throws IOException {
    ClassPool classPool = new ClassPool(true);
    if (null != classLoader) {
        classPool.appendClassPath(new LoaderClassPath(classLoader));
    }
    CtClass clazz = classPool.makeClass(new ByteArrayInputStream(classFileBuffer), false);
    clazz.defrost();
    return clazz;
}
Also used : CtClass(javassist.CtClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Example 50 with CtClass

use of javassist.CtClass in project uavstack by uavorg.

the class TtlAdaptor method updateMethod.

static void updateMethod(CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException {
    if (!updateMethodNames.contains(method.getName())) {
        return;
    }
    if (method.getDeclaringClass() != clazz) {
        return;
    }
    final int modifiers = method.getModifiers();
    if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
        return;
    }
    CtClass[] parameterTypes = method.getParameterTypes();
    StringBuilder insertCode = new StringBuilder();
    for (int i = 0; i < parameterTypes.length; i++) {
        CtClass paraType = parameterTypes[i];
        if (RUNNABLE_CLASS_NAME.equals(paraType.getName())) {
            String code = String.format("$%d = %s.get($%d, false, true);", i + 1, TTL_RUNNABLE_CLASS_NAME, i + 1);
            insertCode.append(code);
        } else if (CALLABLE_CLASS_NAME.equals(paraType.getName())) {
            String code = String.format("$%d = %s.get($%d, false, true);", i + 1, TTL_CALLABLE_CLASS_NAME, i + 1);
            insertCode.append(code);
        }
    }
    if (insertCode.length() > 0) {
        method.insertBefore(insertCode.toString());
    }
}
Also used : CtClass(javassist.CtClass)

Aggregations

CtClass (javassist.CtClass)271 CtMethod (javassist.CtMethod)96 ClassPool (javassist.ClassPool)93 NotFoundException (javassist.NotFoundException)85 Test (org.junit.Test)63 CannotCompileException (javassist.CannotCompileException)62 CtField (javassist.CtField)53 IOException (java.io.IOException)35 CtConstructor (javassist.CtConstructor)26 Method (java.lang.reflect.Method)17 LoaderClassPath (javassist.LoaderClassPath)16 ClassFile (javassist.bytecode.ClassFile)14 ArrayList (java.util.ArrayList)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)11 ConstPool (javassist.bytecode.ConstPool)11 File (java.io.File)8 FileNotFoundException (java.io.FileNotFoundException)8 Collectors (java.util.stream.Collectors)8 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)7