-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassFinder.java
More file actions
189 lines (171 loc) · 6.91 KB
/
ClassFinder.java
File metadata and controls
189 lines (171 loc) · 6.91 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* The MIT License
*
* Copyright (c) 2010 The Broad Institute
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package picard.cmdline;
import htsjdk.samtools.util.Log;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* Utility class that can scan for classes in the classpath and find all the ones
* annotated with a particular annotation.
*
* @author Tim Fennell
*/
public class ClassFinder {
private final Set<Class<?>> classes = new HashSet<Class<?>>();
private final ClassLoader loader;
private Class<?> parentType;
// If not null, only look for classes in this jar
private String jarPath = null;
private static final Log log = Log.getInstance(ClassFinder.class);
public ClassFinder() {
loader = Thread.currentThread().getContextClassLoader();
}
public ClassFinder(final ClassLoader loader) {
this.loader = loader;
}
public ClassFinder(final File jarFile) throws IOException {
// The class loader must have the context in order to load dependent classes when loading a class,
// but the jarPath is remembered so that the iteration over the classpath skips anything other than
// the jarPath.
jarPath = jarFile.getCanonicalPath();
final URL[] urls = {new File(jarPath).toURI().toURL()};
loader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
}
/** Convert a filename to a class name by removing '.class' and converting '/'s to '.'s. */
public String toClassName(final String filename) {
return filename.substring(0, filename.lastIndexOf(".class"))
.replace('/', '.').replace('\\', '.');
}
/**
* Scans the classpath for classes within the specified package and sub-packages that
* extend the parentType. This method can be called repeatedly
* with different packages. Classes are accumulated internally and
* can be accessed by calling {@link #getClasses()}.
*/
public void find(String packageName, final Class<?> parentType) {
this.parentType = parentType;
packageName = packageName.replace('.', '/');
final Enumeration<URL> urls;
try {
urls = loader.getResources(packageName);
}
catch (IOException ioe) {
log.warn("Could not read package: " + packageName, ioe);
return;
}
while (urls.hasMoreElements()) {
try {
String urlPath = urls.nextElement().getFile();
// convert URL to URI
// http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4466485
// using URLDecode does not work if urlPath has a '+' character
try {
URI uri = new URI(urlPath);
urlPath = uri.getPath();
} catch (URISyntaxException e) {
log.warn("Cannot convert to URI the " + urlPath + " URL");
}
if (urlPath.indexOf('!') > 0) {
urlPath = urlPath.substring(0, urlPath.indexOf('!'));
}
if (jarPath != null && !jarPath.equals(urlPath)) {
continue;
}
//Log.info("Looking for classes in location: " + urlPath);
final File file = new File(urlPath);
if ( file.isDirectory() ) {
scanDir(file, packageName);
}
else {
scanJar(file, packageName);
}
}
catch (IOException ioe) {
log.warn("could not read entries", ioe);
}
}
}
/**
* Scans the entries in a ZIP/JAR file for classes under the parent package.
* @param file the jar file to be scanned
* @param packagePath the top level package to start from
*/
protected void scanJar(final File file, final String packagePath) throws IOException {
final ZipFile zip = new ZipFile(file);
final Enumeration<? extends ZipEntry> entries = zip.entries();
while ( entries.hasMoreElements() ) {
final ZipEntry entry = entries.nextElement();
final String name = entry.getName();
if (name.startsWith(packagePath)) {
handleItem(name);
}
}
}
/**
* Scans a directory on the filesystem for classes.
* @param file the directory or file to examine
* @param path the package path acculmulated so far (e.g. edu/mit/broad)
*/
protected void scanDir(final File file, final String path) {
for ( final File child: file.listFiles() ) {
final String newPath = (path==null ? child.getName() : path + '/' + child.getName() );
if ( child.isDirectory() ) {
scanDir(child, newPath);
}
else {
handleItem(newPath);
}
}
}
/**
* Checks an item to see if it is a class and is annotated with the specified
* annotation. If so, adds it to the set, otherwise ignores it.
* @param name the path equivelant to the package + class/item name
*/
protected void handleItem(final String name) {
if (name.endsWith(".class")) {
final String classname = toClassName(name);
try {
final Class<?> type = loader.loadClass(classname);
if (parentType.isAssignableFrom(type)) {
this.classes.add(type);
}
}
catch (Throwable t) {
log.debug("could not load class: " + classname, t);
}
}
}
/** Fetches the set of classes discovered so far. */
public Set<Class<?>> getClasses() {
return this.classes;
}
}