Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functions for getting the parameter names of method #357

Merged
merged 2 commits into from Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 getConstPool().getUtf8Info(nameIndex(i));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had better use 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit tests for the changes.

}

/**
* Returns the value of <code>access_flags</code> of the i-th element of <code>parameters</code>.
*
Expand Down