Class ReflectionUtility
java.lang.Object
org.gcube.vremanagement.executor.utils.ReflectionUtility
Got from
https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection#answer-22462785
The method first gets the current ClassLoader. It then fetches all resources
that contain said package and iterates of these URLs. It then creates a
URLConnection and determines what type of URL we have. It can either be a
directory (FileURLConnection) or a directory inside a jar or zip file
(JarURLConnection). Depending on what type of connection we have two
different methods will be called.
First lets see what happens if it is a FileURLConnection.
It first checks if the passed File exists and is a directory. If that's the
case it checks if it is a class file. If so a Class object will be created
and put in the List. If it is not a class file but is a directory, we
simply iterate into it and do the same thing. All other cases/files will be
ignored.
If the URLConnection is a JarURLConnection the other private helper method
will be called. This method iterates over all Entries in the zip/jar
archive. If one entry is a class file and is inside of the package a Class
object will be created and stored in the ArrayList.
After all resources have been parsed it (the main method) returns the
ArrayList containing all classes in the given package, that the current
ClassLoader knows about.
If the process fails at any point a ClassNotFoundException will be thrown
containing detailed information about the exact cause.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetClassesForPackage(Package packageObject) getClassesForPackage(String pckgname) Attempts to list all the classes in the specified package as determined by the context class loader
-
Constructor Details
-
ReflectionUtility
public ReflectionUtility()
-
-
Method Details
-
getClassesForPackage
public static List<Class<?>> getClassesForPackage(Package packageObject) throws ClassNotFoundException - Throws:
ClassNotFoundException
-
getClassesForPackage
Attempts to list all the classes in the specified package as determined by the context class loader- Parameters:
pckgname- the package name to search- Returns:
- a list of classes that exist within that package
- Throws:
ClassNotFoundException- if something went wrong
-