From 095a705d4b57900da1c54e187681ea94a2746388 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 12 Jul 2021 21:04:21 +0200 Subject: [PATCH] [DEV] rework bean register model --- .../builder/IntrospectionModelComplex.java | 100 ++++++----- .../exml/builder/IntrospectionProperty.java | 156 ++++++++++-------- .../builder/IntrospectionPropertyField.java | 40 +---- .../builder/IntrospectionPropertyGetter.java | 7 + .../builder/IntrospectionPropertyMethod.java | 106 ------------ .../IntrospectionPropertyMethodGetter.java | 78 +++++++++ .../IntrospectionPropertyMethodSetter.java | 32 ++++ .../builder/IntrospectionPropertySetter.java | 13 ++ .../atriasoft/exml/reflect/ReflectTools.java | 20 +++ 9 files changed, 300 insertions(+), 252 deletions(-) create mode 100644 src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java delete mode 100644 src/org/atriasoft/exml/builder/IntrospectionPropertyMethod.java create mode 100644 src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java create mode 100644 src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java create mode 100644 src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java b/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java index e9a7dc1..8346691 100644 --- a/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java +++ b/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java @@ -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 Constructor constructorEmpty; private final List constructors = new ArrayList<>(); - private final List nodes = new ArrayList<>(); - - private final List attributes = new ArrayList<>(); - + private final List elements = new ArrayList<>(); @Override public List getNodes() { - return this.nodes; + List out = new ArrayList<>(); + for (IntrospectionProperty elem : elements) { + if (!elem.isAttribute()) { + out.add(elem); + } + } + return out; } @Override public List getAttributes() { - return this.attributes; + List out = new ArrayList<>(); + for (IntrospectionProperty elem : elements) { + if (elem.isAttribute()) { + out.add(elem); + } + } + return out; } // private boolean checkIfOneIsNull(Boolean[] values, int offset) { @@ -86,10 +95,6 @@ public class IntrospectionModelComplex extends IntrospectionModel { 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() + "'"); final Constructor[] constructors = this.classType.getConstructors(); Log.verbose(" Constructors: (" + constructors.length + ")"); @@ -198,9 +203,6 @@ public class IntrospectionModelComplex extends IntrospectionModel { } } } - - - final Field[] fields = this.classType.getFields(); Log.verbose(" Fields: (" + fields.length + ")"); for (final Field elem : fields) { @@ -209,22 +211,41 @@ public class IntrospectionModelComplex extends IntrospectionModel { continue; } // 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())) { continue; } - final Boolean isAttribute = ReflectTools.getIsAttribute(elem, isDefaultAttribute); - 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[] names = ReflectTools.getNames(elem, null); final String listName = ReflectTools.getListName(elem, null); - // TODO check if property does not already exist ... - if (isManaged) { - if (isAttribute) { - this.attributes.add(new IntrospectionPropertyField(elem, names, listName, caseSensitive, isOptionnal)); - } else { - this.nodes.add(new IntrospectionPropertyField(elem, names, listName, caseSensitive, isOptionnal)); - } + Class[] types = ReflectTools.getTypeField(elem); + IntrospectionProperty prop = new IntrospectionProperty(Tools.decapitalizeFirst(elem.getName()), types, names); + this.elements.add(prop); + + final Boolean isAttribute = ReflectTools.getIsAttribute(elem, null); + 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()); } @@ -431,9 +452,9 @@ public class IntrospectionModelComplex extends IntrospectionModel { final String listNameGet = ReflectTools.getListName(elem.getter, null); final String listName = listNameSet != null? listNameSet: listNameGet; 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 { - 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 { Boolean isManaged = null; @@ -466,17 +487,24 @@ public class IntrospectionModelComplex extends IntrospectionModel { } if (isManaged) { 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)); this.attributes.add(tmpp); } 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)); 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) { ex.printStackTrace(); 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; - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionProperty.java b/src/org/atriasoft/exml/builder/IntrospectionProperty.java index 4c5f24f..ba05af8 100644 --- a/src/org/atriasoft/exml/builder/IntrospectionProperty.java +++ b/src/org/atriasoft/exml/builder/IntrospectionProperty.java @@ -7,18 +7,39 @@ import java.util.List; import org.atriasoft.eStringSerialize.StringSerializer; import org.atriasoft.exml.exception.ExmlBuilderException; -public abstract class IntrospectionProperty { - protected final Boolean caseSensitive; - protected final Boolean isOptionnal; - // name that can take the Node - protected final String[] names; +public final class IntrospectionProperty { + // if the element is not managed by us (set at null while not define by a function attribute, parameter ...) + private Boolean managed = null; + // Case sensitive in parsing XML or not (set at null while not define by a function attribute, parameter ...) + 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 - 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() { return this.listName; } - protected boolean canBeSetByConstructor = false; - public boolean isCanBeSetByConstructor() { return this.canBeSetByConstructor; } @@ -26,21 +47,28 @@ public abstract class IntrospectionProperty { public void setCanBeSetByConstructor(final boolean 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.subType = type[1]; this.names = names; - this.listName = listName; - this.caseSensitive = caseSensitive; - this.isOptionnal = isOptionnal; + } + + public void setSetter(IntrospectionPropertySetter setter) { + this.setter = setter; + } + public void setGetter(IntrospectionPropertyGetter getter) { + this.getter = getter; } - public abstract boolean canGetValue(); + public boolean canGetValue() { + return this.getter != null; + } - public abstract boolean canSetValue(); + public boolean canSetValue() { + return this.canBeSetByConstructor || this.setter != null; + } public String[] getNames() { return this.names; @@ -54,32 +82,11 @@ public abstract class IntrospectionProperty { return this.subType; } - public String getValueString(final Object object) throws ExmlBuilderException { - Object value = getValue(object); - if (value == null) { - return null; + public Object getValue(Object object) throws ExmlBuilderException { + if (getter != null) { + return this.getter.getValue(object); } - if (StringSerializer.contains(this.type)) { - return StringSerializer.toString(value); - } - if (value instanceof Collection) { - ArrayList data = new ArrayList<>((Collection)value); - StringBuilder out = new StringBuilder(); - for (int iii=0; iii 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 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()); + public void setExistingValue(Object object, Object value) throws ExmlBuilderException { + if (setter != null) { + this.setter.setValue(object, value); } + 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; + } + } \ No newline at end of file diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java index e785fe9..bc8fbe8 100644 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java +++ b/src/org/atriasoft/exml/builder/IntrospectionPropertyField.java @@ -7,44 +7,17 @@ import java.lang.reflect.Type; import org.atriasoft.exml.exception.ExmlBuilderException; -public class IntrospectionPropertyField extends IntrospectionProperty { +public class IntrospectionPropertyField implements IntrospectionPropertyGetter, IntrospectionPropertySetter { private final Field fieldDescription; 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) { - super(IntrospectionPropertyField.getTypeField(fieldDescription), names, listName, caseSensitive, isOptionnal); + public IntrospectionPropertyField(final Field fieldDescription) { this.fieldDescription = fieldDescription; this.finalValue = Modifier.isFinal(fieldDescription.getModifiers()); } - @Override - public boolean canGetValue() { - return true; - } - - @Override - public boolean canSetValue() { - return this.canBeSetByConstructor || !this.finalValue; - } @Override public Object getValue(final Object object) throws ExmlBuilderException { try { @@ -55,10 +28,11 @@ public class IntrospectionPropertyField extends IntrospectionProperty { throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); } } - - @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 { this.fieldDescription.set(object, value); } catch (IllegalArgumentException | IllegalAccessException e) { @@ -67,5 +41,5 @@ public class IntrospectionPropertyField extends IntrospectionProperty { throw new ExmlBuilderException("Can not set value ... " + e.getMessage()); } } - + } diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java new file mode 100644 index 0000000..6f50f86 --- /dev/null +++ b/src/org/atriasoft/exml/builder/IntrospectionPropertyGetter.java @@ -0,0 +1,7 @@ +package org.atriasoft.exml.builder; + +import org.atriasoft.exml.exception.ExmlBuilderException; + +public interface IntrospectionPropertyGetter { + Object getValue(Object object) throws ExmlBuilderException; +} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethod.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethod.java deleted file mode 100644 index 6a31aa4..0000000 --- a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethod.java +++ /dev/null @@ -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()); - } - } -} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java new file mode 100644 index 0000000..6e3371a --- /dev/null +++ b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodGetter.java @@ -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()); + } + } +} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java new file mode 100644 index 0000000..2fe6c93 --- /dev/null +++ b/src/org/atriasoft/exml/builder/IntrospectionPropertyMethodSetter.java @@ -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()); + } + } +} diff --git a/src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java b/src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java new file mode 100644 index 0000000..8ae9eff --- /dev/null +++ b/src/org/atriasoft/exml/builder/IntrospectionPropertySetter.java @@ -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; +} diff --git a/src/org/atriasoft/exml/reflect/ReflectTools.java b/src/org/atriasoft/exml/reflect/ReflectTools.java index c97253e..e9da9d3 100644 --- a/src/org/atriasoft/exml/reflect/ReflectTools.java +++ b/src/org/atriasoft/exml/reflect/ReflectTools.java @@ -5,6 +5,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; 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.XmlCaseSensitive; @@ -21,6 +23,24 @@ import org.atriasoft.exml.exception.ExmlBuilderException; public class 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 { final Annotation[] annotation = element.getDeclaredAnnotationsByType(XmlCaseSensitive.class); if (annotation.length == 0) {