mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1297 - ENH: Add DtoQuery ... such that we can map native SQL queries automatically into "DTO beans"
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package io.ebeaninternal.server.dto;
|
||||
|
||||
import io.ebeaninternal.server.type.TypeManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.beans.BeanInfo;
|
||||
import java.beans.IntrospectionException;
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Build the DtoMeta for a bean.
|
||||
* <p>
|
||||
* Use TypeManager to map bean property types to ScalarTypes.
|
||||
*/
|
||||
class DtoMetaBuilder {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DtoMetaBuilder.class);
|
||||
|
||||
private final TypeManager typeManager;
|
||||
|
||||
private final Class<?> dtoType;
|
||||
|
||||
private final List<DtoMetaProperty> properties = new ArrayList<>();
|
||||
|
||||
private final List<DtoMetaConstructor> constructorList = new ArrayList<>();
|
||||
|
||||
DtoMetaBuilder(Class<?> dtoType, TypeManager typeManager) {
|
||||
this.dtoType = dtoType;
|
||||
this.typeManager = typeManager;
|
||||
}
|
||||
|
||||
public DtoMeta build() throws IntrospectionException {
|
||||
|
||||
readConstructors();
|
||||
readProperties();
|
||||
|
||||
return new DtoMeta(dtoType, constructorList, properties);
|
||||
}
|
||||
|
||||
private void readProperties() throws IntrospectionException {
|
||||
|
||||
BeanInfo beanInfo = Introspector.getBeanInfo(dtoType);
|
||||
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
|
||||
if (include(propertyDescriptor)) {
|
||||
try {
|
||||
properties.add(new DtoMetaProperty(typeManager, propertyDescriptor, dtoType));
|
||||
} catch (Exception e) {
|
||||
log.debug("exclude on " + dtoType + " property " + propertyDescriptor.getName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void readConstructors() {
|
||||
|
||||
Constructor<?>[] constructors = dtoType.getConstructors();
|
||||
|
||||
for (Constructor<?> constructor : constructors) {
|
||||
try {
|
||||
constructorList.add(new DtoMetaConstructor(typeManager, constructor, dtoType));
|
||||
} catch (Exception e) {
|
||||
// we don't want that constructor
|
||||
log.debug("exclude on " + dtoType + " constructor " + constructor, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean include(PropertyDescriptor property) {
|
||||
return !property.getName().equals("class");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user