| Line | Hits | Source |
|---|---|---|
| 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 | */ | |
| 48 | 9 | public abstract class MiscUtils { |
| 49 | ||
| 50 | /** Empty array for no-parameter methods. */ | |
| 51 | 2 | private static final Class[] NO_PARAMS = new Class[0]; |
| 52 | ||
| 53 | /** Empty array for no-parameter methods. */ | |
| 54 | 1 | private static final Object[] EMPTY_ARRAY = new Object[0]; |
| 55 | ||
| 56 | /** Mapping between primitive types and their wrappers. */ | |
| 57 | 1 | private static final Map WRAPPER_MAP = initWrapperMap(); |
| 58 | ||
| 59 | /** For locating methods with a string parameter via reflection. */ | |
| 60 | 1 | 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 { | |
| 73 | 104 | BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); |
| 74 | 104 | PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); |
| 75 | 405 | for (int i = 0; i < descriptors.length; i++) { |
| 76 | 405 | if (descriptors[i].getName().equals(property)) { |
| 77 | 104 | return descriptors[i].getPropertyType(); |
| 78 | } | |
| 79 | } | |
| 80 | 0 | } catch (Exception ex) { |
| 81 | 0 | throw new IllegalArgumentException( |
| 82 | "The property " | |
| 83 | + property | |
| 84 | + " is not valid for the class " | |
| 85 | + beanClass.getName()); | |
| 86 | } | |
| 87 | 0 | 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) { | |
| 101 | 457 | 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) { | |
| 112 | 117 | Class wrapper = (Class) WRAPPER_MAP.get(type); |
| 113 | 117 | if (wrapper == null) { |
| 114 | 74 | return type; |
| 115 | } else { | |
| 116 | 43 | 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() { | |
| 126 | 1 | Map map = new HashMap(); |
| 127 | 1 | map.put(Byte.TYPE, Byte.class); |
| 128 | 1 | map.put(Short.TYPE, Short.class); |
| 129 | 1 | map.put(Integer.TYPE, Integer.class); |
| 130 | 1 | map.put(Long.TYPE, Long.class); |
| 131 | 1 | map.put(Float.TYPE, Float.class); |
| 132 | 1 | map.put(Double.TYPE, Double.class); |
| 133 | 1 | map.put(Character.TYPE, Character.class); |
| 134 | 1 | map.put(Boolean.TYPE, Boolean.class); |
| 135 | 1 | return map; |
| 136 | } | |
| 137 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |