[DEV] detect empty constructor (one)

This commit is contained in:
Edouard DUPIN 2021-07-08 14:18:29 +02:00
parent 6bd92d1a57
commit a6c90e5515
2 changed files with 35 additions and 11 deletions

View File

@ -14,7 +14,6 @@ A POJO parse is in progress (based on SAX too)
POJO need to implement (TODO list): POJO need to implement (TODO list):
- Enumeration reader / writer
- Immutable reader/writer - Immutable reader/writer
- Record reader/writer - Record reader/writer

View File

@ -36,6 +36,8 @@ public class IntrospectionModelComplex extends IntrospectionModel {
// TODO Optimize this with external object for basic types.... // TODO Optimize this with external object for basic types....
private final Method valueof; // used for the set Text if the object is an end point... private final Method valueof; // used for the set Text if the object is an end point...
private final Method tostring; // used for the set Text if the object is an end point... private final Method tostring; // used for the set Text if the object is an end point...
private final boolean isSubClass; // if true, the constructor must be called with a null first object.
private final Constructor<?> constructorEmpty;
private final List<IntrospectionProperty> nodes = new ArrayList<>(); private final List<IntrospectionProperty> nodes = new ArrayList<>();
private final List<IntrospectionProperty> attributes = new ArrayList<>(); private final List<IntrospectionProperty> attributes = new ArrayList<>();
@ -53,6 +55,14 @@ public class IntrospectionModelComplex extends IntrospectionModel {
public IntrospectionModelComplex(final Class<?> classType) throws ExmlBuilderException { public IntrospectionModelComplex(final Class<?> classType) throws ExmlBuilderException {
super(classType); super(classType);
try { try {
if (classType.getNestHost() == classType) {
this.isSubClass = false;
} else if (!Modifier.isStatic(classType.getModifiers())) {
this.isSubClass = true;
} else {
this.isSubClass = false;
}
final Boolean isDefaultAttribute = getIsDefaultAttribute(classType, IntrospectionModel.DEFAULT_ATTRIBUTE); final Boolean isDefaultAttribute = getIsDefaultAttribute(classType, IntrospectionModel.DEFAULT_ATTRIBUTE);
final Boolean isDefaultManaged = getIsDefaultManaged(classType, IntrospectionModel.DEFAULT_MANAGED); final Boolean isDefaultManaged = getIsDefaultManaged(classType, IntrospectionModel.DEFAULT_MANAGED);
final Boolean isDefaultOptional = getIsDefaultOptional(classType, IntrospectionModel.DEFAULT_OPTIONAL); final Boolean isDefaultOptional = getIsDefaultOptional(classType, IntrospectionModel.DEFAULT_OPTIONAL);
@ -60,9 +70,27 @@ public class IntrospectionModelComplex extends IntrospectionModel {
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 + ")");
Constructor<?> emptyConstructorTmp = null;
for (final Constructor<?> elem : constructors) { for (final Constructor<?> elem : constructors) {
Log.verbose(" - " + elem.toGenericString()); // we does not manage private field
if (!Modifier.isPublic(elem.getModifiers())) {
continue;
}
if (this.isSubClass) {
if (elem.getParameterCount() == 1) {
emptyConstructorTmp = elem;
Log.verbose(" >>> " + elem.toGenericString());
} else {
Log.verbose(" - " + elem.toGenericString());
}
} else if (elem.getParameterCount() == 0) {
emptyConstructorTmp = elem;
Log.verbose(" >>> " + elem.toGenericString());
} else {
Log.verbose(" - " + elem.toGenericString());
}
} }
this.constructorEmpty = emptyConstructorTmp;
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) {
@ -347,18 +375,15 @@ public class IntrospectionModelComplex extends IntrospectionModel {
@Override @Override
public Object createObject(final Map<String, Object> properties, final Map<String, List<Object>> nodes) throws ExmlBuilderException { public Object createObject(final Map<String, Object> properties, final Map<String, List<Object>> nodes) throws ExmlBuilderException {
Object tmp; Object tmp;
if (this.constructorEmpty == null) {
throw new ExmlBuilderException("No constructor accessible for class: " + this.classType.getCanonicalName());
}
try { try {
// pb here, can not create a primitive object with the correct elements... ==> must be generated with a siblist of elements if (this.isSubClass) {
Constructor<?>[] constructors = this.classType.getConstructors();
//Log.critical("Modifiers : " + Modifier.toString(this.classType.getModifiers()) + " ==> " + this.classType.getNestHost());
if (this.classType.getNestHost() == this.classType) {
// if nest class is identical ==> native class declaration.
tmp = constructors[0].newInstance();
} else if (!Modifier.isStatic(this.classType.getModifiers())) {
Object tmp2 = null; Object tmp2 = null;
tmp = constructors[0].newInstance(tmp2); tmp = this.constructorEmpty.newInstance(tmp2);
} else { } else {
tmp = constructors[0].newInstance(); tmp = this.constructorEmpty.newInstance();
} }
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block