Skip to content

Commit

Permalink
Merge pull request #357 from eshizhan/master
Browse files Browse the repository at this point in the history
add functions for getting the parameter names of method
  • Loading branch information
chibash committed Jan 30, 2021
2 parents 1c4e31b + 5bccb19 commit fc728c4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/javassist/bytecode/LocalVariableAttribute.java
Expand Up @@ -217,6 +217,22 @@ public String variableName(int i) {
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>.
Expand Down
8 changes: 8 additions & 0 deletions src/main/javassist/bytecode/MethodParametersAttribute.java
Expand Up @@ -56,6 +56,14 @@ public int name(int i) {
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>.
*
Expand Down
4 changes: 4 additions & 0 deletions src/test/javassist/JvstTest4.java
Expand Up @@ -1019,11 +1019,15 @@ public void testMethodParameters() throws Exception {
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
Expand Down
3 changes: 3 additions & 0 deletions src/test/javassist/bytecode/BytecodeTest.java
Expand Up @@ -354,6 +354,9 @@ public void testLocalVarAttribute() throws Exception {
assertEquals("I", ainfo2.descriptor(i));
}
print("**** end ***");

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

public void testAnnotations() throws Exception {
Expand Down

0 comments on commit fc728c4

Please sign in to comment.