View Javadoc

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.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                  //we should route through our shared forName, due to plugins and due to better classloader resolution
59                  c  = ClassUtils.classForName(nc);
60                  //old code left for double checking
61                  //c = Class.forName(nc, false, ClassUtils.getContextClassLoader());
62                  c = Array.newInstance(c, 0).getClass();
63              }
64              else
65              {
66                  c  = ClassUtils.classForName(name);
67                  //old code left for double checking
68                  //c = Class.forName(name, false, ClassUtils.getContextClassLoader());
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       * Converts an array of Class names to Class types
91       * 
92       * @param s
93       * @return
94       * @throws ClassNotFoundException
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      * Converts an array of Class types to Class names
114      * 
115      * @param c
116      * @return
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 }