1 43 44 package org.jfree.xml.factory.objects; 45 46 import java.lang.reflect.Constructor ; 47 import java.lang.reflect.Modifier ; 48 49 import org.jfree.util.ObjectUtilities; 50 51 56 public class ClassLoaderObjectDescription extends AbstractObjectDescription { 57 58 61 public ClassLoaderObjectDescription() { 62 super(Object .class); 63 setParameterDefinition("class", String .class); 64 } 65 66 71 public Object createObject() { 72 try { 73 final String o = (String ) getParameter("class"); 74 return ObjectUtilities.getClassLoader(getClass()).loadClass(o).newInstance(); 75 } 76 catch (Exception e) { 77 return null; 78 } 79 } 80 81 89 public void setParameterFromObject(final Object o) throws ObjectFactoryException { 90 if (o == null) { 91 throw new ObjectFactoryException("The Object is null."); 92 } 93 try { 94 final Constructor c = o.getClass().getConstructor(new Class [0]); 95 if (!Modifier.isPublic(c.getModifiers())) { 96 throw new ObjectFactoryException 97 ("The given object has no public default constructor. [" + o.getClass() + "]"); 98 } 99 setParameter("class", o.getClass().getName()); 100 } 101 catch (Exception e) { 102 throw new ObjectFactoryException 103 ("The given object has no default constructor. [" + o.getClass() + "]", e); 104 } 105 } 106 } 107 | Popular Tags |