Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,9 @@ private boolean createParameterizedType(TypeReference parameterizedTypeReference
if (skipTypeInAnnotation) {
return true;
}
context.enter(factory.Code().createTypeAccessWithoutCloningReference(references.buildTypeReference(parameterizedTypeReference, null)), parameterizedTypeReference);
CtTypeReference typeReference = references.buildTypeReference(parameterizedTypeReference, null);
CtTypeAccess typeAccess = factory.Code().createTypeAccessWithoutCloningReference(typeReference);
context.enter(typeAccess, parameterizedTypeReference);
return true;
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ <T> CtTypeReference<T> buildTypeReference(TypeReference type, Scope scope) {
if (type == null) {
return null;
}
return buildTypeReferenceInternal(this.<T>getTypeReference(type.resolvedType, type), type, scope);
CtTypeReference<T> typeReference = this.<T>getTypeReference(type.resolvedType, type);
return buildTypeReferenceInternal(typeReference, type, scope);
}

/**
Expand Down Expand Up @@ -457,7 +458,8 @@ <T> CtTypeReference<T> getTypeReference(TypeBinding binding, TypeReference ref)
insertGenericTypesInNoClasspathFromJDTInSpoon(ref, ctRef);
return ctRef;
}
return getTypeReference(ref);
CtTypeReference<T> result = getTypeReference(ref);
return result;
}

CtTypeReference<Object> getTypeParameterReference(TypeBinding binding, TypeReference ref) {
Expand Down Expand Up @@ -542,7 +544,9 @@ <T> CtTypeReference<T> getTypeReference(TypeReference ref) {
}
if (!res.toString().replace(", ?", ",?").endsWith(CharOperation.toString(ref.getParameterizedTypeName()))) {
// verify that we did not match a class that have the same name in a different package
return this.jdtTreeBuilder.getFactory().Type().createReference(CharOperation.toString(ref.getParameterizedTypeName()));
CtTypeReference result = this.jdtTreeBuilder.getFactory().Type().createReference(res.getQualifiedName());
result.setActualTypeArguments(res.getActualTypeArguments());
return result;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@tdurieux could you review this piece of code: you introduce the condition in #1480 to fix another bug

Copy link
Collaborator

Choose a reason for hiding this comment

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

Your patch is invalid, it brokes my test (and my test is good)

Copy link
Collaborator

Choose a reason for hiding this comment

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

the bug is in getQualifiedName of jdt in my testcase

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Your patch is invalid, it brokes my test (and my test is good)

Yeah, I saw that. Still working on it, I come back to you when tests are passing, I was a bit too confident on the first trial ;)

}
return res;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1230,4 +1230,25 @@ public void testImportStarredPackageWithNonVisibleClass() throws IOException {

assertEquals(3, cu.getImports().size());
}

@Test
public void testImportWithGenerics() throws IOException {
final Launcher launcher = new Launcher();
Copy link
Collaborator

Choose a reason for hiding this comment

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

contract line:

launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
launcher.getEnvironment().setAutoImports(true);
launcher.getEnvironment().setShouldCompile(true);
launcher.getEnvironment().setNoClasspath(true);
launcher.setSourceOutputDirectory("./target/import-with-generics");
launcher.run();

PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses.TestWithGenerics");
List<CtType<?>> toPrint = new ArrayList<>();
toPrint.add(element);

prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
String output = prettyPrinter.getResult();

assertTrue(output.contains("import spoon.test.imports.testclasses.withgenerics.Target;"));
}
}
7 changes: 7 additions & 0 deletions src/test/resources/import-with-generics/TestWithGenerics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package spoon.test.imports.testclasses;
/**
* Created by urli on 16/10/2017.
*/
public class TestWithGenerics {
public spoon.test.imports.testclasses.withgenerics.Target<Object, Object> myfields;
}