Example usage for java.lang.reflect Method isVarArgs

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

Introduction

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

Prototype

@Override
public boolean isVarArgs() 

Source Link

Usage

From source file:Main.java

public static void printMethods(Class cl) {
    Method[] methods = cl.getDeclaredMethods();

    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        System.out.println(m.isVarArgs());

    }//from  ww  w.j a  v a  2s  .  c o m
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Method findVarArgsMethod(Object object, String methodName) {
    for (Method method : object.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.isVarArgs()) {
            return method;
        }/*from   w w  w.ja v a  2  s . c o  m*/
    }
    return null;
}

From source file:com.arpnetworking.jackson.BuilderDeserializer.java

static boolean isSetterMethod(final Class<?> builderClass, final Method method) {
    return method.getName().startsWith(SETTER_PREFIX) && builderClass.equals(method.getReturnType())
            && !method.isVarArgs() && method.getParameterTypes().length == 1;
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Method findMethod(Object object, String methodName, int arity) {
    for (Method method : object.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == arity
                && !method.isVarArgs()) {
            return method;
        }/*from w  w w  . j ava 2  s  .c  om*/
    }
    return null;
}

From source file:org.protelis.lang.util.ReflectionUtils.java

private static boolean compatibleLength(final Method m, final Object[] args) {
    if (m.isVarArgs()) {
        return args.length >= (m.getParameterTypes().length - 1);
    } else {/*from   w ww .j  av a  2  s  . c o  m*/
        return m.getParameterTypes().length == args.length;
    }
}

From source file:org.protelis.lang.util.ReflectionUtils.java

private static Object[] repackageIfVarArgs(final Method m, final Object[] args) {
    if (!m.isVarArgs()) {
        return args;
    } else {/*from  www. j  a  va 2  s  . co  m*/
        final Class<?>[] expectedArgs = m.getParameterTypes();
        // We will repackage into an array of the expected length
        final Object[] newargs = new Object[expectedArgs.length];
        // repackage all the base args
        System.arraycopy(args, 0, newargs, 0, Math.max(expectedArgs.length - 1, 0));
        // Determine how many arguments need repackaging
        final int numVarArgs = args.length - (expectedArgs.length - 1);
        // Make an array of the appropriate type, then fill it in
        final Class<?> varargType = expectedArgs[expectedArgs.length - 1];
        Object[] vararg = (Object[]) Array.newInstance(varargType.getComponentType(), numVarArgs);
        for (int i = 0; i < numVarArgs; i++) {
            vararg[i] = args[i + expectedArgs.length - 1];
        }
        // Put the new array in the last argument and return
        newargs[newargs.length - 1] = vararg;
        return newargs;
    }
}

From source file:org.protelis.lang.util.ReflectionUtils.java

private static Class<?> nthArgumentType(final Method m, final int n) {
    final Class<?>[] expectedArgs = m.getParameterTypes();
    if (m.isVarArgs() && n >= (expectedArgs.length - 1)) {
        final Class<?> varargType = expectedArgs[expectedArgs.length - 1];
        return varargType.getComponentType();
    } else {/*from ww w .ja  va 2 s .  c o m*/
        return expectedArgs[n];
    }
}

From source file:com.evolveum.midpoint.util.ReflectionUtil.java

public static Object invokeMethod(Object object, String methodName, List<?> argList)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Method method = findMethod(object, methodName, argList);
    if (method == null) {
        throw new NoSuchMethodException(
                "No method " + methodName + " for arguments " + debugDumpArgList(argList) + " in " + object);
    }//from www  .  j a  v  a  2 s  .c  o  m
    Object[] args = argList.toArray();
    if (method.isVarArgs()) {
        Class<?> parameterTypeClass = method.getParameterTypes()[0];
        Object[] varArgs = (Object[]) Array.newInstance(parameterTypeClass.getComponentType(), args.length);
        for (int i = 0; i < args.length; i++) {
            varArgs[i] = args[i];
        }
        args = new Object[] { varArgs };
    }
    try {
        return method.invoke(object, args);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                e.getMessage() + " for arguments " + debugDumpArgList(argList) + " in " + object, e);
    }
}

From source file:com.github.michalbednarski.intentslab.editor.BundleAdapter.java

