From a6c90e55156fdefa9d3444454ab55fc6a965229c Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 8 Jul 2021 14:18:29 +0200 Subject: [PATCH] [DEV] detect empty constructor (one) --- README.md | 1 - .../builder/IntrospectionModelComplex.java | 45 ++++++++++++++----- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0f83f32..0ecb711 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ A POJO parse is in progress (based on SAX too) POJO need to implement (TODO list): - - Enumeration reader / writer - Immutable reader/writer - Record reader/writer diff --git a/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java b/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java index 1a669cb..ca4d123 100644 --- a/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java +++ b/src/org/atriasoft/exml/builder/IntrospectionModelComplex.java @@ -36,6 +36,8 @@ public class IntrospectionModelComplex extends IntrospectionModel { // 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 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 nodes = new ArrayList<>(); private final List attributes = new ArrayList<>(); @@ -53,6 +55,14 @@ public class IntrospectionModelComplex extends IntrospectionModel { public IntrospectionModelComplex(final Class classType) throws ExmlBuilderException { super(classType); 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 isDefaultManaged = getIsDefaultManaged(classType, IntrospectionModel.DEFAULT_MANAGED); final Boolean isDefaultOptional = getIsDefaultOptional(classType, IntrospectionModel.DEFAULT_OPTIONAL); @@ -60,9 +70,27 @@ public class IntrospectionModelComplex extends IntrospectionModel { Log.verbose("Introspect class: '" + classType.getCanonicalName() + "'"); final Constructor[] constructors = this.classType.getConstructors(); Log.verbose(" Constructors: (" + constructors.length + ")"); + Constructor emptyConstructorTmp = null; 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(); Log.verbose(" Fields: (" + fields.length + ")"); for (final Field elem : fields) { @@ -347,18 +375,15 @@ public class IntrospectionModelComplex extends IntrospectionModel { @Override public Object createObject(final Map properties, final Map> nodes) throws ExmlBuilderException { Object tmp; + if (this.constructorEmpty == null) { + throw new ExmlBuilderException("No constructor accessible for class: " + this.classType.getCanonicalName()); + } try { - // pb here, can not create a primitive object with the correct elements... ==> must be generated with a siblist of elements - 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())) { + if (this.isSubClass) { Object tmp2 = null; - tmp = constructors[0].newInstance(tmp2); + tmp = this.constructorEmpty.newInstance(tmp2); } else { - tmp = constructors[0].newInstance(); + tmp = this.constructorEmpty.newInstance(); } } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { // TODO Auto-generated catch block