} else {
Class<?> targetType;
try {
targetType = classLoader.loadClass(typeName);
} catch (ClassNotFoundException ex) {
OpenDataException odex = new OpenDataException("Cannot load target type: " + typeName);
odex.initCause(ex);
throw odex;
}
Constructor<?> ctor = null;
boolean isDefaultCtor = false;
for (Constructor<?> aux : targetType.getConstructors()) {
isDefaultCtor = aux.getParameterTypes().length == 0;
if (isDefaultCtor) {
ctor = aux;
break;
} else if (aux.getAnnotation(ConstructorProperties.class) != null) {
ctor = aux;
}
}
IllegalStateAssertion.assertNotNull(ctor, "Cannot mxbean compliant constructor for: " + targetType.getName());
try {
if (isDefaultCtor) {
result = ctor.newInstance((Object[]) null);
for (String key : ctype.keySet()) {
OpenType<?> itemType = ctype.getType(key);
Object itemValue = cdata.get(key);
Object javaValue = fromOpenData(itemType, classLoader, itemValue);
invokeSetter(result, key, javaValue);
}
} else {
List<Object> params = new ArrayList<>();
ConstructorProperties props = ctor.getAnnotation(ConstructorProperties.class);
for (String key : props.value()) {
OpenType<?> itemType = ctype.getType(key);
Object itemValue = cdata.get(key);
Object javaValue = fromOpenData(itemType, classLoader, itemValue);
params.add(javaValue);
}
Class<?>[] paramTypes = ctor.getParameterTypes();
for (Object param : params) {
int index = params.indexOf(param);
Class<?> paramType = paramTypes[index];
param = toTargetType(paramType, param);
if (param != params.get(index)) {
params.set(index, param);
}
}
result = ctor.newInstance(params.toArray());
}
} catch (Exception ex) {
OpenDataException odex = new OpenDataException("Cannot construct object from: " + cdata);
odex.initCause(ex);
throw odex;
}
}
return result;
}