Reflections about Reflection [email protected] Seen Somewhere while(i.hasNext()) { Element courant = (Element)i.next(); contact = courant.getChild("contact").getText(); description = courant.getChild("description").getText(); email = courant.getChild("email").getText(); laboratoire = courant.getChild("laboratoire").getText(); titre = courant.getChild("titre").getText(); groupeEtudiant = courant.getChild("groupeEtudiant").getText(); Projet p = new Projet(titre,description,laboratoire,contact,email,groupeEtudiant); this.lesProjets.addProjet(p); } What About • Meaning • Evolution • (Rename | Add | Remove) of children Patterns? varname = courant.getChild("varname").getText() • 6 times • Recursion? Discussion Improvements? Generic Code • Child nodes processing • Node processing • Document processing Child Node varname = courant.getChild("varname").getText() new Project(varname, ...) ; • Meaning • Get information from node • Set information to object Child Node // p = new Project() ; varname = courant.getChild("varname").getText() ; p.setVarname(varname) ; • Rewriting • Same meaning Child Node value = courant.getChild(childname).getText() ; String methodName = ... ; Method setter = p.getClass().getMethod(methodName, ...) ; setter.invoke(p, ...) ; • Rewriting • Get information from node • Get proper method for object update Node Processing • Considering • A Java class per XML node • Recursive loading of nodes Node Processing Class theClass = Class.forName(courant.getName()) ; Object obj = theClass.newInstance() ; // we already have processChild() // add object to parent list of childs • Instantiate proper class • Process childs Document Processing • Process document root (a node) Conclusion • Balance to find • Code generation • Generic programming • Be lazy :-) That’s all folks!
© Copyright 2026 Paperzz