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

fix: fix NPE in ReferenceBuilder#tryRecoverTypeArguments #3737

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,19 +598,22 @@ private void tryRecoverTypeArguments(CtTypeReference<?> type) {
}

AllocationExpression alloc = (AllocationExpression) stack.peek().node;
if (alloc.expectedType() == null || !(alloc.expectedType() instanceof ParameterizedTypeBinding)) {
// the expected type is not available/parameterized if the constructor call occurred in e.g. an unresolved
// method, or in a method that did not expect a parameterized argument
type.addActualTypeArgument(jdtTreeBuilder.getFactory().Type().OMITTED_TYPE_ARG_TYPE.clone());
} else {
if (alloc.expectedType() instanceof ParameterizedTypeBinding) {
ParameterizedTypeBinding expectedType = (ParameterizedTypeBinding) alloc.expectedType();
// type arguments can be recovered from the expected type
for (TypeBinding binding : expectedType.typeArguments()) {
CtTypeReference<?> typeArgRef = getTypeReference(binding);
typeArgRef.setImplicit(true);
type.addActualTypeArgument(typeArgRef);
if (expectedType.typeArguments() != null) {
// type arguments can be recovered from the expected type
for (TypeBinding binding : expectedType.typeArguments()) {
CtTypeReference<?> typeArgRef = getTypeReference(binding);
typeArgRef.setImplicit(true);
type.addActualTypeArgument(typeArgRef);
}
}
return;
}

// the expected type is not available/parameterized if the constructor call occurred in e.g. an unresolved
// method, or in a method that did not expect a parameterized argument
type.addActualTypeArgument(jdtTreeBuilder.getFactory().Type().OMITTED_TYPE_ARG_TYPE.clone());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ public void testCompileMethodReferenceGeneratedBySpoon() {
canBeBuilt(new File("./target/spooned/spoon/test/methodreference/testclasses/"), 8);
}

@Test
public void testTryRecoverTypeArgumentsHandlesNullTypeArguments() {
canBeBuilt("./src/test/resources/ReferenceBuilder-null-pointer/", 8);
}

@Test
public void testNoClasspathExecutableReferenceExpression() {
final Launcher launcher = new Launcher();
Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/ReferenceBuilder-null-pointer/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import java.util.ArrayList;
import java.util.Collection;

public class Foo {
public void accept(Collection y) {}

public void doSomething(Collection x) {
accept(new ArrayList<>(x));
}
}