1 package net.sf.openrocket.util;
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.InvocationTargetException;
6 import net.sf.openrocket.rocketcomponent.RocketComponent;
9 public class Reflection {
11 private static final String ROCKETCOMPONENT_PACKAGE = "net.sf.openrocket.rocketcomponent";
14 * Simple wrapper class that converts the Method.invoke() exceptions into suitable
17 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19 public static class Method {
20 private final java.lang.reflect.Method method;
21 public Method(java.lang.reflect.Method m) {
25 * Same as Method.invoke(), but the possible exceptions are wrapped into
28 public Object invoke(Object obj, Object... args) {
30 return method.invoke(obj, args);
31 } catch (IllegalArgumentException e) {
32 throw new RuntimeException("Error while invoking method '"+method+"'. "+
33 "Please report this as a bug.",e);
34 } catch (IllegalAccessException e) {
35 throw new RuntimeException("Error while invoking method '"+method+"'. "+
36 "Please report this as a bug.",e);
37 } catch (InvocationTargetException e) {
38 throw new RuntimeException("Error while invoking method '"+method+"'. "+
39 "Please report this as a bug.",e);
43 * Invoke static method. Equivalent to invoke(null, args...).
45 public Object invokeStatic(Object... args) {
46 return invoke(null,args);
49 * Same as Method.toString().
52 public String toString() {
53 return method.toString();
59 * Throws an exception if method not found.
61 public static Reflection.Method findMethodStatic(
62 Class<? extends RocketComponent> componentClass,
63 String method, Class<?>... params) {
64 Reflection.Method m = findMethod(ROCKETCOMPONENT_PACKAGE, componentClass,
67 throw new RuntimeException("Could not find method for componentClass="
68 +componentClass+" method="+method);
75 public static Reflection.Method findMethod(String pack, RocketComponent component,
76 String method, Class<?>...params) {
77 return findMethod(pack,component.getClass(),"",method,params);
81 public static Reflection.Method findMethod(String pack, RocketComponent component,
82 String suffix, String method, Class<?>... params) {
83 return findMethod(pack, component.getClass(), suffix, method, params);
87 public static Reflection.Method findMethod(String pack,
88 Class<? extends RocketComponent> componentClass,
89 String suffix, String method, Class<?>... params) {
90 Class<?> currentclass;
93 currentclass = componentClass;
94 while ((currentclass != null) && (currentclass != Object.class)) {
95 name = currentclass.getCanonicalName();
96 if (name.lastIndexOf('.')>=0)
97 name = name.substring(name.lastIndexOf(".")+1);
98 name = pack + "." + name + suffix;
101 Class<?> c = Class.forName(name);
102 java.lang.reflect.Method m = c.getMethod(method,params);
103 return new Reflection.Method(m);
104 } catch (ClassNotFoundException ignore) {
105 } catch (NoSuchMethodException ignore) {
108 currentclass = currentclass.getSuperclass();
114 public static Object construct(String pack, RocketComponent component, String suffix,
117 Class<?> currentclass;
120 currentclass = component.getClass();
121 while ((currentclass != null) && (currentclass != Object.class)) {
122 name = currentclass.getCanonicalName();
123 if (name.lastIndexOf('.')>=0)
124 name = name.substring(name.lastIndexOf(".")+1);
125 name = pack + "." + name + suffix;
128 Class<?> c = Class.forName(name);
129 Class<?>[] paramClasses = new Class<?>[params.length];
130 for (int i=0; i < params.length; i++) {
131 paramClasses[i] = params[i].getClass();
134 // Constructors must be searched manually. Why?!
135 main: for (Constructor<?> constructor: c.getConstructors()) {
136 Class<?>[] parameterTypes = constructor.getParameterTypes();
137 if (params.length != parameterTypes.length)
139 for (int i=0; i < params.length; i++) {
140 if (!parameterTypes[i].isInstance(params[i]))
143 // Matching constructor found
144 return constructor.newInstance(params);
146 } catch (ClassNotFoundException ignore) {
147 } catch (IllegalArgumentException e) {
148 throw new RuntimeException("Construction of "+name+" failed",e);
149 } catch (InstantiationException e) {
150 throw new RuntimeException("Construction of "+name+" failed",e);
151 } catch (IllegalAccessException e) {
152 throw new RuntimeException("Construction of "+name+" failed",e);
153 } catch (InvocationTargetException e) {
154 throw new RuntimeException("Construction of "+name+" failed",e);
157 currentclass = currentclass.getSuperclass();
159 throw new RuntimeException("Suitable constructor for component "+component+