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

org.mule.mvel2.util.Varargs Maven / Gradle / Ivy

Go to download

MVEL is a powerful expression language for Java-based applications. It provides a plethora of features and is suited for everything from the smallest property binding and extraction, to full blown scripts. This is a fork of MVEL customized for use in Mule.

There is a newer version: 2.1.9-MULE-019
Show newest version
package org.mule.mvel2.util;

import java.lang.reflect.*;

public class Varargs {

    public static Object[] normalizeArgsForVarArgs(Class[] parameterTypes, Object[] args, boolean isVarArgs) {
        if (!isVarArgs) return args;
        if (parameterTypes.length == args.length && args[args.length-1].getClass().isArray()) return args;

        int varargLength = args.length - parameterTypes.length + 1;
        Object vararg = Array.newInstance(parameterTypes[parameterTypes.length-1].getComponentType(), varargLength);
        for (int i = 0; i < varargLength; i++) Array.set(vararg, i, args[parameterTypes.length - 1 + i]);

        Object[] normalizedArgs = new Object[parameterTypes.length];
        for (int i = 0; i < parameterTypes.length - 1; i++) normalizedArgs[i] = args[i];
        normalizedArgs[parameterTypes.length - 1] = vararg;
        return normalizedArgs;
    }

    public static Class paramTypeVarArgsSafe(Class[] parameterTypes, int i, boolean isVarArgs) {
      if (!isVarArgs) return parameterTypes[i];
      if (i < parameterTypes.length-1) return parameterTypes[i];
      return parameterTypes[parameterTypes.length-1].getComponentType();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy