Java Code Examples for net.sf.cglib.core.ReflectUtils#newInstance()

The following examples show how to use net.sf.cglib.core.ReflectUtils#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseConfigAction.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the command this Action will use.  This method uses the
 * config value:
 *
 * web.com.redhat.rhn.frontend.action.satellite.GeneralConfigAction.command
 *
 * to determine a dynamic classname to use to instantiate the
 * ConfigureSatelliteCommand. This can be useful if you want to
 * specify a different class to use for the Command at runtime.
 *
 * @param currentUser who is requesting this config.
 * @return ConfigureSatelliteCommand instance
 */
protected SatelliteConfigurator getCommand(User currentUser) {
    if (logger.isDebugEnabled()) {
        logger.debug("getCommand(User currentUser=" + currentUser + ") - start");
    }

    String className = getCommandClassName();

    try {
        Class c = Class.forName(className);
        Class[] paramTypes = new Class[1];
        paramTypes[0] = User.class;
        Object[] args = new Object[1];
        args[0] = currentUser;

        SatelliteConfigurator sc = (SatelliteConfigurator)
            ReflectUtils.newInstance(c, paramTypes, args);

        if (logger.isDebugEnabled()) {
            logger.debug("getCommand(User) - end - return value=" + sc);
        }
        return sc;
    }
    catch (ClassNotFoundException e) {
        logger.error("getCommand(User)", e);

        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: TestEnhancer.java    From cglib with Apache License 2.0 5 votes vote down vote up
public void testArgInit() throws Throwable{

         Enhancer e = new Enhancer();
         e.setSuperclass(ArgInit.class);
         e.setCallbackType(MethodInterceptor.class);
         Class f = e.createClass();
         ArgInit a = (ArgInit)ReflectUtils.newInstance(f,
                                                       new Class[]{ String.class },
                                                       new Object[]{ "test" });
         assertEquals("test", a.toString());
         ((Factory)a).setCallback(0, TEST_INTERCEPTOR);
         assertEquals("test", a.toString());

         Callback[] callbacks = new Callback[]{ TEST_INTERCEPTOR };
         ArgInit b = (ArgInit)((Factory)a).newInstance(new Class[]{ String.class },
                                                       new Object[]{ "test2" },
                                                       callbacks);
         assertEquals("test2", b.toString());
         try{
             ((Factory)a).newInstance(new Class[]{  String.class, String.class },
                                      new Object[]{"test"},
                                      callbacks);
             fail("must throw exception");
         }catch( IllegalArgumentException iae ){
         
         }
    }
 
Example 3
Source File: BaseConfigAction.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the command this Action will use.  This method uses the
 * config value:
 *
 * web.com.redhat.rhn.frontend.action.satellite.GeneralConfigAction.command
 *
 * to determine a dynamic classname to use to instantiate the
 * ConfigureSatelliteCommand. This can be useful if you want to
 * specify a different class to use for the Command at runtime.
 *
 * @param currentUser who is requesting this config.
 * @return ConfigureSatelliteCommand instance
 */
protected SatelliteConfigurator getCommand(User currentUser) {
    if (logger.isDebugEnabled()) {
        logger.debug("getCommand(User currentUser=" + currentUser + ") - start");
    }

    String className = getCommandClassName();

    try {
        Class c = Class.forName(className);
        Class[] paramTypes = new Class[1];
        paramTypes[0] = User.class;
        Object[] args = new Object[1];
        args[0] = currentUser;

        SatelliteConfigurator sc = (SatelliteConfigurator)
            ReflectUtils.newInstance(c, paramTypes, args);

        if (logger.isDebugEnabled()) {
            logger.debug("getCommand(User) - end - return value=" + sc);
        }
        return sc;
    }
    catch (ClassNotFoundException e) {
        logger.error("getCommand(User)", e);

        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: TestEnhancer.java    From cglib with Apache License 2.0 4 votes vote down vote up
private static ArgInit newArgInit(Class clazz, String value) {
    return (ArgInit)ReflectUtils.newInstance(clazz,
                                             new Class[]{ String.class },
                                             new Object[]{ value });
}
 
Example 5
Source File: TestGenerator.java    From cglib with Apache License 2.0 4 votes vote down vote up
protected Object firstInstance(Class type) throws Exception {
    return ReflectUtils.newInstance(type);
}