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 IndexOutOfBoundsException in getType() #1932

Merged
merged 3 commits into from
Apr 1, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public CtTypeReference<T> getType() {
.includingInterfaces(false)
.includingSelf(true)
.returnTypeReferences(true)).list();
if (superTypesOfFirst.isEmpty()) {
return null;
}
int commonSuperTypeIdx = 0;
//index of Throwable. Last is Object
int throwableIdx = superTypesOfFirst.size() - 2;
Expand Down
24 changes: 19 additions & 5 deletions src/test/java/spoon/test/trycatch/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void testTryCatchVariableGetType() throws Exception {
CtTry tryStmt = (CtTry) clazz.getElements(new TypeFilter<>(CtTry.class)).get(0);
List<CtCatch> catchers = tryStmt.getCatchers();
assertEquals(1, catchers.size());

CtCatchVariable<?> catchVariable = catchers.get(0).getParameter();

assertEquals(
Expand All @@ -230,28 +230,28 @@ public void testTryCatchVariableGetType() throws Exception {
assertEquals(
RuntimeException.class,
catchVariable.getMultiTypes().get(0).getActualClass());

//contract: the manipulation with catch variable type is possible
catchVariable.setType((CtTypeReference)factory.Type().createReference(IllegalArgumentException.class));
assertEquals(IllegalArgumentException.class,catchVariable.getType().getActualClass());
//contract setType influences multitypes
assertEquals(1, catchVariable.getMultiTypes().size());
assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(0).getActualClass());

catchVariable.setMultiTypes(Collections.singletonList((CtTypeReference)factory.Type().createReference(UnsupportedOperationException.class)));
assertEquals(UnsupportedOperationException.class,catchVariable.getType().getActualClass());
//contract setType influences multitypes
assertEquals(1, catchVariable.getMultiTypes().size());
assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());

catchVariable.setMultiTypes(Arrays.asList(
factory.Type().createReference(UnsupportedOperationException.class),
factory.Type().createReference(IllegalArgumentException.class)
));
assertEquals(2, catchVariable.getMultiTypes().size());
assertEquals(UnsupportedOperationException.class, catchVariable.getMultiTypes().get(0).getActualClass());
assertEquals(IllegalArgumentException.class, catchVariable.getMultiTypes().get(1).getActualClass());

//contract setMultiTypes influences types, which contains common super class of all multi types
assertEquals(RuntimeException.class,catchVariable.getType().getActualClass());
}
Expand Down Expand Up @@ -289,4 +289,18 @@ public void testCatchWithExplicitFinalVariable() throws IOException {

assertTrue(content.contains("catch (final java.lang.Exception e)"));
}

@Test
public void testCatchWithUnknownExceptions() {
// contract: unknown exception type in multicatch should not cause IndexOutOfBoundsException
String inputResource = "./src/test/resources/spoon/test/noclasspath/exceptions/Foo.java";
Launcher launcher = new Launcher();
launcher.addInputResource(inputResource);
launcher.getEnvironment().setNoClasspath(true);
CtModel model = launcher.buildModel();

List<CtCatch> catches = model.getElements(new TypeFilter<CtCatch>(CtCatch.class));
catches.get(0).getParameter().getType(); // catch with single UnknownException
catches.get(1).getParameter().getType(); // multicatch with UnknownException
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you please add assert, which checks the returned value too?
assertNull(...)

}
}
21 changes: 21 additions & 0 deletions src/test/resources/spoon/test/noclasspath/exceptions/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

public class Foo {

void testCatchWithUnknownException(File file) {
try {
new FileReader(file);
}
catch (UnknownException e) {
System.out.println(e);
}
}

void testMultiCatchWithUnknownException(File file) {
try {
new FileReader(file);
}
catch (UnknownException | FileNotFoundException e) {
System.out.println(e);
}
}
}