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 protected static final String[] EMPTY_STRING = new String[0];
29
30 protected static final String[] PRIMITIVE_NAMES = new String[] { "boolean", "byte", "char", "double", "float",
31 "int", "long", "short", "void" };
32
33 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 super();
43 }
44
45 public static Class<?> forName(String name) throws ClassNotFoundException
46 {
47 if (null == name || "".equals(name))
48 {
49 return null;
50 }
51
52 Class<?> c = forNamePrimitive(name);
53 if (c == null)
54 {
55 if (name.endsWith("[]"))
56 {
57 String nc = name.substring(0, name.length() - 2);
58
59 c = ClassUtils.classForName(nc);
60
61
62 c = Array.newInstance(c, 0).getClass();
63 }
64 else
65 {
66 c = ClassUtils.classForName(name);
67
68
69 }
70 }
71
72 return c;
73 }
74
75 protected static Class<?> forNamePrimitive(String name)
76 {
77 if (name.length() <= 8)
78 {
79 int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
80 if (p >= 0)
81 {
82 return PRIMITIVES[p];
83 }
84 }
85
86 return null;
87 }
88
89
90
91
92
93
94
95
96 public static Class<?>[] toTypeArray(String[] s) throws ClassNotFoundException
97 {
98 if (s == null)
99 {
100 return null;
101 }
102
103 Class<?>[] c = new Class[s.length];
104 for (int i = 0; i < s.length; i++)
105 {
106 c[i] = forName(s[i]);
107 }
108
109 return c;
110 }
111
112
113
114
115
116
117
118 public static String[] toTypeNameArray(Class<?>[] c)
119 {
120 if (c == null)
121 {
122 return null;
123 }
124
125 String[] s = new String[c.length];
126 for (int i = 0; i < c.length; i++)
127 {
128 s[i] = c[i].getName();
129 }
130
131 return s;
132 }
133
134 protected static final String paramString(Class<?>... types)
135 {
136 if (types != null)
137 {
138 StringBuilder sb = new StringBuilder();
139 for (Class<?> type : types)
140 {
141 sb.append(type.getName()).append(", ");
142 }
143
144 if (sb.length() > 2)
145 {
146 sb.setLength(sb.length() - 2);
147 }
148
149 return sb.toString();
150 }
151
152 return null;
153 }
154 }