1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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.ClassLoaderUtils;
25 import org.apache.myfaces.shared.util.ClassUtils;
26
27 public class ReflectionUtil
28 {
29 protected static final String[] EMPTY_STRING = new String[0];
30
31 protected static final String[] PRIMITIVE_NAMES = new String[] { "boolean", "byte", "char", "double", "float",
32 "int", "long", "short", "void" };
33
34 protected static final Class<?>[] PRIMITIVES = new Class[] { Boolean.TYPE, Byte.TYPE, Character.TYPE, Double.TYPE,
35 Float.TYPE, Integer.TYPE, Long.TYPE, Short.TYPE,
36 Void.TYPE };
37
38 /**
39 *
40 */
41 private ReflectionUtil()
42 {
43 super();
44 }
45
46 public static Class<?> forName(String name) throws ClassNotFoundException
47 {
48 if (null == name || "".equals(name))
49 {
50 return null;
51 }
52
53 Class<?> c = forNamePrimitive(name);
54 if (c == null)
55 {
56 if (name.endsWith("[]"))
57 {
58 String nc = name.substring(0, name.length() - 2);
59 //we should route through our shared forName, due to plugins and due to better classloader resolution
60 c = ClassUtils.classForName(nc);
61 //old code left for double checking
62 //c = Class.forName(nc, false, ClassUtils.getContextClassLoader());
63 c = Array.newInstance(c, 0).getClass();
64 }
65 else
66 {
67 c = ClassUtils.classForName(name);
68 //old code left for double checking
69 //c = Class.forName(name, false, ClassUtils.getContextClassLoader());
70 }
71 }
72
73 return c;
74 }
75
76 protected static Class<?> forNamePrimitive(String name)
77 {
78 if (name.length() <= 8)
79 {
80 int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
81 if (p >= 0)
82 {
83 return PRIMITIVES[p];
84 }
85 }
86
87 return null;
88 }
89
90 /**
91 * Converts an array of Class names to Class types
92 *
93 * @param s
94 * @return
95 * @throws ClassNotFoundException
96 */
97 public static Class<?>[] toTypeArray(String[] s) throws ClassNotFoundException
98 {
99 if (s == null)
100 {
101 return null;
102 }
103
104 Class<?>[] c = new Class[s.length];
105 for (int i = 0; i < s.length; i++)
106 {
107 c[i] = forName(s[i]);
108 }
109
110 return c;
111 }
112
113 /**
114 * Converts an array of Class types to Class names
115 *
116 * @param c
117 * @return
118 */
119 public static String[] toTypeNameArray(Class<?>[] c)
120 {
121 if (c == null)
122 {
123 return null;
124 }
125
126 String[] s = new String[c.length];
127 for (int i = 0; i < c.length; i++)
128 {
129 s[i] = c[i].getName();
130 }
131
132 return s;
133 }
134
135 protected static final String paramString(Class<?>... types)
136 {
137 if (types != null)
138 {
139 StringBuilder sb = new StringBuilder();
140 for (Class<?> type : types)
141 {
142 sb.append(type.getName()).append(", ");
143 }
144
145 if (sb.length() > 2)
146 {
147 sb.setLength(sb.length() - 2);
148 }
149
150 return sb.toString();
151 }
152
153 return null;
154 }
155 }