-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnnotationProcessor.java
120 lines (110 loc) · 3.68 KB
/
AnnotationProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.kn.processor;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
/**
* @author krishnanand
*
*/
public class AnnotationProcessor {
/**
* @param annotation
* @param packages
* @return {@linkplain Set} of {@linkplain Class}es that are annotated with
* annotation in the specified package. Set will be empty if none
* found
*/
public Set<Class<?>> processAnnotations(Class<? extends Annotation> annotation, String[] packages) {
Set<Class<?>> classes = getAllClasses(packages);
Set<Class<?>> annotatedClasses = new HashSet<>();
for (Class<?> clazz : classes) {
Annotation declaredAnnotation = clazz.getAnnotation(annotation);
if (declaredAnnotation != null) {
annotatedClasses.add(clazz);
}
}
return annotatedClasses;
}
/**
* @param annotation
* @param packages
* @param classLoader
* @return {@linkplain Set} of {@linkplain Class}es that are annotated with
* annotation in the specified package. Set will be empty if none
* found
*/
public Set<Class<?>> processAnnotations(Class<? extends Annotation> annotation, String[] packages,
ClassLoader classLoader) {
Set<Class<?>> classes = getAllClasses(packages, classLoader);
Set<Class<?>> annotatedClasses = new HashSet<>();
for (Class<?> clazz : classes) {
Annotation declaredAnnotation = clazz.getAnnotation(annotation);
if (declaredAnnotation != null) {
annotatedClasses.add(clazz);
}
}
return annotatedClasses;
}
public Set<Class<?>> getAllClasses(String[] packages, ClassLoader classLoader) {
Set<Class<?>> classes = new HashSet<>();
for (String pakage : packages) {
classes.addAll(getAllClasses(pakage, classLoader));
}
return classes;
}
public Set<Class<?>> getAllClasses(String[] packages) {
Set<Class<?>> classes = new HashSet<>();
for (String pakage : packages) {
classes.addAll(getAllClasses(pakage));
}
return classes;
}
/**
* @param scanPackage
* @return {@linkplain Set} of {@linkplain Class}es that are loaded from the
* specified package. Set will be empty if none found
*/
public Set<Class<?>> getAllClasses(String scanPackage, ClassLoader classLoader) {
String realPath = scanPackage.replace(".", "/");
Set<Class<?>> classes = new HashSet<>();
try {
Enumeration<URL> urls = classLoader.getResources(realPath);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
try {
File directory = new File(url.toURI());
if (directory.isDirectory()) {
File[] files = directory.listFiles();
for (File file : files) {
String fileName = file.getName();
if (fileName.endsWith(".class")) {
String className = scanPackage + '.' + fileName.subSequence(0, fileName.length() - 6);
try {
classes.add(Class.forName(className));
} catch (ClassNotFoundException e) {
System.err.println("class: " + className + " could not be loaded");
}
}
}
}
} catch (URISyntaxException e) {
System.err.println("this is not supposed to happen");
e.printStackTrace();
}
}
} catch (IOException e) {
System.err.println("could not get " + realPath + " as resource");
}
return classes;
}
public Set<Class<?>> getAllClasses(String scanPackage) {
System.out.println("Using system classloader");
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
return getAllClasses(scanPackage, classLoader);
}
}