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: Remove incorrect implicitness for package reference #3357

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
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,6 @@ <T> CtTypeReference<T> getTypeReference(TypeBinding binding, boolean resolveGene
ref.setDeclaringType(getTypeReference(binding.enclosingType()));
} else {
CtPackageReference packageReference = getPackageReference(binding.getPackage());
packageReference.setImplicit(true);
ref.setPackage(packageReference);
}
ref.setSimpleName(new String(binding.sourceName()));
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtArrayTypeReference;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtParameterReference;
import spoon.reflect.reference.CtReference;
import spoon.reflect.reference.CtTypeParameterReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtWildcardReference;
Expand Down Expand Up @@ -702,4 +704,31 @@ public void testIsInTheSamePackageNoClasspath() {
CtTypeReference tr2 = fields.get(0).getType();
assertTrue(vars.get(0).getReference().getType().canAccess(tr2));
}

@Test
public void testQualifiedArrayTypeReferenceNoClasspath() {
// contract: component type of explicitly qualified array type reference should have explicit package reference
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/QualifiedArrayType.java");
launcher.getEnvironment().setNoClasspath(true);
CtModel model = launcher.buildModel();
List<CtArrayTypeReference<?>> refs = model.getElements(e -> true);

int loopIterations = 0; // for meta assert
for (CtArrayTypeReference<?> arrayTypeRef : refs) {
CtTypeReference<?> compType = getDeepestComponentType(arrayTypeRef);
assertFalse(compType.getPackage().isImplicit());
loopIterations++;
}

assertTrue("Test loop did not execute", loopIterations > 0);
}

private static CtTypeReference<?> getDeepestComponentType(CtArrayTypeReference<?> arrayTypeRef) {
CtReference ref = arrayTypeRef;
while (ref instanceof CtArrayTypeReference) {
ref = ((CtArrayTypeReference<?>) ref).getComponentType();
}
return (CtTypeReference<?>) ref;
}
}
6 changes: 3 additions & 3 deletions src/test/java/spoon/test/spoonifier/SpoonifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ public void testGeneratedSpoonifyCode() {
"\t\t\t\t\tCtExecutableReference ctExecutableReference0 = factory.createExecutableReference();\n" +
"\t\t\t\t\tctExecutableReference0.setSimpleName(\"<init>\");\n" +
"\t\t\t\t\tctInvocation0.setValueByRole(CtRole.EXECUTABLE_REF, ctExecutableReference0);\n" +
"\t\t\t\t\t\tctExecutableReference0.setValueByRole(CtRole.DECLARING_TYPE, factory.Type().createSimplyQualifiedReference(\"java.lang.Object\"));\n" +
"\t\t\t\t\t\tctExecutableReference0.setValueByRole(CtRole.TYPE, factory.Type().createSimplyQualifiedReference(\"java.lang.Object\"));\n" +
"\t\t\t\t\t\tctExecutableReference0.setValueByRole(CtRole.DECLARING_TYPE, factory.Type().createReference(\"java.lang.Object\"));\n" +
"\t\t\t\t\t\tctExecutableReference0.setValueByRole(CtRole.TYPE, factory.Type().createReference(\"java.lang.Object\"));\n" +
"\t\t\tctBlock0.setValueByRole(CtRole.STATEMENT, ctBlock0Statements);\n" +
"\t\tCtField ctField0 = factory.createField();\n" +
"\t\tctField0.setSimpleName(\"i\");\n" +
Expand Down Expand Up @@ -273,7 +273,7 @@ public void testGeneratedSpoonifyCode() {
"\t\t\t\t\t\tCtParameterReference ctParameterReference0 = factory.createParameterReference();\n" +
"\t\t\t\t\t\tctParameterReference0.setSimpleName(\"toto\");\n" +
"\t\t\t\t\t\tctVariableRead0.setValueByRole(CtRole.VARIABLE, ctParameterReference0);\n" +
"\t\t\t\t\t\t\tctParameterReference0.setValueByRole(CtRole.TYPE, factory.Type().createSimplyQualifiedReference(\"java.lang.String\"));\n" +
"\t\t\t\t\t\t\tctParameterReference0.setValueByRole(CtRole.TYPE, factory.Type().createReference(\"java.lang.String\"));\n" +
"\t\t\tctBlock1.setValueByRole(CtRole.STATEMENT, ctBlock1Statements);\n" +
"\t\tctMethod0.setValueByRole(CtRole.PARAMETER, ctMethod0Parameters);\n" +
"\tctClass0.setValueByRole(CtRole.TYPE_MEMBER, ctClass0TypeMembers);\n";
Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/noclasspath/QualifiedArrayType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.util.*;

public class QualifiedArrayType {
java.lang.Object[] builtinTypeArray;
java.util.Collection[] importedTypeArray;
}