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:Main.java

public static Class<?> inferFromArrayType(final Class<?> type) {
    if (!isArrayType(type)) {
        return null;
    }/*from   w ww  .  j a v a2  s.  c  om*/
    if (type.isArray()) {
        final Class<?> componentType = type.getComponentType();
        return componentType;
    }
    return null;
}

From source file:org.apache.sling.nosql.generic.resource.impl.NoSqlValueMap.java

static boolean isValidType(Class clazz) {
    if (clazz.isArray()) {
        if (clazz.getComponentType() == byte.class) {
            // byte only supported as array
            return true;
        }/*from  w  w w . j  a  v a 2  s.  c  om*/
        return isValidType(clazz.getComponentType());
    } else {
        return clazz == String.class || clazz == Integer.class || clazz == Long.class || clazz == Double.class
                || clazz == Boolean.class || Calendar.class.isAssignableFrom(clazz);
    }
}

From source file:ShowClass.java

/** Return the name of an interface or primitive type, handling arrays. */
public static String typename(Class t) {
    String brackets = "";
    while (t.isArray()) {
        brackets += "[]";
        t = t.getComponentType();
    }//from w ww.  j a  v a2s . com
    String name = t.getName();
    int pos = name.lastIndexOf('.');
    if (pos != -1)
        name = name.substring(pos + 1);
    return name + brackets;
}

From source file:Main.java

/**
 * Extracts the package name from the given class object
 *//*  w w  w .  jav  a 2s.  c  o m*/
public static String getPackage(Class<?> cls) {
    // cls.getPackage() sometimes returns null, in which case fall back to string massaging.
    java.lang.Package pkg = cls.isArray() ? cls.getComponentType().getPackage() : cls.getPackage();
    if (pkg == null) {
        int dotPos;
        int dolPos = cls.getName().indexOf('$');
        if (dolPos > 0) {
            // we have nested classes, so adjust dotpos to before first $
            dotPos = cls.getName().substring(0, dolPos).lastIndexOf('.');
        } else {
            dotPos = cls.getName().lastIndexOf('.');
        }

        if (dotPos > 0) {
            return cls.getName().substring(0, dotPos);
        } else {
            // must be default package.
            return "";
        }
    } else {
        return pkg.getName();
    }
}

From source file:org.apache.sling.contextaware.config.spi.metadata.PropertyMetadata.java

private static boolean isSupportedType(Class<?> paramType) {
    if (paramType.isArray()) {
        return isSupportedType(paramType.getComponentType());
    }//from w  w w  .j ava2 s  .  c  om
    for (Class<?> type : SUPPORTED_TYPES) {
        if (type.equals(paramType)) {
            return true;
        }
    }
    if (paramType == ConfigurationMetadata.class) {
        return true;
    }
    return false;
}

From source file:Main.java

public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }//from w w w . ja v a 2 s  . c om
    final int length = Array.getLength(input);
    final Class componentType = type.getComponentType();

    final Object result = Array.newInstance(componentType, length);
    for (int idx = 0; idx < length; idx++) {
        Array.set(result, idx, Array.get(input, idx));
    }
    return result;
}

From source file:Main.java

public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = (Object) newType == (Object) Object[].class ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;/*from w  w w.jav  a  2 s.com*/
}

From source file:Main.java

private static boolean isSimpleObjectClass(Class<?> objectClass) {
    if (objectClass.isArray()) {
        return isSimpleObjectClass(objectClass.getComponentType());
    } else {/*from w w  w  . j av a2  s.c  o m*/
        return objectClass.isPrimitive() || CharSequence.class.isAssignableFrom(objectClass)
                || Character.class.isAssignableFrom(objectClass) || Boolean.class.isAssignableFrom(objectClass)
                || Number.class.isAssignableFrom(objectClass);
    }
}

From source file:Main.java

/**
 * @return an Object array for the given object.
 *
 * @param obj  Object to convert to an array.  Converts primitive
 *             arrays to Object arrays consisting of their wrapper
 *             classes.  If the object is not an array (object or primitve)
 *             then a new array of the given type is created and the
 *             object is set as the sole element.
 *//*from  ww  w.j a va  2  s .  c o  m*/
public static Object[] toArray(final Object obj) {
    // if the object is an array, the cast and return it.
    if (obj instanceof Object[]) {
        return (Object[]) obj;
    }

    // if the object is an array of primitives then wrap the array
    Class type = obj.getClass();
    Object array;
    if (type.isArray()) {
        int length = Array.getLength(obj);
        Class componentType = type.getComponentType();
        array = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(array, i, Array.get(obj, i));
        }
    } else {
        array = Array.newInstance(type, 1);
        Array.set(array, 0, obj);
    }

    return (Object[]) array;
}

From source file:Main.java

public static String idOfClass(Class<?> clazz) {
    Class<?> theClass = clazz;
    StringBuilder sb = new StringBuilder();
    if (theClass.isArray()) {
        do {//from  w w  w .ja va  2s .c  om
            sb.append("Array"); //$NON-NLS-1$
            theClass = theClass.getComponentType();
        } while (theClass.isArray());
    }
    String clazzName = theClass.getName();
    clazzName = clazzName.substring(clazzName.lastIndexOf('.') + 1);
    return clazzName + sb.toString();
}