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

feat(CtPackage): add two utility methods #isEmpty() and #hasPackageInfo() #2957

Merged
merged 3 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
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: 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