ReflectionUtil.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.util;
00020 
00021 import java.lang.reflect.InvocationTargetException;
00022 import java.lang.reflect.Method;
00023 import java.lang.reflect.Modifier;
00024 
00025 
00028 public class ReflectionUtil {
00042     public static boolean setProperty(Object pObject, String pPropertyName, String pPropertyValue)
00043             throws IllegalAccessException, InvocationTargetException {
00044         final String methodName = "set" + pPropertyName.substring(0, 1).toUpperCase() + pPropertyName.substring(1);   
00045         // try to find method signature that matches init param
00046         Method[] methods = pObject.getClass().getMethods();
00047 
00048         for (int i = 0;  i < methods.length;  i++) {
00049             final Method method = methods[i];
00050             if (!method.getName().equals(methodName)) {
00051                 continue; // Ignore methods, which does have the right name 
00052             }
00053             if (!Modifier.isPublic(method.getModifiers())) {
00054                 continue;  // Ignore methods, which aren't public
00055             }
00056             
00057             Class[] parameterTypes = method.getParameterTypes();
00058             if (parameterTypes.length != 1) {
00059                 continue; // Ignore methods, which don't not have exactly one parameter
00060             }
00061             
00062             Class parameterType = parameterTypes[0];
00063             final Object param;
00064             try {
00065                 if (parameterType.equals(boolean.class) || parameterType.equals(Boolean.class)) {
00066                     param = Boolean.valueOf(pPropertyValue);
00067                 } else if (parameterType.equals(char.class) || parameterType.equals(Character.class)) {
00068                     if (pPropertyValue.length() != 1) {
00069                         throw new IllegalArgumentException("Invalid value for parameter "
00070                                 + pPropertyName + "(length != 1):"
00071                                 + pPropertyValue);
00072                     }
00073                     param = new Character(pPropertyValue.charAt(0));
00074                 } else if (parameterType.equals(byte.class) || parameterType.equals(Byte.class)) {
00075                     param = Byte.valueOf(pPropertyValue);
00076                 } else if (parameterType.equals(short.class) || parameterType.equals(Short.class)) {
00077                     param = Short.valueOf(pPropertyValue);
00078                 } else if (parameterType.equals(int.class) || parameterType.equals(Integer.class)) {
00079                     param = Integer.valueOf(pPropertyValue);
00080                 } else if (parameterType.equals(long.class) || parameterType.equals(Long.class)) {
00081                     param = Long.valueOf(pPropertyValue);
00082                 } else if (parameterType.equals(float.class) || parameterType.equals(Float.class)) {
00083                     param = Float.valueOf(pPropertyValue);
00084                 } else if (parameterType.equals(double.class) || parameterType.equals(Double.class)) {
00085                     param = Double.valueOf(pPropertyValue);
00086                 } else if (parameterType.equals(String.class)) {
00087                     param = pPropertyValue;
00088                 } else {
00089                     throw new IllegalStateException("The property " + pPropertyName
00090                             + " has an unsupported type of " + parameterType.getName());
00091                 }
00092             } catch (NumberFormatException e) {
00093                 throw new IllegalArgumentException("Invalid value for property "
00094                         + pPropertyName + ": " + pPropertyValue);
00095             }
00096             method.invoke(pObject, new Object[]{param});
00097             return true;
00098         }
00099         return false;
00100     }
00101 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49