Android examples for java.lang.reflect:Field Value
set Value Of Field
//package com.java2s; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.WeakHashMap; import android.text.TextUtils; import android.util.Log; public class Main { private static final WeakHashMap<Field, Method> setterCache = new WeakHashMap<Field, Method>(); public static void setValueOfField(Object object, String fieldName, Object value) {//from w w w. ja v a 2s. co m if (object == null) { return; } Class<?> clazz = object.getClass(); try { final Field field = clazz.getDeclaredField(fieldName); Method setter = getSetter(field); if (setter != null) { Class<?> paramType = setter.getParameterTypes()[0]; if (value == null) { value = getBasicTypeNullValue(paramType); } setter.invoke(object, value); return; } field.setAccessible(true); if (value == null) { value = getBasicTypeNullValue(object.getClass()); } field.set(object, value); } catch (NoSuchFieldException e) { Log.e("ReflectUtil File", "setValueOfField:" + object.getClass().getName() + "#" + fieldName); throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { Log.e("ReflectUtil File", "setValueOfField:" + object.getClass().getName() + "#" + fieldName); throw new IllegalArgumentException(e); } catch (InvocationTargetException e) { Log.e("ReflectUtil File", "setValueOfField:" + object.getClass().getName() + "#" + fieldName); throw new IllegalArgumentException(e); } catch (IllegalArgumentException e) { Log.e("ReflectUtil File", "setValueOfField:" + object.getClass().getName() + "#" + fieldName + "|" + value); throw new IllegalArgumentException(e); } } public static Field getDeclaredField(Object object, String fieldName) { Field field = null; Class<?> clazz = object.getClass(); while (clazz != Object.class) { try { field = clazz.getDeclaredField(fieldName); return field; } catch (Exception e) { Log.i("ReflectUtil File", ""); clazz = clazz.getSuperclass(); } } return null; } public static Method getSetter(Field field) { String fieldName = field.getName(); Method setter = (Method) setterCache.get(field); if (setter != null) { return setter; } String methodName = generateSetterName(fieldName); Class<?> clazz = field.getDeclaringClass(); try { setter = clazz.getMethod(methodName, new Class[] { field.getType() }); setterCache.put(field, setter); return setter; } catch (SecurityException e) { return null; } catch (NoSuchMethodException e) { } return null; } public static Object getBasicTypeNullValue(Class<?> clazz) { if (clazz == Integer.TYPE) { return Integer.valueOf(0); } if (clazz == Long.TYPE) { return Long.valueOf(0L); } if (clazz == Float.TYPE) { return Float.valueOf(0.0F); } if (clazz == Double.TYPE) { return Double.valueOf(0.0D); } if (clazz == Byte.TYPE) { return Byte.valueOf((byte) 0); } if (clazz == Short.TYPE) { return Short.valueOf((short) 0); } if (clazz == Character.TYPE) { return 0; } if (clazz == Boolean.TYPE) { return Boolean.FALSE; } return null; } public static String generateSetterName(String fieldName) { if (TextUtils.isEmpty(fieldName)) { throw new IllegalArgumentException("invalid field."); } String firstCH = fieldName.substring(0); Character secondCH = null; if (fieldName.length() > 1) { secondCH = Character.valueOf(fieldName.charAt(1)); } String setterName = null; if ((secondCH == null) || (!Character.isUpperCase(secondCH.charValue()))) { setterName = "set" + fieldName .replaceFirst(firstCH, firstCH.toUpperCase()); } else { setterName = "set" + fieldName; } return setterName; } }