Performance Optimization in CLI mode

refs #30
- Abort object building if all params found
This commit is contained in:
Fabian Becker 2014-11-02 13:39:58 +01:00
parent 2a2162c5da
commit 29a1bb64e3

View File

@ -110,13 +110,18 @@ public final class OptimizationBuilder {
} else {
Class<?>[] params = new Class[0];
try {
Constructor constr = clazz.getConstructor(params);
instance = (T)constr.newInstance(null);
Constructor constructor = clazz.getConstructor(params);
instance = (T)constructor.newInstance(null);
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
}
}
/* No need to continue if there are no parameters to set */
if (tree.isEmpty()) {
return instance;
}
BeanInfo info;
try {
if (clazz.isInterface()) {
@ -125,6 +130,7 @@ public final class OptimizationBuilder {
info = Introspector.getBeanInfo(clazz, Object.class);
}
PropertyDescriptor[] properties = info.getPropertyDescriptors();
int foundParameters = 0;
for (PropertyDescriptor pd : properties) {
String name = pd.getName();
Method getter = pd.getReadMethod();
@ -149,6 +155,7 @@ public final class OptimizationBuilder {
* If the tree contains this property we try to set it on the object.
*/
if (tree.containsKey(name)) {
foundParameters++;
Object obj;
if (type.isPrimitive() && ((ArgumentTree)tree.get(name)).getValue() != null) {
obj = BeanInspector.stringToPrimitive((String)((ArgumentTree) tree.get(name)).getValue(), type);
@ -173,6 +180,11 @@ public final class OptimizationBuilder {
BeanInspector.callIfAvailable(instance, setter.getName(), new Object[]{obj});
}
}
// If we configured all parameters in the tree we can break the loop
if (tree.size() == foundParameters) {
break;
}
}
} catch (IntrospectionException ex) {
ex.printStackTrace();