Package uk.gov.gchq.koryphe.util
Class ReflectionUtil
- java.lang.Object
-
- uk.gov.gchq.koryphe.util.ReflectionUtil
-
public final class ReflectionUtil extends Object
Reflection utilities. Contains methods such as getting sub classes. By default only classes prefixed with 'uk.gov.gchq' will be scanned. If you wish to include your own packages/classes in the scanner you can calladdReflectionPackages(String...)
or set the System Property "koryphe.reflection.packages" with a csv of your additional packages.
-
-
Field Summary
Fields Modifier and Type Field Description static Set<String>
DEFAULT_PACKAGES
static String
PACKAGES_KEY
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addReflectionPackages(Iterable<String> newPackages)
static void
addReflectionPackages(String... newPackages)
Adds new reflection packages.static Set<Class>
getAnnotatedTypes(Class<? extends Annotation> annoClass)
Get classes annotated with the given annotation class.static Class<?>
getClassFromName(String className)
Simply returns Class.forName(className), but any ClassNotFoundException exceptions are caught and null is returned.static Set<String>
getReflectionPackages()
static Map<String,Set<Class>>
getSimpleClassNames(Class<?> clazz)
Get a map of simple class name to class, for all sub classes of a given class.static Set<Class>
getSubTypes(Class<?> clazz)
Get implementations of a given class.static boolean
isPublicConcrete(Class clazz)
static void
keepPublicConcreteClasses(Collection<Class> classes)
Removes non public or non concrete classes from the given collection.static void
resetReflectionCache()
Resets the caches.static void
resetReflectionPackages()
Resets the list of packages that are scanned.static void
updateReflectionPackages()
Updates the reflection packages using the system property.
-
-
-
Field Detail
-
PACKAGES_KEY
public static final String PACKAGES_KEY
- See Also:
- Constant Field Values
-
-
Method Detail
-
getClassFromName
public static Class<?> getClassFromName(String className)
Simply returns Class.forName(className), but any ClassNotFoundException exceptions are caught and null is returned.- Parameters:
className
- the class name to lookup- Returns:
- the class if found, otherwise null.
-
getSimpleClassNames
public static Map<String,Set<Class>> getSimpleClassNames(Class<?> clazz)
Get a map of simple class name to class, for all sub classes of a given class. The results are cached.- Parameters:
clazz
- the class to get simple class names for.- Returns:
- a map of simple class name to class
-
getSubTypes
public static Set<Class> getSubTypes(Class<?> clazz)
Get implementations of a given class. The results are cached.- Parameters:
clazz
- the class to get sub types of.- Returns:
- the sub classes.
-
getAnnotatedTypes
public static Set<Class> getAnnotatedTypes(Class<? extends Annotation> annoClass)
Get classes annotated with the given annotation class. The results are cached.- Parameters:
annoClass
- the annotation class to get classes for.- Returns:
- the annotated classes.
-
isPublicConcrete
public static boolean isPublicConcrete(Class clazz)
- Parameters:
clazz
- the class to test- Returns:
- true if the class is public, not abstract and not an interface.
-
keepPublicConcreteClasses
public static void keepPublicConcreteClasses(Collection<Class> classes)
Removes non public or non concrete classes from the given collection.- Parameters:
classes
- the collection of classes to modify.
-
resetReflectionPackages
public static void resetReflectionPackages()
Resets the list of packages that are scanned. It will be set back to the default path and the paths set in the system property "koryphe.reflection.packages".
-
resetReflectionCache
public static void resetReflectionCache()
Resets the caches.
-
updateReflectionPackages
public static void updateReflectionPackages()
Updates the reflection packages using the system property.
-
addReflectionPackages
public static void addReflectionPackages(String... newPackages)
Adds new reflection packages. If any new packages are found then the reflection cache is reset.- Parameters:
newPackages
- new packages to add. These can be CSVs.
-
-