Android examples for java.lang.reflect:Field
copy Fields from one object to Activity via reflection
//package com.java2s; import java.lang.reflect.Field; import android.app.Activity; public class Main { public static void copyFields(Class clazz, String[] fields, Object host, Activity target) { for (String f : fields) { Field declaredField = null; try { declaredField = clazz.getDeclaredField(f); setField(target, declaredField, getFiledValue(clazz, host, f)); } catch (Exception e) { throw new RuntimeException("setField(). field: " + declaredField, e); }/*from w w w . ja v a 2s . c om*/ } } public static void setField(Object object, Field field, Object value) { field.setAccessible(true); try { field.set(object, value); } catch (Exception e) { throw new RuntimeException("setField(). field: " + field, e); } } public static Object getFiledValue(Class clazz, Object object, String fieldName) { Object f = null; try { Field declaredField = getFiled(clazz, object, fieldName); declaredField.setAccessible(true); f = declaredField.get(object); return f; } catch (Exception e) { throw new RuntimeException("getFiledValue(). class: " + clazz + " object: " + object + " field: " + fieldName, e); } } public static Field getFiled(Class clazz, Object object, String fieldName) { try { Field declaredField = clazz.getDeclaredField(fieldName); return declaredField; } catch (Exception e) { throw new RuntimeException("getFiled(). fieldName: " + fieldName, e); } } }