Coverage Report - org.apache.myfaces.view.facelets.util.ReflectionUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionUtil
0%
0/41
0%
0/26
4
 
 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  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  
                 //we should route through our shared forName, due to plugins and due to better classloader resolution
 59  0
                 c  = ClassUtils.classForName(nc);
 60  
                 //old code left for double checking
 61  
                 //c = Class.forName(nc, false, ClassUtils.getContextClassLoader());
 62  0
                 c = Array.newInstance(c, 0).getClass();
 63  0
             }
 64  
             else
 65  
             {
 66  0
                 c  = ClassUtils.classForName(name);
 67  
                 //old code left for double checking
 68  
                 //c = Class.forName(name, false, ClassUtils.getContextClassLoader());
 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  
      * 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  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  
      * 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  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  
 }