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

The following examples show how to use javassist.bytecode.LocalVariableAttribute#tableLength() . 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: 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 2
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 3
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 4
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 5
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);
    }
}