Browse Source

Merge pull request #357 from eshizhan/master

add functions for getting the parameter names of method
tags/rel_3_28_0_ga
Shigeru Chiba 3 years ago
parent
commit
fc728c4989
No account linked to committer's email address

+ 16
- 0
src/main/javassist/bytecode/LocalVariableAttribute.java View File

@@ -217,6 +217,22 @@ public class LocalVariableAttribute extends AttributeInfo {
return getConstPool().getUtf8Info(nameIndex(i));
}

/**
* Returns the name of the local variable with given index.
* If you want get the parameter name of method with correct order,
* should using this method.
*
* @param index the index of the local variable.
*/
public String variableNameByIndex(int index) {
for (int i = 0; i < tableLength(); i++) {
if (index(i) == index) {
return variableName(i);
}
}
throw new ArrayIndexOutOfBoundsException();
}

/**
* Returns the value of
* <code>local_variable_table[i].descriptor_index</code>.

+ 8
- 0
src/main/javassist/bytecode/MethodParametersAttribute.java View File

@@ -56,6 +56,14 @@ public class MethodParametersAttribute extends AttributeInfo {
return ByteArray.readU16bit(info, i * 4 + 1);
}

/**
* Returns the name of the i-th element of <code>parameters</code>.
* @param i the position of the parameter.
*/
public String parameterName(int i) {
return getConstPool().getUtf8Info(name(i));
}

/**
* Returns the value of <code>access_flags</code> of the i-th element of <code>parameters</code>.
*

+ 4
- 0
src/test/javassist/JvstTest4.java View File

@@ -1019,11 +1019,15 @@ public class JvstTest4 extends JvstTestRoot {
assertEquals(2, attr.size());
assertEquals("i", cp.getUtf8Info(attr.name(0)));
assertEquals("s", cp.getUtf8Info(attr.name(1)));
assertEquals("i", attr.parameterName(0));
assertEquals("s", attr.parameterName(1));

attr = (MethodParametersAttribute)attr.copy(cp, null);
assertEquals(2, attr.size());
assertEquals("i", cp.getUtf8Info(attr.name(0)));
assertEquals("s", cp.getUtf8Info(attr.name(1)));
assertEquals("i", attr.parameterName(0));
assertEquals("s", attr.parameterName(1));
}

// JIRA JASSIST-220

+ 3
- 0
src/test/javassist/bytecode/BytecodeTest.java View File

@@ -354,6 +354,9 @@ public class BytecodeTest extends TestCase {
assertEquals("I", ainfo2.descriptor(i));
}
print("**** end ***");

assertEquals("this", ainfo2.variableNameByIndex(0));
assertEquals("i", ainfo2.variableNameByIndex(1));
}

public void testAnnotations() throws Exception {

Loading…
Cancel
Save