All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nakedobjects.nof.reflect.java.Methods Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package org.nakedobjects.nof.reflect.java;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.nakedobjects.applib.DomainObjectContainer;
import org.nakedobjects.nof.core.util.ToString;
import org.nakedobjects.nof.reflect.peer.ReflectionException;

public final class Methods {
    private static final Logger LOG = Logger.getLogger(Methods.class);
    
    private Methods() {}

    public static void injectContainer(
            Object object,
            final Class cls,
            DomainObjectContainer container) {
        invokeSetMethod(cls, object, "setContainer", DomainObjectContainer.class, container);
        invokeSetMethod(cls, object, "set_Container", DomainObjectContainer.class, container);
        invokeSetMethod(cls, object, "setDomainObjectContainer", DomainObjectContainer.class, container);
        invokeSetMethod(cls, object, "set_DomainObjectContainer", DomainObjectContainer.class, container);
    }

    
    public static void injectServices(final Object object, final Class cls, Object[] services) {
        for (int i = 0; i < services.length; i++) {
            Class serviceClass = services[i].getClass();

            Method[] methods = cls.getMethods();
            for (int j = 0; j < methods.length; j++) {
                if (methods[j].getName().startsWith("set")) {
                    Class[] parameterTypes = methods[j].getParameterTypes();
                    if (parameterTypes.length == 1 &&  parameterTypes[0] != Object.class && parameterTypes[0].isAssignableFrom(serviceClass)) {
                        invokeSetMethod(methods[j], object, services[i]);
                        LOG.debug("injected service " + services[i] + " into " + new ToString(object));
                    }
                }
            }
        }
    }

    public static void invokeMethod(Method method, final Object target, final Object[] parameters) {
        try {
            method.invoke(target, parameters);
        } catch (SecurityException e) {
            throw new ReflectionException("Cannot access the " + method.getName() + " method in " + target.getClass().getName());
        } catch (IllegalArgumentException e1) {
            throw new ReflectionException(e1);
        } catch (IllegalAccessException e1) {
            throw new ReflectionException("Cannot access the " + method.getName() + " method in " + target.getClass().getName());
        } catch (InvocationTargetException e) {
            Throwable targetException = e.getTargetException();
            if (targetException instanceof RuntimeException) {
                throw (RuntimeException) targetException;
            } else {
                throw new ReflectionException(targetException);
            }
        }
    }

    public static void invokeSetMethod(
            final Class cls,
            final Object target,
            final String methodName,
            final Class parameterType,
            final Object parameter) {
        try {
            Class[] parameterTypes = new Class[] { parameterType };
            Method set = cls.getMethod(methodName, parameterTypes);
            invokeSetMethod(set, target, parameter);
        } catch (NoSuchMethodException ignore) {
            /*
             * If there is no such method then it will not be called.
             */
        }
    }

    private static void invokeSetMethod(Method set, final Object target, final Object parameter) {
        Object[] parameters = new Object[] { parameter };
        invokeMethod(set, target, parameters);
        LOG.debug("injected " + parameter + " into " + new ToString(target));
    }
}


// Copyright (c) Naked Objects Group Ltd.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy