public static Object getProperty(Object bean, String name) { PropertyDescriptor[] pds = getProperties(bean.getClass()); for (PropertyDescriptor pd : pds) { if (pd.getName().equals(name) && pd.getReadMethod() != null) { try { return pd.getReadMethod().invoke(bean); } catch (Exception e) { throw new RuntimeException("Could not get value for property " + name + " on object " + bean, e); } } } throw new RuntimeException("Property " + name + " not found on object " + bean); }
public PropertyDescriptor(String propertyName, Class<?> beanClass) { this.name = propertyName; try { setReadMethod(beanClass, createDefaultMethodName(propertyName, "is")); } catch (Exception e) { setReadMethod(beanClass, createDefaultMethodName(propertyName, "get")); } setWriteMethod(beanClass, createDefaultMethodName(propertyName, "set")); }
@Override public boolean equals(Object object) { if (!(object instanceof PropertyDescriptor)) return false; PropertyDescriptor pd = (PropertyDescriptor)object; if (!((this.getter == null && pd.getter == null) || (this.getter != null && this.getter.equals(pd.getter)))) return false; if (!((this.setter == null && pd.setter == null) || (this.setter != null && this.setter.equals(pd.setter)))) return false; return this.getPropertyType() == pd.getPropertyType(); }
public PropertyDescriptor(String propertyName, Method getter, Method setter) { this.name = propertyName; setReadMethod(getter); setWriteMethod(setter); }
@Override public void setPropertyValue(Object entity, String name, Object value) { if (entity == null) return; try { boolean found = false; PropertyDescriptor[] pds = Introspector.getPropertyDescriptors(entity.getClass()); for (PropertyDescriptor pd : pds) { if (pd.getName().equals(name) && pd.getWriteMethod() != null) { Object oldValue = null; if (pd.getReadMethod() != null) oldValue = pd.getReadMethod().invoke(entity); pd.getWriteMethod().invoke(entity, value); if (pd.getReadMethod() != null) PropertyChangeHelper.firePropertyChange(entity, name, oldValue, value); found = true; break; } } if (!found) super.setPropertyValue(entity, name, value); } catch (Exception e) { throw new RuntimeException("Could not set property " + name + " on entity " + entity, e); } }
boolean found = false; for (PropertyDescriptor property : properties) { if (property.getName().equals(field.getName())) { found = true; if (property.getReadMethod() == null) { try { Method readMethod = clazz.getDeclaredMethod(getIsMethodName(field.getName())); if (Modifier.isPublic(readMethod.getModifiers()) && !Modifier.isStatic(readMethod.getModifiers())) property.setReadMethod(readMethod); PropertyDescriptor[] propertiesTmp = new PropertyDescriptor[properties.length + 1]; System.arraycopy(properties, 0, propertiesTmp, 0, properties.length); propertiesTmp[properties.length] = new PropertyDescriptor(field.getName(), readMethod, null); properties = propertiesTmp;
Method method = propertyDescriptor.getReadMethod(); if (method != null && ( method.isAnnotationPresent(Id.class) || break; method = propertyDescriptor.getWriteMethod(); if (method != null && ( method.isAnnotationPresent(Id.class) ||
private boolean ignoreProperty( PropertyDescriptor descriptor, String[] ignoreProperties) { boolean ignore = false; if (descriptor.getName().equals("class")) { ignore = true; } else { for (int i = 0; i < ignoreProperties.length; i++) { String ignoreProp = ignoreProperties[i]; if (ignoreProp.equals(descriptor.getName())) { log.debug("Ignoring %s", descriptor.getName()); ignore = true; break; } } } return ignore; }
private void setReadMethod(Class<?> beanClass, String getterName) { try { Method readMethod = beanClass.getMethod(getterName, new Class[] {}); setReadMethod(readMethod); } catch (Exception e) { throw new RuntimeException("Introspection exception", e); } }
throw new RuntimeException("Could not find setter for property " + name); setWriteMethod(writeMethod);
Method setter = (Method)table.get("setter"); PropertyDescriptor propertyDesc = new PropertyDescriptor(propertyName, getter, setter); propertyList.add(propertyDesc);
@Override public void defineProperty(String name) { if (type.length() == 0 || instantiator != null) properties.add(new MapProperty(converters, name)); else { try { // Try to find public getter/setter. PropertyDescriptor[] props = Introspector.getPropertyDescriptors(clazz); for (PropertyDescriptor prop : props) { if (name.equals(prop.getName()) && prop.getWriteMethod() != null && prop.getReadMethod() != null) { properties.add(new MethodProperty(converters, name, prop.getWriteMethod(), prop.getReadMethod())); return; } } // Try to find public field. Field field = clazz.getField(name); if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) properties.add(new FieldProperty(converters, field)); } catch (NoSuchFieldException e) { if ("uid".equals(name)) // ObjectProxy specific property... properties.add(new UIDProperty(converters)); else throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } } }
boolean found = false; for (PropertyDescriptor property : properties) { if (property.getName().equals(field.getName())) { found = true; if (property.getReadMethod() == null) { try { Method readMethod = clazz.getDeclaredMethod(getIsMethodName(field.getName())); if (Modifier.isPublic(readMethod.getModifiers()) && !Modifier.isStatic(readMethod.getModifiers())) property.setReadMethod(readMethod); PropertyDescriptor[] propertiesTmp = new PropertyDescriptor[properties.length + 1]; System.arraycopy(properties, 0, propertiesTmp, 0, properties.length); propertiesTmp[properties.length] = new PropertyDescriptor(field.getName(), readMethod, null); properties = propertiesTmp;
public PropertyDescriptor(String propertyName, Method getter, Method setter) { this.name = propertyName; setReadMethod(getter); setWriteMethod(setter); }
private boolean ignoreProperty( PropertyDescriptor descriptor, String[] ignoreProperties) { boolean ignore = false; if (descriptor.getName().equals("class")) { ignore = true; } else { for (int i = 0; i < ignoreProperties.length; i++) { String ignoreProp = ignoreProperties[i]; if (ignoreProp.equals(descriptor.getName())) { log.debug("Ignoring %s", descriptor.getName()); ignore = true; break; } } } return ignore; }
private void setReadMethod(Class<?> beanClass, String getterName) { try { Method readMethod = beanClass.getMethod(getterName, new Class[] {}); setReadMethod(readMethod); } catch (Exception e) { throw new RuntimeException("Introspection exception", e); } }
throw new RuntimeException("Could not find setter for property " + name); setWriteMethod(writeMethod);
Method setter = (Method)table.get("setter"); PropertyDescriptor propertyDesc = new PropertyDescriptor(propertyName, getter, setter); propertyList.add(propertyDesc);
@Override public void defineProperty(String name) { if (type.length() == 0 || instantiator != null) properties.add(new MapProperty(converters, name)); else { try { // Try to find public getter/setter. PropertyDescriptor[] props = Introspector.getPropertyDescriptors(clazz); for (PropertyDescriptor prop : props) { if (name.equals(prop.getName()) && prop.getWriteMethod() != null && prop.getReadMethod() != null) { properties.add(new MethodProperty(converters, name, prop.getWriteMethod(), prop.getReadMethod())); return; } } // Try to find public field. Field field = clazz.getField(name); if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) properties.add(new FieldProperty(converters, field)); } catch (NoSuchFieldException e) { if ("uid".equals(name)) // ObjectProxy specific property... properties.add(new UIDProperty(converters)); else throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } } }
public static Object getProperty(Object bean, String name) { PropertyDescriptor[] pds = getProperties(bean.getClass()); for (PropertyDescriptor pd : pds) { if (pd.getName().equals(name) && pd.getReadMethod() != null) { try { return pd.getReadMethod().invoke(bean); } catch (Exception e) { throw new RuntimeException("Could not get value for property " + name + " on object " + bean, e); } } } throw new RuntimeException("Property " + name + " not found on object " + bean); }