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

org.sfm.reflect.meta.ObjectClassMeta Maven / Gradle / Ivy

package org.sfm.reflect.meta;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.sfm.map.MapperBuildingException;
import org.sfm.reflect.ReflectionService;
import org.sfm.reflect.SetterHelper;
import org.sfm.reflect.asm.ConstructorDefinition;
import org.sfm.reflect.asm.ConstructorParameter;

public final class ObjectClassMeta implements ClassMeta {
	
	private final List> properties;
	private final List> constructorProperties;
	private final List> constructorDefinitions;

	
	private final ReflectionService reflectService;
	private final Class target;

	private final Map fieldAliases;
	
	public ObjectClassMeta(Class target, ReflectionService reflectService) throws MapperBuildingException {
		this.reflectService = reflectService;
		if (reflectService.isAsmPresent()) {
			try {
				this.constructorDefinitions = ConstructorDefinition.extractConstructors(target);
				this.constructorProperties = Collections.unmodifiableList(listProperties(constructorDefinitions));
			} catch(Exception e) {
				throw new MapperBuildingException(e.getMessage(), e);
			}
		} else {
			this.constructorDefinitions = null;
			this.constructorProperties = null;
		}
		this.fieldAliases = Collections.unmodifiableMap(aliases(reflectService, target));
		this.properties = Collections.unmodifiableList(listProperties(reflectService, target));
		this.target = target;
	}
	
	private Map aliases(final ReflectionService reflectService, Class target) {
		final Map map = new HashMap();
		
		ClassVisitor.visit(target, new FielAndMethodCallBack() {
			@Override
			public void method(Method method) {
				String alias = reflectService.getColumnName(method);
				if (alias != null) {
					map.put(SetterHelper.getPropertyNameFromMethodName(method.getName()), alias);
				}
			}
			
			@Override
			public void field(Field field) {
				String alias = reflectService.getColumnName(field);
				if (alias != null) {
					map.put(field.getName(), alias);
				}
			}
		});
		
		return map;
	}

	private List> listProperties(List> constructorDefinitions) {
		if (constructorDefinitions == null) return null;
		
		Set properties = new HashSet();
		List> constructorProperties = new ArrayList>();
		for(ConstructorDefinition cd : constructorDefinitions) {
			for(ConstructorParameter param : cd.getParameters()) {
				String paramName = param.getName();
				if (!properties.contains(paramName)) {
					constructorProperties.add(new ConstructorPropertyMeta(paramName, paramName, reflectService, param));
					properties.add(paramName);
				}
			}
		}
		return constructorProperties;
	}

	private List> listProperties(final ReflectionService reflectService, Class target) {
		final List> properties = new ArrayList>();
		final Set propertiesSet = new HashSet();
		
		ClassVisitor.visit(target, new FielAndMethodCallBack() {
			@Override
			public void method(Method method) {
				final String name = method.getName();
				if (SetterHelper.methodModifiersMatches(method.getModifiers()) && SetterHelper.isSetter(name)) {
					final String propertyName = SetterHelper.getPropertyNameFromMethodName(name);
					if (!propertiesSet.contains(propertyName)) {
						properties.add(new MethodPropertyMeta(propertyName, getAlias(propertyName), reflectService, method));
						propertiesSet.add(propertyName);
					}
				}
			}
			
			@Override
			public void field(Field field) {
				final String name = field.getName();
				if (SetterHelper.fieldModifiersMatches(field.getModifiers())) {
					if (!propertiesSet.contains(name)) {
						properties.add(new FieldPropertyMeta(field.getName(), getAlias(name), reflectService, field));
						propertiesSet.add(name);
					}
				}
			}
		});
		
		return properties;
	}


	private String getAlias(String propertyName) {
		String columnName = this.fieldAliases.get(propertyName);
		if (columnName == null) {
			columnName = propertyName;
		}
		return columnName;
	}

	List> getConstructorDefinitions() {
		return constructorDefinitions;
	}

	List> getProperties() {
		return properties;
	}

	List> getConstructorProperties() {
		return constructorProperties;
	}

	@Override
	public ReflectionService getReflectionService() {
		return reflectService;
	}

	@Override
	public PropertyFinder newPropertyFinder() {
		return new ObjectPropertyFinder(this);
	}

	public Class getTargetClass() {
		return target;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy