jdk/src/share/classes/java/beans/ReflectionUtils.java
changeset 18061 cd92ca4c1331
parent 18060 7cfaa4558c12
parent 17289 4dec41b3c5e3
child 18062 b16b51f1de87
equal deleted inserted replaced
18060:7cfaa4558c12 18061:cd92ca4c1331
     1 /*
       
     2  * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package java.beans;
       
    26 
       
    27 import java.lang.reflect.Field;
       
    28 
       
    29 /**
       
    30  * A utility class for reflectively finding methods, constuctors and fields
       
    31  * using reflection.
       
    32  */
       
    33 class ReflectionUtils {
       
    34 
       
    35     @SuppressWarnings("rawtypes")
       
    36     public static boolean isPrimitive(Class type) {
       
    37         return primitiveTypeFor(type) != null;
       
    38     }
       
    39 
       
    40     @SuppressWarnings("rawtypes")
       
    41     public static Class primitiveTypeFor(Class wrapper) {
       
    42         if (wrapper == Boolean.class) return Boolean.TYPE;
       
    43         if (wrapper == Byte.class) return Byte.TYPE;
       
    44         if (wrapper == Character.class) return Character.TYPE;
       
    45         if (wrapper == Short.class) return Short.TYPE;
       
    46         if (wrapper == Integer.class) return Integer.TYPE;
       
    47         if (wrapper == Long.class) return Long.TYPE;
       
    48         if (wrapper == Float.class) return Float.TYPE;
       
    49         if (wrapper == Double.class) return Double.TYPE;
       
    50         if (wrapper == Void.class) return Void.TYPE;
       
    51         return null;
       
    52     }
       
    53 
       
    54     /**
       
    55      * Returns the value of a private field.
       
    56      *
       
    57      * @param instance object instance
       
    58      * @param cls class
       
    59      * @param name name of the field
       
    60      * @param el an exception listener to handle exceptions; or null
       
    61      * @return value of the field; null if not found or an error is encountered
       
    62      */
       
    63     @SuppressWarnings("rawtypes")
       
    64     public static Object getPrivateField(Object instance, Class cls,
       
    65                                          String name, ExceptionListener el) {
       
    66         try {
       
    67             Field f = cls.getDeclaredField(name);
       
    68             f.setAccessible(true);
       
    69             return f.get(instance);
       
    70         }
       
    71         catch (Exception e) {
       
    72             if (el != null) {
       
    73                 el.exceptionThrown(e);
       
    74             }
       
    75         }
       
    76         return null;
       
    77     }
       
    78 }