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

review: Fix NPE with union catch inside lambda in noclasspath #1416

Merged
merged 5 commits into from
Jun 23, 2017
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
11 changes: 10 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtUnboundVariableReference;
import spoon.support.comparator.CtLineElementComparator;
Expand Down Expand Up @@ -1415,7 +1416,15 @@ public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope)
return true;
}
if (context.stack.peekFirst().node instanceof UnionTypeReference) {
context.enter(references.<Throwable>getTypeReference(singleTypeReference.resolvedType), singleTypeReference);
if (singleTypeReference.resolvedType == null) {
CtTypeReference typeReference = factory.Type().createReference(singleTypeReference.toString());
CtReference ref = references.getDeclaringReferenceFromImports(singleTypeReference.getLastToken());
references.setPackageOrDeclaringType(typeReference, ref);
context.enter(typeReference, singleTypeReference);
} else {
context.enter(references.<Throwable>getTypeReference(singleTypeReference.resolvedType), singleTypeReference);
}

return true;
} else if (context.stack.peekFirst().element instanceof CtCatch) {
context.enter(helper.createCatchVariable(singleTypeReference), singleTypeReference);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/spoon/test/exceptions/ExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
import spoon.compiler.InvalidClassPathException;
import spoon.compiler.ModelBuildingException;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtCatchVariable;
import spoon.reflect.factory.Factory;
import spoon.reflect.visitor.filter.TypeFilter;

import java.io.FileNotFoundException;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.createFactory;

Expand Down Expand Up @@ -101,4 +107,26 @@ public void testExceptionDuplicateClass() throws Exception {
.build();
}

@Test
public void testUnionCatchExceptionInsideLambdaInNoClasspath() {
// contract: the model should be built when defining a union catch inside a lambda which is not known (noclasspath)
// and the catch variable types should be the same than outside a lambda
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/UnionCatch.java");
launcher.getEnvironment().setNoClasspath(true);
launcher.buildModel();

List<CtCatch> catches = launcher.getFactory().getModel().getElements(new TypeFilter<>(CtCatch.class));
assertEquals(2, catches.size());

CtCatchVariable variable1 = catches.get(0).getParameter(); // inside a lambda
CtCatchVariable variable2 = catches.get(1).getParameter(); // outside the lambda

assertEquals(variable1.getMultiTypes(), variable2.getMultiTypes());

// for now the type of CtCatchVariable is not the same
// this should be fix in the future (see: https://github.com/INRIA/spoon/issues/1420)
//assertEquals(variable2, variable1);
}

}
40 changes: 40 additions & 0 deletions src/test/resources/noclasspath/UnionCatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package spoon.test.exceptions.testclasses;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.function.Function;
import toto.NbpOperator;

/**
* Created by urli on 23/06/2017.
*/
public class UnionCatch {
public void toto() {
bla((NbpOperator) t -> {
try {
Reader reader = new StringReader("machin");
int i = -1;
while (reader.ready()) {
i = reader.read();
}
return i;
} catch (IOException | NullPointerException e) {
System.out.printf("Error");
return 0;
}
});

try {
Reader reader = new StringReader("machin");
int i = -1;
while (reader.ready()) {
i = reader.read();
}
return i;
} catch (IOException | NullPointerException e) {
System.out.printf("Error");
return 0;
}
}
}