/
ScanParams.java
159 lines (128 loc) · 4.21 KB
/
ScanParams.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package org.rapidoid.scan;
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.lambda.Operation;
import org.rapidoid.lambda.Predicate;
import org.rapidoid.log.Log;
import org.rapidoid.u.U;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
@Authors("Nikolche Mihajlovski")
@Since("2.5.0")
public class ScanParams extends RapidoidThing {
private volatile String[] packages;
private volatile String matching;
private volatile Class<? extends java.lang.annotation.Annotation>[] annotated;
private volatile ClassLoader classLoader;
private volatile String[] classpath;
private volatile Predicate<InputStream> bytecodeFilter;
public ScanParams in(String... packages) {
this.packages = packages;
return this;
}
public ScanParams in(Iterable<String> packages) {
return in(U.arrayOf(String.class, packages));
}
public String[] in() {
return this.packages;
}
public ScanParams matching(String matching) {
this.matching = matching;
return this;
}
public String matching() {
return this.matching;
}
public ScanParams annotated(Class<? extends Annotation>... annotated) {
this.annotated = annotated;
return this;
}
@SuppressWarnings("unchecked")
public ScanParams annotated(Collection<Class<? extends Annotation>> annotated) {
return annotated(U.arrayOf(Class.class, annotated));
}
public Class<? extends Annotation>[] annotated() {
return this.annotated;
}
public ScanParams classLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
return this;
}
public ClassLoader classLoader() {
return this.classLoader;
}
public String[] classpath() {
return classpath;
}
public ScanParams classpath(String... classpath) {
this.classpath = classpath;
return this;
}
public Predicate<InputStream> bytecodeFilter() {
return bytecodeFilter;
}
public ScanParams bytecodeFilter(Predicate<InputStream> bytecodeFilter) {
this.bytecodeFilter = bytecodeFilter;
return this;
}
public List<String> getAll() {
return ClasspathUtil.getClasses(this);
}
public List<Class<?>> loadAll() {
return ClasspathUtil.loadClasses(this);
}
public void forEach(Operation<Class<?>> classOperation) {
for (Class<?> cls : loadAll()) {
try {
classOperation.execute(cls);
} catch (Exception e) {
Log.error("Cannot process annotated class!", e);
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ScanParams that = (ScanParams) o;
if (!Arrays.equals(packages, that.packages)) return false;
if (matching != null ? !matching.equals(that.matching) : that.matching != null) return false;
if (!Arrays.equals(annotated, that.annotated)) return false;
if (classLoader != null ? !classLoader.equals(that.classLoader) : that.classLoader != null) return false;
if (!Arrays.equals(classpath, that.classpath)) return false;
return bytecodeFilter != null ? bytecodeFilter.equals(that.bytecodeFilter) : that.bytecodeFilter == null;
}
@Override
public int hashCode() {
int result = Arrays.hashCode(packages);
result = 31 * result + (matching != null ? matching.hashCode() : 0);
result = 31 * result + Arrays.hashCode(annotated);
result = 31 * result + (classLoader != null ? classLoader.hashCode() : 0);
result = 31 * result + Arrays.hashCode(classpath);
result = 31 * result + (bytecodeFilter != null ? bytecodeFilter.hashCode() : 0);
return result;
}
}