Skip to content

Commit

Permalink
feat(CtPackage): add two utility methods #isEmpty() and #hasPackageIn…
Browse files Browse the repository at this point in the history
…fo() (#2957)
  • Loading branch information
nharrand authored and monperrus committed May 15, 2019
1 parent 7da741d commit 5b34164
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,14 @@ public interface CtPackage extends CtNamedElement, CtShadowable {
* See JLS §7.4.2. Unnamed Packages.
*/
boolean isUnnamedPackage();

/**
* @return true if the package contains a package-info.java file
*/
boolean hasPackageInfo();

/**
* @return true if the package contains no types nor any other packages
*/
boolean isEmpty();
}
11 changes: 11 additions & 0 deletions src/main/java/spoon/support/reflect/declaration/CtPackageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package spoon.support.reflect.declaration;

import spoon.reflect.annotations.MetamodelPropertyField;
import spoon.reflect.cu.position.NoSourcePosition;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtPackage;
Expand Down Expand Up @@ -231,4 +232,14 @@ public CtPackage clone() {
public boolean isUnnamedPackage() {
return TOP_LEVEL_PACKAGE_NAME.equals(getSimpleName());
}

@Override
public boolean hasPackageInfo() {
return !(getPosition() instanceof NoSourcePosition);
}

@Override
public boolean isEmpty() {
return getPackages().isEmpty() && getTypes().isEmpty();
}
}
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/pkg/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public void testPackage() throws Exception {
assertEquals("spoon.test.pkg.name", ctPackage.getQualifiedName());
assertEquals("", ctPackage.getDocComment());
assertTrue(CtPackage.class.isAssignableFrom(ctPackage.getParent().getClass()));
assertFalse(ctPackage.hasPackageInfo());
assertFalse(ctPackage.isEmpty());

ctPackage = (CtPackage) ctPackage.getParent();
assertEquals("spoon.test.pkg", ctPackage.getQualifiedName());
Expand All @@ -86,6 +88,7 @@ public void testPackage() throws Exception {
assertEquals(71, ctPackage.getPosition().getSourceEnd());
assertEquals(1, ctPackage.getAnnotations().size());
assertEquals("This is test\nJavaDoc.", ctPackage.getComments().get(0).getContent());
assertTrue(ctPackage.hasPackageInfo());

CtAnnotation<?> annotation = ctPackage.getAnnotations().get(0);
assertSame(Deprecated.class, annotation.getAnnotationType().getActualClass());
Expand All @@ -97,6 +100,9 @@ public void testPackage() throws Exception {
ctPackage = (CtPackage) ctPackage.getParent();
assertEquals("spoon.test", ctPackage.getQualifiedName());
assertEquals("", ctPackage.getDocComment());

//contract: a freshly created package is empty
assertTrue(factory.createPackage().isEmpty());
}

@Test
Expand Down

0 comments on commit 5b34164

Please sign in to comment.