Search in sources :

Example 11 with CodeAttribute

use of javassist.bytecode.CodeAttribute in project pinpoint by naver.

the class JavassistMethod method insertCatch.

private void insertCatch(int from, String src, CtClass exceptionType, String exceptionName) throws CannotCompileException {
    CtClass cc = behavior.getDeclaringClass();
    ConstPool cp = behavior.getMethodInfo().getConstPool();
    CodeAttribute ca = behavior.getMethodInfo().getCodeAttribute();
    CodeIterator iterator = ca.iterator();
    Bytecode b = new Bytecode(cp, ca.getMaxStack(), ca.getMaxLocals());
    b.setStackDepth(1);
    Javac jv = new Javac(b, cc);
    try {
        jv.recordParams(behavior.getParameterTypes(), Modifier.isStatic(getModifiers()));
        jv.recordLocalVariables(ca, from);
        int var = jv.recordVariable(exceptionType, exceptionName);
        b.addAstore(var);
        jv.compileStmnt(src);
        int stack = b.getMaxStack();
        int locals = b.getMaxLocals();
        if (stack > ca.getMaxStack())
            ca.setMaxStack(stack);
        if (locals > ca.getMaxLocals())
            ca.setMaxLocals(locals);
        int len = iterator.getCodeLength();
        int pos = iterator.append(b.get());
        ca.getExceptionTable().add(from, len, len, cp.addClassInfo(exceptionType));
        iterator.append(b.getExceptionTable(), pos);
        behavior.getMethodInfo().rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2());
    } 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) ConstPool(javassist.bytecode.ConstPool) 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 12 with CodeAttribute

use of javassist.bytecode.CodeAttribute 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 13 with CodeAttribute

use of javassist.bytecode.CodeAttribute 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 14 with CodeAttribute

use of javassist.bytecode.CodeAttribute in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

/**
 * 获取方法的参数名称.
 *
 * @param ctMethod
 * @return
 * @throws NotFoundException
 */
public static String[] getParameterNames(CtMethod ctMethod) throws NotFoundException {
    MethodInfo methodInfo = ctMethod.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    // logger.info("methodInfo.getConstPool().getSize():");
    LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    // String[] names = new String[attribute.tableLength() - 1];
    String[] paramNames = new String[ctMethod.getParameterTypes().length];
    int pos = 0;
    if (true) {
        int size = attribute.tableLength();
        if (size > 0) {
            String[] names = new String[size - 1];
            for (int i = 0; i < names.length; i++) {
                names[i] = attribute.variableName(i);
                if ("this".equals(names[i])) {
                    pos = i + 1;
                    break;
                }
            }
        // logger.info(methodInfo.getName() + " pos:" + pos + " allNames:" + StringUtils.join(names, ", "));
        }
    }
    // logger.info(methodInfo.getName() + " pos:" + pos);
    for (int i = 0; i < paramNames.length; i++) {
        // paramNames[i] = attribute.variableName(i + 1);
        try {
            paramNames[i] = attribute.variableName(i + pos);
        // logger.info("paramNames[" + i + "]:" + paramNames[i]);
        } catch (RuntimeException e) {
            throw e;
        }
    }
    // System.err.println("paramNames:" + StringUtils.join(paramNames));
    return paramNames;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute)

Example 15 with CodeAttribute

use of javassist.bytecode.CodeAttribute in project duangframework by tcrct.

the class AutoBuildServiceInterface method getLocalVariableAttributeName.

/**
 * 反射取出方法里的参数名
 * @param cc                类对象
 * @param method        方法名
 * @return      方法名集合
 * @throws Exception
 */
private static List<String> getLocalVariableAttributeName(CtClass cc, Method method) throws Exception {
    List<String> paramNames = null;
    try {
        CtMethod cm = cc.getDeclaredMethod(method.getName());
        MethodInfo methodInfo = cm.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        if (attr != null) {
            int size = cm.getParameterTypes().length;
            paramNames = new ArrayList<>(size);
            int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
            for (int i = 0; i < size; i++) {
                paramNames.add(attr.variableName(i + pos));
            }
        }
    } catch (NotFoundException e) {
        throw new RpcException(e.getMessage(), e);
    }
    return paramNames;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute) CtMethod(javassist.CtMethod)

Aggregations

CodeAttribute (javassist.bytecode.CodeAttribute)21 MethodInfo (javassist.bytecode.MethodInfo)17 Bytecode (javassist.bytecode.Bytecode)11 BadBytecode (javassist.bytecode.BadBytecode)10 LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)9 CodeIterator (javassist.bytecode.CodeIterator)6 CtClass (javassist.CtClass)5 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)5 NotFoundException (javassist.NotFoundException)4 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 CannotCompileException (javassist.CannotCompileException)3 ClassPool (javassist.ClassPool)3 CtMethod (javassist.CtMethod)3 ClassFile (javassist.bytecode.ClassFile)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 Method (java.lang.reflect.Method)2 List (java.util.List)2 ConstPool (javassist.bytecode.ConstPool)2