Unverified Commit 051431e1 authored by Simon Larsén's avatar Simon Larsén Committed by GitHub
Browse files

ix: Fix unqualified type becoming qualified in union type catch (#3918)

parent 245e0761
......@@ -1608,15 +1608,18 @@ public class JDTTreeBuilder extends ASTVisitor {
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);
......
......@@ -45,7 +45,10 @@ import java.util.Collections;
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;
......@@ -349,4 +352,38 @@ public class TryCatchTest {
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());
}
}
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
}
}
}
package spoon.test.trycatch.testclasses;
class MultipleUnqualifiedTypesInSingleCatcher {
public static void main(String[] args) {
try {
throw new InterruptedException();
} catch (InterruptedException | RuntimeException e) {
// pass
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment