1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.util; |
20 | |
|
21 | |
import java.lang.reflect.Array; |
22 | |
import java.util.Arrays; |
23 | |
|
24 | |
import org.apache.myfaces.shared.util.ClassUtils; |
25 | |
|
26 | |
public class ReflectionUtil |
27 | |
{ |
28 | 0 | protected static final String[] EMPTY_STRING = new String[0]; |
29 | |
|
30 | 0 | protected static final String[] PRIMITIVE_NAMES = new String[] { "boolean", "byte", "char", "double", "float", |
31 | |
"int", "long", "short", "void" }; |
32 | |
|
33 | 0 | protected static final Class<?>[] PRIMITIVES = new Class[] { Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE, |
34 | |
Float.TYPE, Integer.TYPE, Long.TYPE, Short.TYPE, |
35 | |
Void.TYPE }; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
private ReflectionUtil() |
41 | |
{ |
42 | 0 | super(); |
43 | 0 | } |
44 | |
|
45 | |
public static Class<?> forName(String name) throws ClassNotFoundException |
46 | |
{ |
47 | 0 | if (null == name || "".equals(name)) |
48 | |
{ |
49 | 0 | return null; |
50 | |
} |
51 | |
|
52 | 0 | Class<?> c = forNamePrimitive(name); |
53 | 0 | if (c == null) |
54 | |
{ |
55 | 0 | if (name.endsWith("[]")) |
56 | |
{ |
57 | 0 | String nc = name.substring(0, name.length() - 2); |
58 | |
|
59 | 0 | c = ClassUtils.classForName(nc); |
60 | |
|
61 | |
|
62 | 0 | c = Array.newInstance(c, 0).getClass(); |
63 | 0 | } |
64 | |
else |
65 | |
{ |
66 | 0 | c = ClassUtils.classForName(name); |
67 | |
|
68 | |
|
69 | |
} |
70 | |
} |
71 | |
|
72 | 0 | return c; |
73 | |
} |
74 | |
|
75 | |
protected static Class<?> forNamePrimitive(String name) |
76 | |
{ |
77 | 0 | if (name.length() <= 8) |
78 | |
{ |
79 | 0 | int p = Arrays.binarySearch(PRIMITIVE_NAMES, name); |
80 | 0 | if (p >= 0) |
81 | |
{ |
82 | 0 | return PRIMITIVES[p]; |
83 | |
} |
84 | |
} |
85 | |
|
86 | 0 | return null; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public static Class<?>[] toTypeArray(String[] s) throws ClassNotFoundException |
97 | |
{ |
98 | 0 | if (s == null) |
99 | |
{ |
100 | 0 | return null; |
101 | |
} |
102 | |
|
103 | 0 | Class<?>[] c = new Class[s.length]; |
104 | 0 | for (int i = 0; i < s.length; i++) |
105 | |
{ |
106 | 0 | c[i] = forName(s[i]); |
107 | |
} |
108 | |
|
109 | 0 | return c; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public static String[] toTypeNameArray(Class<?>[] c) |
119 | |
{ |
120 | 0 | if (c == null) |
121 | |
{ |
122 | 0 | return null; |
123 | |
} |
124 | |
|
125 | 0 | String[] s = new String[c.length]; |
126 | 0 | for (int i = 0; i < c.length; i++) |
127 | |
{ |
128 | 0 | s[i] = c[i].getName(); |
129 | |
} |
130 | |
|
131 | 0 | return s; |
132 | |
} |
133 | |
|
134 | |
protected static final String paramString(Class<?>... types) |
135 | |
{ |
136 | 0 | if (types != null) |
137 | |
{ |
138 | 0 | StringBuilder sb = new StringBuilder(); |
139 | 0 | for (Class<?> type : types) |
140 | |
{ |
141 | 0 | sb.append(type.getName()).append(", "); |
142 | |
} |
143 | |
|
144 | 0 | if (sb.length() > 2) |
145 | |
{ |
146 | 0 | sb.setLength(sb.length() - 2); |
147 | |
} |
148 | |
|
149 | 0 | return sb.toString(); |
150 | |
} |
151 | |
|
152 | 0 | return null; |
153 | |
} |
154 | |
} |