public static Method getReadMethod(PropertyDescriptor descriptor) { return org.apache.commons.beanutils.PropertyUtils.getReadMethod(descriptor); }
private Object getProperty(Object obj, String prop) { if (obj != null && !StringUtils.isBlank(prop)) { try { Method m = PropertyUtils.getReadMethod(new PropertyDescriptor(prop, obj.getClass())); if (m != null && !m.isAnnotationPresent(JsonIgnore.class)) { return PropertyUtils.getProperty(obj, prop); } } catch (Exception e) { } } return null; } }
private Object getProperty(Object obj, String prop) { if (obj != null && !StringUtils.isBlank(prop)) { try { Method m = PropertyUtils.getReadMethod(new PropertyDescriptor(prop, obj.getClass())); if (m != null && !m.isAnnotationPresent(JsonIgnore.class)) { return PropertyUtils.getProperty(obj, prop); } } catch (Exception e) { } } return null; } }
private Object getValue( FaceletContext faceletContext, UIComponent parent, String expressionString, String attributeName) { Class type = Object.class; try { type = PropertyUtils.getReadMethod( new PropertyDescriptor(attributeName, parent.getClass())).getReturnType(); } catch (IntrospectionException e) { LOG.warn("Can't determine expected type", e); } ExpressionFactory expressionFactory = faceletContext.getExpressionFactory(); final ValueExpression valueExpression = expressionFactory .createValueExpression(faceletContext, expressionString, type); return valueExpression.getValue(faceletContext); }
@Override public Object apply(Object o) { if (CollectionUtils.isNotEmpty(super.parameters)) { final String propertyName = (String) getParameters().get(0); try { final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(o, propertyName); final Method readMethod = PropertyUtils.getReadMethod(descriptor); log.debug("Execute getter method '{}' of class [{}]", readMethod.getName(), o.getClass().getName()); return readMethod.invoke(o); } catch (InvocationTargetException e) { log.error("Unable to find property '{}' in object from class [{}]", propertyName, o.getClass().getName(),e); throw new RuntimeException("Unable to find property '"+propertyName+"' in object from class ["+o.getClass().getName()+"]"); } catch (NoSuchMethodException | IllegalAccessException e) { log.error("Unable to find/access getter method for property '{}' in object from class [{}]", propertyName, o.getClass().getName(), e); throw new RuntimeException("Unable to find/access getter method for property '"+propertyName+"' in object from class ["+o.getClass().getName()+"]"); } } else return null; } }
private static void addPropertyWithReflection( SearchableQuery query, String propName, String propValue ) throws Exception { Method getter = PropertyUtils.getReadMethod( new PropertyDescriptor( propName, query.getClass() ) ); Class returnType = getter.getReturnType(); Object objPhrase; if ( returnType.equals( QueryPhrase.class ) ) { objPhrase = new StandardQueryPhraseConverter().objectToPhrase( propValue ); } else if ( returnType.getName().equals( "boolean" ) ) { objPhrase = Boolean.valueOf( propValue ).booleanValue(); } else { throw new IntactException( "Unexpected type " + returnType.getName() + " for property name: " + propName ); } BeanUtils.setProperty( query, propName, objPhrase ); }
private static void addPropertyWithReflection( SearchableQuery query, String propName, String propValue ) throws Exception { Method getter = PropertyUtils.getReadMethod( new PropertyDescriptor( propName, query.getClass() ) ); Class returnType = getter.getReturnType(); Object objPhrase; if ( returnType.equals( QueryPhrase.class ) ) { objPhrase = new StandardQueryPhraseConverter().objectToPhrase( propValue ); } else if ( returnType.getName().equals( "boolean" ) ) { objPhrase = Boolean.valueOf( propValue ).booleanValue(); } else { throw new IntactException( "Unexpected type " + returnType.getName() + " for property name: " + propName ); } BeanUtils.setProperty( query, propName, objPhrase ); }
private static void addPropertyWithReflection( SearchableQuery query, String propName, String propValue ) throws Exception { Method getter = PropertyUtils.getReadMethod( new PropertyDescriptor( propName, query.getClass() ) ); Class returnType = getter.getReturnType(); Object objPhrase; if ( returnType.equals( QueryPhrase.class ) ) { objPhrase = new StandardQueryPhraseConverter().objectToPhrase( propValue ); } else if ( returnType.getName().equals( "boolean" ) ) { objPhrase = Boolean.valueOf( propValue ).booleanValue(); } else { throw new IntactException( "Unexpected type " + returnType.getName() + " for property name: " + propName ); } BeanUtils.setProperty( query, propName, objPhrase ); }
@Override public Object apply(Object o) { if (CollectionUtils.isNotEmpty(super.parameters)) { return getParameters().stream() .map(propertyName -> { try { final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(o, (String) propertyName); final Method readMethod = PropertyUtils.getReadMethod(descriptor); log.debug("Execute getter method '{}' of class [{}]", readMethod.getName(), o.getClass().getName()); final Object methodResult = readMethod.invoke(o); if(Collection.class.isAssignableFrom(methodResult.getClass())) { return ((Collection) methodResult).stream().map(Object::toString).collect(Collectors.joining(" ")); } return methodResult.toString(); } catch (InvocationTargetException e) { log.error("Unable to find property '{}' in object from class [{}]", propertyName, o.getClass().getName(), e); throw new RuntimeException("Unable to find property '" + propertyName + "' in object from class [" + o.getClass().getName() + "]"); } catch (NoSuchMethodException | IllegalAccessException e) { log.error("Unable to find/access getter method for property '{}' in object from class [{}]", propertyName, o.getClass().getName(), e); throw new RuntimeException("Unable to find/access getter method for property '" + propertyName + "' in object from class [" + o.getClass().getName() + "]"); } }) .collect(Collectors.joining(" ")); } else return null; } }
public static Map<String, Object> toMap(Object bean) { if (bean == null) { return Collections.emptyMap(); } try { Map<String, Object> description = new HashMap<String, Object>(); if (bean instanceof DynaBean) { DynaProperty[] descriptors = ((DynaBean) bean).getDynaClass().getDynaProperties(); for (DynaProperty descriptor : descriptors) { String name = descriptor.getName(); description.put(name, BeanUtils.getProperty(bean, name)); } } else { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean); for (PropertyDescriptor descriptor : descriptors) { String name = descriptor.getName(); if (PropertyUtils.getReadMethod(descriptor) != null) { description.put(name, PropertyUtils.getNestedProperty(bean, name)); } } } return description; } catch (Exception e) { return Collections.emptyMap(); } }