Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart9.png 45% of files have more coverage
67   253   31   8.38
14   130   0.46   8
8     3.88  
1    
 
  YodaUtility       Line # 29 67 31 84.3% 0.8426966
 
  (94)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.utility;
17   
18    import java.lang.reflect.Field;
19    import java.lang.reflect.InvocationTargetException;
20    import java.lang.reflect.Method;
21   
22   
23    /**
24    * We learned so much from the great Jedi master. Using the force we can get and set private
25    * fields of arbitrary objects. We can even execute private methods...
26    *
27    * @author Michael Meyling
28    */
 
29    public abstract class YodaUtility {
30   
31    /**
32    * Constructor, should never be called.
33    */
 
34  0 toggle private YodaUtility() {
35    // don't call me
36    }
37   
38    /**
39    * Analyze if a class or one of its super classes contains a given method.
40    * <p>
41    * Example: you can test with <code>YodaUtility.existsMethod("java.net.URLConnection",
42    * "setConnectTimeout", new Class[] {Integer.TYPE}</code> with JDK 1.4.2 and if you run it
43    * with a 1.5 JRE or higher then it will be successfully executed.
44    *
45    * @param clazz Class to analyze.
46    * @param name Method name.
47    * @param parameterTypes Parameter types.
48    * @return Does the class (or one of its super classes) have such a method?
49    */
 
50  5 toggle public static boolean existsMethod(final String clazz, final String name,
51    final Class[] parameterTypes) {
52  5 Class c;
53  5 try {
54  5 c = Class.forName(clazz);
55    } catch (ClassNotFoundException e) {
56  1 return false;
57    }
58  4 return existsMethod(c, name, parameterTypes);
59    }
60   
61    /**
62    * Analyze if a class or one of its super classes contains a given method.
63    * <p>
64    * Example: you can test with <code>YodaUtility.existsMethod(URLConnection.class,
65    * "setConnectTimeout", new Class[] {Integer.TYPE}</code> with JDK 1.4.2 and if you run it
66    * with a 1.5 JRE or higher then it will be successfully executed.
67    *
68    * @param clazz Class to analyze.
69    * @param name Method name.
70    * @param parameterTypes Parameter types.
71    * @return Does the class (or one of its super classes) have such a method?
72    */
 
73  22 toggle public static boolean existsMethod(final Class clazz, final String name,
74    final Class[] parameterTypes) {
75  22 Method method = null;
76  22 Class cl = clazz;
77  22 try {
78  40 while (!Object.class.equals(cl)) {
79  36 try {
80  36 method = cl.getDeclaredMethod(name, parameterTypes);
81  18 break;
82    } catch (NoSuchMethodException ex) {
83  18 cl = cl.getSuperclass();
84    }
85    }
86  22 if (method == null) {
87  4 return false;
88    }
89  18 return true;
90    } catch (SecurityException e) {
91  0 throw new RuntimeException(e);
92    }
93    }
94   
95    /**
96    * This method executes a method on an object or one of its super instances (even if it is
97    * private).
98    * <p>
99    * Example: you can compile <code>YodaUtility.executeMethod((URLConnection) httpConnection,
100    * "setConnectTimeout", new Class[] {Integer.TYPE}, new Object[] { new Integer(100)});</code>
101    * with JDK 1.4.2 and if you run it with a 1.5 JRE or higher then it will be successfully
102    * executed.
103    *
104    * @param obj Object.
105    * @param name Method name.
106    * @param parameterTypes Parameter types.
107    * @param parameter Parameter values.
108    * @return Execution result.
109    * @throws NoSuchMethodException Method not found.
110    * @throws InvocationTargetException Wrapped exception.
111    */
 
112  31 toggle public static Object executeMethod(final Object obj, final String name,
113    final Class[] parameterTypes, final Object[] parameter) throws NoSuchMethodException,
114    InvocationTargetException {
115  31 Method method = null;
116  31 try {
117  31 Class cl = obj.getClass();
118  44 while (!Object.class.equals(cl)) {
119  41 try {
120  41 method = cl.getDeclaredMethod(name, parameterTypes);
121  28 break;
122    } catch (NoSuchMethodException ex) {
123  13 cl = cl.getSuperclass();
124    }
125    }
126  31 if (method == null) {
127  3 throw new NoSuchMethodException(name);
128    }
129  28 method.setAccessible(true);
130    } catch (SecurityException e) {
131  0 throw new RuntimeException(e);
132    }
133  28 try {
134  28 return method.invoke(obj, parameter);
135    } catch (IllegalArgumentException e) {
136  0 throw new RuntimeException(e);
137    } catch (IllegalAccessException e) {
138  0 throw new RuntimeException(e);
139    }
140    }
141   
142    /**
143    * This method executes a static class method (even if it is private).
144    * <p>
145    * Example: you can compile <code>YodaUtility.executeMethod(
146    * "java.util.concurrent.locks.LockSupport",
147    * "park", new Class[0], new Object[0]);</code>
148    * with JDK 1.4.2 and if you run it with a 1.5 JRE or higher then it will be successfully
149    * executed.
150    *
151    * @param clazzName Name of class.
152    * @param name Name of static method to execute.
153    * @param parameterTypes Parameter types.
154    * @param parameter Parameter values.
155    * @return Execution result.
156    * @throws NoSuchMethodException Method not found.
157    * @throws InvocationTargetException Wrapped exception.
158    */
 
159  5 toggle public static Object executeMethod(final String clazzName, final String name,
160    final Class[] parameterTypes, final Object[] parameter) throws NoSuchMethodException,
161    InvocationTargetException {
162  5 Method method = null;
163  5 try {
164  5 Class cl = Class.forName(clazzName);
165  5 method = cl.getDeclaredMethod(name, parameterTypes);
166  1 if (method == null) {
167  0 throw new NoSuchMethodException(name);
168    }
169  1 method.setAccessible(true);
170  1 return method.invoke(cl, parameter);
171    } catch (SecurityException e) {
172  0 throw new RuntimeException(e);
173    } catch (ClassNotFoundException e) {
174  0 throw new RuntimeException(e);
175    } catch (IllegalAccessException e) {
176  0 throw new RuntimeException(e);
177    }
178    }
179   
180    /**
181    * This method returns the contents of an object variable .The class hierarchy is recursively
182    * searched to find such a field (even if it is private).
183    *
184    * @param obj Object.
185    * @param name Variable name.
186    * @return Contents of variable.
187    * @throws NoSuchFieldException Variable of given name was not found.
188    */
 
189  60413 toggle public static Object getFieldValue(final Object obj, final String name) throws NoSuchFieldException {
190  60413 final Field field = getField(obj, name);
191  60412 try {
192  60412 return field.get(obj);
193    } catch (IllegalArgumentException e) {
194  0 throw new RuntimeException(e);
195    } catch (IllegalAccessException e) {
196  0 throw new RuntimeException(e);
197    }
198    }
199   
200    /**
201    * This method sets the contents of an object variable. The class hierarchy is recursively
202    * searched to find such a field (even if it is private).
203    *
204    * @param obj Object.
205    * @param name Variable name.
206    * @param value Value to set.
207    * @throws NoSuchFieldException Variable of given name was not found.
208    */
 
209  7 toggle public static void setFieldContent(final Object obj, final String name, final Object value)
210    throws NoSuchFieldException {
211  7 final Field field = getField(obj, name);
212  7 try {
213  7 field.set(obj, value);
214    } catch (IllegalArgumentException e) {
215  0 throw new RuntimeException(e);
216    } catch (IllegalAccessException e) {
217  1 throw new RuntimeException(e);
218    }
219    }
220   
221    /**
222    * Get field of given name in given object. The class hierarchy is recursively searched
223    * to find such a field (even if it is private).
224    *
225    * @param obj Object to work on.
226    * @param name Search this field.
227    * @return Found field.
228    * @throws NoSuchFieldException Field with name <code>name</code> was not found.
229    */
 
230  60423 toggle public static Field getField(final Object obj, final String name) throws NoSuchFieldException {
231  60423 Field field = null;
232  60423 try {
233  60423 Class cl = obj.getClass();
234  60433 while (!Object.class.equals(cl)) {
235  60431 try {
236  60431 field = cl.getDeclaredField(name);
237  60421 break;
238    } catch (NoSuchFieldException ex) {
239  10 cl = cl.getSuperclass();
240    }
241    }
242  60423 if (field == null) {
243  2 throw (new NoSuchFieldException(name + " within " + obj.getClass()));
244    }
245  60421 field.setAccessible(true);
246    } catch (SecurityException e) {
247  0 throw new RuntimeException(e);
248    }
249  60421 return field;
250    }
251   
252    }
253