Skip to content

Instantly share code, notes, and snippets.

@leifoolsen
Last active August 29, 2015 14:21
Show Gist options
  • Save leifoolsen/41cb32d3b027c80f38b9 to your computer and use it in GitHub Desktop.
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
// 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