From: eshizhan Date: Tue, 26 Jan 2021 09:39:20 +0000 (+0800) Subject: add functions for getting the parameter names of method X-Git-Tag: rel_3_28_0_ga~11^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=084c8e89b5d40bb739b7a6dee61a89452d679c1c;p=javassist.git add functions for getting the parameter names of method --- diff --git a/src/main/javassist/bytecode/LocalVariableAttribute.java b/src/main/javassist/bytecode/LocalVariableAttribute.java index a0a62cc3..c1106be1 100644 --- a/src/main/javassist/bytecode/LocalVariableAttribute.java +++ b/src/main/javassist/bytecode/LocalVariableAttribute.java @@ -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 * local_variable_table[i].descriptor_index. 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 @@ -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 parameters. + * @param i the position of the parameter. + */ + public String parameterName(int i) { + return getConstPool().getUtf8Info(name(i)); + } + /** * Returns the value of access_flags of the i-th element of parameters. *