Skip to content

Commit

Permalink
feat: read parameter names from bytecode if possible (#4972)
Browse files Browse the repository at this point in the history
Co-authored-by: I-Al-Istannen <i-al-istannen@users.noreply.github.com>
  • Loading branch information
danielbobbert and I-Al-Istannen authored Jan 19, 2023
1 parent 2013b69 commit 914b113
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/main/java/spoon/support/visitor/java/reflect/RtParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;

/**
Expand Down Expand Up @@ -143,7 +144,12 @@ public int hashCode() {
public static RtParameter[] parametersOf(RtMethod method) {
RtParameter[] parameters = new RtParameter[method.getParameterTypes().length];
for (int index = 0; index < method.getParameterTypes().length; index++) {
parameters[index] = new RtParameter(null, method.getParameterTypes()[index], method.getGenericParameterTypes()[index], method, null, index);
String name = null;
Parameter parameter = method.getMethod().getParameters()[index];
if (parameter.isNamePresent()) {
name = parameter.getName();
}
parameters[index] = new RtParameter(name, method.getParameterTypes()[index], method.getGenericParameterTypes()[index], method, null, index);
}
return parameters;
}
Expand Down Expand Up @@ -179,7 +185,12 @@ public static RtParameter[] parametersOf(Constructor constructor) {
}

for (int index = 0; index < constructor.getGenericParameterTypes().length; index++) {
parameters[index] = new RtParameter(null, constructor.getParameterTypes()[index + offset], constructor.getGenericParameterTypes()[index], null, constructor, index);
String name = null;
Parameter parameter = constructor.getParameters()[index + offset];
if (parameter.isNamePresent()) {
name = parameter.getName();
}
parameters[index] = new RtParameter(name, constructor.getParameterTypes()[index + offset], constructor.getGenericParameterTypes()[index], null, constructor, index);
}
return parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ void testInnerClassesConstructorParameters() {
assertThat(asClass.getConstructors().iterator().next().getParameters().size(), equalTo(inners.size()));
}

@GitHubIssue(issueNumber = 4972, fixed = false)
@GitHubIssue(issueNumber = 4972, fixed = true)
void parameterNamesAreParsedWhenCompilingWithParametersFlag() throws ClassNotFoundException {
ClassLoader loader = JavacFacade.compileFiles(
Map.of(
Expand Down

0 comments on commit 914b113

Please sign in to comment.