All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sklsft.commons.mapper.beans.MappableBeanFactory Maven / Gradle / Ivy

There is a newer version: 5.0.0-M1
Show newest version
package org.sklsft.commons.mapper.beans;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MappableBeanFactory {

	public static  MappableBean getMappableBean(Class beanClass) {
		MappableBean bean = new MappableBean();
		bean.beanClass = beanClass;
		bean.accessibleFields = getAccessibleFields(beanClass);
		bean.accessibleFieldsMap = getAccessibleFieldsMap(bean.accessibleFields);
		return bean;
	}

	private static List getAccessibleFields(Class beanClass) {
		List result = new ArrayList<>();
		
		Field[] fields = beanClass.getDeclaredFields();
		for (Field field:fields) {
			try {
				PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), beanClass);
				if (propertyDescriptor.getReadMethod() != null && propertyDescriptor.getWriteMethod() != null) {
					result.add(new AccessibleField(field, propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod()));
				}
			} catch (IntrospectionException e) {
				//Nothing to do
			}
		}
		
		return result;
	}
	
	private static Map getAccessibleFieldsMap(List accessibleFields) {
		Map result = new HashMap();
		
		for (AccessibleField field:accessibleFields) {
			result.put(field.field.getName(),field);
		}
		
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy