Skip to content

Commit

Permalink
fix(noclasspath): Anonymous class name is an empty string.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Feb 12, 2016
1 parent 5db7f04 commit c21274e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public interface CtType<T> extends CtNamedElement, CtTypeInformation, CtTypeMemb
* The string separator in a Java innertype qualified name.
*/
String INNERTTYPE_SEPARATOR = "$";
/**
* Used in no classpath when we don't have any information to build the name of the type.
*/
String NAME_UNKNOWN = "<unknown>";

/**
* Returns the simple (unqualified) name of this element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3379,6 +3379,7 @@ public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
if (localTypeDeclaration.binding == null) {
// no classpath mode but JDT returns nothing. We create an empty class.
t = factory.Core().createClass();
t.setSimpleName(CtType.NAME_UNKNOWN);
((CtClass) t).setSuperclass(references.getTypeReference(null, localTypeDeclaration.allocation.type));
} else {
t = createType(localTypeDeclaration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
Expand Down Expand Up @@ -161,7 +162,7 @@ public void testCtNewClassInNoClasspath() throws Exception {
assertEquals("Lock$With", anonymousClass.getSuperclass().getQualifiedName());
assertEquals("Lock", anonymousClass.getSuperclass().getDeclaringType().getSimpleName());
assertEquals("Lock.With", anonymousClass.getSuperclass().toString());
assertNull(anonymousClass.getSimpleName()); // In noclasspath, we don't have this information.
assertEquals(CtType.NAME_UNKNOWN, anonymousClass.getSimpleName()); // In noclasspath, we don't have this information.
assertEquals(1, anonymousClass.getMethods().size());

TestUtils.canBeBuilt("./target/new-class", 8, true);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,21 @@ public void testAnnotationOnMethodWithPrimitiveReturnTypeInNoClasspath() throws
assertEquals("@Override" + System.lineSeparator(), run.getAnnotations().get(0).toString());
}

@Test
public void testAnonymousClassesHaveAnEmptyStringForItsNameInNoClasspath() throws Exception {
// contract: In no classpath mode, a type reference have an empty string for its name.
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/A.java");
launcher.setSourceOutputDirectory("./target/class-declaration");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();

final CtClass<Object> aClass = launcher.getFactory().Class().get("A");
final CtClass anonymousClass = aClass.getElements(new TypeFilter<>(CtNewClass.class)).get(0).getAnonymousClass();
assertEquals(CtType.NAME_UNKNOWN, anonymousClass.getReference().getSimpleName());
assertEquals(7, aClass.getReferencedTypes().size());
}

class A {
class Tacos<K> {
}
Expand Down

0 comments on commit c21274e

Please sign in to comment.