Skip to content

Commit

Permalink
fix: Fix NPE due to duplicated package-info.java with setIgnoreDuplic…
Browse files Browse the repository at this point in the history
…ateDeclarations(true) (#3791)
  • Loading branch information
slarse authored Feb 11, 2021
1 parent 65108d8 commit 6488350
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
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
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/pkg/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
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;

0 comments on commit 6488350

Please sign in to comment.