[DEV] rework bean register model

This commit is contained in:
Edouard DUPIN 2021-07-12 21:04:21 +02:00
parent b9fcbc6780
commit 095a705d4b
9 changed files with 300 additions and 252 deletions

View File

@ -37,18 +37,27 @@ public class IntrospectionModelComplex extends IntrospectionModel {
private final boolean isSubClass; // if true, the constructor must be called with a null first object. private final boolean isSubClass; // if true, the constructor must be called with a null first object.
private final Constructor<?> constructorEmpty; private final Constructor<?> constructorEmpty;
private final List<ConstructorModel> constructors = new ArrayList<>(); private final List<ConstructorModel> constructors = new ArrayList<>();
private final List<IntrospectionProperty> nodes = new ArrayList<>(); private final List<IntrospectionProperty> elements = new ArrayList<>();
private final List<IntrospectionProperty> attributes = new ArrayList<>();
@Override @Override
public List<IntrospectionProperty> getNodes() { public List<IntrospectionProperty> getNodes() {
return this.nodes; List<IntrospectionProperty> out = new ArrayList<>();
for (IntrospectionProperty elem : elements) {
if (!elem.isAttribute()) {
out.add(elem);
}
}
return out;
} }
@Override @Override
public List<IntrospectionProperty> getAttributes() { public List<IntrospectionProperty> getAttributes() {
return this.attributes; List<IntrospectionProperty> out = new ArrayList<>();
for (IntrospectionProperty elem : elements) {
if (elem.isAttribute()) {
out.add(elem);
}
}
return out;
} }
// private boolean checkIfOneIsNull(Boolean[] values, int offset) { // private boolean checkIfOneIsNull(Boolean[] values, int offset) {
@ -86,10 +95,6 @@ public class IntrospectionModelComplex extends IntrospectionModel {
Log.critical("Detect primitive ==> impossible case !!! "); Log.critical("Detect primitive ==> impossible case !!! ");
} }
final Boolean isDefaultAttribute = ReflectTools.getIsDefaultAttribute(classType, IntrospectionModel.DEFAULT_ATTRIBUTE);
final Boolean isDefaultManaged = ReflectTools.getIsDefaultManaged(classType, IntrospectionModel.DEFAULT_MANAGED);
final Boolean isDefaultOptional = ReflectTools.getIsDefaultOptional(classType, IntrospectionModel.DEFAULT_OPTIONAL);
final Boolean isDefaultCaseSensitive = ReflectTools.getIsDefaultCaseSensitive(classType, IntrospectionModel.DEFAULT_CASE_SENSITIVE);
Log.verbose("Introspect class: '" + classType.getCanonicalName() + "'"); Log.verbose("Introspect class: '" + classType.getCanonicalName() + "'");
final Constructor<?>[] constructors = this.classType.getConstructors(); final Constructor<?>[] constructors = this.classType.getConstructors();
Log.verbose(" Constructors: (" + constructors.length + ")"); Log.verbose(" Constructors: (" + constructors.length + ")");
@ -198,9 +203,6 @@ public class IntrospectionModelComplex extends IntrospectionModel {
} }
} }
} }
final Field[] fields = this.classType.getFields(); final Field[] fields = this.classType.getFields();
Log.verbose(" Fields: (" + fields.length + ")"); Log.verbose(" Fields: (" + fields.length + ")");
for (final Field elem : fields) { for (final Field elem : fields) {
@ -209,22 +211,41 @@ public class IntrospectionModelComplex extends IntrospectionModel {
continue; continue;
} }
// we does not manage private field // we does not manage private field
// NOTE: if the field is private I do not check the elements ==> the user want a private API !!! (maybe change ...)
if (!Modifier.isPublic(elem.getModifiers())) { if (!Modifier.isPublic(elem.getModifiers())) {
continue; continue;
} }
final Boolean isAttribute = ReflectTools.getIsAttribute(elem, isDefaultAttribute); final String[] names = ReflectTools.getNames(elem, null);
final Boolean isManaged = ReflectTools.getIsManaged(elem, isDefaultManaged);
final Boolean isOptionnal = ReflectTools.getIsOptional(elem, isDefaultOptional);
final String[] names = ReflectTools.getNames(elem, Tools.decapitalizeFirst(elem.getName()));
final Boolean caseSensitive = ReflectTools.getIsCaseSensitive(elem, isDefaultCaseSensitive);
final String listName = ReflectTools.getListName(elem, null); final String listName = ReflectTools.getListName(elem, null);
// TODO check if property does not already exist ... Class<?>[] types = ReflectTools.getTypeField(elem);
if (isManaged) { IntrospectionProperty prop = new IntrospectionProperty(Tools.decapitalizeFirst(elem.getName()), types, names);
if (isAttribute) { this.elements.add(prop);
this.attributes.add(new IntrospectionPropertyField(elem, names, listName, caseSensitive, isOptionnal));
} else { final Boolean isAttribute = ReflectTools.getIsAttribute(elem, null);
this.nodes.add(new IntrospectionPropertyField(elem, names, listName, caseSensitive, isOptionnal)); if (isAttribute != null) {
} prop.setAttribute(isAttribute);
}
final Boolean isManaged = ReflectTools.getIsManaged(elem, null);
if (isManaged != null) {
prop.setManaged(isManaged);
}
final Boolean isOptionnal = ReflectTools.getIsOptional(elem, null);
if (isOptionnal != null) {
prop.setOptionnal(isOptionnal);
}
final Boolean isCaseSensitive = ReflectTools.getIsCaseSensitive(elem, null);
if (isCaseSensitive != null) {
prop.setCaseSensitive(isCaseSensitive);
}
// generate getter and setter with field.
IntrospectionPropertyField modifier = new IntrospectionPropertyField(elem);
// if the field is final ==> we can not set the value...
if (Modifier.isFinal(elem.getModifiers())) {
prop.setGetter(modifier);
} else {
prop.setSetter(modifier);
prop.setGetter(modifier);
} }
Log.verbose(" - " + elem.toGenericString()); Log.verbose(" - " + elem.toGenericString());
} }
@ -431,9 +452,9 @@ public class IntrospectionModelComplex extends IntrospectionModel {
final String listNameGet = ReflectTools.getListName(elem.getter, null); final String listNameGet = ReflectTools.getListName(elem.getter, null);
final String listName = listNameSet != null? listNameSet: listNameGet; final String listName = listNameSet != null? listNameSet: listNameGet;
if (isAttribute) { if (isAttribute) {
this.attributes.add(new IntrospectionPropertyMethod(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptional)); this.attributes.add(new IntrospectionPropertyMethodGetter(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptional));
} else { } else {
this.nodes.add(new IntrospectionPropertyMethod(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptional)); this.nodes.add(new IntrospectionPropertyMethodGetter(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptional));
} }
} else { } else {
Boolean isManaged = null; Boolean isManaged = null;
@ -466,17 +487,24 @@ public class IntrospectionModelComplex extends IntrospectionModel {
} }
if (isManaged) { if (isManaged) {
if (isAttribute) { if (isAttribute) {
IntrospectionPropertyMethod tmpp = new IntrospectionPropertyMethod(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptionnal); IntrospectionPropertyMethodGetter tmpp = new IntrospectionPropertyMethodGetter(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptionnal);
tmpp.setCanBeSetByConstructor(recordAllPossibleValuesFiltered.contains(elem.name)); tmpp.setCanBeSetByConstructor(recordAllPossibleValuesFiltered.contains(elem.name));
this.attributes.add(tmpp); this.attributes.add(tmpp);
} else { } else {
IntrospectionPropertyMethod tmpp = new IntrospectionPropertyMethod(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptionnal); IntrospectionPropertyMethodGetter tmpp = new IntrospectionPropertyMethodGetter(elem.setter, elem.getter, names, listName, isCaseSensitive, isOptionnal);
tmpp.setCanBeSetByConstructor(recordAllPossibleValuesFiltered.contains(elem.name)); tmpp.setCanBeSetByConstructor(recordAllPossibleValuesFiltered.contains(elem.name));
this.nodes.add(tmpp); this.nodes.add(tmpp);
} }
} }
} }
} }
// Set only at the end ==> no need before...
final Boolean isDefaultAttribute = ReflectTools.getIsDefaultAttribute(classType, IntrospectionModel.DEFAULT_ATTRIBUTE);
final Boolean isDefaultManaged = ReflectTools.getIsDefaultManaged(classType, IntrospectionModel.DEFAULT_MANAGED);
final Boolean isDefaultOptional = ReflectTools.getIsDefaultOptional(classType, IntrospectionModel.DEFAULT_OPTIONAL);
final Boolean isDefaultCaseSensitive = ReflectTools.getIsDefaultCaseSensitive(classType, IntrospectionModel.DEFAULT_CASE_SENSITIVE);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
throw new ExmlBuilderException("Error in creating introspection data ... " + ex.getMessage()); throw new ExmlBuilderException("Error in creating introspection data ... " + ex.getMessage());
@ -912,17 +940,3 @@ public class IntrospectionModelComplex extends IntrospectionModel {
} }
} }
class OrderData {
public Method getter = null;
public final String name;
public String[] names = null;
public Method setter = null;
public Field field = null;
public Boolean isAttribute = null;
public Boolean isOptionnal = null;
public Boolean isManaged = null;
public OrderData(final String name) {
this.name = name;
}
}

