diff options
author | Shigeru Chiba <chibash@users.noreply.github.com> | 2021-01-30 10:57:46 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-30 10:57:46 +0900 |
commit | fc728c498942324eeae686c7a0077d68b3861e34 (patch) | |
tree | c6456844b635ebf4b644e5defef6259157289fa2 | |
parent | 1c4e31b9677d020a1e89aeebc6686396f9e6c68a (diff) | |
parent | 5bccb19933b3eda51518d79ca52a06b89d304bf6 (diff) | |
download | javassist-fc728c498942324eeae686c7a0077d68b3861e34.tar.gz javassist-fc728c498942324eeae686c7a0077d68b3861e34.zip |
Merge pull request #357 from eshizhan/master
add functions for getting the parameter names of method
-rw-r--r-- | src/main/javassist/bytecode/LocalVariableAttribute.java | 16 | ||||
-rw-r--r-- | src/main/javassist/bytecode/MethodParametersAttribute.java | 8 | ||||
-rw-r--r-- | src/test/javassist/JvstTest4.java | 4 | ||||
-rw-r--r-- | src/test/javassist/bytecode/BytecodeTest.java | 3 |
4 files changed, 31 insertions, 0 deletions
diff --git a/src/main/javassist/bytecode/LocalVariableAttribute.java b/src/main/javassist/bytecode/LocalVariableAttribute.java index a0a62cc3..dcee7965 100644 --- a/src/main/javassist/bytecode/LocalVariableAttribute.java +++ b/src/main/javassist/bytecode/LocalVariableAttribute.java @@ -218,6 +218,22 @@ public class LocalVariableAttribute extends AttributeInfo { } /** + * 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>. * This represents the type descriptor of the local variable. diff --git a/src/main/javassist/bytecode/MethodParametersAttribute.java b/src/main/javassist/bytecode/MethodParametersAttribute.java index 12521095..b9c252a9 100644 --- a/src/main/javassist/bytecode/MethodParametersAttribute.java +++ b/src/main/javassist/bytecode/MethodParametersAttribute.java @@ -57,6 +57,14 @@ public class MethodParametersAttribute extends AttributeInfo { } /** + * 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>. * * @param i the position of the parameter. diff --git a/src/test/javassist/JvstTest4.java b/src/test/javassist/JvstTest4.java index d53148bd..259451b9 100644 --- a/src/test/javassist/JvstTest4.java +++ b/src/test/javassist/JvstTest4.java @@ -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 diff --git a/src/test/javassist/bytecode/BytecodeTest.java b/src/test/javassist/bytecode/BytecodeTest.java index 5ddf5d5b..7aef1cce 100644 --- a/src/test/javassist/bytecode/BytecodeTest.java +++ b/src/test/javassist/bytecode/BytecodeTest.java @@ -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 { |