Example usage for java.lang Class getComponentType

List of usage examples for java.lang Class getComponentType

Introduction

In this page you can find the example usage for java.lang Class getComponentType.

Prototype

public Class<?> getComponentType() 

Source Link

Document

Returns the Class representing the component type of an array.

Usage

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  ww w.ja  v  a2  s. c  om
    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:Main.java

/**
 * Transform the class-relative resource name into a global name by appending
 * it to the classes package name. If the name is already a global name (the
 * name starts with a "/"), then the name is returned unchanged.
 * /* w  ww  .  ja v  a 2  s  . c o  m*/
 * @param name
 *          the resource name
 * @param c
 *          the class which the resource is relative to
 * @return the tranformed name.
 */
private static String convertName(final String name, Class c) {
    if (name.startsWith("/")) {
        // strip leading slash..
        return name.substring(1);
    }

    // we cant work on arrays, so remove them ...
    while (c.isArray()) {
        c = c.getComponentType();
    }
    // extract the package ...
    final String baseName = c.getName();
    final int index = baseName.lastIndexOf('.');
    if (index == -1) {
        return name;
    }

    final String pkgName = baseName.substring(0, index);
    return pkgName.replace('.', '/') + "/" + name;
}

From source file:com.vk.sdk.api.model.ParseUtils.java

/**
 * Parses array from given JSONArray./*from w w w .jav  a2  s.co m*/
 * Supports parsing of primitive types and {@link com.vk.sdk.api.model.VKApiModel} instances.
 * @param array JSONArray to parse
 * @param arrayClass type of array field in class.
 * @return object to set to array field in class
 * @throws JSONException if given array have incompatible type with given field.
 */
private static Object parseArrayViaReflection(JSONArray array, Class arrayClass) throws JSONException {
    Object result = Array.newInstance(arrayClass.getComponentType(), array.length());
    Class<?> subType = arrayClass.getComponentType();
    for (int i = 0; i < array.length(); i++) {
        try {
            Object item = array.opt(i);
            if (VKApiModel.class.isAssignableFrom(subType) && item instanceof JSONObject) {
                VKApiModel model = (VKApiModel) subType.newInstance();
                item = model.parse((JSONObject) item);
            }
            Array.set(result, i, item);
        } catch (InstantiationException e) {
            throw new JSONException(e.getMessage());
        } catch (IllegalAccessException e) {
            throw new JSONException(e.getMessage());
        } catch (IllegalArgumentException e) {
            throw new JSONException(e.getMessage());
        }
    }
    return result;
}

From source file:org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter.java

private static TypeDescription buildArrayTypeDescription(final Class<?> arrayType) {
    final TypeDescription typeDescription = new TypeDescription(new TypeName(arrayType));
    typeDescription.setElementTypeName(new TypeName(arrayType.getComponentType()));
    typeDescription.setArray(true);//from   w  ww . j  av  a 2  s .  c  om
    return typeDescription;
}

From source file:FlipPolygon.java

private static <T, U> T[] copyOf(U[] arr, int newSize, Class<? extends T[]> newType) {
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newSize]
            : (T[]) Array.newInstance(newType.getComponentType(), newSize);
    System.arraycopy(arr, 0, copy, 0, Math.min(arr.length, newSize));
    return copy;//from  w ww . j  a va 2 s .c om
}

From source file:org.gerzog.jstataggr.core.templates.TemplateHelper.java

protected static String getTypeName(final Class<?> type) {
    if (type == null) {
        return null;
    }// w w  w. j a v a  2 s . c  o m

    if (type.isArray()) {
        return getTypeName(type.getComponentType()) + "[]";
    }
    return type.getName();
}

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

/**
 * recursively retrieve base array class
 *
 * @param clazz// w w w  .  j  av  a  2s  .  c o m
 * @return
 */
@SuppressWarnings({ "unused", "rawtypes" })
private static Class retrieveArrayBase(Class clazz) {
    if (clazz.isArray())
        return retrieveArrayBase(clazz.getComponentType());
    return clazz;
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }/*from  w  w  w .j  ava2s.com*/
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:kelly.util.BeanUtils.java

/**
 * Check if the given type represents a "simple" property:
 * a primitive, a String or other CharSequence, a Number, a Date,
 * a URI, a URL, a Locale, a Class, or a corresponding array.
 * <p>Used to determine properties to check for a "simple" dependency-check.
 * @param clazz the type to check/*from  w  w  w .j a va2  s . com*/
 * @return whether the given type represents a "simple" property
 * @see org.springframework.beans.factory.support.RootBeanDefinition#DEPENDENCY_CHECK_SIMPLE
 * @see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#checkDependencies
 */
public static boolean isSimpleProperty(Class<?> clazz) {
    Validate.notNull(clazz, "Class must not be null");
    return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType()));
}

From source file:Main.java

/**
 * Copies the specified range of the specified array into a new array.
 * The initial index of the range (<tt>from</tt>) must lie between zero
 * and <tt>original.length</tt>, inclusive.  The value at
 * <tt>original[from]</tt> is placed into the initial element of the copy
 * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
 * Values from subsequent elements in the original array are placed into
 * subsequent elements in the copy.  The final index of the range
 * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
 * may be greater than <tt>original.length</tt>, in which case
 * <tt>null</tt> is placed in all elements of the copy whose index is
 * greater than or equal to <tt>original.length - from</tt>.  The length
 * of the returned array will be <tt>to - from</tt>.
 * The resulting array is of the class <tt>newType</tt>.
 *
 * @param original the array from which a range is to be copied
 * @param from the initial index of the range to be copied, inclusive
 * @param to the final index of the range to be copied, exclusive.
 *     (This index may lie outside the array.)
 * @param newType the class of the copy to be returned
 * @return a new array containing the specified range from the original array,
 *     truncated or padded with nulls to obtain the required length
 * @throws ArrayIndexOutOfBoundsException if <tt>from &lt; 0</tt>
 *     or <tt>from &gt; original.length()</tt>
 * @throws IllegalArgumentException if <tt>from &gt; to</tt>
 * @throws NullPointerException if <tt>original</tt> is null
 * @throws ArrayStoreException if an element copied from
 *     <tt>original</tt> is not of a runtime type that can be stored in
 *     an array of class <tt>newType</tt>.
 * @since 1.6//from  w  w  w  .  j  a v  a2s  .c o  m
 */
@SuppressWarnings("unchecked")
public static <T, U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
    return copy;
}