Skip to content

Commit

Permalink
[fix INRIA#465] Missing package in type reference with noclasspath mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Dec 27, 2015
1 parent 33272e3 commit 31fe688
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,16 @@ public boolean visit(MessageSend messageSend, BlockScope scope) {
if (messageSend.receiver instanceof SingleNameReference || messageSend.receiver instanceof QualifiedNameReference) {
final CtTypeReference<Object> 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 {
Expand Down Expand Up @@ -2827,6 +2837,16 @@ public boolean visit(SingleNameReference singleNameReference, BlockScope scope)
final CtTypeAccess<Object> ta = factory.Core().createTypeAccess();
final CtTypeReference<Object> 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;
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> aClass = launcher.getFactory().Class().get("Demo");
final Set<CtTypeReference<?>> referencedTypes = aClass.getReferencedTypes();

boolean containsDemoReference = false;
boolean containsVoidReference = false;
boolean containsStringReference = false;
boolean containsJoinerReference = false;

for (Iterator<CtTypeReference<?>> 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<K> {
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/noclasspath/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import com.google.common.base.Joiner;

public class Demo {

public static void main(String[] args) {
Joiner.on();
}
}

0 comments on commit 31fe688

Please sign in to comment.