|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConstructorCall.java | 100% | 95,5% | 100% | 97,1% |
|
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.Constructor;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @since 0.2a
|
|
26 |
* @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
|
|
27 |
*/
|
|
28 |
public class ConstructorCall extends ParametersAccumulator { |
|
29 |
private Class calledClass;
|
|
30 |
|
|
31 | 25 |
public ConstructorCall(String className) throws ClassNotFoundException { |
32 | 25 |
calledClass = Class.forName(className); |
33 |
} |
|
34 |
|
|
35 | 21 |
public Object call() throws NoSuchMethodException, MethodCallException { |
36 | 21 |
try {
|
37 | 21 |
Object[] values = createValuesArray(); |
38 | 21 |
return findConstructor().newInstance(values);
|
39 |
} catch (NoSuchMethodException e) {
|
|
40 | 2 |
throw e;
|
41 |
} catch (Exception e) {
|
|
42 | 0 |
throw new MethodCallException("Could not instantiate object with parameters " |
43 |
+ getParameterTypes().toString() + " - " + getParameterValues().toString(), e);
|
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 | 21 |
private Constructor findConstructor() throws NoSuchMethodException { |
48 | 21 |
Class[] callParams = createParametersArray(); |
49 |
|
|
50 | 21 |
Constructor[] ctors = calledClass.getConstructors(); |
51 | 21 |
for (int i = 0; i < ctors.length; i++) { |
52 | 76 |
Constructor ctor = ctors[i]; |
53 |
|
|
54 | 76 |
Class[] ctorParams = ctor.getParameterTypes(); |
55 | 76 |
if (ctorParams.length == callParams.length) {
|
56 | 36 |
boolean isValidConstructor = true; |
57 |
|
|
58 | 36 |
for (int j = 0; j < ctorParams.length; j++) { |
59 | 47 |
Class ctorParam = ctorParams[j]; |
60 | 47 |
Class callParam = callParams[j]; |
61 |
|
|
62 | 47 |
if (ctorParam.isAssignableFrom(callParam) == false) { |
63 | 17 |
isValidConstructor = false;
|
64 | 17 |
break;
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 | 36 |
if (isValidConstructor) {
|
69 | 19 |
return ctor;
|
70 |
} |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 | 2 |
throw new NoSuchMethodException("Unable to find constructor with specified parameters"); |
75 |
} |
|
76 |
} |
|
77 |
|
|