Example usage for org.apache.commons.lang3 ArrayUtils EMPTY_CLASS_ARRAY

List of usage examples for org.apache.commons.lang3 ArrayUtils EMPTY_CLASS_ARRAY

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ArrayUtils EMPTY_CLASS_ARRAY.

Prototype

Class EMPTY_CLASS_ARRAY

To view the source code for org.apache.commons.lang3 ArrayUtils EMPTY_CLASS_ARRAY.

Click Source Link

Document

An empty immutable Class array.

Usage

From source file:com.gargoylesoftware.htmlunit.javascript.MethodWrapper.java

/**
 * Facility constructor to wrap a method without arguments.
 * @param methodName the name of the method to wrap
 * @param clazz the class declaring the method
 * @throws NoSuchMethodException if the method is no found
 *///from w  w w  . j  av a2s.  c om
MethodWrapper(final String methodName, final Class<?> clazz) throws NoSuchMethodException {
    this(methodName, clazz, ArrayUtils.EMPTY_CLASS_ARRAY);
}

From source file:com.gargoylesoftware.htmlunit.javascript.ScriptableWrapper.java

/**
 * Constructs a wrapper for the java object.
 * @param scope the scope of the executing script
 * @param javaObject the javaObject to wrap
 * @param staticType the static type of the object
 *///from www.ja va2  s.  c o  m
public ScriptableWrapper(final Scriptable scope, final Object javaObject, final Class<?> staticType) {
    javaObject_ = javaObject;
    setParentScope(scope);

    // all these information should come from the XML js configuration file
    // just for a first time...
    if (NodeList.class.equals(staticType) || NamedNodeMap.class.equals(staticType)) {
        try {
            jsClassName_ = staticType.getSimpleName();

            // is there a better way that would avoid to keep local
            // information?
            // it seems that defineProperty with only accepts delegate if
            // its method takes a ScriptableObject
            // as parameter.
            final Method length = javaObject.getClass().getMethod("getLength", ArrayUtils.EMPTY_CLASS_ARRAY);
            properties_.put("length", length);

            final Method item = javaObject.getClass().getMethod("item", METHOD_PARAMS_INT);
            defineProperty("item", new MethodWrapper("item", staticType, METHOD_PARAMS_INT), 0);

            final Method toString = getClass().getMethod("jsToString", ArrayUtils.EMPTY_CLASS_ARRAY);
            defineProperty("toString", new FunctionObject("toString", toString, this), 0);

            getByIndexMethod_ = item;

            if (NamedNodeMap.class.equals(staticType)) {
                final Method getNamedItem = javaObject.getClass().getMethod("getNamedItem",
                        METHOD_PARAMS_STRING);
                defineProperty("getNamedItem",
                        new MethodWrapper("getNamedItem", staticType, METHOD_PARAMS_STRING), 0);

                getByNameFallback_ = getNamedItem;
            }
        } catch (final Exception e) {
            throw new RuntimeException("Method not found", e);
        }
    } else {
        throw new RuntimeException("Unknown type: " + staticType.getName());
    }
}

From source file:objenome.util.ClassUtils.java

