[DEV] detect empty constructor (one)
This commit is contained in:
parent
6bd92d1a57
commit
a6c90e5515
@ -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
|
||||
|
||||
|
@ -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<IntrospectionProperty> nodes = 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 {
|
||||
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) {
|
||||
// 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<String, Object> properties, final Map<String, List<Object>> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user