I'm writing a plugin that scans my project's code on compile-time and finds the controllers to make sure they have some other custom annotation that I've created.
For some reason, all of Spring's annotations don't appear in the annotations array on
Class<?> clazz = classInfo.load();
Annotation[] annotations = clazz.getAnnotations();
Why are the spring annotations missing from this array? And where can I find them?
Here's my relevant code:
URL[] urls;
List<URL> listOfURL = new ArrayList<>();
SourceSetContainer ssc = getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
File classesDir = ssc.getByName("main").getOutput().getClassesDir();
listOfURL.add(classesDir.toURI().toURL());
urls = listOfURL.toArray(new URL[0]);
final ClassLoader loader = new URLClassLoader(urls);
ClassPath classpath = ClassPath.from(loader); // scans the class path used by classloader
for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(packageName)) {
if (classInfo.getName().toLowerCase().contains("controller")) {
Class<?> clazz = classInfo.load();
if (clazz.isAnnotationPresent(RestController.class)) {
Method[] methods = clazz.getMethods();
for (Method method : methods) {
doSomething(clazz, method);
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire