Java Code Examples for javassist.bytecode.CodeAttribute#getAttribute()

The following examples show how to use javassist.bytecode.CodeAttribute#getAttribute() . 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: 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 3
Source File: MethodParameterName.java    From Summer with Apache License 2.0 6 votes vote down vote up
public String[] getParameterNameByMethod(Method method) throws NotFoundException {
	CtMethod ctm = ct.getDeclaredMethod(method.getName());
	MethodInfo methodInfo = ctm.getMethodInfo();
	CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
	LocalVariableAttribute attr = (LocalVariableAttribute)codeAttribute.getAttribute(LocalVariableAttribute.tag);
	String[] params = null;
	if (attr != null) {
		int len = ctm.getParameterTypes().length;
		params = new String[len];
		TreeMap<Integer, String> sortMap = Maps.newTreeMap();
		for (int i = 0; i < attr.tableLength(); i++)
			sortMap.put(attr.index(i), attr.variableName(i));
		int pos = Modifier.isStatic(ctm.getModifiers()) ? 0 : 1;
		params = Arrays.copyOfRange(sortMap.values().toArray(new String[0]), pos, params.length + pos);
	}
	return params;
}
 
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: HandlerMethodArgsBuilder.java    From potato-webmvc with MIT License 6 votes vote down vote up
private void initMethodParamInfo(Object controller, String methodName) {
	methodParamInfo = new LinkedHashMap<Class<?>, String>();
	Class<?> clazz = controller.getClass();
	try {
		ClassPool classPool = ClassPool.getDefault();
		classPool.insertClassPath(new ClassClassPath(clazz));
		CtClass ctClass = classPool.get(clazz.getName());
		CtMethod ctMethod = ctClass.getDeclaredMethod(methodName);

		MethodInfo methodInfo = ctMethod.getMethodInfo();
		CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
		LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
				.getAttribute("LocalVariableTable");

		CtClass[] parameterTypes = ctMethod.getParameterTypes();
		int pos = Modifier.isStatic(ctMethod.getModifiers()) ? 0 : 1;
		for (int i = 0; i < parameterTypes.length; i++)
			methodParamInfo.put(Class.forName(parameterTypes[i].getName()),
					attr.variableName(i + pos));
	} catch (Exception e) {
		throw new RuntimeException(e.getMessage());
	}
}
 
Example 6
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 7
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
@Override
public LocalVariableTable getLocalVariableTable(Signature methodSignature) 
throws MethodNotFoundException, MethodCodeNotFoundException  {
    final CodeAttribute ca = getMethodCodeAttribute(methodSignature);
    final LocalVariableAttribute lvtJA = (LocalVariableAttribute) ca.getAttribute(LocalVariableAttribute.tag);

    if (lvtJA == null) {
        return defaultLocalVariableTable(methodSignature);
    }

    //builds the local variable table from the LocalVariableTable attribute 
    //information; this has always success
    final LocalVariableTable lvt = new LocalVariableTable(ca.getMaxLocals());
    for (int i = 0; i < lvtJA.tableLength(); ++i) {
        lvt.addRow(lvtJA.index(i), lvtJA.descriptor(i), 
                     lvtJA.variableName(i), lvtJA.startPc(i),  lvtJA.codeLength(i));
    }
    return lvt;
}
 
Example 8
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
@Override
public LocalVariableTable getLocalVariableTypeTable(Signature methodSignature)
throws MethodNotFoundException, MethodCodeNotFoundException {
    final CodeAttribute ca = getMethodCodeAttribute(methodSignature);
    final LocalVariableTypeAttribute lvttJA = (LocalVariableTypeAttribute) ca.getAttribute(LocalVariableTypeAttribute.tag);

    if (lvttJA == null) {
        return new LocalVariableTable(0);
    }

    //builds the local variable type table from the LocalVariableTypeTable attribute 
    //information; this has always success
    final LocalVariableTable lvt = new LocalVariableTable(ca.getMaxLocals());
    for (int i = 0; i < lvttJA.tableLength(); ++i) {
        lvt.addRow(lvttJA.index(i), lvttJA.signature(i), 
                     lvttJA.variableName(i), lvttJA.startPc(i),  lvttJA.codeLength(i));
    }
    return lvt;
}
 
Example 9
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;
}
 
