From 87ba7ee1193761819dfcfdf5afe7d8b8fbec44af Mon Sep 17 00:00:00 2001 From: leventov Date: Thu, 16 Jun 2016 13:14:23 +0700 Subject: [PATCH 1/3] Fix a number of bugs related to handling of unnamed packages; Default TypeReference-building factory methods return references with unnamed package rather than null package and null declaring type, to preserve invariant that either ref.getPackage() or ref.getDeclaringType() is not null, unless the type reference represent a primitive type class or void.class --- .../spoon/reflect/declaration/CtPackage.java | 6 +++ .../spoon/reflect/factory/PackageFactory.java | 3 ++ .../spoon/reflect/factory/TypeFactory.java | 12 +++++- .../reflect/reference/CtPackageReference.java | 6 +++ .../visitor/DefaultJavaPrettyPrinter.java | 6 +-- .../reflect/visitor/ImportScannerImpl.java | 9 +++-- .../spoon/support/JavaOutputProcessor.java | 2 +- .../compiler/jdt/JDTBasedSpoonCompiler.java | 2 +- .../compiler/jdt/JDTBatchCompiler.java | 2 +- .../support/compiler/jdt/JDTTreeBuilder.java | 39 +++++++++++-------- .../reflect/declaration/CtPackageImpl.java | 11 ++++-- .../reflect/declaration/CtTypeImpl.java | 11 +++--- .../reference/CtPackageReferenceImpl.java | 5 +++ .../reference/CtTypeReferenceImpl.java | 2 +- .../java/spoon/test/imports/ImportTest.java | 4 +- .../java/spoon/test/parent/ParentTest.java | 6 +-- .../test/reference/TypeReferenceTest.java | 3 +- .../spoon/testing/CtElementAssertTest.java | 3 +- 18 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/main/java/spoon/reflect/declaration/CtPackage.java b/src/main/java/spoon/reflect/declaration/CtPackage.java index eb43cda100d..30d311f7439 100644 --- a/src/main/java/spoon/reflect/declaration/CtPackage.java +++ b/src/main/java/spoon/reflect/declaration/CtPackage.java @@ -127,4 +127,10 @@ public interface CtPackage extends CtNamedElement, CtShadowable { @Override CtPackage clone(); + + /** + * Returns {@code true} if this is an unnamed Java package. + * See JLS §7.4.2. Unnamed Packages. + */ + boolean isUnnamedPackage(); } diff --git a/src/main/java/spoon/reflect/factory/PackageFactory.java b/src/main/java/spoon/reflect/factory/PackageFactory.java index 921a7b00221..d35305cc350 100644 --- a/src/main/java/spoon/reflect/factory/PackageFactory.java +++ b/src/main/java/spoon/reflect/factory/PackageFactory.java @@ -103,6 +103,9 @@ public CtPackage create(CtPackage parent, String simpleName) { * the full name of the package */ public CtPackage getOrCreate(String qualifiedName) { + if (qualifiedName.isEmpty()) { + return factory.getModel().getRootPackage(); + } StringTokenizer token = new StringTokenizer(qualifiedName, CtPackage.PACKAGE_SEPARATOR); CtPackage last = factory.getModel().getRootPackage(); diff --git a/src/main/java/spoon/reflect/factory/TypeFactory.java b/src/main/java/spoon/reflect/factory/TypeFactory.java index 19bad045a3c..ce91f54e373 100644 --- a/src/main/java/spoon/reflect/factory/TypeFactory.java +++ b/src/main/java/spoon/reflect/factory/TypeFactory.java @@ -30,7 +30,9 @@ import spoon.support.visitor.java.JavaReflectionTreeBuilder; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -42,6 +44,10 @@ */ public class TypeFactory extends SubFactory { + private static final Set NULL_PACKAGE_CLASSES = new HashSet( + Arrays.asList("void", "boolean", "byte", "short", "char", "int", "float", "long", + "double")); + public final CtTypeReference NULL_TYPE = createReference(CtTypeReference.NULL_TYPE_NAME); public final CtTypeReference VOID = createReference(Void.class); public final CtTypeReference STRING = createReference(String.class); @@ -319,6 +325,8 @@ public CtTypeReference createReference(String qualifiedName) { ref.setDeclaringType(createReference(getDeclaringTypeName(qualifiedName))); } else if (hasPackage(qualifiedName) > 0) { ref.setPackage(factory.Package().createReference(getPackageName(qualifiedName))); + } else if (!NULL_PACKAGE_CLASSES.contains(qualifiedName)) { + ref.setPackage(factory.Package().topLevel()); } ref.setSimpleName(getSimpleName(qualifiedName)); return ref; @@ -377,7 +385,7 @@ public boolean matches(CtNewClass element) { if (packageIndex > 0) { pack = factory.Package().get(qualifiedName.substring(0, packageIndex)); } else { - pack = factory.Package().get(CtPackage.TOP_LEVEL_PACKAGE_NAME); + pack = factory.Package().getRootPackage(); } if (pack == null) { @@ -462,7 +470,7 @@ protected String getPackageName(String qualifiedName) { if (hasPackage(qualifiedName) >= 0) { return qualifiedName.substring(0, hasPackage(qualifiedName)); } - return CtPackage.TOP_LEVEL_PACKAGE_NAME; + return ""; } /** diff --git a/src/main/java/spoon/reflect/reference/CtPackageReference.java b/src/main/java/spoon/reflect/reference/CtPackageReference.java index d81ed51b3bd..1934fca32d9 100644 --- a/src/main/java/spoon/reflect/reference/CtPackageReference.java +++ b/src/main/java/spoon/reflect/reference/CtPackageReference.java @@ -38,6 +38,12 @@ public interface CtPackageReference extends CtReference { */ void replace(CtPackageReference packageReference); + /** + * Returns {@code true} if this is a reference to an unnamed + * Java package. See JLS §7.4.2. Unnamed Packages. + */ + boolean isUnnamedPackage(); + @Override CtPackageReference clone(); } diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index b72e4ebcbeb..a7e1ed64de9 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -299,7 +299,7 @@ public String getPackageDeclaration() { a.accept(this); } - if (!context.currentTopLevel.getPackage().getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) { + if (!context.currentTopLevel.getPackage().isUnnamedPackage()) { write("package " + context.currentTopLevel.getPackage().getQualifiedName() + ";"); } String ret = sbf.toString(); @@ -1886,7 +1886,7 @@ public void visitCtOperatorAssignment(CtOperatorAssignment ref, boolean withGenerics) } } else { if (ref.getPackage() != null && !importsContext.isImported(ref)) { - if (!CtPackage.TOP_LEVEL_PACKAGE_NAME.equals(ref.getPackage().getSimpleName())) { + if (!ref.getPackage().isUnnamedPackage()) { scan(ref.getPackage()).write(CtPackage.PACKAGE_SEPARATOR); } } diff --git a/src/main/java/spoon/reflect/visitor/ImportScannerImpl.java b/src/main/java/spoon/reflect/visitor/ImportScannerImpl.java index 8a517570d0a..b762e84d451 100644 --- a/src/main/java/spoon/reflect/visitor/ImportScannerImpl.java +++ b/src/main/java/spoon/reflect/visitor/ImportScannerImpl.java @@ -192,12 +192,11 @@ private Collection> getImports( if (imports.isEmpty()) { return Collections.EMPTY_LIST; } - CtPackageReference pack = ((CtTypeReference) imports - .get(simpleType.getSimpleName())).getPackage(); + CtPackageReference pack = simpleType.getPackage().getReference(); List> refs = new ArrayList<>(); for (CtTypeReference ref : imports.values()) { // ignore non-top-level type - if (ref.getPackage() != null) { + if (ref.getPackage() != null && !ref.getPackage().isUnnamedPackage()) { // ignore java.lang package if (!ref.getPackage().getSimpleName().equals("java.lang")) { // ignore type in same package @@ -218,6 +217,10 @@ private boolean addImport(CtTypeReference ref) { if (imports.containsKey(ref.getSimpleName())) { return isImported(ref); } + // don't import unnamed package elements + if (ref.getPackage() == null || ref.getPackage().isUnnamedPackage()) { + return false; + } imports.put(ref.getSimpleName(), ref); return true; } diff --git a/src/main/java/spoon/support/JavaOutputProcessor.java b/src/main/java/spoon/support/JavaOutputProcessor.java index abb93bee1d5..bf4d460b86a 100644 --- a/src/main/java/spoon/support/JavaOutputProcessor.java +++ b/src/main/java/spoon/support/JavaOutputProcessor.java @@ -141,7 +141,7 @@ public void createJavaFile(CtType element) { // create package directory File packageDir; - if (pack.getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) { + if (pack.isUnnamedPackage()) { packageDir = new File(directory.getAbsolutePath()); } else { // Create current package dir diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java index 8c8ca9998e4..a5c37d26215 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBasedSpoonCompiler.java @@ -550,7 +550,7 @@ protected void generateProcessedSourceFilesUsingCUs() { // create package directory File packageDir; - if (pack.getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) { + if (pack.isUnnamedPackage()) { packageDir = new File(outputDirectory.getAbsolutePath()); } else { // Create current package directory diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java index cd95568f78b..d0d24550418 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java @@ -122,7 +122,7 @@ private void addVirtualJavaFile(List unitList, CtType ctType // create package directory File packageDir; - if (CtPackage.TOP_LEVEL_PACKAGE_NAME.equals(pack.getQualifiedName())) { + if (pack.isUnnamedPackage()) { packageDir = new File(directory.getAbsolutePath()); } else { packageDir = new File(directory.getAbsolutePath() + File.separatorChar + pack.getQualifiedName().replace('.', File.separatorChar)); diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 7c60bef3dd9..9191a370faa 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -679,7 +679,7 @@ public CtExecutableReference getExecutableReference(MethodBinding exec) { public CtPackageReference getPackageReference(PackageBinding reference) { String name = new String(reference.shortReadableName()); if (name.length() == 0) { - return null; + return factory.Package().topLevel(); } CtPackageReference ref = factory.Core().createPackageReference(); ref.setSimpleName(name); @@ -743,7 +743,7 @@ private void insertGenericTypesInNoClasspathFromJDTInSpoon(TypeReference ori } /** - * JDT doesn't returns a correct AST with the resolved type of the reference. + * JDT doesn't return a correct AST with the resolved type of the reference. * This method try to build a correct Spoon AST from the name of the JDT * reference, thanks to the parsing of the string, the name parameterized from * the JDT reference and java convention. @@ -1030,9 +1030,7 @@ public CtTypeReference getTypeReference(TypeBinding binding) { ref.setImplicit(isImplicit || !JDTTreeBuilder.this.context.isLambdaParameterImplicitlyTyped); ref.setSimpleName(new String(binding.readableName())); final CtReference declaring = references.getDeclaringReferenceFromImports(binding.sourceName()); - if (declaring instanceof CtPackageReference) { - ref.setPackage((CtPackageReference) declaring); - } + setPackageOrDeclaringType(ref, declaring); } else if (binding instanceof SpoonReferenceBinding) { ref = factory.Core().createTypeReference(); ref.setSimpleName(new String(binding.sourceName())); @@ -1152,6 +1150,22 @@ public List> getBoundedTypesReferences(TypeBinding[] genericT } } + /** + * Sets {@code declaring} as inner of {@code ref}, as either the package or the declaring type + */ + private void setPackageOrDeclaringType(CtTypeReference ref, CtReference declaring) { + if (declaring instanceof CtPackageReference) { + ref.setPackage((CtPackageReference) declaring); + } else if (declaring instanceof CtTypeReference) { + ref.setDeclaringType((CtTypeReference) declaring); + } else if (declaring == null) { + ref.setPackage(factory.Package().topLevel()); + } else { + throw new AssertionError( + "unexpected declaring type: " + declaring.getClass() + " of " + declaring); + } + } + public static Set getModifiers(int mod) { Set ret = EnumSet.noneOf(ModifierKind.class); if ((mod & ClassFileConstants.AccPublic) != 0) { @@ -2933,15 +2947,12 @@ public boolean visit(MessageSend messageSend, BlockScope scope) { CtTypeReference typeReference = factory.Core().createTypeReference(); typeReference.setSimpleName(messageSend.receiver.toString()); final CtReference declaring = references.getDeclaringReferenceFromImports(((SingleNameReference) messageSend.receiver).token); - if (declaring instanceof CtPackageReference) { - typeReference.setPackage((CtPackageReference) declaring); - } else if (declaring instanceof CtTypeReference) { - typeReference = (CtTypeReference) declaring; - } + setPackageOrDeclaringType(typeReference, declaring); ref.setDeclaringType(typeReference); } else if (messageSend.receiver instanceof QualifiedNameReference) { QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) messageSend.receiver; + // TODO try to determine package/class boundary by upper case char[][] packageName = CharOperation.subarray(qualifiedNameReference.tokens, 0, qualifiedNameReference.tokens.length - 1); char[][] className = CharOperation.subarray(qualifiedNameReference.tokens, qualifiedNameReference.tokens.length - 1, qualifiedNameReference.tokens.length); if (packageName.length > 0) { @@ -3415,11 +3426,7 @@ public boolean visit(SingleNameReference singleNameReference, BlockScope scope) CtTypeReference typeReference = factory.Core().createTypeReference(); typeReference.setSimpleName(new String(singleNameReference.binding.readableName())); final CtReference declaring = references.getDeclaringReferenceFromImports(singleNameReference.token); - if (declaring instanceof CtPackageReference) { - typeReference.setPackage((CtPackageReference) declaring); - } else if (declaring instanceof CtTypeReference) { - typeReference = (CtTypeReference) declaring; - } + setPackageOrDeclaringType(typeReference, declaring); final CtTypeAccess ta = factory.Code().createTypeAccess(typeReference); context.enter(ta, singleNameReference); return true; @@ -3745,7 +3752,7 @@ public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope if (typeDeclaration.binding.fPackage.shortReadableName() != null && typeDeclaration.binding.fPackage.shortReadableName().length > 0) { pack = factory.Package().getOrCreate(new String(typeDeclaration.binding.fPackage.shortReadableName())); } else { - pack = factory.Package().getOrCreate(CtPackage.TOP_LEVEL_PACKAGE_NAME); + pack = factory.Package().getRootPackage(); } context.enter(pack, typeDeclaration); context.compilationunitdeclaration = scope.referenceContext; diff --git a/src/main/java/spoon/support/reflect/declaration/CtPackageImpl.java b/src/main/java/spoon/support/reflect/declaration/CtPackageImpl.java index 828d84dd016..4f0e8c6e85b 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtPackageImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtPackageImpl.java @@ -125,11 +125,11 @@ public Set getPackages() { @Override public String getQualifiedName() { - if (getDeclaringPackage() == null || TOP_LEVEL_PACKAGE_NAME.equals( - ((CtPackageImpl) getDeclaringPackage()).simpleName)) { + if (getDeclaringPackage() == null || getDeclaringPackage().isUnnamedPackage()) { return getSimpleName(); + } else { + return getDeclaringPackage().getQualifiedName() + "." + getSimpleName(); } - return getDeclaringPackage().getQualifiedName() + "." + getSimpleName(); } @Override @@ -221,4 +221,9 @@ public E setShadow(boolean isShadow) { public CtPackage clone() { return (CtPackage) super.clone(); } + + @Override + public boolean isUnnamedPackage() { + return TOP_LEVEL_PACKAGE_NAME.equals(getSimpleName()); + } } diff --git a/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java b/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java index c4e90685c6c..619e50dde7d 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java @@ -655,15 +655,16 @@ public List> getMethodsByName(String name) { @Override public String getQualifiedName() { if (isTopLevel()) { - if ((getPackage() != null) && !getPackage().getSimpleName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) { + if (getPackage() != null && !getPackage().isUnnamedPackage()) { return getPackage().getQualifiedName() + "." + getSimpleName(); + } else { + return getSimpleName(); } - return getSimpleName(); - } - if (getDeclaringType() != null) { + } else if (getDeclaringType() != null) { return getDeclaringType().getQualifiedName() + INNERTTYPE_SEPARATOR + getSimpleName(); + } else { + return getSimpleName(); } - return getSimpleName(); } @Override diff --git a/src/main/java/spoon/support/reflect/reference/CtPackageReferenceImpl.java b/src/main/java/spoon/support/reflect/reference/CtPackageReferenceImpl.java index 17d6c26a814..c7a138a595f 100644 --- a/src/main/java/spoon/support/reflect/reference/CtPackageReferenceImpl.java +++ b/src/main/java/spoon/support/reflect/reference/CtPackageReferenceImpl.java @@ -58,4 +58,9 @@ protected AnnotatedElement getActualAnnotatedElement() { public CtPackageReference clone() { return (CtPackageReference) super.clone(); } + + @Override + public boolean isUnnamedPackage() { + return getSimpleName().isEmpty(); + } } diff --git a/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java b/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java index 344a1a0fbf4..d8c80033fec 100644 --- a/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java +++ b/src/main/java/spoon/support/reflect/reference/CtTypeReferenceImpl.java @@ -177,7 +177,7 @@ public CtPackageReference getPackage() { public String getQualifiedName() { if (getDeclaringType() != null) { return getDeclaringType().getQualifiedName() + CtType.INNERTTYPE_SEPARATOR + getSimpleName(); - } else if (getPackage() != null && !CtPackage.TOP_LEVEL_PACKAGE_NAME.equals(getPackage().getSimpleName())) { + } else if (getPackage() != null && !getPackage().isUnnamedPackage()) { return getPackage().getSimpleName() + CtPackage.PACKAGE_SEPARATOR + getSimpleName(); } else { return getSimpleName(); diff --git a/src/test/java/spoon/test/imports/ImportTest.java b/src/test/java/spoon/test/imports/ImportTest.java index 6a72ead02cb..050ae004f29 100644 --- a/src/test/java/spoon/test/imports/ImportTest.java +++ b/src/test/java/spoon/test/imports/ImportTest.java @@ -204,13 +204,13 @@ public boolean matches(CtInvocation element) { assertCorrectInvocation(new Expected().name("staticD").target("pack2.C.D").declaringType("D").typeIsNull(false), elements.get(11)); // Invocation for a static method with the declaring class specified and an import *. - assertCorrectInvocation(new Expected().name("staticE").target("pack3.E").declaringType("E").typeIsNull(true), elements.get(12)); + assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(true), elements.get(12)); // Invocation for a static method without the declaring class specified and an import *. assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(true), elements.get(13)); // Invocation for a static method with the declaring class specified, a return type and an import *. - assertCorrectInvocation(new Expected().name("staticE").target("pack3.E").declaringType("E").typeIsNull(false), elements.get(14)); + assertCorrectInvocation(new Expected().name("staticE").target("pack3.E.E").declaringType("E").typeIsNull(false), elements.get(14)); // Invocation for a static method without the declaring class specified, a return type and an import *. assertCorrectInvocationWithLimit(new Expected().name("staticE").typeIsNull(false), elements.get(15)); diff --git a/src/test/java/spoon/test/parent/ParentTest.java b/src/test/java/spoon/test/parent/ParentTest.java index 4b9f7de58b6..68055915774 100644 --- a/src/test/java/spoon/test/parent/ParentTest.java +++ b/src/test/java/spoon/test/parent/ParentTest.java @@ -124,15 +124,15 @@ public void testParentOfCtPackageReference() throws Exception { final CtType panini = launcher.getFactory().Type().get("Panini"); - CtPackage pack2 = (CtPackage) panini.getPackage().getParent(); - assertNotNull(pack2); + CtElement topLevelParent = panini.getPackage().getParent(); + assertNotNull(topLevelParent); assertEquals(CtPackage.TOP_LEVEL_PACKAGE_NAME, panini.getPackage().getSimpleName()); CtPackage pack1 = factory.Package().getRootPackage(); // the factory are not the same assertNotEquals(factory, launcher.getFactory()); // so the root packages are not deeply equals - assertNotEquals(pack1, pack2); + assertNotEquals(pack1, topLevelParent); final CtTypeReference burritos = panini.getReferences(new ReferenceTypeFilter>(CtTypeReference.class) { @Override diff --git a/src/test/java/spoon/test/reference/TypeReferenceTest.java b/src/test/java/spoon/test/reference/TypeReferenceTest.java index 4974ffe4dec..8143ebd1989 100644 --- a/src/test/java/spoon/test/reference/TypeReferenceTest.java +++ b/src/test/java/spoon/test/reference/TypeReferenceTest.java @@ -447,7 +447,8 @@ public void testAnonymousClassesHaveAnEmptyStringForItsNameInNoClasspath() throw final CtClass aClass = launcher.getFactory().Class().get("A"); final CtClass anonymousClass = aClass.getElements(new TypeFilter<>(CtNewClass.class)).get(0).getAnonymousClass(); assertEquals("1", anonymousClass.getReference().getSimpleName()); - assertEquals(7, aClass.getReferencedTypes().size()); + Set> referencedTypes = aClass.getReferencedTypes(); + assertEquals(7, referencedTypes.size()); } @Test diff --git a/src/test/java/spoon/testing/CtElementAssertTest.java b/src/test/java/spoon/testing/CtElementAssertTest.java index 0b81b44a0e3..66501a89283 100644 --- a/src/test/java/spoon/testing/CtElementAssertTest.java +++ b/src/test/java/spoon/testing/CtElementAssertTest.java @@ -22,7 +22,8 @@ public void testEqualityBetweenTwoCtElement() throws Exception { expected.setSimpleName("i"); expected.setType(factory.Type().INTEGER_PRIMITIVE); expected.addModifier(ModifierKind.PUBLIC); - assertThat(type.getField("i")).isEqualTo(expected); + CtField f = type.getField("i"); + assertThat(f).isEqualTo(expected); } @Test From fc4b7e43f5133c30a7c7a4de7e201023a7a61835 Mon Sep 17 00:00:00 2001 From: GerardPaligot Date: Thu, 16 Jun 2016 10:46:06 +0200 Subject: [PATCH 2/3] fix: Sets unamed package for null exp built by JDT. --- src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 9191a370faa..3ed7d08b272 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -3075,8 +3075,7 @@ public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { @Override public boolean visit(NullLiteral nullLiteral, BlockScope scope) { CtLiteral lit = factory.Core().createLiteral(); - CtTypeReference ref = factory.Core().createTypeReference(); - ref.setSimpleName(CtTypeReference.NULL_TYPE_NAME); + CtTypeReference ref = factory.Type().nullType(); lit.setType(ref); context.enter(lit, nullLiteral); return true; From 3b854e10c4dd785020baf8ce34ec74cb7868b410 Mon Sep 17 00:00:00 2001 From: GerardPaligot Date: Thu, 16 Jun 2016 10:50:09 +0200 Subject: [PATCH 3/3] fix: Referenced type of a class. Before this PR, aClass.getReferencedTypes() in the test case testAnonymousClassesHaveAnEmptyStringForItsNameInNoClasspath returns 7 items because the reference A was referenced twice in the sample class. Now, these references are same and we return a Set so we don't have any more two references for A. --- src/test/java/spoon/test/reference/TypeReferenceTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/spoon/test/reference/TypeReferenceTest.java b/src/test/java/spoon/test/reference/TypeReferenceTest.java index 8143ebd1989..7bcc8664c69 100644 --- a/src/test/java/spoon/test/reference/TypeReferenceTest.java +++ b/src/test/java/spoon/test/reference/TypeReferenceTest.java @@ -447,8 +447,7 @@ public void testAnonymousClassesHaveAnEmptyStringForItsNameInNoClasspath() throw final CtClass aClass = launcher.getFactory().Class().get("A"); final CtClass anonymousClass = aClass.getElements(new TypeFilter<>(CtNewClass.class)).get(0).getAnonymousClass(); assertEquals("1", anonymousClass.getReference().getSimpleName()); - Set> referencedTypes = aClass.getReferencedTypes(); - assertEquals(7, referencedTypes.size()); + assertEquals(6, aClass.getReferencedTypes().size()); } @Test