/**
 * <p>Checks if an array of Classes can be assigned to another array of Classes.</p>
 *
 * <p>This method calls {@link #isAssignable(Class, Class) isAssignable} for each
 * Class pair in the input arrays. It can be used to check if a set of arguments
 * (the first parameter) are suitably compatible with a set of method parameter types
 * (the second parameter).</p>/*from  w ww.  jav  a  2  s .c o m*/
 *
 * <p>Unlike the {@link Class#isAssignableFrom(Class)} method, this
 * method takes into account widenings of primitive classes and
 * {@code null}s.</p>
 *
 * <p>Primitive widenings allow an int to be assigned to a {@code long},
 * {@code float} or {@code double}. This method returns the correct
 * result for these cases.</p>
 *
 * <p>{@code Null} may be assigned to any reference type. This method will
 * return {@code true} if {@code null} is passed in and the toClass is
 * non-primitive.</p>
 *
 * <p>Specifically, this method tests whether the type represented by the
 * specified {@code Class} parameter can be converted to the type
 * represented by this {@code Class} object via an identity conversion
 * widening primitive or widening reference conversion. See
 * <em><a href="http://docs.oracle.com/javase/specs/">The Java Language Specification</a></em>,
 * sections 5.1.1, 5.1.2 and 5.1.4 for details.</p>
 *
 * @param classArray  the array of Classes to check, may be {@code null}
 * @param toClassArray  the array of Classes to try to assign into, may be {@code null}
 * @param autoboxing  whether to use implicit autoboxing/unboxing between primitives and wrappers
 * @return {@code true} if assignment possible
 */
public static boolean isAssignable(Class<?>[] classArray, Class<?>[] toClassArray, boolean autoboxing) {
    if (!ArrayUtils.isSameLength(classArray, toClassArray)) {
        return false;
    }
    if (classArray == null) {
        classArray = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (toClassArray == null) {
        toClassArray = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    for (int i = 0; i < classArray.length; i++) {
        if (!isAssignable(classArray[i], toClassArray[i], autoboxing)) {
            return false;
        }
    }
    return true;
}

From source file:objenome.util.ClassUtils.java

/**
 * <p>Converts an array of {@code Object} in to an array of {@code Class} objects.
 * If any of these objects is null, a null element will be inserted into the array.</p>
 *
 * <p>This method returns {@code null} for a {@code null} input array.</p>
 *
 * @param array an {@code Object} array/*from   ww w  .j  av a  2s  . co m*/
 * @return a {@code Class} array, {@code null} if null array input
 * @since 2.4
 */
public static Class<?>[] toClass(Object... array) {
    if (array == null) {
        return null;
    }
    if (array.length == 0) {
        return ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Class<?>[] classes = new Class[array.length];
    for (int i = 0; i < array.length; i++) {
        classes[i] = array[i] == null ? null : array[i].getClass();
    }
    return classes;
}

From source file:org.apache.bval.jsr.ApacheValidatorFactory.java

private static Class<?>[] safeArray(Class<?>... array) {
    return ArrayUtils.isEmpty(array) ? ArrayUtils.EMPTY_CLASS_ARRAY : ArrayUtils.clone(array);
}

From source file:org.apache.bval.jsr.xml.ValidationMappingParser.java

private Class<?>[] getGroups(GroupsType groupsType, String defaultPackage) {
    if (groupsType == null) {
        return ArrayUtils.EMPTY_CLASS_ARRAY;
    }//from   ww w  . j a v a 2  s.  c  o  m

    List<Class<?>> groupList = new ArrayList<Class<?>>();
    for (String groupClass : groupsType.getValue()) {
        groupList.add(loadClass(groupClass, defaultPackage));
    }
    return groupList.toArray(new Class[groupList.size()]);
}

From source file:org.blocks4j.reconf.client.constructors.CollectionConstructor.java

public Object construct(MethodData data) throws Throwable {
    Class<?> returnClass;/*from w w  w . j a va  2s. c  o m*/
    Type innerClass = null;

    if (data.getReturnType() instanceof ParameterizedType) {
        ParameterizedType parameterized = (ParameterizedType) data.getReturnType();
        returnClass = (Class<?>) parameterized.getRawType();

        if (parameterized.getActualTypeArguments()[0] instanceof ParameterizedType) {
            innerClass = parameterized.getActualTypeArguments()[0];

        } else if (parameterized.getActualTypeArguments()[0] instanceof Class<?>) {
            innerClass = parameterized.getActualTypeArguments()[0];
        }
    } else if (data.getReturnType() instanceof Class) {
        returnClass = (Class<?>) data.getReturnType();

        if (returnClass.getGenericSuperclass() != null
                && returnClass.getGenericSuperclass() instanceof ParameterizedType) {
            ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass();
            if (parameterized.getActualTypeArguments().length != 1) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            }
            if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            } else {
                innerClass = parameterized.getActualTypeArguments()[0];
            }

        } else {
            innerClass = Object.class;
        }

    } else {
        throw new IllegalArgumentException(msg.format("error.return", data.getMethod()));
    }

    if (returnClass.isInterface()) {
        returnClass = getDefaultImplementation(data, returnClass);
    }

    Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
    Collection<Object> collectionInstance = (Collection<Object>) constructor
            .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);

    if (null == data.getValue()) {
        return collectionInstance;
    }

    for (String s : new StringParser(data).getTokens()) {
        Object o = ObjectConstructorFactory.get(innerClass)
                .construct(new MethodData(data.getMethod(), innerClass, s));
        if (o != null) {
            collectionInstance.add(o);
        }
    }

    return collectionInstance;
}

From source file:org.blocks4j.reconf.client.constructors.complex.StringParserTest.java

private MethodData methodDataOf(String arg) {
    try {/*from w w  w.  ja va  2 s  .  co m*/
        return new MethodData(Object.class.getMethod("toString", ArrayUtils.EMPTY_CLASS_ARRAY), String.class,
                arg);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.blocks4j.reconf.client.constructors.MapConstructor.java

public Object construct(MethodData data) throws Throwable {
    Class<?> returnClass;/*from w ww  . j  a  va  2  s . c o  m*/
    Type keyType = null;
    Type valueType = null;

    if (data.getReturnType() instanceof ParameterizedType) {
        ParameterizedType parameterized = (ParameterizedType) data.getReturnType();
        returnClass = (Class<?>) parameterized.getRawType();

        if (parameterized.getActualTypeArguments().length == 1) {
            Type first = parameterized.getActualTypeArguments()[0];
            if (returnClass.getGenericSuperclass() != null
                    && returnClass.getGenericSuperclass() instanceof ParameterizedType) {
                parameterized = (ParameterizedType) returnClass.getGenericSuperclass();
                if (parameterized.getActualTypeArguments().length != 2) {
                    throw new IllegalArgumentException(
                            msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
                }
                if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) {
                    keyType = first;
                    valueType = parameterized.getActualTypeArguments()[1];

                } else if (parameterized.getActualTypeArguments()[1] instanceof TypeVariable) {
                    valueType = first;
                    keyType = parameterized.getActualTypeArguments()[0];

                } else {
                    throw new IllegalArgumentException(
                            msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
                }
            }

        } else {
            keyType = parameterized.getActualTypeArguments()[0];
            valueType = parameterized.getActualTypeArguments()[1];
        }

    } else if (data.getReturnType() instanceof Class) {
        returnClass = (Class<?>) data.getReturnType();

        if (returnClass.getGenericSuperclass() != null
                && returnClass.getGenericSuperclass() instanceof ParameterizedType) {
            ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass();
            if (parameterized.getActualTypeArguments().length != 2) {
                throw new IllegalArgumentException(
                        msg.format("error.cant.build.type", data.getReturnType(), data.getMethod()));
            }
            keyType = parameterized.getActualTypeArguments()[0];
            valueType = parameterized.getActualTypeArguments()[1];

        } else {
            keyType = Object.class;
            valueType = Object.class;
        }

    } else {
        throw new IllegalArgumentException(msg.format("error.return", data.getMethod()));
    }

    if (returnClass.isInterface()) {
        returnClass = getDefaultImplementation(data, returnClass);
    }

    Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
    Map<Object, Object> mapInstance = (Map<Object, Object>) constructor
            .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);

    if (null == data.getValue() || StringUtils.isEmpty(data.getValue())) {
        return mapInstance;
    }

    if ((!(keyType instanceof Class))
            || (!StringUtils.startsWith(data.getValue(), "[") || !StringUtils.endsWith(data.getValue(), "]"))) {
        throw new IllegalArgumentException(msg.format("error.build", data.getValue(), data.getMethod()));
    }

    StringParser parser = new StringParser(data);
    for (Entry<String, String> each : parser.getTokensAsMap().entrySet()) {
        Object value = ObjectConstructorFactory.get(valueType)
                .construct(new MethodData(data.getMethod(), valueType, each.getValue()));
        mapInstance.put(ObjectConstructorFactory.get(keyType)
                .construct(new MethodData(data.getMethod(), keyType, each.getKey())), value);
    }

    return mapInstance;
}

From source file:org.blocks4j.reconf.client.proxy.MethodConfiguration.java

private ConfigurationAdapter getRemoteAdapter() {
    if (null == remoteItem || remoteItem.getAdapter() == null) {
        return null;
    }//  w  w  w .j a  v a  2s  . co m
    try {
        Constructor<? extends ConfigurationAdapter> constructor = remoteItem.getAdapter()
                .getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY);
        return constructor.newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY);
    } catch (Throwable t) {
        throw new IllegalArgumentException(msg.get("error.adapter"), t);
    }
}