Java Code Examples for javassist.bytecode.LocalVariableAttribute#variableName()

The following examples show how to use javassist.bytecode.LocalVariableAttribute#variableName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReflectUtil.java    From koper with Apache License 2.0 7 votes vote down vote up
/**
 * Get method arg names.
 *
 * @param clazz
 * @param methodName
 * @return
 */
public static <T> String[] getMethodArgNames(Class<T> clazz, String methodName) {
    try {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get(clazz.getName());

        CtMethod cm = cc.getDeclaredMethod(methodName);

        // 使用javaassist的反射方法获取方法的参数名
        MethodInfo methodInfo = cm.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        if (attr == null) {
            throw new RuntimeException("LocalVariableAttribute of method is null! Class " + clazz.getName() + ",method name is " + methodName);
        }
        String[] paramNames = new String[cm.getParameterTypes().length];
        int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
        for (int i = 0; i < paramNames.length; i++)
            paramNames[i] = attr.variableName(i + pos);

        return paramNames;
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example 2
Source File: ParamInfo.java    From ModTheSpire with MIT License 6 votes vote down vote up
private static String findName(CtBehavior ctBehavior, int position)
{
    MethodInfo methodInfo = ctBehavior.getMethodInfo2();
    LocalVariableAttribute table = (LocalVariableAttribute) methodInfo.getCodeAttribute().getAttribute(LocalVariableAttribute.tag);
    if (table != null) {
        int j = 0;
        for (int i=0; i<table.tableLength(); ++i) {
            if (table.startPc(i) == 0) {
                if (j == 0 && !table.variableName(i).equals("this")) {
                    ++j; // Skip to position 1 if `this` doesn't exist (static method)
                }
                if (j == position) {
                    return table.variableName(i);
                }
                ++j;
            }
        }
    }
    return null;
}
 
Example 3
Source File: ReflectionUtils.java    From cola-cloud with MIT License 6 votes vote down vote up
private static String[] getMethodParamNames(CtMethod ctMethod) throws RuntimeException {
    CtClass ctClass = ctMethod.getDeclaringClass();
    MethodInfo methodInfo = ctMethod.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    if (attr == null) {
        throw new RuntimeException(ctClass.getName());
    }
    try {
        String[] paramNames = new String[ctMethod.getParameterTypes().length];
        int pos = Modifier.isStatic(ctMethod.getModifiers()) ? 0 : 1;
        for (int i = 0; i < paramNames.length; i++) {
            paramNames[i] = attr.variableName(i + pos);
        }
        return paramNames;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: ClassHelper.java    From TakinRPC with Apache License 2.0 6 votes vote down vote up
/**
 * get method parameter names
 * @param cls
 * @param method
 * @return
 * @throws Exception
 */
public static String[] getParamNames(Class<?> cls, Method method) throws Exception {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get(cls.getName());

    Class<?>[] paramAry = method.getParameterTypes();
    String[] paramTypeNames = new String[paramAry.length];
    for (int i = 0; i < paramAry.length; i++) {
        paramTypeNames[i] = paramAry[i].getName();
    }

    CtMethod cm = cc.getDeclaredMethod(method.getName(), pool.get(paramTypeNames));

    MethodInfo methodInfo = cm.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    if (attr == null) {
        throw new Exception("class:" + cls.getName() + ", have no LocalVariableTable, please use javac -g:{vars} to compile the source file");
    }
    String[] paramNames = new String[cm.getParameterTypes().length];
    int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
    for (int i = 0; i < paramNames.length; i++) {
        paramNames[i] = attr.variableName(i + pos);
    }
    return paramNames;
}
 
Example 5
Source File: BeanUtil.java    From framework with Apache License 2.0 6 votes vote down vote up
/**
 * 获取方法参数名称
 * 
 * @param cm <br>
 * @return <br>
 * @throws NotFoundException <br>
 * @throws UtilException 如果最终编译的class文件不包含局部变量表信息 <br>
 */
protected static String[] getMethodParamNames(final CtMethod cm) throws NotFoundException, UtilException {
    CtClass cc = cm.getDeclaringClass();
    MethodInfo methodInfo = cm.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    if (codeAttribute == null) {
        throw new UtilException(ErrorCodeDef.CAN_NOT_FIND_VER_NAME_10003, cc.getName());
    }
    LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    if (null == attr) {
        throw new UtilException(ErrorCodeDef.CAN_NOT_FIND_VER_NAME_10003, cc.getName());
    }
    String[] paramNames = new String[cm.getParameterTypes().length];
    int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
    for (int i = 0; i < paramNames.length; i++) {
        paramNames[i] = attr.variableName(i + pos);
    }
    return paramNames;
}
 
Example 6
Source File: ClassUtil.java    From common-project with Apache License 2.0 5 votes vote down vote up
/**
 * 基于javassist获取类方法参数名称
 * @param method
 * @return
 * @throws Exception
 */
public static String[] getMethodParamsName(Method method) throws Exception{
    Class<?> clazz = method.getDeclaringClass();
    ClassPool pool = ClassPool.getDefault();
    CtClass clz = pool.get(clazz.getName());
    CtClass[] params = new CtClass[method.getParameterTypes().length];
    for (int i = 0; i < method.getParameterTypes().length; i++) {
        params[i] = pool.getCtClass(method.getParameterTypes()[i].getName());
    }
    CtMethod cm = clz.getDeclaredMethod(method.getName(), params);
    MethodInfo methodInfo = cm.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    if (codeAttribute==null) {
        return null;
    }
    LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
            .getAttribute(LocalVariableAttribute.tag);
    int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
    String[] paramNames = new String[cm.getParameterTypes().length];
    if (attr==null) {
        return null;
    }
    for (int i = 0; i < paramNames.length; i++) {
        paramNames[i] = attr.variableName(i + pos);
    }
    return paramNames;
}