Example 10
Source File: Dagger2Extension.java    From EasyMVP with Apache License 2.0 5 votes vote down vote up
private void deleteLine(CtMethod method, int lineNumberToReplace) {
    CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();
    LineNumberAttribute
            lineNumberAttribute = (LineNumberAttribute) codeAttribute.getAttribute(LineNumberAttribute.tag);
    int startPc = lineNumberAttribute.toStartPc(lineNumberToReplace);
    int endPc = lineNumberAttribute.toStartPc(lineNumberToReplace + 1);
    byte[] code = codeAttribute.getCode();
    for (int i = startPc; i < endPc; i++) {
        code[i] = CodeAttribute.NOP;
    }
}
 
Example 11
Source File: LocalvariablesNamesEnhancer.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public static List<String> lookupParameterNames(Constructor constructor) {
    try {
        List<String> parameters = new ArrayList<String>();

        ClassPool classPool = newClassPool();
        CtClass ctClass = classPool.get(constructor.getDeclaringClass().getName());
        CtClass[] cc = new CtClass[constructor.getParameterTypes().length];
        for (int i = 0; i < constructor.getParameterTypes().length; i++) {
            cc[i] = classPool.get(constructor.getParameterTypes()[i].getName());
        }
        CtConstructor ctConstructor = ctClass.getDeclaredConstructor(cc);

        // Signatures names
        CodeAttribute codeAttribute = (CodeAttribute) ctConstructor.getMethodInfo().getAttribute("Code");
        if (codeAttribute != null) {
            LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute.getAttribute("LocalVariableTable");
            if (localVariableAttribute != null && localVariableAttribute.tableLength() >= ctConstructor.getParameterTypes().length) {
                for (int i = 0; i < ctConstructor.getParameterTypes().length + 1; i++) {
                    String name = localVariableAttribute.getConstPool().getUtf8Info(localVariableAttribute.nameIndex(i));
                    if (!name.equals("this")) {
                        parameters.add(name);
                    }
                }
            }
        }

        return parameters;
    } catch (Exception e) {
        throw new UnexpectedException("Cannot extract parameter names", e);
    }
}
 
Example 12
Source File: LocalvariablesNamesEnhancer.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public static List<String> lookupParameterNames(Method method) {
    try {
        List<String> parameters = new ArrayList<String>();

        ClassPool classPool = newClassPool();
        CtClass ctClass = classPool.get(method.getDeclaringClass().getName());
        CtClass[] cc = new CtClass[method.getParameterTypes().length];
        for (int i = 0; i < method.getParameterTypes().length; i++) {
            cc[i] = classPool.get(method.getParameterTypes()[i].getName());
        }
        CtMethod ctMethod = ctClass.getDeclaredMethod(method.getName(),cc);

        // Signatures names
        CodeAttribute codeAttribute = (CodeAttribute) ctMethod.getMethodInfo().getAttribute("Code");
        if (codeAttribute != null) {
            LocalVariableAttribute localVariableAttribute = (LocalVariableAttribute) codeAttribute.getAttribute("LocalVariableTable");
            if (localVariableAttribute != null && localVariableAttribute.tableLength() >= ctMethod.getParameterTypes().length) {
                for (int i = 0; i < ctMethod.getParameterTypes().length + 1; i++) {
                    String name = localVariableAttribute.getConstPool().getUtf8Info(localVariableAttribute.nameIndex(i));
                    if (!name.equals("this")) {
                        parameters.add(name);
                    }
                }
            }
        }

        return parameters;
    } catch (Exception e) {
        throw new UnexpectedException("Cannot extract parameter names", e);
    }
}
 
Example 13
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public LineNumberTable getLineNumberTable(Signature methodSignature) 
throws MethodNotFoundException, MethodCodeNotFoundException {
    final CodeAttribute ca = this.getMethodCodeAttribute(methodSignature);
    final LineNumberAttribute lnJA = (LineNumberAttribute) ca.getAttribute("LineNumberTable");

    if (lnJA == null) {
        return defaultLineNumberTable();
    }
    final LineNumberTable LN = new LineNumberTable(lnJA.tableLength());
    for (int i = 0; i < lnJA.tableLength(); ++i) {
        LN.addRow(lnJA.startPc(i), lnJA.lineNumber(i));
    }
    return LN;
}