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