Skip to content

Commit

Permalink
Get rid of JavaBeans and BeanUtils fot make it compatible with Android
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejch committed Oct 9, 2020
1 parent 121859d commit c8e04cb
Showing 1 changed file with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@

package kentico.kontent.delivery;

import com.madrobot.beans.IntrospectionException;
import com.madrobot.beans.PropertyDescriptor;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.ScanResult;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConstructorUtils;
import org.apache.commons.beanutils.PropertyUtilsBean;

import java.beans.PropertyDescriptor;
import java.lang.reflect.*;
import java.util.*;

Expand Down Expand Up @@ -162,13 +160,16 @@ <T> T convert(ContentItem item, Map<String, ContentItem> linkedItems, Class<T> t
for (Field field : fields) {
Object value = getValueForField(item, linkedItems, bean, field);
if (value != null) {
BeanUtils.setProperty(bean, field.getName(), value);
com.madrobot.beans.BeanInfo beanInfo = com.madrobot.beans.Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
Optional<PropertyDescriptor> propertyDescriptor = Arrays.stream(properties).filter(descriptor -> descriptor.getName().equals(field.getName())).findFirst();

if(propertyDescriptor.isPresent()) {
propertyDescriptor.get().getWriteMethod().invoke(bean, value);
}
}
}
} catch (NoSuchMethodException |
IllegalAccessException |
InvocationTargetException |
InstantiationException e) {
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException | IntrospectionException e) {
handleReflectionException(e);
}
//Return bean
Expand Down Expand Up @@ -303,21 +304,24 @@ private static Object castCollection(Class<?> type, Map<String, ?> items) {

private static Type getType(Object bean, Field field) {
//Because of type erasure, we will find the setter method and get the generic types off it's arguments
PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
PropertyDescriptor propertyDescriptor = null;
Optional<PropertyDescriptor> propertyDescriptor = null;

try {
propertyDescriptor = propertyUtils.getPropertyDescriptor(bean, field.getName());
} catch (IllegalAccessException |
InvocationTargetException |
NoSuchMethodException e) {
handleReflectionException(e);
com.madrobot.beans.BeanInfo beanInfo = com.madrobot.beans.Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
propertyDescriptor = Arrays.stream(properties).filter(descriptor -> descriptor.getName().equals(field.getName())).findFirst();

} catch (IntrospectionException e) {
e.printStackTrace();
}
if (propertyDescriptor == null) {

if(!propertyDescriptor.isPresent()){
//Likely no accessors
log.debug("Property descriptor for object {} with field {} is null", bean, field);
return null;
}
Method writeMethod = propertyUtils.getWriteMethod(propertyDescriptor);

Method writeMethod = propertyDescriptor.get().getWriteMethod();
if (writeMethod == null) {
log.debug("No write method for property {}", propertyDescriptor);
return null;
Expand All @@ -332,7 +336,37 @@ private static Type getType(Object bean, Field field) {
String.format("%s#%s", bean.getClass().getSimpleName(), field.getName()));

return type;

//
// java.beans.PropertyDescriptor propertyDescriptor = null;
//
// PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
// try {
// propertyDescriptor = propertyUtils.getPropertyDescriptor(bean, field.getName());
// } catch (IllegalAccessException |
// InvocationTargetException |
// NoSuchMethodException e) {
// handleReflectionException(e);
// }
// if (propertyDescriptor == null) {
// //Likely no accessors
// log.debug("Property descriptor for object {} with field {} is null", bean, field);
// return null;
// }
// Method writeMethod = propertyUtils.getWriteMethod(propertyDescriptor);
// if (writeMethod == null) {
// log.debug("No write method for property {}", propertyDescriptor);
// return null;
// }
// Type[] actualTypeArguments = ((ParameterizedType) writeMethod.getGenericParameterTypes()[0])
// .getActualTypeArguments();
//
// Type type = (Map.class.isAssignableFrom(field.getType())) ? actualTypeArguments[1] : actualTypeArguments[0];
//
// log.debug("Got type {} from {}",
// type.getTypeName(),
// String.format("%s#%s", bean.getClass().getSimpleName(), field.getName()));
//
// return type;
}

private static void handleReflectionException(Exception ex) {
Expand Down

0 comments on commit c8e04cb

Please sign in to comment.