diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 30b1e42e771..663bca56df8 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -2394,6 +2394,16 @@ public boolean visit(MessageSend messageSend, BlockScope scope) { if (messageSend.receiver instanceof SingleNameReference || messageSend.receiver instanceof QualifiedNameReference) { final CtTypeReference typeReference = factory.Core().createTypeReference(); typeReference.setSimpleName(messageSend.receiver.toString()); + if (context.compilationunitdeclaration != null && context.compilationunitdeclaration.imports != null) { + for (ImportReference anImport : context.compilationunitdeclaration.imports) { + if (CharOperation.equals(anImport.getImportName()[anImport.getImportName().length - 1], ((SingleNameReference) messageSend.receiver).token)) { + char[][] packageName = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1); + final PackageBinding aPackage = context.compilationunitdeclaration.scope.environment.createPackage(packageName); + typeReference.setPackage(references.getPackageReference(aPackage)); + break; + } + } + } ref.setDeclaringType(typeReference); } } else { @@ -2827,6 +2837,16 @@ public boolean visit(SingleNameReference singleNameReference, BlockScope scope) final CtTypeAccess ta = factory.Core().createTypeAccess(); final CtTypeReference typeReference = factory.Core().createTypeReference(); typeReference.setSimpleName(new String(singleNameReference.binding.readableName())); + if (context.compilationunitdeclaration != null && context.compilationunitdeclaration.imports != null) { + for (ImportReference anImport : context.compilationunitdeclaration.imports) { + if (CharOperation.equals(anImport.getImportName()[anImport.getImportName().length - 1], singleNameReference.token)) { + char[][] packageName = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1); + final PackageBinding aPackage = context.compilationunitdeclaration.scope.environment.createPackage(packageName); + typeReference.setPackage(references.getPackageReference(aPackage)); + break; + } + } + } ta.setType(typeReference); context.enter(ta, singleNameReference); return true; diff --git a/src/test/java/spoon/test/reference/TypeReferenceTest.java b/src/test/java/spoon/test/reference/TypeReferenceTest.java index 7958451bf04..2f021ad828e 100644 --- a/src/test/java/spoon/test/reference/TypeReferenceTest.java +++ b/src/test/java/spoon/test/reference/TypeReferenceTest.java @@ -256,6 +256,40 @@ public void testRecursiveTypeReferenceInGenericType() throws Exception { assertTrue(circularRef instanceof CtCircularTypeReference); } + @Test + public void testPackageInNoClasspath () { + final Launcher launcher = new Launcher(); + launcher.addInputResource("./src/test/resources/noclasspath/Demo.java"); + launcher.setSourceOutputDirectory("./target/class"); + launcher.getEnvironment().setNoClasspath(true); + launcher.run(); + + final CtClass aClass = launcher.getFactory().Class().get("Demo"); + final Set> referencedTypes = aClass.getReferencedTypes(); + + boolean containsDemoReference = false; + boolean containsVoidReference = false; + boolean containsStringReference = false; + boolean containsJoinerReference = false; + + for (Iterator> iterator = referencedTypes.iterator(); iterator.hasNext(); ) { + CtTypeReference reference = iterator.next(); + if(reference.toString().equals("Demo")) { + containsDemoReference = true; + } else if(reference.toString().equals("void")) { + containsVoidReference = true; + } else if(reference.toString().equals("java.lang.String")) { + containsStringReference = true; + } else if(reference.toString().equals("com.google.common.base.Joiner")) { + containsJoinerReference = true; + } + } + assertTrue("Reference to Demo is missing", containsDemoReference); + assertTrue("Reference to void is missing", containsVoidReference); + assertTrue("Reference to String is missing", containsStringReference); + assertTrue("Reference to Joiner is missing", containsJoinerReference); + } + class A { class Tacos { } diff --git a/src/test/resources/noclasspath/Demo.java b/src/test/resources/noclasspath/Demo.java new file mode 100644 index 00000000000..8834c70b8df --- /dev/null +++ b/src/test/resources/noclasspath/Demo.java @@ -0,0 +1,8 @@ +import com.google.common.base.Joiner; + +public class Demo { + + public static void main(String[] args) { + Joiner.on(); + } +} \ No newline at end of file