Last active
August 29, 2015 14:21
-
-
Save leifoolsen/41cb32d3b027c80f38b9 to your computer and use it in GitHub Desktop.
Find class annotated with a given annotation using com.google.common.reflect.ClassPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Find class annotated with @ApplicationPath and extract value from annotation: @ApplicationPath("/api/*") | |
ClassPath cp = ClassPath.from(Thread.currentThread().getContextClassLoader()); | |
String appPath = ""; | |
for (ClassPath.ClassInfo classInfo : cp.getTopLevelClassesRecursive("com.github.leifoolsen")) { | |
Class<?> clazz = classInfo.load(); | |
if(clazz.isAnnotationPresent(ApplicationPath.class)) { | |
appPath = clazz.getAnnotation(ApplicationPath.class).value(); | |
break; | |
} | |
} | |
assertEquals("/api/*", appPath); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment