Skip to content

Commit

Permalink
Adding the ability to get a Package from a PojoClass.
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed Oct 23, 2015
1 parent 94633d1 commit 08eebf4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/openpojo/reflection/PojoClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ public interface PojoClass extends PojoElement {
*/
List<PojoClass> getInterfaces();

/**
* Get Enclosing Package
*
* @return the package that has this class is part of.
*/
PojoPackage getPackage();

/**
* This method returns the underlying class represented by this instance.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/openpojo/reflection/impl/PojoClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.PojoField;
import com.openpojo.reflection.PojoMethod;
import com.openpojo.reflection.PojoPackage;
import com.openpojo.reflection.exception.ReflectionException;
import com.openpojo.reflection.java.Java;
import com.openpojo.reflection.java.packageloader.impl.URLToFileSystemAdapter;
Expand Down Expand Up @@ -177,6 +178,10 @@ public List<PojoClass> getInterfaces() {
return interfaces;
}

public PojoPackage getPackage() {
return PojoPackageFactory.getPojoPackage(clazz.getPackage().getName());
}

public String getSourcePath() {
try {
final ClassLoader cl = this.getClazz().getClassLoader();
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/openpojo/reflection/impl/PojoClassImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.PojoField;
import com.openpojo.reflection.PojoMethod;
import com.openpojo.reflection.PojoPackage;
import com.openpojo.reflection.construct.InstanceFactory;
import com.openpojo.reflection.exception.ReflectionException;
import com.openpojo.reflection.impl.sampleannotation.AnotherAnnotation;
Expand Down Expand Up @@ -395,6 +396,15 @@ public void testGetPath() {
Affirm.affirmTrue("Should end with this class's package path [" + sourcePath + "]", sourcePath.endsWith(thisClassEndingPath));
}

@Test
public void testGetPackage() {
PojoClass pojoClass = getPojoClassImplForClass(this.getClass());

PojoPackage pojoPackage = pojoClass.getPackage();
Affirm.affirmNotNull("Null package received", pojoPackage);
Affirm.affirmEquals("Invalid package retrieved", this.getClass().getPackage().getName(), pojoPackage.getName());
}

private static PojoClass getPojoClassImplForClass(final Class<?> clazz) {
return PojoClassFactory.getPojoClass(clazz);
}
Expand Down

0 comments on commit 08eebf4

Please sign in to comment.