Coverage details for org.chwf.util.MiscUtils

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.util;
34  
35 import java.beans.BeanInfo;
36 import java.beans.Introspector;
37 import java.beans.PropertyDescriptor;
38 import java.util.HashMap;
39 import java.util.Map;
40  
41 /**
42  * Utility class for static methods with no better location. The methods
43  * in this class are <b>not</b> stable, and may move or change when a
44  * more appropriate place for the operation is found.
45  *
46  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
47  */
489public abstract class MiscUtils {
49  
50   /** Empty array for no-parameter methods. */
512  private static final Class[] NO_PARAMS = new Class[0];
52  
53   /** Empty array for no-parameter methods. */
541  private static final Object[] EMPTY_ARRAY = new Object[0];
55  
56   /** Mapping between primitive types and their wrappers. */
571  private static final Map WRAPPER_MAP = initWrapperMap();
58  
59   /** For locating methods with a string parameter via reflection. */
601  private static final Class[] STRING_PARAM = { String.class };
61  
62   /**
63    * Derive property type.
64    *
65    * @param beanClass The bean class.
66    * @param property The property.
67    * @return The datatype.
68    * @throws IllegalArgumentException If the property is invalid.
69    */
70   public static Class deriveType(Class beanClass, String property)
71     throws IllegalArgumentException {
72     try {
73104      BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
74104      PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
75405      for (int i = 0; i < descriptors.length; i++) {
76405        if (descriptors[i].getName().equals(property)) {
77104          return descriptors[i].getPropertyType();
78         }
79       }
800    } catch (Exception ex) {
810      throw new IllegalArgumentException(
82         "The property "
83           + property
84           + " is not valid for the class "
85           + beanClass.getName());
86     }
870    throw new IllegalArgumentException(
88       "The property "
89         + property
90         + " is not valid for the class "
91         + beanClass.getName());
92   }
93  
94   /**
95    * Converts first character to uppercase.
96    *
97    * @param string The string.
98    * @return The capitalized string.
99    */
100   public static String capitalize(String string) {
101457    return Character.toUpperCase(string.charAt(0)) + string.substring(1);
102   }
103  
104   /**
105    * Get object wrapper for primitive types. Returns the wrapper class or
106    * the original class for non-primitive types.
107    *
108    * @param type The type.
109    * @return The wrapper.
110    */
111   public static Class getWrapper(Class type) {
112117    Class wrapper = (Class) WRAPPER_MAP.get(type);
113117    if (wrapper == null) {
11474      return type;
115     } else {
11643      return wrapper;
117     }
118   }
119  
120   /**
121    * Init mapping between primitive types and their wrappers.
122    *
123    * @return The wrapper map.
124    */
125   private static Map initWrapperMap() {
1261    Map map = new HashMap();
1271    map.put(Byte.TYPE, Byte.class);
1281    map.put(Short.TYPE, Short.class);
1291    map.put(Integer.TYPE, Integer.class);
1301    map.put(Long.TYPE, Long.class);
1311    map.put(Float.TYPE, Float.class);
1321    map.put(Double.TYPE, Double.class);
1331    map.put(Character.TYPE, Character.class);
1341    map.put(Boolean.TYPE, Boolean.class);
1351    return map;
136   }
137 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.