Skip to content

Commit

Permalink
Make BeanUtils support beans with class hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Warren authored and Warren committed Oct 19, 2017
1 parent 832de5f commit 949a73d
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 169 deletions.
Empty file.
16 changes: 16 additions & 0 deletions spring-content-autoconfigure/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.springframework.content.commons.utils;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.util.ReflectionUtils;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.util.ReflectionUtils;

public final class BeanUtils {

private static final Condition MATCHING_CONDITION = new Condition() {
Expand All @@ -26,28 +26,11 @@ private BeanUtils() {}
public static boolean hasFieldWithAnnotation(Object domainObj, Class<? extends Annotation> annotationClass)
throws SecurityException, BeansException {

BeanWrapper wrapper = new BeanWrapperImpl(domainObj);
PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
String prop = descriptor.getName();
try {
Field candidate = domainObj.getClass().getDeclaredField(prop);
if (candidate != null) {
if (candidate.getAnnotation(annotationClass) != null) {
return true;
}
}
} catch (NoSuchFieldException ex) {
continue;
}
}

for (Field field : domainObj.getClass().getFields()) {
if (field.getAnnotation(annotationClass) != null) {
return true;
}
}

Field field = findFieldWithAnnotation(domainObj, annotationClass);
if (field != null && field.getAnnotation(annotationClass) != null) {
return true;
}

return false;
}

Expand Down Expand Up @@ -112,65 +95,33 @@ protected static Field getField(Class<?> type, String fieldName) {
public static Class<?> getFieldWithAnnotationType(Object domainObj, Class<? extends Annotation> annotationClass)
throws SecurityException, BeansException {
Class<?> type = null;
BeanWrapper wrapper = new BeanWrapperImpl(domainObj);
PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
String prop = descriptor.getName();
Field theField = null;
try {
theField = domainObj.getClass().getDeclaredField(prop);
if (theField != null && theField.getAnnotation(annotationClass) != null) {
type = theField.getType();
break;
}
} catch (NoSuchFieldException ex) {
continue;
}
}

if (type == null) {
for (Field field : domainObj.getClass().getFields()) {
if (field.getAnnotation(annotationClass) != null) {
try {
type = field.getType();
} catch (IllegalArgumentException iae) {}
}
}
}

Field field = findFieldWithAnnotation(domainObj, annotationClass);
if (field != null && field.getAnnotation(annotationClass) != null) {
type = field.getType();
}

return type;
}

public static Object getFieldWithAnnotation(Object domainObj, Class<? extends Annotation> annotationClass)
throws SecurityException, BeansException {
Object value = null;
BeanWrapper wrapper = new BeanWrapperImpl(domainObj);
PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
String prop = descriptor.getName();
Field theField = null;
try {
theField = domainObj.getClass().getDeclaredField(prop);
if (theField != null) {
if (theField.getAnnotation(annotationClass) != null) {
value = wrapper.getPropertyValue(prop);
}
}
} catch (NoSuchFieldException ex) {
continue;
}
}

if (value == null) {
for (Field field : domainObj.getClass().getFields()) {
if (field.getAnnotation(annotationClass) != null) {
try {
value = ReflectionUtils.getField(field, domainObj);
} catch (IllegalArgumentException iae) {}
}
}
}

Field field = findFieldWithAnnotation(domainObj, annotationClass);
if (field != null && field.getAnnotation(annotationClass) != null) {
try {
PropertyDescriptor descriptor = org.springframework.beans.BeanUtils.getPropertyDescriptor(domainObj.getClass(), field.getName());
if (descriptor != null) {
BeanWrapper wrapper = new BeanWrapperImpl(domainObj);
value = wrapper.getPropertyValue(field.getName());
} else {
value = ReflectionUtils.getField(field, domainObj);
}
return value;
} catch (IllegalArgumentException iae) {}
}

return value;
}

Expand Down Expand Up @@ -201,30 +152,20 @@ public static void setFieldWithAnnotation(Object domainObj, Class<? extends Anno
* the condition that must be satisfied to allow the match
*/
public static void setFieldWithAnnotationConditionally(Object domainObj, Class<? extends Annotation> annotationClass, Object value, Condition condition) {
BeanWrapper wrapper = new BeanWrapperImpl(domainObj);

for (Field field : domainObj.getClass().getFields()) {
if (field.getAnnotation(annotationClass) != null && condition.matches(field)) {
try {
ReflectionUtils.setField(field, domainObj, value);
} catch (IllegalArgumentException iae) {}
}
}

PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
for (PropertyDescriptor descriptor : descriptors) {
String prop = descriptor.getName();
Field theField = null;
Field field = findFieldWithAnnotation(domainObj, annotationClass);
if (field != null && field.getAnnotation(annotationClass) != null && condition.matches(field)) {
try {
theField = domainObj.getClass().getDeclaredField(prop);
if (theField != null) {
if (theField.getAnnotation(annotationClass) != null && condition.matches(theField)) {
wrapper.setPropertyValue(prop, value);
}
}
} catch (NoSuchFieldException ex) {
continue;
}
PropertyDescriptor descriptor = org.springframework.beans.BeanUtils.getPropertyDescriptor(domainObj.getClass(), field.getName());
if (descriptor != null) {
BeanWrapper wrapper = new BeanWrapperImpl(domainObj);
wrapper.setPropertyValue(field.getName(), value);
return;
} else {
ReflectionUtils.setField(field, domainObj, value);
}
return;
} catch (IllegalArgumentException iae) {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ public void noop() {
public static class ContentObject {
@MimeType
public String mimeType;


public ContentObject() {}

public ContentObject(String mimeType) {
this.mimeType = mimeType;
}
Expand Down
Loading

0 comments on commit 949a73d

Please sign in to comment.