Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix NPE due to duplicated package-info.java with setIgnoreDuplicateDeclarations(true) #3791

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
@@ -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();
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/pkg/PackageTest.java
Original file line number Diff line number Diff line change
@@ -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<CtCompilationUnit> 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 {
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.pkg;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package org.pkg;