public Class getPropertyType(String name) { if (ReflectionUtil.hasProperty(type, name)) return ReflectionUtil.getPropertyDescriptor(type, name).getPropertyType(); else { try { Field field = type.getDeclaredField(name); if (ReflectionUtil.isMemberField(field)) { field.setAccessible(true); return field.getType(); } } catch (Exception e) { } return null; } }
public Collection keys() { Object prototype = createPrototype(); Set<String> set = new HashSet<String>(); for (PropertyDescriptor prop : ReflectionUtil.getProperties(getType())) { if (config.isPropertyAccessibleForEncoding(prop)) try { if (!Utilities.same( getProperty(getObject(), prop.getName()), getProperty(prototype, prop.getName()))) set.add(prop.getName()); } catch (Exception e) { } } for (Field field : getType().getDeclaredFields()) if (config.isFieldAccessibleForEncoding(field)) { field.setAccessible(true); try { if (!Utilities.same(field.get(prototype), field.get(getObject()))) set.add(field.getName()); } catch (Exception e) { } } // sort the keys alphabetically List<String> ret = new ArrayList<String>(set); Collections.sort( ret, new Comparator<String>() { public int compare(String o1, String o2) { return o1.compareTo(o2); } }); return ret; }
public Object getProperty(Object obj, String name) { try { PropertyDescriptor prop = ReflectionUtil.getPropertyDescriptor(type, name); if (config.isPropertyAccessibleForEncoding(prop)) { Method rm = prop.getReadMethod(); rm.setAccessible(true); return rm.invoke(obj, null); } } catch (Exception e) { } try { Field field = type.getDeclaredField(name); if (config.isFieldAccessibleForEncoding(field)) { field.setAccessible(true); return field.get(obj); } } catch (Exception e) { } throw new PropertyAccessException("Can't get " + name + " property on type " + type + "."); }
public void setProperty(String name, Object value) throws PropertyAccessException { try { PropertyDescriptor prop = ReflectionUtil.getPropertyDescriptor(type, name); if (config.isPropertyAccessibleForEncoding(prop)) { Method wm = prop.getWriteMethod(); wm.setAccessible(true); wm.invoke(getObject(), new Object[] {value}); return; } } catch (Exception e) { } try { Field field = type.getDeclaredField(name); if (config.isFieldAccessibleForDecoding(field)) { field.setAccessible(true); field.set(getObject(), value); } return; } catch (Exception e) { } // ignore this }
public boolean hasProperty(String name) { PropertyDescriptor prop = ReflectionUtil.getPropertyDescriptor(type, name); return config.isPropertyAccessibleForEncoding(prop); }