mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
Add Period support via CompoundTypePeriod, rename server/reflect to server/properties
This commit is contained in:
1 parent
98f6e1e6af
commit
07cdb69a5b
27 files changed
+501
-333
No files matched your search
@@ -0,0 +1,45 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Determines the properties on a given bean.
|
||||
*/
|
||||
public class BeanPropertiesReader {
|
||||
|
||||
private final Map<String,Integer> propertyIndexMap = new HashMap<String,Integer>();
|
||||
|
||||
private final String[] props;
|
||||
|
||||
public BeanPropertiesReader(Class<?> clazz) {
|
||||
this.props = getProperties(clazz);
|
||||
for (int i=0; i<props.length; i++) {
|
||||
propertyIndexMap.put(props[i], Integer.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
public String[] getProperties() {
|
||||
return props;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return Arrays.toString(props);
|
||||
}
|
||||
|
||||
public Integer getPropertyIndex(String property) {
|
||||
return propertyIndexMap.get(property);
|
||||
}
|
||||
|
||||
private String[] getProperties(Class<?> clazz) {
|
||||
try {
|
||||
Field field = clazz.getField("_ebean_props");
|
||||
return (String[]) field.get(null);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Error getting _ebean_props field on type "+clazz, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
|
||||
/**
|
||||
* The getter implementation for a given bean property.
|
||||
*/
|
||||
public interface BeanPropertyGetter {
|
||||
|
||||
/**
|
||||
* Return the value of a given bean property.
|
||||
*/
|
||||
public Object get(EntityBean bean);
|
||||
|
||||
public Object getIntercept(EntityBean bean);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
/**
|
||||
* Provides getter setter and construction methods for beans.
|
||||
* <p>
|
||||
* This enables the implementation to use standard reflection or
|
||||
* code generation.
|
||||
* </p>
|
||||
*/
|
||||
public interface BeanPropertyInfo {
|
||||
|
||||
/**
|
||||
* Create an EntityBean for this type.
|
||||
*/
|
||||
public Object createEntityBean();
|
||||
|
||||
/**
|
||||
* Return the getter for a given bean property.
|
||||
*/
|
||||
public BeanPropertyGetter getGetter(String name, int position);
|
||||
|
||||
/**
|
||||
* Return the setter for a given bean property.
|
||||
*/
|
||||
public BeanPropertySetter getSetter(String name, int position);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
|
||||
/**
|
||||
* Creates BeanReflect object used to provide getter setter and construction
|
||||
* for the beans.
|
||||
*/
|
||||
public interface BeanPropertyInfoFactory {
|
||||
|
||||
/**
|
||||
* Create the BeanReflect for the given plain bean and its EntityBean equivalent.
|
||||
*/
|
||||
public BeanPropertyInfo create(Class<?> entityBeanType);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
|
||||
/**
|
||||
* The setter for a given bean property.
|
||||
*/
|
||||
public interface BeanPropertySetter {
|
||||
|
||||
/**
|
||||
* Set the property value of a bean.
|
||||
*/
|
||||
public void set(EntityBean bean, Object value);
|
||||
|
||||
/**
|
||||
* Set the property value of a bean with interception checks.
|
||||
* <p>
|
||||
* This could invoke lazy loading and or oldValues creation.
|
||||
* </p>
|
||||
*/
|
||||
public void setIntercept(EntityBean bean, Object value);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
|
||||
/**
|
||||
* A BeanReflect implementation based on the enhancement that creates EntityBean
|
||||
* implementations.
|
||||
*/
|
||||
public final class EnhanceBeanPropertyInfo implements BeanPropertyInfo {
|
||||
|
||||
private static final Object[] constuctorArgs = new Object[0];
|
||||
|
||||
private final Constructor<?> constructor;
|
||||
|
||||
public EnhanceBeanPropertyInfo(Class<?> clazz) {
|
||||
try {
|
||||
if (Modifier.isAbstract(clazz.getModifiers())) {
|
||||
this.constructor = null;
|
||||
} else {
|
||||
this.constructor = defaultConstructor(clazz);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new PersistenceException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Constructor<?> defaultConstructor(Class<?> cls) {
|
||||
try {
|
||||
Class<?>[] params = new Class[0];
|
||||
return cls.getDeclaredConstructor(params);
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Object createEntityBean() {
|
||||
try {
|
||||
return constructor.newInstance(constuctorArgs);
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public BeanPropertyGetter getGetter(String name, int position) {
|
||||
return new Getter(position);
|
||||
}
|
||||
|
||||
public BeanPropertySetter getSetter(String name, int position) {
|
||||
return new Setter(position);
|
||||
}
|
||||
|
||||
static final class Getter implements BeanPropertyGetter {
|
||||
|
||||
private final int fieldIndex;
|
||||
|
||||
Getter(int fieldIndex) {
|
||||
this.fieldIndex = fieldIndex;
|
||||
}
|
||||
|
||||
public Object get(EntityBean bean) {
|
||||
return bean._ebean_getField(fieldIndex);
|
||||
}
|
||||
|
||||
public Object getIntercept(EntityBean bean) {
|
||||
return bean._ebean_getFieldIntercept(fieldIndex);
|
||||
}
|
||||
}
|
||||
|
||||
static final class Setter implements BeanPropertySetter {
|
||||
|
||||
private final int fieldIndex;
|
||||
|
||||
Setter(int fieldIndex) {
|
||||
this.fieldIndex = fieldIndex;
|
||||
}
|
||||
|
||||
public void set(EntityBean bean, Object value) {
|
||||
bean._ebean_setField(fieldIndex, value);
|
||||
}
|
||||
|
||||
public void setIntercept(EntityBean bean, Object value) {
|
||||
bean._ebean_setFieldIntercept(fieldIndex, value);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.avaje.ebeaninternal.server.properties;
|
||||
|
||||
/**
|
||||
* Creates a BeanReflectFactory based on the enhancement that
|
||||
* creates EntityBean implementations.
|
||||
*/
|
||||
public final class EnhanceBeanPropertyInfoFactory implements BeanPropertyInfoFactory {
|
||||
|
||||
public BeanPropertyInfo create(Class<?> entityBeanType) {
|
||||
return new EnhanceBeanPropertyInfo(entityBeanType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
|
||||
<TITLE>Bean Reflection</TITLE>
|
||||
</HEAD>
|
||||
<Body BGCOLOR="#ffffff">
|
||||
Bean reflection Implementation
|
||||
|
||||
<p>
|
||||
Abstracts the bean property getter and setter methods.
|
||||
Allows Code generation or reflection to be used.
|
||||
</p>
|
||||
|
||||
</Body>
|
||||
</HTML>
|
||||
Reference in new issue
Block a user