|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MethodCall.java | 100% | 90,5% | 100% | 94,2% |
|
1 |
/**
|
|
2 |
* Java Gui Builder - A library to build GUIs using an XML file.
|
|
3 |
* Copyright 2002, 2003 (C) François Beausoleil
|
|
4 |
*
|
|
5 |
* This library is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
|
7 |
* License as published by the Free Software Foundation; either
|
|
8 |
* version 2.1 of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This library 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 GNU
|
|
13 |
* Lesser General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
|
16 |
* License along with this library; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 |
*/
|
|
19 |
|
|
20 |
package jgb.builder.utils;
|
|
21 |
|
|
22 |
import java.lang.reflect.Method;
|
|
23 |
import java.util.Arrays;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* An object that accumulates parameters until such time the caller is ready
|
|
27 |
* to call the specified method.
|
|
28 |
* When the caller is ready to call the method, it calls {@link #call() call()}
|
|
29 |
* to get a reference to the return value of the method. If the named method
|
|
30 |
* returns <code>void</code>, {@link #call() call()} returns
|
|
31 |
* <code>null</code> (just like
|
|
32 |
* {@link java.lang.reflect.Method#invoke(Object, Object[]) Method.invoke(Object, Object[])}).
|
|
33 |
* @since 0.2a
|
|
34 |
* @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
|
|
35 |
*/
|
|
36 |
public class MethodCall extends ParametersAccumulator { |
|
37 |
private Class calledClass;
|
|
38 |
private String methodName;
|
|
39 |
private Object callingObject;
|
|
40 |
|
|
41 | 33 |
protected MethodCall(String className) throws ClassNotFoundException { |
42 | 33 |
calledClass = Class.forName(className); |
43 |
} |
|
44 |
|
|
45 | 33 |
public MethodCall(String className, String methodName, Object callingObject) throws ClassNotFoundException, NoSuchMethodException { |
46 | 33 |
this(className);
|
47 | 33 |
this.methodName = methodName;
|
48 | 33 |
this.callingObject = callingObject;
|
49 |
|
|
50 | 33 |
checkMethodExists(methodName); |
51 |
} |
|
52 |
|
|
53 | 9 |
public Class getReturnValueType() {
|
54 | 9 |
try {
|
55 | 9 |
Method method = findMethod(); |
56 | 9 |
return method.getReturnType();
|
57 |
} catch (NoSuchMethodException e) {
|
|
58 | 0 |
RuntimeException e1 = |
59 |
new RuntimeException("Could not find method that existed earlier: " |
|
60 |
+ e.getMessage()); |
|
61 | 0 |
e1.printStackTrace(); |
62 | 0 |
throw e1;
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 | 3 |
public String getMethodName() {
|
67 | 3 |
return methodName;
|
68 |
} |
|
69 |
|
|
70 | 1 |
public Class getCalledClass() {
|
71 | 1 |
return calledClass;
|
72 |
} |
|
73 |
|
|
74 | 18 |
public Object call() throws NoSuchMethodException, MethodCallException { |
75 | 18 |
try {
|
76 | 18 |
return findMethod().invoke(callingObject, createValuesArray());
|
77 |
} catch (NoSuchMethodException e) {
|
|
78 | 1 |
throw new NoSuchMethodException("Could not execute method " + methodName |
79 |
+ " on object's of class " + calledClass + " with parameters " |
|
80 |
+ Arrays.asList(createParametersArray())); |
|
81 |
} catch (Exception e) {
|
|
82 | 0 |
throw new MethodCallException(e); |
83 |
} |
|
84 |
} |
|
85 |
|
|
86 | 27 |
private Method findMethod() throws NoSuchMethodException { |
87 | 27 |
Class[] callingTypes = createParametersArray(); |
88 | 27 |
Method[] allMethods = calledClass.getMethods(); |
89 | 27 |
for (int i = 0; i < allMethods.length; i++) { |
90 | 233 |
Method method = allMethods[i]; |
91 | 233 |
if (method.getName().equals(methodName)) {
|
92 | 43 |
Class[] paramTypes = method.getParameterTypes(); |
93 | 43 |
if (paramTypes.length == callingTypes.length) {
|
94 | 33 |
boolean compatible = true; |
95 | 33 |
for (int j = 0; j < paramTypes.length; j++) { |
96 | 24 |
if (isCompatible(paramTypes[j], callingTypes[j])) {
|
97 | 7 |
compatible = false;
|
98 | 7 |
break;
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 | 33 |
if (compatible) {
|
103 | 26 |
return method;
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 | 1 |
throw new NoSuchMethodException("Class " + getCalledClass().getName() |
110 |
+ " does not have a method with arguments of type " + getParameterTypes());
|
|
111 |
} |
|
112 |
|
|
113 | 24 |
private boolean isCompatible(Class type, Class callingType) { |
114 | 24 |
return false == type.isAssignableFrom(callingType); |
115 |
} |
|
116 |
|
|
117 | 33 |
private void checkMethodExists(String methodName) throws NoSuchMethodException { |
118 | 33 |
boolean methodFound = false; |
119 | 33 |
Method[] methods = calledClass.getMethods(); |
120 | 33 |
for (int i = 0; i < methods.length; i++) { |
121 | 241 |
Method method = methods[i]; |
122 | 241 |
if (method.getName().equals(methodName)) {
|
123 | 30 |
methodFound = true;
|
124 | 30 |
break;
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 | 33 |
if (methodFound == false) { |
129 | 3 |
throw new NoSuchMethodException("no method with the specified name - " |
130 |
+ methodName + " on objects of class " + calledClass.getName());
|
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
|
|