|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConstructorUtils.java | 68.2% | 85% | 83.3% | 79.4% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.util;
|
|
16 |
|
|
17 |
import java.lang.reflect.Constructor;
|
|
18 |
import java.lang.reflect.InvocationTargetException;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.Map;
|
|
21 |
|
|
22 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Static methods for invoking constructors.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
*/
|
|
29 |
public class ConstructorUtils |
|
30 |
{ |
|
31 |
|
|
32 |
/**
|
|
33 |
* Map from primitive type to wrapper type.
|
|
34 |
*/
|
|
35 |
private static final Map _primitiveMap = new HashMap(); |
|
36 |
|
|
37 |
static
|
|
38 |
{ |
|
39 | 1 |
_primitiveMap.put(boolean.class, Boolean.class); |
40 | 1 |
_primitiveMap.put(byte.class, Byte.class); |
41 | 1 |
_primitiveMap.put(char.class, Character.class); |
42 | 1 |
_primitiveMap.put(short.class, Short.class); |
43 | 1 |
_primitiveMap.put(int.class, Integer.class); |
44 | 1 |
_primitiveMap.put(long.class, Long.class); |
45 | 1 |
_primitiveMap.put(float.class, Float.class); |
46 | 1 |
_primitiveMap.put(double.class, Double.class); |
47 |
} |
|
48 |
|
|
49 |
// Prevent instantiation
|
|
50 |
|
|
51 | 0 |
private ConstructorUtils()
|
52 |
{ |
|
53 |
} |
|
54 |
|
|
55 |
/**
|
|
56 |
* Searches for a constructor matching against the provided arguments.
|
|
57 |
*
|
|
58 |
* @param targetClass
|
|
59 |
* the class to be instantiated
|
|
60 |
* @param parameters
|
|
61 |
* the parameters to pass to the constructor (may be null or empty)
|
|
62 |
* @return the new instance
|
|
63 |
* @throws ApplicationRuntimeException
|
|
64 |
* on any failure
|
|
65 |
*/
|
|
66 | 32 |
public static Object invokeConstructor(Class targetClass, Object[] parameters) |
67 |
{ |
|
68 | 32 |
if (parameters == null) |
69 | 0 |
parameters = new Object[0];
|
70 |
|
|
71 | 32 |
Class[] parameterTypes = new Class[parameters.length];
|
72 |
|
|
73 | 32 |
for (int i = 0; i < parameters.length; i++) |
74 | 32 |
parameterTypes[i] = parameters[i] == null ? null : parameters[i].getClass(); |
75 |
|
|
76 | 32 |
return invokeMatchingConstructor(targetClass, parameterTypes, parameters);
|
77 |
} |
|
78 |
|
|
79 | 32 |
private static Object invokeMatchingConstructor(Class targetClass, Class[] parameterTypes, |
80 |
Object[] parameters) |
|
81 |
{ |
|
82 | 32 |
Constructor[] constructors = targetClass.getConstructors(); |
83 |
|
|
84 | 32 |
for (int i = 0; i < constructors.length; i++) |
85 |
{ |
|
86 | 32 |
Constructor c = constructors[i]; |
87 |
|
|
88 | 32 |
if (isMatch(c, parameterTypes))
|
89 | 32 |
return invoke(c, parameters);
|
90 |
} |
|
91 |
|
|
92 | 0 |
throw new ApplicationRuntimeException(UtilMessages.noMatchingConstructor(targetClass), null); |
93 |
} |
|
94 |
|
|
95 | 32 |
private static boolean isMatch(Constructor c, Class[] types) |
96 |
{ |
|
97 | 32 |
Class[] actualTypes = c.getParameterTypes(); |
98 |
|
|
99 | 32 |
if (actualTypes.length != types.length)
|
100 | 0 |
return false; |
101 |
|
|
102 | 32 |
for (int i = 0; i < types.length; i++) |
103 |
{ |
|
104 | 32 |
if (types[i] == null && !actualTypes[i].isPrimitive()) |
105 | 0 |
continue;
|
106 |
|
|
107 | 32 |
if (!isCompatible(actualTypes[i], types[i]))
|
108 | 0 |
return false; |
109 |
} |
|
110 |
|
|
111 | 32 |
return true; |
112 |
} |
|
113 |
|
|
114 | 50 |
public static boolean isCompatible(Class actualType, Class parameterType) |
115 |
{ |
|
116 | 50 |
if (actualType.isAssignableFrom(parameterType))
|
117 | 38 |
return true; |
118 |
|
|
119 |
// Reflection fudges the assignment of a wrapper class to a primitive
|
|
120 |
// type ... we check for that the hard way.
|
|
121 |
|
|
122 | 12 |
if (actualType.isPrimitive())
|
123 |
{ |
|
124 | 5 |
Class wrapperClass = (Class) _primitiveMap.get(actualType); |
125 |
|
|
126 | 5 |
return wrapperClass.isAssignableFrom(parameterType);
|
127 |
} |
|
128 |
|
|
129 | 7 |
return false; |
130 |
} |
|
131 |
|
|
132 | 530 |
public static Object invoke(Constructor c, Object[] parameters) |
133 |
{ |
|
134 | 530 |
try
|
135 |
{ |
|
136 | 530 |
return c.newInstance(parameters);
|
137 |
} |
|
138 |
catch (InvocationTargetException ex)
|
|
139 |
{ |
|
140 | 1 |
Throwable cause = ex.getTargetException(); |
141 |
|
|
142 | 1 |
throw new ApplicationRuntimeException(UtilMessages.invokeFailed(c, cause), null, cause); |
143 |
} |
|
144 |
catch (Exception ex)
|
|
145 |
{ |
|
146 | 0 |
throw new ApplicationRuntimeException(UtilMessages.invokeFailed(c, ex), null, ex); |
147 |
} |
|
148 |
} |
|
149 |
} |
|