diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 819265d192a..022ecdec797 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -1728,15 +1728,14 @@ public boolean visit(TypeDeclaration memberTypeDeclaration, ClassScope scope) { @Override public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope) { - if ("package-info".equals(new String(typeDeclaration.name))) { + if (typeDeclaration.binding == null && getFactory().getEnvironment().isIgnoreDuplicateDeclarations()) { + // skip the type declaration that are already declared + return false; + } else if ("package-info".equals(new String(typeDeclaration.name))) { context.enter(factory.Package().getOrCreate(new String(typeDeclaration.binding.fPackage.readableName())), typeDeclaration); return true; } else { CtModule module; - // skip the type declaration that are already declared - if (typeDeclaration.binding == null && getFactory().getEnvironment().isIgnoreDuplicateDeclarations()) { - return false; - } if (typeDeclaration.binding.module != null && !typeDeclaration.binding.module.isUnnamed() && typeDeclaration.binding.module.shortReadableName() != null && typeDeclaration.binding.module.shortReadableName().length > 0) { module = factory.Module().getOrCreate(String.valueOf(typeDeclaration.binding.module.shortReadableName())); } else { @@ -1754,7 +1753,6 @@ public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope return true; } } - @Override public boolean visit(UnaryExpression unaryExpression, BlockScope scope) { CtUnaryOperator op = factory.Core().createUnaryOperator(); diff --git a/src/test/java/spoon/test/pkg/PackageTest.java b/src/test/java/spoon/test/pkg/PackageTest.java index cc7bf2c5b6d..bb4b34c28ad 100644 --- a/src/test/java/spoon/test/pkg/PackageTest.java +++ b/src/test/java/spoon/test/pkg/PackageTest.java @@ -28,6 +28,7 @@ import spoon.reflect.declaration.CtAnnotation; import spoon.reflect.declaration.CtAnnotationType; import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtCompilationUnit; import spoon.reflect.declaration.CtField; import spoon.reflect.declaration.CtPackage; import spoon.reflect.factory.Factory; @@ -46,9 +47,11 @@ import java.io.File; import java.io.FileReader; import java.nio.file.Files; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -148,6 +151,22 @@ public void testPrintPackageInfoWhenNothingInPackage() { assertThat(aPackage).isEqualTo(ModelUtils.build(new File("./target/spooned/package/spoon/test/pkg/testclasses/internal")).Package().get("spoon.test.pkg.testclasses.internal")); } + + @Test + public void testHandlesDuplicatePackageInfo(){ + // contract: with setIgnoreDuplicateDeclarations(true), duplicated package-info.java files should be allowed + + final Launcher launcher = new Launcher(); + launcher.getEnvironment().setIgnoreDuplicateDeclarations(true); + launcher.addInputResource("./src/test/resources/duplicate-package-info"); + launcher.run(); + + List compilationUnits = new ArrayList<>(launcher.getFactory().CompilationUnit().getMap().values()); + + org.hamcrest.MatcherAssert.assertThat(compilationUnits.size(), equalTo(2)); + compilationUnits.forEach( + cu -> org.hamcrest.MatcherAssert.assertThat(cu.getFile().getName(), equalTo("package-info.java"))); + } @Test public void testAnnotationInPackageInfoWhenTemplatesCompiled() throws Exception { diff --git a/src/test/resources/duplicate-package-info/subproj-A/org/pkg/package-info.java b/src/test/resources/duplicate-package-info/subproj-A/org/pkg/package-info.java new file mode 100644 index 00000000000..0d13acfa210 --- /dev/null +++ b/src/test/resources/duplicate-package-info/subproj-A/org/pkg/package-info.java @@ -0,0 +1 @@ +package org.pkg; \ No newline at end of file diff --git a/src/test/resources/duplicate-package-info/subproj-B/org/pkg/package-info.java b/src/test/resources/duplicate-package-info/subproj-B/org/pkg/package-info.java new file mode 100644 index 00000000000..0d13acfa210 --- /dev/null +++ b/src/test/resources/duplicate-package-info/subproj-B/org/pkg/package-info.java @@ -0,0 +1 @@ +package org.pkg; \ No newline at end of file