Search in sources :

Example 21 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)

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