From a061f49da5e015e19a1e2e1784aca1c896134859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Tue, 9 Feb 2021 16:50:10 +0100 Subject: [PATCH 1/3] Add test case to replicate crash --- src/test/java/spoon/test/pkg/PackageTest.java | 19 +++++++++++++++++++ .../subproj-A/org/pkg/package-info.java | 1 + .../subproj-B/org/pkg/package-info.java | 1 + 3 files changed, 21 insertions(+) create mode 100644 src/test/resources/duplicate-package-info/subproj-A/org/pkg/package-info.java create mode 100644 src/test/resources/duplicate-package-info/subproj-B/org/pkg/package-info.java diff --git a/src/test/java/spoon/test/pkg/PackageTest.java b/src/test/java/spoon/test/pkg/PackageTest.java index dbfbaa2bfaa..704ccd0beaa 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; @@ -146,6 +149,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 From c0bf78ad671a0fde154b43f9e5d31688d880c842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Wed, 10 Feb 2021 14:02:45 +0100 Subject: [PATCH 2/3] Solve NPE crash on duplicated package-info files --- .../spoon/support/compiler/jdt/JDTTreeBuilder.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 819265d192a..0619bd85fc2 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -7,6 +7,8 @@ */ package spoon.support.compiler.jdt; +import org.eclipse.jdt.core.compiler.CategorizedProblem; +import org.eclipse.jdt.core.compiler.IProblem; import org.slf4j.Logger; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ASTVisitor; @@ -160,6 +162,8 @@ import spoon.support.reflect.CtExtendedModifier; import java.lang.invoke.MethodHandles; +import java.util.Arrays; +import java.util.function.Function; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getBinaryOperatorKind; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getModifiers; @@ -1728,15 +1732,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 +1757,6 @@ public boolean visit(TypeDeclaration typeDeclaration, CompilationUnitScope scope return true; } } - @Override public boolean visit(UnaryExpression unaryExpression, BlockScope scope) { CtUnaryOperator op = factory.Core().createUnaryOperator(); From e96ef3ef0e379cb63b8821b254cdcd0bd2588e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lars=C3=A9n?= Date: Wed, 10 Feb 2021 14:05:27 +0100 Subject: [PATCH 3/3] Remove unused imports --- src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index 0619bd85fc2..022ecdec797 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -7,8 +7,6 @@ */ package spoon.support.compiler.jdt; -import org.eclipse.jdt.core.compiler.CategorizedProblem; -import org.eclipse.jdt.core.compiler.IProblem; import org.slf4j.Logger; import org.eclipse.jdt.core.compiler.CharOperation; import org.eclipse.jdt.internal.compiler.ASTVisitor; @@ -162,8 +160,6 @@ import spoon.support.reflect.CtExtendedModifier; import java.lang.invoke.MethodHandles; -import java.util.Arrays; -import java.util.function.Function; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getBinaryOperatorKind; import static spoon.support.compiler.jdt.JDTTreeBuilderQuery.getModifiers;