org.sfm.reflect.ClassMeta Maven / Gradle / Ivy
package org.sfm.reflect;
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.asm.ConstructorDefinition;
import org.sfm.reflect.asm.ConstructorParameter;
public final class ClassMeta {
private final List> properties;
private final List> constructorProperties;
private final List> constructorDefinitions;
private final String prefix;
private final ReflectionService reflectService;
public ClassMeta(Class target, ReflectionService reflectService) throws MapperBuildingException {
this(null, target, reflectService);
}
public ClassMeta(String prefix, Class target, ReflectionService reflectService) throws MapperBuildingException {
this.prefix = prefix;
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(addPrefix(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(!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(addPrefix(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(addPrefix(field.getName()), reflectService, field));
propertiesSet.add(name);
}
}
}
currentClass = currentClass.getSuperclass();
}
return properties;
}
private String addPrefix(String propertyName) {
return prefix == null ? propertyName : prefix + propertyName;
}
public List> getConstructorDefinitions() {
return constructorDefinitions;
}
public List> getProperties() {
return properties;
}
public List> getConstructorProperties() {
return constructorProperties;
}
public ReflectionService getReflectionService() {
return reflectService;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy