1 52 53 package freemarker.template.utility; 54 55 import java.util.List ; 56 import freemarker.ext.beans.BeansWrapper; 57 import freemarker.template.*; 58 import freemarker.template.utility.ClassUtil; 59 60 73 74 public class ObjectConstructor implements TemplateMethodModelEx 75 { 76 public Object exec(List args) throws TemplateModelException { 77 if (args.isEmpty()) { 78 throw new TemplateModelException("This method must have at least one argument, the name of the class to instantiate."); 79 } 80 String classname = args.get(0).toString(); 81 Class cl = null; 82 try { 83 cl = ClassUtil.forName(classname); 84 } 85 catch (Exception e) { 86 throw new TemplateModelException(e.getMessage()); 87 } 88 BeansWrapper bw = BeansWrapper.getDefaultInstance(); 89 Object obj = bw.newInstance(cl, args.subList(1, args.size())); 90 return bw.wrap(obj); 91 } 92 } 93 | Popular Tags |