File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\ClassLoaderReflectionToolkit.java.html

1 package jenkins;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.net.URL;
6 import java.util.Enumeration;
7
8 /**
9 * Reflection access to various {@link ClassLoader} methods.
10 *
11 * @author Kohsuke Kawaguchi
12 */
13 public class ClassLoaderReflectionToolkit {
14 /**
15 * ClassLoader.findClass(String) for a call that bypasses access modifier.
16 */
17 private final Method FIND_CLASS, FIND_LOADED_CLASS, FIND_RESOURCE, FIND_RESOURCES;
18
19 public ClassLoaderReflectionToolkit() {
20 try {
21 FIND_CLASS = ClassLoader.class.getDeclaredMethod("findClass",String.class);
22 FIND_CLASS.setAccessible(true);
23 FIND_LOADED_CLASS = ClassLoader.class.getDeclaredMethod("findLoadedClass",String.class);
24 FIND_LOADED_CLASS.setAccessible(true);
25 FIND_RESOURCE = ClassLoader.class.getDeclaredMethod("findResource",String.class);
26 FIND_RESOURCE.setAccessible(true);
27 FIND_RESOURCES = ClassLoader.class.getDeclaredMethod("findResources",String.class);
28 FIND_RESOURCES.setAccessible(true);
29 } catch (NoSuchMethodException e) {
30 throw new AssertionError(e);
31 }
32 }
33
34 public Class findLoadedClass(ClassLoader cl, String name) throws InvocationTargetException {
35 try {
36 return (Class)FIND_LOADED_CLASS.invoke(cl,name);
37 } catch (IllegalAccessException e) {
38 throw new Error(e);
39 }
40 }
41
42 public Class findClass(ClassLoader cl, String name) throws InvocationTargetException {
43 try {
44 return (Class)FIND_CLASS.invoke(cl,name);
45 } catch (IllegalAccessException e) {
46 throw new Error(e);
47 }
48 }
49
50 public URL findResource(ClassLoader cl, String name) throws InvocationTargetException {
51 try {
52 return (URL)FIND_RESOURCE.invoke(cl,name);
53 } catch (IllegalAccessException e) {
54 throw new Error(e);
55 }
56 }
57
58 public Enumeration<URL> findResources(ClassLoader cl, String name) throws InvocationTargetException {
59 try {
60 return (Enumeration<URL>)FIND_RESOURCES.invoke(cl,name);
61 } catch (IllegalAccessException e) {
62 throw new Error(e);
63 }
64 }
65
66 // private void check(InvocationTargetException e) {
67 // Throwable t = e.getTargetException();
68 // if (t instanceof Error)
69 // throw (Error)t;
70 // if (t instanceof RuntimeException)
71 // throw (RuntimeException)t;
72 // }
73 }
74