Example usage for java.lang.reflect Method getParameterTypes

List of usage examples for java.lang.reflect Method getParameterTypes

Introduction

In this page you can find the example usage for java.lang.reflect Method getParameterTypes.

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:ch.ralscha.extdirectspring.util.MethodInfo.java

/**
 * Find a method that is annotated with a specific annotation. Starts with the method
 * and goes up to the superclasses of the class.
 *
 * @param method the starting method//from w  ww .  ja  v a  2  s. c om
 * @param annotation the annotation to look for
 * @return the method if there is a annotated method, else null
 */
public static Method findMethodWithAnnotation(Method method, Class<? extends Annotation> annotation) {
    if (method.isAnnotationPresent(annotation)) {
        return method;
    }

    Class<?> cl = method.getDeclaringClass();
    while (cl != null && cl != Object.class) {
        try {
            Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
            if (equivalentMethod.isAnnotationPresent(annotation)) {
                return equivalentMethod;
            }
        } catch (NoSuchMethodException e) {
            // do nothing here
        }
        cl = cl.getSuperclass();
    }

    return null;
}

From source file:kelly.util.BeanUtils.java

/**
 * Copy the property values of the given source bean into the given target bean.
 * <p>Note: The source and target classes do not have to match or even be derived
 * from each other, as long as the properties match. Any bean properties that the
 * source bean exposes but the target bean does not will silently be ignored.
 * @param source the source bean/*from www . j av a2s  .  c o m*/
 * @param target the target bean
 * @param editable the class (or interface) to restrict property setting to
 * @param ignoreProperties array of property names to ignore
 * @throws BeansException if the copying failed
 * @see BeanWrapper
 */
private static void copyProperties(Object source, Object target, Class<?> editable,
        String... ignoreProperties) {

    Validate.notNull(source, "Source must not be null");
    Validate.notNull(target, "Target must not be null");

    Class<?> actualEditable = target.getClass();
    if (editable != null) {
        if (!editable.isInstance(target)) {
            throw new IllegalArgumentException("Target class [" + target.getClass().getName()
                    + "] not assignable to Editable class [" + editable.getName() + "]");
        }
        actualEditable = editable;
    }
    PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
    List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;

    for (PropertyDescriptor targetPd : targetPds) {
        Method writeMethod = targetPd.getWriteMethod();
        if (writeMethod != null && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
            PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
            if (sourcePd != null) {
                Method readMethod = sourcePd.getReadMethod();
                if (readMethod != null
                        && writeMethod.getParameterTypes()[0].isAssignableFrom(readMethod.getReturnType())) {
                    try {
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object value = readMethod.invoke(source);
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, value);
                    } catch (Throwable ex) {
                        throw new BeanException(
                                "Could not copy property '" + targetPd.getName() + "' from source to target",
                                ex);
                    }
                }
            }
        }
    }
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

private static <A extends Annotation> A searchOnInterfaces(final Method method, final Class<A> annotationType,
        final Class<?>[] ifcs) {
    A annotation = null;/*from  w  w  w . j a va2 s. com*/
    for (final Class<?> iface : ifcs) {
        if (isInterfaceWithAnnotatedMethods(iface)) {
            try {
                final Method equivalentMethod = iface.getMethod(method.getName(), method.getParameterTypes());
                annotation = getAnnotation(equivalentMethod, annotationType);
            } catch (final NoSuchMethodException ex) {
                // Skip this interface - it doesn't have the method...
            }
            if (annotation != null) {
                break;
            }
        }
    }
    return annotation;
}

From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java

private static ASMMethod getMethod(ExtendableClassLoader pcl, Resource classRoot, Type type, Class clazz,
        Method method) throws IOException, InstantiationException, IllegalAccessException,
        IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException {
    String className = createMethodName(clazz, method.getName(), method.getParameterTypes());

    // check if already in memory cache
    ASMMethod asmm = methods.get(className);
    if (asmm != null)
        return asmm;

    // try to load existing ASM Class
    Class<?> asmClass;/*from   ww  w .  j a v a2 s  .co m*/
    try {
        asmClass = pcl.loadClass(className);
    } catch (ClassNotFoundException cnfe) {
        byte[] barr = _createMethod(type, clazz, method, classRoot, className);
        asmClass = pcl.loadClass(className, barr);
    }

    asmm = newInstance(asmClass, clazz, method.getParameterTypes());
    methods.put(className, asmm);
    return asmm;
}

