Skip to content

Commit

Permalink
ix: Fix unqualified type becoming qualified in union type catch (INRI…
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse authored and woutersmeenk committed Aug 29, 2021
1 parent 803655a commit 1102f31
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1608,15 +1608,18 @@ public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope)
return true;
}
if (context.stack.peekFirst().node instanceof UnionTypeReference) {
CtTypeReference<?> typeReference;

if (singleTypeReference.resolvedType == null) {
CtTypeReference typeReference = factory.Type().createReference(singleTypeReference.toString());
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);
typeReference = references.<Throwable>getTypeReference(singleTypeReference.resolvedType);
}

context.enter(typeReference, singleTypeReference);
typeReference.setSimplyQualified(true);
return true;
} else if (context.stack.peekFirst().element instanceof CtCatch) {
context.enter(helper.createCatchVariable(singleTypeReference, scope), singleTypeReference);
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/trycatch/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
import java.util.List;
import java.util.Set;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -349,4 +352,38 @@ public void testCatchUnqualifiedReferenceIsMarkedSimplyQualified() throws Except
CtTypeReference<?> catchParamType = targetCatch.getParameter().getType();
assertTrue(catchParamType.isSimplyQualified());
}

@Test
public void testCatchUnqualifiedReferenceMarkedSimplyQualifiedWhenMultipleTypesAreSpecified() throws Exception {
// contract: Unqualified type references should have implicit packages when there are
// multiple types in a single catcher

CtClass<?> clazz = build(
"spoon.test.trycatch.testclasses", "MultipleUnqualifiedTypesInSingleCatcher");
List<CtCatch> catches = clazz.getElements(e -> true);
assertEquals(1, catches.size());

CtCatch targetCatch = catches.get(0);
List<CtTypeReference<?>> paramTypes = targetCatch.getParameter().getMultiTypes();
assertThat(paramTypes.size(), equalTo(2));
assertTrue("first type reference is fully qualified", paramTypes.get(0).isSimplyQualified());
assertTrue("second type reference is fully qualified", paramTypes.get(1).isSimplyQualified());
}

@Test
public void testCatchWithQualifiedAndUnqualifiedTypeReferencesInSameCatcher() throws Exception {
// contract: It should be possible for qualified and unqualified type references to exist in
// the same catcher

CtClass<?> clazz = build(
"spoon.test.trycatch.testclasses", "CatcherWithQualifiedAndUnqualifiedTypeRefs");
List<CtCatch> catches = clazz.getElements(e -> true);
assertEquals(1, catches.size());

CtCatch targetCatch = catches.get(0);
List<CtTypeReference<?>> paramTypes = targetCatch.getParameter().getMultiTypes();
assertThat(paramTypes.size(), equalTo(2));
assertTrue("first type reference should be unqualified", paramTypes.get(0).isSimplyQualified());
assertFalse("second type reference should be qualified", paramTypes.get(1).isSimplyQualified());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spoon.test.trycatch.testclasses;

public class CatcherWithQualifiedAndUnqualifiedTypeRefs {
public static void main(String[] args) {
try {
throw new InterruptedException();
} catch (InterruptedException | java.lang.RuntimeException e) {
// pass
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spoon.test.trycatch.testclasses;

class MultipleUnqualifiedTypesInSingleCatcher {
public static void main(String[] args) {
try {
throw new InterruptedException();
} catch (InterruptedException | RuntimeException e) {
// pass
}
}
}

0 comments on commit 1102f31

Please sign in to comment.