From 084c8e89b5d40bb739b7a6dee61a89452d679c1c Mon Sep 17 00:00:00 2001 From: eshizhan Date: Tue, 26 Jan 2021 17:39:20 +0800 Subject: [PATCH] add functions for getting the parameter names of method --- .../bytecode/LocalVariableAttribute.java | 16 ++++++++++++++++ .../bytecode/MethodParametersAttribute.java | 8 ++++++++ 2 files changed, 24 insertions(+) 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. * -- 2.39.5