Skip to content

Commit

Permalink
fix: Fix unqualified type reference in catch block becoming qualified (
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse authored Feb 22, 2021
1 parent 59f72bc commit e49d161
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ public boolean visit(QualifiedTypeReference qualifiedTypeReference, BlockScope s
context.enter(reference, qualifiedTypeReference);
return true;
} else if (context.stack.peekFirst().element instanceof CtCatch) {
context.enter(helper.createCatchVariable(qualifiedTypeReference), qualifiedTypeReference);
context.enter(helper.createCatchVariable(qualifiedTypeReference, scope), qualifiedTypeReference);
return true;
}
context.enter(factory.Code().createTypeAccessWithoutCloningReference(references.buildTypeReference(qualifiedTypeReference, scope)), qualifiedTypeReference);
Expand Down Expand Up @@ -1593,7 +1593,7 @@ public boolean visit(UnionTypeReference unionTypeReference, BlockScope scope) {
if (!(context.stack.peekFirst().node instanceof Argument)) {
throw new SpoonException("UnionType is only supported for CtCatch.");
}
context.enter(helper.createCatchVariable(unionTypeReference), unionTypeReference);
context.enter(helper.createCatchVariable(unionTypeReference, scope), unionTypeReference);
return true;
}

Expand All @@ -1619,7 +1619,7 @@ public boolean visit(SingleTypeReference singleTypeReference, BlockScope scope)

return true;
} else if (context.stack.peekFirst().element instanceof CtCatch) {
context.enter(helper.createCatchVariable(singleTypeReference), singleTypeReference);
context.enter(helper.createCatchVariable(singleTypeReference, scope), singleTypeReference);
return true;
}
CtTypeReference<?> typeRef = references.buildTypeReference(singleTypeReference, scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jdt.internal.compiler.lookup.ProblemBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
Expand Down Expand Up @@ -117,7 +118,7 @@ static String createQualifiedTypeName(char[][] typeName) {
* Corresponds to the exception type declared in the catch.
* @return a catch variable.
*/
CtCatchVariable<Throwable> createCatchVariable(TypeReference typeReference) {
CtCatchVariable<Throwable> createCatchVariable(TypeReference typeReference, Scope scope) {
final Argument jdtCatch = (Argument) jdtTreeBuilder.getContextBuilder().stack.peekFirst().node;
final Set<CtExtendedModifier> modifiers = getModifiers(jdtCatch.modifiers, false, false);

Expand All @@ -127,12 +128,7 @@ CtCatchVariable<Throwable> createCatchVariable(TypeReference typeReference) {
//do not set type of variable yet. It will be initialized later by visit of multiple types. Each call then ADDs one type
return result;
} else {
CtTypeReference ctTypeReference;
if (typeReference.resolvedType instanceof ProblemReferenceBinding) {
ctTypeReference = jdtTreeBuilder.getReferencesBuilder().buildTypeReference(typeReference, null);
} else {
ctTypeReference = jdtTreeBuilder.getReferencesBuilder().<Throwable>getTypeReference(typeReference.resolvedType);
}
CtTypeReference ctTypeReference = jdtTreeBuilder.getReferencesBuilder().buildTypeReference(typeReference, scope);
return result.<CtCatchVariable>setType(ctTypeReference);
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/trycatch/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,17 @@ public void testCatchQualifiedReferenceNoClasspath() {
assertEquals("CustomException", caughtType.getSimpleName());
assertEquals("some.neat.pkg.CustomException", caughtType.getQualifiedName());
}

@Test
public void testCatchUnqualifiedReferenceIsMarkedSimplyQualified() throws Exception {
// contract: An unqualified type reference in a catch clause should have its package marked implicit

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

CtCatch targetCatch = catches.get(0);
CtTypeReference<?> catchParamType = targetCatch.getParameter().getType();
assertTrue(catchParamType.isSimplyQualified());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spoon.test.trycatch.testclasses;

import java.io.IOException;

public class CatchWithUnqualifiedType {
public static void main(String[] args) {
try {
throw new IOException();
} catch (IOException e) {
}
}
}

0 comments on commit e49d161

Please sign in to comment.