Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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;
00052 }
00053 if (!Modifier.isPublic(method.getModifiers())) {
00054 continue;
00055 }
00056
00057 Class[] parameterTypes = method.getParameterTypes();
00058 if (parameterTypes.length != 1) {
00059 continue;
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 }