/** * Get bean's property value and maps to String * * @see io.katharsis.core.internal.utils.PropertyUtils#getProperty(Object, String) * * @param bean bean to be accessed * @param field bean's field * @return bean's property value */ public static String getProperty(Object bean, String field) { Object property = PropertyUtils.getProperty(bean, field); if (property == null) { return "null"; } return property.toString(); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private Collection<D> getOrCreateCollection(Object source, String fieldName) { Object property = PropertyUtils.getProperty(source, fieldName); if (property == null) { Class<?> propertyClass = PropertyUtils.getPropertyClass(source.getClass(), fieldName); boolean isList = List.class.isAssignableFrom(propertyClass); property = isList ? new ArrayList() : new HashSet(); PropertyUtils.setProperty(source, fieldName, property); } return (Collection<D>) property; }
/** * Get bean's property value. The sequence of searches for getting a value is as follows: * <ol> * <li>All class fields are found using {@link ClassUtils#getClassFields(Class)}</li> * <li>Search for a field with the name of the desired one is made</li> * <li>If a field is found and it's a non-public field, the value is returned using the accompanying getter</li> * <li>If a field is found and it's a public field, the value is returned using the public field</li> * <li>If a field is not found, a search for a getter is made - all class getters are found using * {@link ClassUtils#getClassFields(Class)}</li> * <li>From class getters, an appropriate getter with name of the desired one is used</li> * </ol> * * @param bean bean to be accessed * @param field bean's fieldName * @return bean's property value */ public static Object getProperty(Object bean, String field) { INSTANCE.checkParameters(bean, field); try { return INSTANCE.getPropertyValue(bean, field); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw handleReflectionException(bean, field, e); } }
/** * Similar to {@link PropertyUtils#getPropertyClass(Class,List)} but returns the property class. * * @param beanClass bean to be accessed * @param field bean's fieldName * @return bean's property class */ public static Class<?> getPropertyClass(Class<?> beanClass, String field) { try { return INSTANCE.findPropertyClass(beanClass, field); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw handleReflectionException(beanClass, field, e); } }
/** * Similar to {@link PropertyUtils#getPropertyClass(Class,List)} but returns the property class. * * @param beanClass bean to be accessed * @param field bean's fieldName * @return bean's property class */ public static Type getPropertyType(Class<?> beanClass, String field) { try { return INSTANCE.findPropertyType(beanClass, field); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw handleReflectionException(beanClass, field, e); } }
public void setId(Object resource, Object id) { PropertyUtils.setProperty(resource, idField.getUnderlyingName(), id); }
private void setPropertyValue(Object bean, String fieldName, Object value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Field foundField = findField(bean.getClass(), fieldName); if (foundField != null) { if ( !Modifier.isPublic(foundField.getModifiers())) { Method setter = getSetter(bean, foundField.getName(), foundField.getType()); setter.invoke(bean, prepareValue(value, setter.getParameterTypes()[0])); } else { foundField.set(bean, prepareValue(value, foundField.getType())); } } else { Method getter = findGetter(bean.getClass(), fieldName); if (getter == null) { String message = String.format("Cannot find a getter for %s.%s", bean.getClass().getCanonicalName(), fieldName); throw new PropertyException(message, bean.getClass(), fieldName); } String getterFieldName = getGetterFieldName(getter); Method setter = getSetter(bean, getterFieldName, getter.getReturnType()); setter.invoke(bean, prepareValue(value, setter.getParameterTypes()[0])); } }
/** * Similar to {@link PropertyUtils#getPropertyClass(Class,String)} but returns the property class for the tail of the given property path. * * @param clazz bean to be accessed * @param propertyPath bean's fieldName * @return property class */ public static Class<?> getPropertyClass(Class<?> clazz, List<String> propertyPath) { Class<?> current = clazz; for(String propertyName : propertyPath){ current = getPropertyClass(current, propertyName); } return current; }
INSTANCE.checkParameters(bean, field); INSTANCE.setPropertyValue(bean, field, value); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw handleReflectionException(bean, field, e);
private Object getPropertyValue(Object bean, String fieldName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Field foundField = findField(bean.getClass(), fieldName); if (foundField != null) { if (!Modifier.isPublic(foundField.getModifiers())) { Method getter = getGetter(bean.getClass(), foundField.getName()); return getter.invoke(bean); } else { return foundField.get(bean); } } else { Method getter = findGetter(bean.getClass(), fieldName); if (getter == null) { String message = String .format("Cannot find an getter for %s.%s", bean.getClass().getCanonicalName(), fieldName); throw new PropertyException(message, bean.getClass(), fieldName); } return getter.invoke(bean); } }
@Override public void onInitialized(MetaProviderContext context, MetaElement element) { super.onInitialized(context, element); if (element.getParent() instanceof MetaJpaDataObject && element instanceof MetaAttribute) { MetaAttribute attr = (MetaAttribute) element; MetaDataObject parent = attr.getParent(); Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName()); Class<?> elementType = getElementType(implementationType); boolean jpaObject = attr.isAssociation() || elementType.getAnnotation(Embeddable.class) != null; Class<? extends MetaType> metaClass = jpaObject ? MetaJpaDataObject.class : MetaType.class; MetaType metaType = context.getLookup().getMeta(implementationType, metaClass); attr.setType(metaType); } }
private Type findPropertyType(Class<?> beanClass, String fieldName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Field foundField = findField(beanClass, fieldName); if (foundField != null) { return foundField.getGenericType(); } else { Method getter = findGetter(beanClass, fieldName); if (getter == null) { String message = String .format("Cannot find an getter for %s.%s", beanClass.getCanonicalName(), fieldName); throw new PropertyException(message, beanClass, fieldName); } return getter.getGenericReturnType(); } }
private void setProperty(ObjectMapper objectMapper, T instance, JsonNode valueNode, String propertyName) { Optional<ResourceField> staticField = findStaticField(propertyName); try{ if (staticField.isPresent()) { String underlyingName = staticField.get().getUnderlyingName(); Type valueType = staticField.get().getGenericType(); Object value; if(valueNode != null){ JavaType jacksonValueType = objectMapper.getTypeFactory().constructType(valueType); ObjectReader reader = objectMapper.reader().forType(jacksonValueType); value = reader.readValue(valueNode); }else{ value = null; } PropertyUtils.setProperty(instance, underlyingName, value); } else if(jsonAnySetter != null){ // Needed for JsonIgnore and dynamic attributes Object value = objectMapper.reader().forType(Object.class).readValue(valueNode); jsonAnySetter.invoke(instance, propertyName, value); } } catch (IOException | IllegalAccessException | InvocationTargetException e) { throw new ResourceException( String.format("Exception while reading %s.%s=%s due to %s", instance, propertyName, valueNode, e.getMessage()), e); } }
private Class<?> getAttributeType(QuerySpec querySpec, List<String> attributePath) { try { return PropertyUtils.getPropertyClass(querySpec.getResourceClass(), attributePath); } catch (PropertyException e) { if (allowUnknownAttributes) { return String.class; } else { throw e; } } }
@Override public void onInitialized(MetaProviderContext context, MetaElement element) { if (element instanceof MetaAttribute && element.getParent().getClass() == getMetaClass()) { MetaAttribute attr = (MetaAttribute) element; MetaDataObject parent = attr.getParent(); Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName()); MetaElement metaType = context.getLookup().getMeta(implementationType, getMetaClass(), true); if (metaType == null) { metaType = context.getLookup().getMeta(implementationType); } attr.setType(metaType.asType()); } }
private Class<?> findPropertyClass(Class<?> beanClass, String fieldName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Field foundField = findField(beanClass, fieldName); if (foundField != null) { return foundField.getType(); } else { Method getter = findGetter(beanClass, fieldName); if (getter == null) { String message = String .format("Cannot find an getter for %s.%s", beanClass.getCanonicalName(), fieldName); throw new PropertyException(message, beanClass, fieldName); } return getter.getReturnType(); } }
/** * Similar to {@link PropertyUtils#getProperty(Object,String)} but returns the property value for the tail of the given property path. * * @param bean bean to be accessed * @param propertyPath property path * @return value */ public static Object getProperty(Object bean, List<String> propertyPath) { Object current = bean; for(String propertyName : propertyPath){ if(current == null) return null; if(current instanceof Iterable){ // follow multi-valued property List<Object> result = new ArrayList<>(); Iterable<?> iterable = (Iterable<?>)current; for(Object currentElem : iterable){ result.add(getProperty(currentElem, propertyName)); } current = result; }else{ // follow single-valued property current = getProperty(current, propertyName); } } return current; }
protected void setMeta(Resource dataBody, Object instance, ResourceInformation resourceInformation) { ResourceField metaField = resourceInformation.getMetaField(); if (dataBody.getMeta() != null && metaField != null) { JsonNode metaNode = dataBody.getMeta(); Class<?> metaClass = metaField.getType(); ObjectReader metaMapper = objectMapper.readerFor(metaClass); try { Object meta = metaMapper.readValue(metaNode); PropertyUtils.setProperty(instance, metaField.getUnderlyingName(), meta); } catch (IOException e) { throw new ResponseBodyException("failed to parse links information", e); } } }
private static void addMergeInclusions(Deque<String> attributePath, JpaQueryExecutor<?> executor, Class<?> resourceClass) { JpaMergeRelations annotation = resourceClass.getAnnotation(JpaMergeRelations.class); if (annotation != null) { for (String attrName : annotation.attributes()) { attributePath.push(attrName); executor.fetch(new ArrayList<>(attributePath)); // recurse Class attrType = PropertyUtils.getPropertyClass(resourceClass, attrName); addMergeInclusions(attributePath, executor, attrType); attributePath.pop(); } } }
MetaAttribute attr = (MetaAttribute) element; MetaDataObject parent = attr.getParent(); Type implementationType = PropertyUtils.getPropertyType(parent.getImplementationClass(), attr.getName());