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.HashSet;
import java.util.List;
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;
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.properties = Collections.unmodifiableList(listProperties(reflectService, target));
}
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, reflectService, param));
properties.add(paramName);
}
}
}
return constructorProperties;
}
private List> listProperties(ReflectionService reflectService, Class> target) {
final List> properties = new ArrayList<>();
final Set propertiesSet = new HashSet();
Class> currentClass = target;
while(currentClass != null && !Object.class.equals(currentClass)) {
for(Method method : currentClass.getDeclaredMethods()) {
final String name = method.getName();
if (SetterHelper.methodModifiersMatches(method.getModifiers()) && SetterHelper.isSetter(name)) {
final String propertyName = name.substring(3,4).toLowerCase() + name.substring(4);
if (!propertiesSet.contains(propertyName)) {
properties.add(new MethodPropertyMeta(propertyName, reflectService, method));
propertiesSet.add(propertyName);
}
}
}
for(Field field : currentClass.getDeclaredFields()) {
final String name = field.getName();
if (SetterHelper.fieldModifiersMatches(field.getModifiers())) {
if (!propertiesSet.contains(name)) {
properties.add(new FieldPropertyMeta(field.getName(), reflectService, field));
propertiesSet.add(name);
}
}
}
currentClass = currentClass.getSuperclass();
}
return properties;
}
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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy