Skip to content

Commit

Permalink
fix: fix incorrect parent for type reference of a CtParameter (INRIA#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbwogi authored Nov 24, 2020
1 parent c305bc7 commit e436d36
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/spoon/reflect/factory/ExecutableFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ public <T> CtParameter<T> createParameter(CtExecutable<?> parent, CtTypeReferenc
public <T> CtParameterReference<T> createParameterReference(CtParameter<T> parameter) {
CtParameterReference<T> ref = factory.Core().createParameterReference();
ref.setSimpleName(parameter.getSimpleName());
ref.setType(parameter.getType());

// true when parameter comes from a lambda expression
if (parameter.getType() == null) {
ref.setType(parameter.getType());
} else {
ref.setType(parameter.getType().clone());
}
return ref;
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/spoon/test/parameters/ParameterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,15 @@ public void testMultiParameterLambdaTypeReference() {
assertNull(refType);
}
}

@Test
public void testGetParentAfterGetParameterReference() {
// contract: after getting a parameter reference, the parent of the parameter type reference should still be the parameter itself
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/resources/parameter/ParameterResource.java");
spoon.buildModel();
CtParameter parameter = spoon.getModel().getRootPackage().getElements(new TypeFilter<>(CtParameter.class)).get(0);
CtParameterReference pref = parameter.getReference();
assertEquals(parameter, parameter.getType().getParent());
}
}
3 changes: 3 additions & 0 deletions src/test/resources/parameter/ParameterResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class ParameterResource {
public void empty(int parameter) { }
}

0 comments on commit e436d36

Please sign in to comment.