Search in sources :

Example 6 with LocalVariableAttribute

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

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

Example 8 with LocalVariableAttribute

use of javassist.bytecode.LocalVariableAttribute in project yyl_example by Relucent.

the class GetMethodParamNameTest method getMethodParamNames.

/**
 * 获得参数名 (JDK 自带类 ,接口方法和抽象方法无法正确获取参数名)
 */
private static String[] getMethodParamNames(final Method method) throws NotFoundException {
    final String methodName = method.getName();
    final Class<?>[] methodParameterTypes = method.getParameterTypes();
    final int methodParameterCount = methodParameterTypes.length;
    final String className = method.getDeclaringClass().getName();
    final boolean isStatic = Modifier.isStatic(method.getModifiers());
    final String[] methodParametersNames = new String[methodParameterCount];
    ClassPool pool = ClassPool.getDefault();
    CtClass ctClass = pool.get(className);
    CtClass[] ctTypes = new CtClass[methodParameterTypes.length];
    for (int i = 0; i < methodParameterCount; i++) {
        ctTypes[i] = pool.get(methodParameterTypes[i].getName());
    }
    CtMethod ctMethod = ctClass.getDeclaredMethod(methodName, ctTypes);
    MethodInfo methodInfo = ctMethod.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    // 如果是静态方法,第一个参数就是方法参数,非静态方法,则第一个参数是 this ,然后才是方法的参数
    if (attribute != null) {
        int variableCount = isStatic ? methodParameterCount : methodParameterCount + 1;
        for (int index = 0; index < variableCount; index++) {
            int methodParameterIndex = isStatic ? index : index - 1;
            if (0 <= methodParameterIndex && methodParameterIndex < methodParameterCount) {
                methodParametersNames[methodParameterIndex] = attribute.variableName(index);
            }
        }
    }
    return methodParametersNames;
}
Also used : ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) CodeAttribute(javassist.bytecode.CodeAttribute) CtClass(javassist.CtClass) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute) CtMethod(javassist.CtMethod)

Aggregations

LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)8 MethodInfo (javassist.bytecode.MethodInfo)8 CodeAttribute (javassist.bytecode.CodeAttribute)7 ArrayList (java.util.ArrayList)2 CtMethod (javassist.CtMethod)2 RpcException (com.duangframework.core.exceptions.RpcException)1 Modifier (java.lang.reflect.Modifier)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 NotFoundException (javassist.NotFoundException)1 AttributeInfo (javassist.bytecode.AttributeInfo)1 ClassFile (javassist.bytecode.ClassFile)1 MetadataAdapter (org.reflections.adapters.MetadataAdapter)1 JavassistHelper (org.reflections.util.JavassistHelper)1