View File

@ -7,18 +7,39 @@ import java.util.List;
import org.atriasoft.eStringSerialize.StringSerializer; import org.atriasoft.eStringSerialize.StringSerializer;
import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlBuilderException;
public abstract class IntrospectionProperty { public final class IntrospectionProperty {
protected final Boolean caseSensitive; // if the element is not managed by us (set at null while not define by a function attribute, parameter ...)
protected final Boolean isOptionnal; private Boolean managed = null;
// name that can take the Node // Case sensitive in parsing XML or not (set at null while not define by a function attribute, parameter ...)
protected final String[] names; private Boolean caseSensitive = null;
// Optional or not (set at null while not define by a function attribute, parameter ...)
private Boolean optionnal = null;
// Attribute or Node (set at null while not define by a function attribute, parameter ...)
private Boolean attribute = null;
// name of the field or the function before renaming...
private final String beanName;
// names that can take the Node or the aatibute
private String[] names = null;
// if organized in sublist (!= null) then the subNode have this value // if organized in sublist (!= null) then the subNode have this value
protected final String listName; private String listName = null;
private boolean canBeSetByConstructor = false;
private final Class<?> type;
private final Class<?> subType;
// can get the property, if null not gettable ... ==> TODO need to remove this property ???
// First function call
// second field access
IntrospectionPropertyGetter getter = null;
// can get the property, if null not settable (otherwise use the constructor???)
// First constructor call
// second function call
// third field access
IntrospectionPropertySetter setter = null;
public String getListName() { public String getListName() {
return this.listName; return this.listName;
} }
protected boolean canBeSetByConstructor = false;
public boolean isCanBeSetByConstructor() { public boolean isCanBeSetByConstructor() {
return this.canBeSetByConstructor; return this.canBeSetByConstructor;
} }
@ -26,21 +47,28 @@ public abstract class IntrospectionProperty {
public void setCanBeSetByConstructor(final boolean canBeSetByConstructor) { public void setCanBeSetByConstructor(final boolean canBeSetByConstructor) {
this.canBeSetByConstructor = canBeSetByConstructor; this.canBeSetByConstructor = canBeSetByConstructor;
} }
protected final Class<?> type;
protected final Class<?> subType;
public IntrospectionProperty(final Class<?>[] type, final String[] names, final String listName, final Boolean caseSensitive, final Boolean isOptionnal) { public IntrospectionProperty(final String beanName, final Class<?>[] type, final String[] names) {
this.beanName = beanName;
this.type = type[0]; this.type = type[0];
this.subType = type[1]; this.subType = type[1];
this.names = names; this.names = names;
this.listName = listName;
this.caseSensitive = caseSensitive;
this.isOptionnal = isOptionnal;
} }
public abstract boolean canGetValue(); public void setSetter(IntrospectionPropertySetter setter) {
this.setter = setter;
}
public void setGetter(IntrospectionPropertyGetter getter) {
this.getter = getter;
}
public abstract boolean canSetValue(); public boolean canGetValue() {
return this.getter != null;
}
public boolean canSetValue() {
return this.canBeSetByConstructor || this.setter != null;
}
public String[] getNames() { public String[] getNames() {
return this.names; return this.names;
@ -54,32 +82,11 @@ public abstract class IntrospectionProperty {
return this.subType; return this.subType;
} }
public String getValueString(final Object object) throws ExmlBuilderException { public Object getValue(Object object) throws ExmlBuilderException {
Object value = getValue(object); if (getter != null) {
if (value == null) { return this.getter.getValue(object);
return null;
} }
if (StringSerializer.contains(this.type)) { throw new ExmlBuilderException("Property: " + names + " have no getter");
return StringSerializer.toString(value);
}
if (value instanceof Collection) {
ArrayList<Object> data = new ArrayList<>((Collection<?>)value);
StringBuilder out = new StringBuilder();
for (int iii=0; iii<data.size(); iii++) {
if (iii != 0) {
out.append(";");
}
out.append(data.get(iii).toString());
}
return out.toString();
}
return value.toString();
}
public abstract Object getValue(Object object) throws ExmlBuilderException;
public boolean isCaseSensitive() {
return this.caseSensitive;
} }
public boolean isCompatible(String name) { public boolean isCompatible(String name) {
@ -105,35 +112,44 @@ public abstract class IntrospectionProperty {
* @param value Value to set in the Object * @param value Value to set in the Object
* @throws Exception An error occurred * @throws Exception An error occurred
*/ */
public abstract void setExistingValue(Object object, Object value) throws ExmlBuilderException; public void setExistingValue(Object object, Object value) throws ExmlBuilderException {
if (setter != null) {
/** this.setter.setValue(object, value);
* Create a value adapted to the property type.
* @apiNote generic type is transformed byte -> Byte, int -> Integer ...
* @param value Value to set in the Object
* @throws Exception An error occurred
* @return The object created
*/
protected Object createValue(final String value) throws ExmlBuilderException {
try {
if (StringSerializer.contains(this.type)) {
// TODO This might be a deprecated code ....
return StringSerializer.valueOf(this.type, value);
}
if (this.type.isEnum()) {
} else if ((this.type != List.class) || !StringSerializer.contains(this.subType)) {
throw new ExmlBuilderException("Can not parse the specific element ... need to introspect and find the 'xxx valueOf(String data);'");
}
ArrayList<Object> out = new ArrayList<>();
for (String elem : value.split(";")) {
out.add(StringSerializer.valueOf(this.subType, elem));
}
return out;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ExmlBuilderException("Error in parsing the property value ... " + e.getMessage());
} }
throw new ExmlBuilderException("Property: " + names + " have no setter");
} }
public Boolean isCaseSensitive() {
return caseSensitive;
}
public void setCaseSensitive(Boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public Boolean isOptionnal() {
return optionnal;
}
public void setOptionnal(Boolean optionnal) {
this.optionnal = optionnal;
}
public Boolean isAttribute() {
return attribute;
}
public void setAttribute(Boolean attribute) {
this.attribute = attribute;
}
public String getBeanName() {
return beanName;
}
public void setNames(String[] names) {
this.names = names;
}
public void setListName(String listName) {
this.listName = listName;
}
public Boolean getManaged() {
return managed;
}
public void setManaged(Boolean managed) {
this.managed = managed;
}
} }

View File

@ -7,44 +7,17 @@ import java.lang.reflect.Type;
import org.atriasoft.exml.exception.ExmlBuilderException; import org.atriasoft.exml.exception.ExmlBuilderException;
public class IntrospectionPropertyField extends IntrospectionProperty { public class IntrospectionPropertyField implements IntrospectionPropertyGetter, IntrospectionPropertySetter {
private final Field fieldDescription; private final Field fieldDescription;
private final boolean finalValue; private final boolean finalValue;
private static Class<?>[] getTypeField(final Field fieldDescription) {
Class<?> type = fieldDescription.getType();
Class<?> subType = null;
Type empppe = fieldDescription.getGenericType();
if (empppe instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
try {
subType = Class.forName(realType[0].getTypeName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return new Class<?>[] {type, subType};
}
public IntrospectionPropertyField(final Field fieldDescription, final String[] names, final String listName, final Boolean caseSensitive, final Boolean isOptionnal) { public IntrospectionPropertyField(final Field fieldDescription) {
super(IntrospectionPropertyField.getTypeField(fieldDescription), names, listName, caseSensitive, isOptionnal);
this.fieldDescription = fieldDescription; this.fieldDescription = fieldDescription;
this.finalValue = Modifier.isFinal(fieldDescription.getModifiers()); this.finalValue = Modifier.isFinal(fieldDescription.getModifiers());
} }
@Override
public boolean canGetValue() {
return true;
}
@Override
public boolean canSetValue() {
return this.canBeSetByConstructor || !this.finalValue;
}
@Override @Override
public Object getValue(final Object object) throws ExmlBuilderException { public Object getValue(final Object object) throws ExmlBuilderException {
try { try {
@ -55,10 +28,11 @@ public class IntrospectionPropertyField extends IntrospectionProperty {
throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); throw new ExmlBuilderException("Can not set value ... " + e.getMessage());
} }
} }
@Override @Override
public void setExistingValue(final Object object, final Object value) throws ExmlBuilderException { public void setValue(final Object object, final Object value) throws ExmlBuilderException {
if (this.finalValue) {
throw new ExmlBuilderException("Can not set value The value is FINAL!!!");
}
try { try {
this.fieldDescription.set(object, value); this.fieldDescription.set(object, value);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (IllegalArgumentException | IllegalAccessException e) {

View File

@ -0,0 +1,7 @@
package org.atriasoft.exml.builder;
import org.atriasoft.exml.exception.ExmlBuilderException;
public interface IntrospectionPropertyGetter {
Object getValue(Object object) throws ExmlBuilderException;
}

View File

@ -1,106 +0,0 @@
package org.atriasoft.exml.builder;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.atriasoft.exml.exception.ExmlBuilderException;
import org.atriasoft.exml.internal.Log;
public class IntrospectionPropertyMethod extends IntrospectionProperty {
private static Class<?>[] getTypefunction(final Method setter, final Method getter) throws Exception {
Class<?> type = null;
Class<?> subType = null;
if (setter == null && getter == null) {
// impossible case...
throw new Exception("kjhkhjkj");
}
if (getter != null) {
type = getter.getReturnType();
if (Enum.class.isAssignableFrom(type)) {
Log.verbose("Find an enum ...");
} else {
Type empppe = getter.getGenericReturnType();
if (empppe instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
subType = Class.forName(realType[0].getTypeName());
}
}
}
}
if (setter != null) {
if (type != null && setter.getParameters()[0].getType() != type) {
throw new Exception("The type of the setter ands the type return by the getter are not the same ...");
}
type = setter.getParameters()[0].getType();
if (List.class.isAssignableFrom(type)) {
Class<?> internalModelClass = null;
Type[] empppe = setter.getGenericParameterTypes();
if (empppe.length > 0) {
if (empppe[0] instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
Log.warning(" -->> " + realType[0]);
internalModelClass = Class.forName(realType[0].getTypeName());
}
}
}
if (getter!=null && internalModelClass != subType) {
throw new Exception("The type of the setter and the type return by the getter are not the same ...");
}
subType = internalModelClass;
}
}
return new Class<?>[] {type, subType};
}
protected Method setter;
protected Method getter;
public IntrospectionPropertyMethod(final Method setter, final Method getter, final String[] names, final String listName, final Boolean isCaseSensitive, final Boolean isOptional) throws Exception {
super(IntrospectionPropertyMethod.getTypefunction(setter, getter), names, listName, isCaseSensitive, isOptional);
this.setter = setter;
this.getter = getter;
}
@Override
public boolean canGetValue() {
return this.getter != null;
}
@Override
public boolean canSetValue() {
return this.canBeSetByConstructor || this.setter != null;
}
@Override
public Object getValue(final Object object) throws ExmlBuilderException {
if (this.getter == null) {
throw new ExmlBuilderException("no getter availlable");
}
try {
return this.getter.invoke(object);
} catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
throw new ExmlBuilderException("Can not set value ... " + e.getMessage());
}
}
@Override
public void setExistingValue(final Object object, final Object value) throws ExmlBuilderException {
if (this.setter == null) {
throw new ExmlBuilderException("no setter availlable");
}
try {
this.setter.invoke(object, value);
} catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ExmlBuilderException("Can not set value ... " + e.getMessage());
}
}
}

View File

@ -0,0 +1,78 @@
package org.atriasoft.exml.builder;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.atriasoft.exml.exception.ExmlBuilderException;
import org.atriasoft.exml.internal.Log;
public class IntrospectionPropertyMethodGetter implements IntrospectionPropertyGetter {
// private static Class<?>[] getTypefunction(final Method setter, final Method getter) throws Exception {
// Class<?> type = null;
// Class<?> subType = null;
// if (setter == null && getter == null) {
// // impossible case...
// throw new Exception("kjhkhjkj");
// }
// if (getter != null) {
// type = getter.getReturnType();
// if (Enum.class.isAssignableFrom(type)) {
// Log.verbose("Find an enum ...");
// } else {
// Type empppe = getter.getGenericReturnType();
// if (empppe instanceof ParameterizedType plopppppp) {
// Type[] realType = plopppppp.getActualTypeArguments();
// if (realType.length > 0) {
// subType = Class.forName(realType[0].getTypeName());
// }
// }
// }
// }
// if (setter != null) {
// if (type != null && setter.getParameters()[0].getType() != type) {
// throw new Exception("The type of the setter ands the type return by the getter are not the same ...");
// }
// type = setter.getParameters()[0].getType();
// if (List.class.isAssignableFrom(type)) {
// Class<?> internalModelClass = null;
// Type[] empppe = setter.getGenericParameterTypes();
// if (empppe.length > 0) {
// if (empppe[0] instanceof ParameterizedType plopppppp) {
// Type[] realType = plopppppp.getActualTypeArguments();
// if (realType.length > 0) {
// Log.warning(" -->> " + realType[0]);
// internalModelClass = Class.forName(realType[0].getTypeName());
// }
// }
// }
// if (getter!=null && internalModelClass != subType) {
// throw new Exception("The type of the setter and the type return by the getter are not the same ...");
// }
// subType = internalModelClass;
// }
// }
// return new Class<?>[] {type, subType};
// }
//
protected Method getter;
public IntrospectionPropertyMethodGetter(final Method getter) throws Exception {
this.getter = getter;
}
@Override
public Object getValue(final Object object) throws ExmlBuilderException {
if (this.getter == null) {
throw new ExmlBuilderException("no getter availlable");
}
try {
return this.getter.invoke(object);
} catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
throw new ExmlBuilderException("Can not set value ... " + e.getMessage());
}
}
}

View File

@ -0,0 +1,32 @@
package org.atriasoft.exml.builder;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.atriasoft.exml.exception.ExmlBuilderException;
import org.atriasoft.exml.internal.Log;
public class IntrospectionPropertyMethodSetter implements IntrospectionPropertySetter {
protected Method setter;
public IntrospectionPropertyMethodSetter(final Method setter) throws Exception {
this.setter = setter;
}
@Override
public void setValue(final Object object, final Object value) throws ExmlBuilderException {
if (this.setter == null) {
throw new ExmlBuilderException("no setter availlable");
}
try {
this.setter.invoke(object, value);
} catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new ExmlBuilderException("Can not set value ... " + e.getMessage());
}
}
}

View File

@ -0,0 +1,13 @@
package org.atriasoft.exml.builder;
import org.atriasoft.exml.exception.ExmlBuilderException;
public interface IntrospectionPropertySetter {
/**
* set the specific string value in the specified object
* @param object Object to add the value
* @param value Value to set in the Object
* @throws Exception An error occurred
*/
void setValue(Object object, Object value) throws ExmlBuilderException;
}

View File

@ -5,6 +5,8 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.atriasoft.exml.annotation.XmlAttribute; import org.atriasoft.exml.annotation.XmlAttribute;
import org.atriasoft.exml.annotation.XmlCaseSensitive; import org.atriasoft.exml.annotation.XmlCaseSensitive;
@ -21,6 +23,24 @@ import org.atriasoft.exml.exception.ExmlBuilderException;
public class ReflectTools { public class ReflectTools {
private ReflectTools() {} private ReflectTools() {}
public static Class<?>[] getTypeField(final Field fieldDescription) {
Class<?> type = fieldDescription.getType();
Class<?> subType = null;
Type empppe = fieldDescription.getGenericType();
if (empppe instanceof ParameterizedType plopppppp) {
Type[] realType = plopppppp.getActualTypeArguments();
if (realType.length > 0) {
try {
subType = Class.forName(realType[0].getTypeName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return new Class<?>[] {type, subType};
}
public static Boolean getIsCaseSensitive(final Field element, final Boolean defaultValue) throws ExmlBuilderException { public static Boolean getIsCaseSensitive(final Field element, final Boolean defaultValue) throws ExmlBuilderException {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(XmlCaseSensitive.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(XmlCaseSensitive.class);
if (annotation.length == 0) { if (annotation.length == 0) {