From source file:com.earnstone.geo.GeocandraService.java

private static void loadCassandraSettings(CassandraHostConfigurator config, String settings) {
    try {/*from  w w w. ja v a  2s.c  o m*/
        String[] keyvalues = settings.split(";");

        for (String keyValueStr : keyvalues) {
            String[] keyValue = keyValueStr.split("=");

            Method method = null;
            for (Method temp : config.getClass().getMethods()) {
                if (temp.getName().equals("set" + keyValue[0])) {
                    method = temp;
                    break;
                }
            }

            Object value = null;

            if (method.getParameterTypes()[0].getName().equals("int"))
                value = Integer.parseInt(keyValue[1]);
            else if (method.getParameterTypes()[0].getName().equals("long"))
                value = Integer.parseInt(keyValue[1]);
            else if (method.getParameterTypes()[0].getName().equals("boolean"))
                value = Boolean.parseBoolean(keyValue[1]);

            method.invoke(config, new Object[] { value });
        }
    } catch (Exception ex) {
        throw new IllegalArgumentException("Error with loading cassandra settings", ex);
    }
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Determine whether the given method is a "hashCode" method.
 * //from   ww w .  j  av a2 s  .  c o  m
 * @see java.lang.Object#hashCode
 */
public static boolean isHashCodeMethod(final Method method) {
    return method != null && method.getName().equals("hashCode") && method.getParameterTypes().length == 0;
}

From source file:com.liferay.cli.support.util.ReflectionUtils.java

/**
 * Determine whether the given method is a "toString" method.
 * /*from  w w w . jav  a 2  s .  c  o  m*/
 * @see java.lang.Object#toString()
 */
public static boolean isToStringMethod(final Method method) {
    return method != null && method.getName().equals("toString") && method.getParameterTypes().length == 0;
}

From source file:Mopex.java

/**
 * Determines if the signatures of two method objects are equal. In Java, a
 * signature comprises the method name and the array of of formal parameter
 * types. For two signatures to be equal, the method names must be the same
 * and the formal parameters must be of the same type (in the same order).
 * /*from w  w w.j  a v a2 s .  com*/
 * @return boolean
 * @param m1
 *            java.lang.Method
 * @param m2
 *            java.lang.Method
 */
//start extract equalSignatures
public static boolean equalSignatures(Method m1, Method m2) {
    if (!m1.getName().equals(m2.getName()))
        return false;
    if (!Arrays.equals(m1.getParameterTypes(), m2.getParameterTypes()))
        return false;
    return true;
}

From source file:com.evolveum.midpoint.report.impl.ReportUtils.java

public static String prettyPrintForReport(Object value) {
    if (value == null) {
        return "";
    }// ww w .  j a  v a2s  .c  om

    if (value instanceof MetadataType) {
        return "";
    }

    //special handling for byte[], some problems with jasper when printing 
    if (byte[].class.equals(value.getClass())) {
        return prettyPrintForReport((byte[]) value);
    }

    // 1. Try to find prettyPrintForReport in this class first
    if (value instanceof Containerable) { //e.g. RoleType needs to be converted to PCV in order to format properly
        value = (((Containerable) value).asPrismContainerValue());
    }

    for (Method method : ReportUtils.class.getMethods()) {
        if (method.getName().equals("prettyPrintForReport")) {
            Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length == 1 && parameterTypes[0].equals(value.getClass())) {
                try {
                    return (String) method.invoke(null, value);
                } catch (Throwable e) {
                    return "###INTERNAL#ERROR### " + e.getClass().getName() + ": " + e.getMessage()
                            + "; prettyPrintForReport method for value " + value;
                }
            }
        }
    }

    // 2. Default to PrettyPrinter.prettyPrint
    String str = PrettyPrinter.prettyPrint(value);
    if (str.length() > 1000) {
        return str.substring(0, 1000);
    }
    return str;

}

From source file:io.stallion.reflection.PropertyUtils.java

public static Boolean isWriteable(Object target, String propertyName) {
    if (propertyName == null || "".equals(propertyName))
        throw new PropertyException("encountered invalid null or empty property name");
    String setterName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    Method[] methods = target.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if (method.getName().equals(setterName) && method.getParameterTypes().length == 1) {
            return true;
        }/* w  ww.j  av a2 s .c  om*/
    }
    return false;
}