1 package org.nanocontainer.script.groovy; 2 3 import org.picocontainer.ComponentAdapter; 4 import org.picocontainer.MutablePicoContainer; 5 6 import java.util.Collection ; 7 import java.util.Iterator ; 8 9 20 public class GroovyScriptGenerator { 21 public String generateScript(MutablePicoContainer pico) { 24 StringBuffer groovy = new StringBuffer (); 25 groovy.append("pico = new org.nanocontainer.reflection.DefaultNanoPicoContainer()\n"); 26 27 Collection componentAdapters = pico.getComponentAdapters(); 28 for (Iterator iterator = componentAdapters.iterator(); iterator.hasNext();) { 29 ComponentAdapter componentAdapter = (ComponentAdapter) iterator.next(); 30 31 Object componentKey = componentAdapter.getComponentKey(); 32 String groovyKey = null; 33 if (componentKey instanceof Class ) { 34 groovyKey = ((Class ) componentKey).getName(); 35 } else if (componentKey instanceof String ) { 36 groovyKey = "\"" + componentKey + "\""; 37 } 38 39 Object componentInstance = componentAdapter.getComponentInstance(pico); 40 41 if (componentInstance instanceof String ) { 42 groovy.append("pico.registerComponentInstance(" + groovyKey + ", \"" + componentInstance + "\")\n"); 43 } else { 44 groovy.append("pico.registerComponentImplementation(" + groovyKey + ", " + componentInstance.getClass().getName() + ")\n"); 45 } 46 } 47 return groovy.toString(); 48 } 49 } | Popular Tags |