public static void putInBundle(Bundle bundle, String key, Object value) {
    if (value == null) {
        bundle.putString(key, null);//from  w  w w.ja v  a2s.com
        return;
    }

    Pattern putMethodName = Pattern.compile("put[A-Z][A-Za-z]+");
    for (Method method : Bundle.class.getMethods()) {
        if (putMethodName.matcher(method.getName()).matches() && !method.isVarArgs()) {
            final Class<?>[] parameterTypes = method.getParameterTypes();
            if (parameterTypes.length == 2 && parameterTypes[0] == String.class
                    && Utils.toWrapperClass(parameterTypes[1]).isInstance(value)) {
                try {
                    method.invoke(bundle, key, value);
                    return;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    // continue
                } catch (InvocationTargetException e) {
                    throw new RuntimeException("Method " + method.getName() + " of bundle thrown exception", e);
                }
            }
        }
    }
    throw new RuntimeException("No put* method found");
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

@SuppressWarnings({ "unchecked", "unchecked" })
public static Object invokeMethod(Class<?> clazz, Object obj, String methodName, Object... args)
        throws NoSuchMethodException, IllegalArgumentException, InvocationTargetException,
        IllegalAccessException {/*from  w  w  w  .ja  va 2s. com*/
    try {
        List<Method> validMethods = new ArrayList<>();

        for (Method method : clazz.getMethods()) {
            Class<?>[] parameterTypes = null;

            if (!method.getName().equals(methodName)) {
                continue;
            }

            parameterTypes = method.getParameterTypes();
            if (method.isVarArgs()) {
                if (method.isVarArgs() && (parameterTypes.length - args.length >= 2)) {
                    parameterTypes = null;
                    continue;
                }
            } else {
                if (parameterTypes.length != args.length) {
                    parameterTypes = null;
                    continue;
                }
            }

            if (method.isVarArgs()) {
                boolean matches = false;

                // check non vararg part
                for (int i = 0; i < parameterTypes.length - 1; i++) {
                    matches = checkMatch(parameterTypes[i], args[i]);
                    if (!matches)
                        break;
                }

                // check rest
                for (int i = parameterTypes.length - 1; i < args.length; i++) {
                    Class<?> arrayType = parameterTypes[parameterTypes.length - 1].getComponentType();

                    matches = checkMatch(arrayType, args[i]);
                    if (!matches)
                        break;
                }

                if (matches) {
                    validMethods.add(method);
                }
            } else {
                boolean matches = true;

                for (int i = 0; i < parameterTypes.length; i++) {
                    matches = checkMatch(parameterTypes[i], args[i]);
                    if (!matches)
                        break;
                }

                if (matches) {
                    validMethods.add(method);
                }
            }
        }

        if (!validMethods.isEmpty()) {
            Method method = validMethods.get(0);
            for (int i = 1; i < validMethods.size(); i++) {
                Method targetMethod = validMethods.get(i);

                Class<?>[] currentParams = method.getParameterTypes();
                Class<?>[] targetParams = targetMethod.getParameterTypes();

                if (method.isVarArgs() && targetMethod.isVarArgs()) {
                    for (int j = 0; j < currentParams.length; j++) {
                        if (currentParams[j].isAssignableFrom(targetParams[j])) {
                            method = targetMethod;
                            break;
                        }
                    }
                } else if (method.isVarArgs()) {
                    //usually, non-vararg is more specific method. So we use that
                    method = targetMethod;
                } else if (targetMethod.isVarArgs()) {
                    //do nothing
                } else {
                    for (int j = 0; j < currentParams.length; j++) {
                        if (targetParams[j].isEnum()) { // enum will be handled later
                            method = targetMethod;
                            break;
                        } else if (ClassUtils.isAssignable(targetParams[j], currentParams[j], true)) { //narrow down to find the most specific method
                            method = targetMethod;
                            break;
                        }
                    }
                }
            }

            method.setAccessible(true);

            for (int i = 0; i < args.length; i++) {
                Class<?>[] parameterTypes = method.getParameterTypes();

                if (args[i] instanceof String && i < parameterTypes.length && parameterTypes[i].isEnum()) {
                    try {
                        args[i] = Enum.valueOf((Class<? extends Enum>) parameterTypes[i], (String) args[i]);
                    } catch (IllegalArgumentException ex1) {
                        // Some overloaded methods already has
                        // String to Enum conversion
                        // So just lets see if one exists
                        Class<?>[] types = new Class<?>[args.length];
                        for (int k = 0; k < args.length; k++)
                            types[k] = args[k].getClass();

                        try {
                            Method alternative = clazz.getMethod(methodName, types);
                            return alternative.invoke(obj, args);
                        } catch (NoSuchMethodException ex2) {
                            throw new RuntimeException(
                                    "Tried to convert value [" + args[i] + "] to Enum [" + parameterTypes[i]
                                            + "] or find appropriate method but found nothing. Make sure"
                                            + " that the value [" + args[i]
                                            + "] matches exactly with one of the Enums in [" + parameterTypes[i]
                                            + "] or the method you are looking exists.");
                        }
                    }
                }
            }

            if (method.isVarArgs()) {
                Class<?>[] parameterTypes = method.getParameterTypes();

                Object varargs = Array.newInstance(parameterTypes[parameterTypes.length - 1].getComponentType(),
                        args.length - parameterTypes.length + 1);
                for (int k = 0; k < Array.getLength(varargs); k++) {
                    Array.set(varargs, k, args[parameterTypes.length - 1 + k]);
                }

                Object[] newArgs = new Object[parameterTypes.length];
                for (int k = 0; k < newArgs.length - 1; k++) {
                    newArgs[k] = args[k];
                }
                newArgs[newArgs.length - 1] = varargs;

                args = newArgs;
            }

            return method.invoke(obj, args);
        }

        if (args.length > 0) {
            StringBuilder builder = new StringBuilder(String.valueOf(args[0].getClass().getSimpleName()));

            for (int i = 1; i < args.length; i++) {
                builder.append(", " + args[i].getClass().getSimpleName());
            }

            throw new NoSuchMethodException(methodName + "(" + builder.toString() + ")");
        } else {
            throw new NoSuchMethodException(methodName + "()");
        }
    } catch (NullPointerException e) {
        StringBuilder builder = new StringBuilder(String.valueOf(args[0]));
        for (int i = 1; i < args.length; i++)
            builder.append("," + String.valueOf(args[i]));
        throw new NullPointerException("Call " + methodName + "(" + builder.toString() + ")");
    }
}