In the project we needed a way to create an object after the
parsing of the xml file was done. The planned object for creation was a Project
object which has the basic information about a Catroid project. We focuses on
two possible alternatives to do this. One was use setter methods to populate
the project object, the other was using Java reflection to do it.
We conducted another performance experiment to do this. One method used setters to populate the project object. The other used reflection method invocation to do it. Project object did not have setters already on it. So instead of changing the existing Project code I created a ProjectProxy class for the sake of the test. The test proved that there was no drastic change in performance in the two methods. But we decided to go with reflection because it was useful in many other ways to do repetitive things in a loop.
We conducted another performance experiment to do this. One method used setters to populate the project object. The other used reflection method invocation to do it. Project object did not have setters already on it. So instead of changing the existing Project code I created a ProjectProxy class for the sake of the test. The test proved that there was no drastic change in performance in the two methods. But we decided to go with reflection because it was useful in many other ways to do repetitive things in a loop.
The reflection method was enhanced to set private fields
from the parsed document values. That enabled me to create a project object
successfully without much change to the existing Project class code. I only had
to add a default constructor. The written tests for these tasks passed.
try { project = Project.class.newInstance(); Field[] projectClassFields = Project.class.getDeclaredFields(); for (Field field : projectClassFields) { boolean isCurrentFieldTransient = Modifier.isTransient(field.getModifiers()); if (isCurrentFieldTransient) { continue; } String tagName = extractTagName(field); Object value = null; String className = field.getType().getCanonicalName(); if (className.equals("int")) { value = new Integer(Integer.valueOf(headerValues.get(tagName))); } else if (field.getType().getCanonicalName().equals("java.lang.String")) { value = headerValues.get(tagName); } if (value != null) { field.setAccessible(true); field.set(project, value); } } } catch (Throwable e) { System.err.println(e); }