|
32 | 32 |
|
33 | 33 | package org.scijava.util; |
34 | 34 |
|
35 | | -import java.io.File; |
36 | 35 | import java.lang.annotation.Annotation; |
37 | 36 | import java.lang.reflect.AnnotatedElement; |
38 | 37 | import java.lang.reflect.Field; |
39 | 38 | import java.lang.reflect.Method; |
40 | 39 | import java.lang.reflect.Type; |
41 | | -import java.net.MalformedURLException; |
42 | 40 | import java.net.URL; |
43 | 41 | import java.util.ArrayList; |
44 | 42 | import java.util.Collections; |
@@ -89,111 +87,6 @@ private ClassUtils() { |
89 | 87 |
|
90 | 88 | // -- Class loading, querying and reflection -- |
91 | 89 |
|
92 | | - /** |
93 | | - * Gets the base location of the given class. |
94 | | - * <p> |
95 | | - * If the class is directly on the file system (e.g., |
96 | | - * "/path/to/my/package/MyClass.class") then it will return the base directory |
97 | | - * (e.g., "/path/to"). |
98 | | - * </p> |
99 | | - * <p> |
100 | | - * If the class is within a JAR file (e.g., |
101 | | - * "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the |
102 | | - * path to the JAR (e.g., "/path/to/my-jar.jar"). |
103 | | - * </p> |
104 | | - * |
105 | | - * @param className The name of the class whose location is desired. |
106 | | - * @see FileUtils#urlToFile(URL) to convert the result to a {@link File}. |
107 | | - */ |
108 | | - public static URL getLocation(final String className) { |
109 | | - return getLocation(className, null); |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * Gets the base location of the given class. |
114 | | - * <p> |
115 | | - * If the class is directly on the file system (e.g., |
116 | | - * "/path/to/my/package/MyClass.class") then it will return the base directory |
117 | | - * (e.g., "/path/to"). |
118 | | - * </p> |
119 | | - * <p> |
120 | | - * If the class is within a JAR file (e.g., |
121 | | - * "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the |
122 | | - * path to the JAR (e.g., "/path/to/my-jar.jar"). |
123 | | - * </p> |
124 | | - * |
125 | | - * @param className The name of the class whose location is desired. |
126 | | - * @param classLoader The class loader to use when loading the class. |
127 | | - * @see FileUtils#urlToFile(URL) to convert the result to a {@link File}. |
128 | | - */ |
129 | | - public static URL getLocation(final String className, |
130 | | - final ClassLoader classLoader) |
131 | | - { |
132 | | - final Class<?> c = Types.load(className, classLoader); |
133 | | - return getLocation(c); |
134 | | - } |
135 | | - |
136 | | - /** |
137 | | - * Gets the base location of the given class. |
138 | | - * <p> |
139 | | - * If the class is directly on the file system (e.g., |
140 | | - * "/path/to/my/package/MyClass.class") then it will return the base directory |
141 | | - * (e.g., "file:/path/to"). |
142 | | - * </p> |
143 | | - * <p> |
144 | | - * If the class is within a JAR file (e.g., |
145 | | - * "/path/to/my-jar.jar!/my/package/MyClass.class") then it will return the |
146 | | - * path to the JAR (e.g., "file:/path/to/my-jar.jar"). |
147 | | - * </p> |
148 | | - * |
149 | | - * @param c The class whose location is desired. |
150 | | - * @see FileUtils#urlToFile(URL) to convert the result to a {@link File}. |
151 | | - */ |
152 | | - public static URL getLocation(final Class<?> c) { |
153 | | - if (c == null) return null; // could not load the class |
154 | | - |
155 | | - // try the easy way first |
156 | | - try { |
157 | | - final URL codeSourceLocation = |
158 | | - c.getProtectionDomain().getCodeSource().getLocation(); |
159 | | - if (codeSourceLocation != null) return codeSourceLocation; |
160 | | - } |
161 | | - catch (final SecurityException e) { |
162 | | - // NB: Cannot access protection domain. |
163 | | - } |
164 | | - catch (final NullPointerException e) { |
165 | | - // NB: Protection domain or code source is null. |
166 | | - } |
167 | | - |
168 | | - // NB: The easy way failed, so we try the hard way. We ask for the class |
169 | | - // itself as a resource, then strip the class's path from the URL string, |
170 | | - // leaving the base path. |
171 | | - |
172 | | - // get the class's raw resource path |
173 | | - final URL classResource = c.getResource(c.getSimpleName() + ".class"); |
174 | | - if (classResource == null) return null; // cannot find class resource |
175 | | - |
176 | | - final String url = classResource.toString(); |
177 | | - final String suffix = c.getCanonicalName().replace('.', '/') + ".class"; |
178 | | - if (!url.endsWith(suffix)) return null; // weird URL |
179 | | - |
180 | | - // strip the class's path from the URL string |
181 | | - final String base = url.substring(0, url.length() - suffix.length()); |
182 | | - |
183 | | - String path = base; |
184 | | - |
185 | | - // remove the "jar:" prefix and "!/" suffix, if present |
186 | | - if (path.startsWith("jar:")) path = path.substring(4, path.length() - 2); |
187 | | - |
188 | | - try { |
189 | | - return new URL(path); |
190 | | - } |
191 | | - catch (final MalformedURLException e) { |
192 | | - e.printStackTrace(); |
193 | | - return null; |
194 | | - } |
195 | | - } |
196 | | - |
197 | 90 | /** |
198 | 91 | * Gets the given class's {@link Method}s marked with the annotation of the |
199 | 92 | * specified class. |
@@ -587,6 +480,26 @@ public static boolean hasClass(final String className, |
587 | 480 | return Types.load(className, classLoader) != null; |
588 | 481 | } |
589 | 482 |
|
| 483 | + /** @deprecated Use {@link Types#location} and {@link Types#load} instead. */ |
| 484 | + @Deprecated |
| 485 | + public static URL getLocation(final String className) { |
| 486 | + return Types.location(Types.load(className)); |
| 487 | + } |
| 488 | + |
| 489 | + /** @deprecated Use {@link Types#location} and {@link Types#load} instead. */ |
| 490 | + @Deprecated |
| 491 | + public static URL getLocation(final String className, |
| 492 | + final ClassLoader classLoader) |
| 493 | + { |
| 494 | + return Types.location(Types.load(className, classLoader)); |
| 495 | + } |
| 496 | + |
| 497 | + /** @deprecated Use {@link Types#location} and {@link Types#load} instead. */ |
| 498 | + @Deprecated |
| 499 | + public static URL getLocation(final Class<?> c) { |
| 500 | + return Types.location(c); |
| 501 | + } |
| 502 | + |
590 | 503 | /** @deprecated Use {@link Types#isBoolean} instead. */ |
591 | 504 | @Deprecated |
592 | 505 | public static boolean isBoolean(final Class<?> type) { |
|
0 commit comments