components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/misc/BeanUtil.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.misc;
       
    27 
       
    28 import java.beans.*;
       
    29 import java.lang.reflect.*;
       
    30 
       
    31 public class BeanUtil {
       
    32     //
       
    33     // Static data
       
    34     //
       
    35 
       
    36     private static final String MUTATOR_PREFIX = "set";
       
    37 
       
    38     //
       
    39     // Static methods
       
    40     //
       
    41 
       
    42     public static void setPropertyInBean(Object bean, String property,
       
    43 	String valueAsText) throws IntrospectionException,
       
    44 	IllegalAccessException, NoSuchMethodException,
       
    45 	InvocationTargetException {
       
    46 
       
    47 	String methodName = getMutatorMethodName(property);
       
    48 	Method method = null;
       
    49 
       
    50 //	System.out.println("Bean: " + bean.getClass().getName());
       
    51 
       
    52 	if (valueAsText == null) {
       
    53 //	    System.out.println("Looking for null setter: " + methodName);
       
    54 	    // Find and invoke a mutator method with no parameters
       
    55 	    try {
       
    56 		method = bean.getClass().getMethod(methodName);
       
    57 		method.invoke(bean);
       
    58 //		System.out.println("Found method: " + method.getName());
       
    59 	    }
       
    60 	    catch (NoSuchMethodException ignore) {}
       
    61 	}
       
    62 
       
    63 	if (method == null) {
       
    64 	    // Search for an appropriate mutator method (throws
       
    65 	    // IntrospectionException)
       
    66 	    BeanInfo info = Introspector.getBeanInfo(bean.getClass());
       
    67 
       
    68 	    if (info != null) {
       
    69 		PropertyDescriptor descriptors[] =
       
    70 		    info.getPropertyDescriptors();
       
    71 
       
    72 		for (PropertyDescriptor descriptor : descriptors) {
       
    73 		    if (descriptor.getName().equals(property)) {
       
    74 			method = descriptor.getWriteMethod();
       
    75 
       
    76 			if (method != null) {
       
    77 			    Class type = descriptor.getPropertyType();
       
    78 
       
    79 			    // Is there a property editor for this type?
       
    80 			    PropertyEditor editor = null;
       
    81 			    Class propEdClass =
       
    82 				descriptor.getPropertyEditorClass();
       
    83 			    if (propEdClass != null) {
       
    84 				try {
       
    85 				    editor = (PropertyEditor)
       
    86 					propEdClass.newInstance();
       
    87 				}
       
    88 				catch (Exception ignore) {}
       
    89 			    }
       
    90 
       
    91 			    // No editor specifically for this
       
    92 			    // property -- check the PropertyEditorManager
       
    93 			    if (editor == null) {
       
    94 				editor = PropertyEditorManager.findEditor(type);
       
    95 			    }
       
    96 
       
    97 			    Object value = null;
       
    98 
       
    99 			    // If an editor was found...
       
   100 			    if (editor != null) {
       
   101 				// Use it to convert text to usable value
       
   102 				editor.setAsText(valueAsText);
       
   103 				value = editor.getValue();
       
   104 			    } else {
       
   105 				// Convert text to usable value
       
   106 				value = getValueAs(type, valueAsText);
       
   107 			    }
       
   108 
       
   109 			    method.invoke(bean, value);
       
   110 			}
       
   111 			break;
       
   112 		    }
       
   113 		}
       
   114 	    }
       
   115 	}
       
   116 
       
   117 	if (method == null) {
       
   118 	    throw new NoSuchMethodException(
       
   119 		bean.getClass().getName() + "." + methodName);
       
   120 	}
       
   121     }
       
   122 
       
   123     public static String getMutatorMethodName(String property) {
       
   124 	if (property == null || property.isEmpty()) {
       
   125 	    return null;
       
   126 	}
       
   127 	return MUTATOR_PREFIX +
       
   128 	    property.substring(0, 1).toUpperCase() + property.substring(1);
       
   129     }
       
   130 
       
   131     //
       
   132     // Private static methods
       
   133     //
       
   134 
       
   135     private static Object getValueAs(Class type, String value) {
       
   136 	if (value == null) {
       
   137 	    return null;
       
   138 	}
       
   139 
       
   140 	if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
       
   141 	    return new Boolean(value);
       
   142 	}
       
   143 
       
   144 	if (type.equals(Byte.class) || type.equals(Byte.TYPE)) {
       
   145 	    return new Byte(value);
       
   146 	}
       
   147 
       
   148 	if (type.equals(Character.class) || type.equals(Character.TYPE)) {
       
   149 	    return value.isEmpty() ? null : new Character(value.charAt(0));
       
   150 	}
       
   151 
       
   152 	if (type.equals(Short.class) || type.equals(Short.TYPE)) {
       
   153 	    return new Short(value);
       
   154 	}
       
   155 
       
   156 	if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
       
   157 	    return new Integer(value);
       
   158 	}
       
   159 
       
   160 	if (type.equals(Float.class) || type.equals(Float.TYPE)) {
       
   161 	    return new Float(value);
       
   162 	}
       
   163 
       
   164 	if (type.equals(Long.class) || type.equals(Long.TYPE)) {
       
   165 	    return new Long(value);
       
   166 	}
       
   167 
       
   168 	if (type.equals(Double.class) || type.equals(Double.TYPE)) {
       
   169 	    return new Double(value);
       
   170 	}
       
   171 
       
   172 	if (type.isInstance(value)) {
       
   173 	    return value;
       
   174 	}
       
   175 
       
   176 	return null;
       
   177     }
       
   178 }