net.sf.cglib.core.ReflectUtils Java Examples

The following examples show how to use net.sf.cglib.core.ReflectUtils. 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: AddInitTransformer.java    From cglib with Apache License 2.0 5 votes vote down vote up
public AddInitTransformer(Method method) {
    info = ReflectUtils.getMethodInfo(method);
    
    Type[] types = info.getSignature().getArgumentTypes();
    if (types.length != 1 ||
    !types[0].equals(Constants.TYPE_OBJECT) ||
    !info.getSignature().getReturnType().equals(Type.VOID_TYPE)) {
        throw new IllegalArgumentException(method + " illegal signature");
    }
}
 
Example #3
Source File: MixinEverythingEmitter.java    From cglib with Apache License 2.0 5 votes vote down vote up
protected Class[] getInterfaces(Class[] classes) {
    List list = new ArrayList();
    for (int i = 0; i < classes.length; i++) {
        ReflectUtils.addAllInterfaces(classes[i], list);
    }
    return (Class[])list.toArray(new Class[list.size()]);
}
 
Example #4
Source File: CallbackHelper.java    From cglib with Apache License 2.0 5 votes vote down vote up
public Class[] getCallbackTypes()
{
    if (callbacks.size() == 0)
        return new Class[0];
    if (callbacks.get(0) instanceof Callback) {
        return ReflectUtils.getClasses(getCallbacks());
    } else {
        return (Class[])callbacks.toArray(new Class[callbacks.size()]);
    }
}
 
Example #5
Source File: TestTransformingLoader.java    From cglib with Apache License 2.0 5 votes vote down vote up
public void testAddStatic() throws Exception {
    Method m = ReflectUtils.findMethod("net.sf.cglib.transform.impl.TestTransformingLoader.initStatic(Class)");
    ClassTransformer t = new AddStaticInitTransformer(m);
    // t = new ClassTransformerChain(new ClassTransformer[]{ t, new ClassTransformerTee(new org.objectweb.asm.util.TraceClassVisitor(null, new java.io.PrintWriter(System.out))) });
    Class loaded = loadHelper(t, Example.class);
    Object obj = loaded.newInstance();
    // TODO
}
 
Example #6
Source File: TestBeanGenerator.java    From cglib with Apache License 2.0 5 votes vote down vote up
public void testSimple() throws Exception {
    BeanGenerator bg = new BeanGenerator();
    bg.addProperty("sin", Double.TYPE);
    Object bean = bg.create();
    
    PropertyDescriptor[] pds = ReflectUtils.getBeanProperties(bean.getClass());
    assertTrue(pds.length == 1);
    assertTrue(pds[0].getName().equals("sin"));
    assertTrue(pds[0].getPropertyType().equals(Double.TYPE));
}
 
Example #7
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 #8
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 #9
Source File: DalTransactionalEnabler.java    From dal with Apache License 2.0 4 votes vote down vote up
private void replaceBeanDefinition() {
    for (String beanName : getBeanDefinitionNames()) {
        BeanDefinition beanDef = getBeanDefinition(beanName);
        String beanClassName = beanDef.getBeanClassName();

        if (beanClassName == null || beanClassName.equals(BEAN_FACTORY_NAME))
            continue;

        Class beanClass;
        try {
            beanClass = Class.forName(beanDef.getBeanClassName());
        } catch (ClassNotFoundException e) {
            throw new BeanDefinitionValidationException("Cannot validate bean: " + beanName, e);
        }

        boolean annotated = false;

        List<Method> methods = new ArrayList<>();
        ReflectUtils.addAllMethods(beanClass, methods);
        List<Method> unsupportedMethods = new ArrayList<>();

        for (int i = 0; i < methods.size(); i++) {
            Method currentMethod = methods.get(i);

            if (isTransactionAnnotated(currentMethod)) {
                if (Modifier.isFinal(currentMethod.getModifiers()) || Modifier.isStatic(currentMethod.getModifiers()) || Modifier.isPrivate(currentMethod.getModifiers()))
                    unsupportedMethods.add(currentMethod);
                annotated = true;
            }
        }

        if (!unsupportedMethods.isEmpty()){
            StringBuilder errMsg=new StringBuilder();
            errMsg.append(String.format("The Methods below are not supported in dal transaction due to private, final or static modifier, please use public,protected or default modifier instead:"));
            errMsg.append("\n");
            int index=1;
            for (Method method : unsupportedMethods) {
                errMsg.append(String.format("%d. %s", index, method.toString()));
                errMsg.append("\n");
                index++;
            }
            throw new DalRuntimeException(errMsg.toString());
        }

        if (!annotated)
            continue;

        beanDef.setBeanClassName(BEAN_FACTORY_NAME);
        beanDef.setFactoryMethodName(FACTORY_METHOD_NAME);

        ConstructorArgumentValues cav = beanDef.getConstructorArgumentValues();

        if (cav.getArgumentCount() != 0)
            throw new BeanDefinitionValidationException("The transactional bean can only be instantiated with default constructor.");

        cav.addGenericArgumentValue(beanClass.getName());
    }
}
 
Example #10
Source File: MixinBeanEmitter.java    From cglib with Apache License 2.0 4 votes vote down vote up
protected Method[] getMethods(Class type) {
    return ReflectUtils.getPropertyMethods(ReflectUtils.getBeanProperties(type), true, true);
}
 
Example #11
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 #12
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);
}