]> source.dussan.org Git - javassist.git/commitdiff
add functions for getting the parameter names of method
authoreshizhan <eshizhan@126.com>
Tue, 26 Jan 2021 09:39:20 +0000 (17:39 +0800)
committereshizhan <eshizhan@126.com>
Tue, 26 Jan 2021 09:39:20 +0000 (17:39 +0800)
src/main/javassist/bytecode/LocalVariableAttribute.java
src/main/javassist/bytecode/MethodParametersAttribute.java

index a0a62cc32637dfc9455767f1bd3f53c6d49cbe04..c1106be1735b3a6c8f242aa860da929c6249c94e 100644 (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 getConstPool().getUtf8Info(nameIndex(i));
+            }
+        }
+        throw new ArrayIndexOutOfBoundsException();
+    }
+
     /**
      * Returns the value of
      * <code>local_variable_table[i].descriptor_index</code>.
index 12521095eef2360590175c6be0f35b303325acae..b9c252a9f470cc6ee483d81a30c6b1933ae38bd8 100644 (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>.
      *