1 package com.thoughtworks.xstream.core.util; 2 3 import java.lang.reflect.Field ; 4 5 11 public class Fields { 12 public static Field find(Class type, String name) { 13 try { 14 Field result = type.getDeclaredField(name); 15 result.setAccessible(true); 16 return result; 17 } catch (NoSuchFieldException e) { 18 throw new RuntimeException ("Could not access " + type.getName() + "." + name + " field"); 19 } 20 } 21 22 public static void write(Field field, Object instance, Object value) { 23 try { 24 field.set(instance, value); 25 } catch (IllegalAccessException e) { 26 throw new RuntimeException ("Could not write " + field.getType().getName() + "." + field.getName() + " field"); 27 } 28 } 29 30 public static Object read(Field field, Object instance) { 31 try { 32 return field.get(instance); 33 } catch (IllegalAccessException e) { 34 throw new RuntimeException ("Could not read " + field.getType().getName() + "." + field.getName() + " field"); 35 } 36 } 37 } 38 | Popular Tags |