/** * Create a new object given a class (not an interface). To create an object from interfaces * use createObject(Set classes) or simpleCreateObject(Class). * * @param clazz the class of the object to instantiate * @param <C> The type of the object that is expected * @return the object * @throws IllegalArgumentException if an error occurs */ public static <C extends FastPathObject> C createObject(Class<C> clazz) { C retval = null; try { retval = clazz.newInstance(); } catch (Exception e) { IllegalArgumentException e2 = new IllegalArgumentException(); e2.initCause(e); throw e2; } if (retval instanceof Factory) { ((Factory) retval).setCallback(0, new DynamicBean()); } return retval; }
/** * Create a new object given a class (not an interface). To create an object from interfaces * use createObject(Set classes) or simpleCreateObject(Class). * * @param clazz the class of the object to instantiate * @param <C> The type of the object that is expected * @return the object * @throws IllegalArgumentException if an error occurs */ public static <C extends FastPathObject> C createObject(Class<C> clazz) { C retval = null; try { retval = clazz.newInstance(); } catch (Exception e) { IllegalArgumentException e2 = new IllegalArgumentException(); e2.initCause(e); throw e2; } if (retval instanceof Factory) { ((Factory) retval).setCallback(0, new DynamicBean()); } return retval; }
/** * Create a DynamicBean * * @param clazz the class to extend * @param inter the interfaces to implement * @return the DynamicBean */ public static FastPathObject create(Class<? extends FastPathObject> clazz, Class<?> [] inter) { if ((clazz != null) && clazz.isInterface()) { throw new IllegalArgumentException("clazz must not be an interface"); } // If Enhancer.create() called with a null class it will alter java.lang.Object // this causes a security exception if run with Kaffe JRE //if ( clazz == null) { // clazz = DynamicBean.class; //} Callback[] callbacks = {new DynamicBean(), NoOp.INSTANCE}; Enhancer e = new Enhancer(); e.setSuperclass(clazz); e.setInterfaces(inter); e.setCallbackFilter(FINALIZE_FILTER); e.setCallbacks(callbacks); return (FastPathObject) e.create(); }
/** * Create a DynamicBean * * @param clazz the class to extend * @param inter the interfaces to implement * @return the DynamicBean */ public static FastPathObject create(Class<? extends FastPathObject> clazz, Class<?> [] inter) { if ((clazz != null) && clazz.isInterface()) { throw new IllegalArgumentException("clazz must not be an interface"); } // If Enhancer.create() called with a null class it will alter java.lang.Object // this causes a security exception if run with Kaffe JRE //if ( clazz == null) { // clazz = DynamicBean.class; //} Callback[] callbacks = {new DynamicBean(), NoOp.INSTANCE}; Enhancer e = new Enhancer(); e.setSuperclass(clazz); e.setInterfaces(inter); e.setCallbackFilter(FINALIZE_FILTER); e.setCallbacks(callbacks); return (FastPathObject) e.create(); }
public void testConstructors() throws Exception { Class c = DynamicUtil.composeClass(Company.class, Broke.class); Company obj = (Company) c.newInstance(); ((net.sf.cglib.proxy.Factory) obj).setCallback(0, new DynamicBean()); obj.setName("Fred"); assertEquals("Fred", obj.getName()); }