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:net.servicefixture.util.ReflectionUtils.java

/**
 * Returns the extra [0] suffix(s) for an multi-dimensional array.
 *///from   w w  w .ja v  a  2  s .c  om
public static String getExtraMultiDimensionalArraySuffix(Class type) {
    StringBuilder sb = new StringBuilder();
    int i = 0;
    while (type.isArray()) {
        type = type.getComponentType();
        if (++i > 1) {
            sb.append("[0]");
        }
    }
    return sb.toString();
}

From source file:com.github.michalbednarski.intentslab.Utils.java

public static Object[] deepCastArray(Object[] array, Class targetType) {
    assert targetType.isArray() && !targetType.getComponentType().isPrimitive();

    if (targetType.isInstance(array) || array == null) {
        return array;
    }//from w w w  .j a  va 2s  .c  o  m

    Class componentType = targetType.getComponentType();
    Class nestedComponentType = componentType.getComponentType();
    Object[] newArray = (Object[]) Array.newInstance(componentType, array.length);
    if (nestedComponentType != null && !nestedComponentType.isPrimitive()) {
        for (int i = 0; i < array.length; i++) {
            newArray[i] = deepCastArray((Object[]) array[i], nestedComponentType);
        }
    } else {
        System.arraycopy(array, 0, newArray, 0, array.length);
    }
    return newArray;
}

From source file:tools.xor.util.ClassUtil.java

public static int getDimensionCount(Object array) {
    int count = 0;
    Class<?> arrayClass = array.getClass();
    while (arrayClass.isArray()) {
        count++;//from   w w w . j  a  v a2s.  co  m
        arrayClass = arrayClass.getComponentType();
    }
    return count;
}

From source file:org.apache.sling.models.impl.model.AbstractInjectableElement.java

private static Object getDefaultValue(AnnotatedElement element, Type type,
        InjectAnnotationProcessor2 annotationProcessor) {
    if (annotationProcessor != null && annotationProcessor.hasDefault()) {
        return annotationProcessor.getDefault();
    }/*from ww w  .j a  va 2  s .  c om*/

    Default defaultAnnotation = element.getAnnotation(Default.class);
    if (defaultAnnotation == null) {
        return null;
    }

    Object value = null;

    if (type instanceof Class) {
        Class<?> injectedClass = (Class<?>) type;
        if (injectedClass.isArray()) {
            Class<?> componentType = injectedClass.getComponentType();
            if (componentType == String.class) {
                value = defaultAnnotation.values();
            } else if (componentType == Integer.TYPE) {
                value = defaultAnnotation.intValues();
            } else if (componentType == Integer.class) {
                value = ArrayUtils.toObject(defaultAnnotation.intValues());
            } else if (componentType == Long.TYPE) {
                value = defaultAnnotation.longValues();
            } else if (componentType == Long.class) {
                value = ArrayUtils.toObject(defaultAnnotation.longValues());
            } else if (componentType == Boolean.TYPE) {
                value = defaultAnnotation.booleanValues();
            } else if (componentType == Boolean.class) {
                value = ArrayUtils.toObject(defaultAnnotation.booleanValues());
            } else if (componentType == Short.TYPE) {
                value = defaultAnnotation.shortValues();
            } else if (componentType == Short.class) {
                value = ArrayUtils.toObject(defaultAnnotation.shortValues());
            } else if (componentType == Float.TYPE) {
                value = defaultAnnotation.floatValues();
            } else if (componentType == Float.class) {
                value = ArrayUtils.toObject(defaultAnnotation.floatValues());
            } else if (componentType == Double.TYPE) {
                value = defaultAnnotation.doubleValues();
            } else if (componentType == Double.class) {
                value = ArrayUtils.toObject(defaultAnnotation.doubleValues());
            } else {
                log.warn("Default values for {} are not supported", componentType);
            }
        } else {
            if (injectedClass == String.class) {
                value = defaultAnnotation.values().length == 0 ? "" : defaultAnnotation.values()[0];
            } else if (injectedClass == Integer.class) {
                value = defaultAnnotation.intValues().length == 0 ? 0 : defaultAnnotation.intValues()[0];
            } else if (injectedClass == Long.class) {
                value = defaultAnnotation.longValues().length == 0 ? 0l : defaultAnnotation.longValues()[0];
            } else if (injectedClass == Boolean.class) {
                value = defaultAnnotation.booleanValues().length == 0 ? false
                        : defaultAnnotation.booleanValues()[0];
            } else if (injectedClass == Short.class) {
                value = defaultAnnotation.shortValues().length == 0 ? ((short) 0)
                        : defaultAnnotation.shortValues()[0];
            } else if (injectedClass == Float.class) {
                value = defaultAnnotation.floatValues().length == 0 ? 0f : defaultAnnotation.floatValues()[0];
            } else if (injectedClass == Double.class) {
                value = defaultAnnotation.doubleValues().length == 0 ? 0d : defaultAnnotation.doubleValues()[0];
            } else {
                log.warn("Default values for {} are not supported", injectedClass);
            }
        }
    } else {
        log.warn("Cannot provide default for {}", type);
    }
    return value;
}

From source file:org.codehaus.groovy.grails.web.converters.ConverterUtil.java

public static Object invokeOriginalAsTypeMethod(Object delegate, Class clazz) {
    if (clazz.isInstance(delegate))
        return delegate;
    else if (delegate instanceof Collection && clazz.isArray()) {
        int size = ((Collection) delegate).size();
        if (clazz.getComponentType() == Object.class) {
            if (size == 0) {
                return EMPTY_OBJECT_ARRAY;
            } else {
                return ((Collection) delegate)
                        .toArray((Object[]) Array.newInstance(clazz.getComponentType(), size));
            }// www .j  a v  a2s. co  m
        } else if (size == 0) {
            return Array.newInstance(clazz.getComponentType(), 0);
        } else {
            return DefaultTypeTransformation.asArray(delegate, clazz);
        }
    } else if (delegate instanceof Collection)
        return DefaultGroovyMethods.asType((Collection) delegate, clazz);
    else if (delegate instanceof Closure)
        return DefaultGroovyMethods.asType((Closure) delegate, clazz);
    else if (delegate instanceof Map)
        return DefaultGroovyMethods.asType((Map) delegate, clazz);
    else if (delegate instanceof Number)
        return DefaultGroovyMethods.asType((Number) delegate, clazz);
    else if (delegate instanceof File)
        return DefaultGroovyMethods.asType((File) delegate, clazz);
    else if (delegate instanceof String)
        return DefaultGroovyMethods.asType((String) delegate, clazz);
    else
        return DefaultGroovyMethods.asType(delegate, clazz);
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Maps primitives and other simple Java types into simple types supported by RAML
 * /*from   ww w. jav  a2 s. c om*/
 * @param clazz The Class to map
 * @return The Simple RAML ParamType which maps to this class or null if one is not found
 */
public static ParamType mapSimpleType(Class<?> clazz) {
    Class<?> targetClazz = clazz;
    if (targetClazz.isArray() && clazz.getComponentType() != null) {
        targetClazz = clazz.getComponentType();
    }
    if (targetClazz.equals(Long.TYPE) || targetClazz.equals(Long.class) || targetClazz.equals(Integer.TYPE)
            || targetClazz.equals(Integer.class) || targetClazz.equals(Short.TYPE)
            || targetClazz.equals(Short.class) || targetClazz.equals(Byte.TYPE)
            || targetClazz.equals(Byte.class)) {
        return ParamType.INTEGER;
    } else if (targetClazz.equals(Float.TYPE) || targetClazz.equals(Float.class)
            || targetClazz.equals(Double.TYPE) || targetClazz.equals(Double.class)
            || targetClazz.equals(BigDecimal.class)) {
        return ParamType.NUMBER;
    } else if (targetClazz.equals(Boolean.class) || targetClazz.equals(Boolean.TYPE)) {
        return ParamType.BOOLEAN;
    } else if (targetClazz.equals(String.class)) {
        return ParamType.STRING;
    }
    return null; // default to string
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RSMapper createReusable(Method method) {
    final UseRSMapper useRSMapper = method.getAnnotation(UseRSMapper.class);
    if (useRSMapper != null) {
        try {/*from ww w.  jav a 2 s.c o  m*/
            return useRSMapper.value().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    Class<?> returnClass = method.getReturnType();
    if (returnClass.isArray()) {
        Class itemType = returnClass.getComponentType();
        RowMapperFactory rowMapperFactory = getRowMapperFactory(itemType);
        return new ArrayRSMapper(rowMapperFactory, itemType);
    }
    Type returnType = method.getGenericReturnType();
    if (Collection.class.isAssignableFrom(returnClass)) {
        Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
        Type itemType = actualTypeArguments[0];
        return getCollectionORMapper(itemType);
    }
    if (Map.class.isAssignableFrom(returnClass)) {
        Type[] actualTypeArguments = ((ParameterizedType) returnType).getActualTypeArguments();
        Type keyType = actualTypeArguments[0];
        Type valueType = actualTypeArguments[1];
        RowMapperFactory keyMapperFactory = getScalarMapper(keyType, 1, true);
        RowMapperFactory valueMapperFactory = getRowMapperFactory(valueType, 2);
        return new MapRSMapper(keyMapperFactory, valueMapperFactory);
    }
    return new SingleRowRSMapper(getRowMapperFactory(returnType));
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * <p>Checks if the specified type is permitted as an annotation member.</p>
 *
 * <p>The Java language specification only permits certain types to be used
 * in annotations. These include {@link String}, {@link Class}, primitive
 * types, {@link Annotation}, {@link Enum}, and single-dimensional arrays of
 * these types.</p>/*from w  ww . ja  v  a 2 s .  c o m*/
 *
 * @param type the type to check, {@code null}
 * @return {@code true} if the type is a valid type to use in an annotation
 */
public static boolean isValidAnnotationMemberType(Class<?> type) {
    if (type == null) {
        return false;
    }
    if (type.isArray()) {
        type = type.getComponentType();
    }
    return type.isPrimitive() || type.isEnum() || type.isAnnotation() || String.class.equals(type)
            || Class.class.equals(type);
}

From source file:org.dhatim.javabean.BeanInstanceCreator.java

private static String toClassName(Class<?> beanClass) {
    if (!beanClass.isArray()) {
        return beanClass.getName();
    } else {//from w  w w  .  jav a2 s .  com
        return beanClass.getComponentType().getName() + "[]";
    }
}

From source file:com.jsen.javascript.java.HostedJavaMethod.java

/**
 * Casts the given arguments into given expected types.
 * //from ww w  .  jav a 2 s .co  m
 * @param expectedTypes Types into which should be casted the given arguments.
 * @param args Arguments to be casted.
 * @return Array of the casted arguments if casting was successful, otherwise null.
 */
public static Object[] castArgs(Class<?>[] expectedTypes, Object... args) {
    if (expectedTypes != null && args != null) {

        if (expectedTypes.length <= args.length + 1 && expectedTypes.length > 0) {
            Class<?> lastType = expectedTypes[expectedTypes.length - 1];
            if (lastType.isArray()) {
                Class<?> arrayType = lastType.getComponentType();

                boolean maybeVarargs = true;
                if (expectedTypes.length == args.length) {
                    Object lastArg = args[args.length - 1];
                    Class<?> lastArgClass = (lastArg != null) ? lastArg.getClass() : null;
                    maybeVarargs = lastArgClass != null && !ClassUtils.isAssignable(lastArgClass, lastType);
                }

                if (maybeVarargs) {
                    for (int i = expectedTypes.length - 1; i < args.length; i++) {
                        if (args[i] == null) {
                            continue;
                        }
                        Class<?> argType = args[i].getClass();

                        if (!ClassUtils.isAssignable(argType, arrayType)) {
                            maybeVarargs = false;
                            break;
                        }
                    }

                    if (maybeVarargs) {
                        Object[] oldArgs = args;
                        args = new Object[expectedTypes.length];

                        for (int i = 0; i < expectedTypes.length - 1; i++) {
                            args[i] = oldArgs[i];
                        }

                        Object[] varargs = new Object[oldArgs.length - expectedTypes.length + 1];

                        for (int i = expectedTypes.length - 1; i < oldArgs.length; i++) {
                            varargs[i - expectedTypes.length + 1] = oldArgs[i];
                        }

                        args[expectedTypes.length - 1] = varargs;
                    }
                }
            }
        }

        if (expectedTypes.length == args.length) {
            Object[] castedArgs = new Object[args.length];
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                Class<?> expectedType = expectedTypes[i];
                if (arg == null) {
                    castedArgs[i] = null;
                } else if (arg == Undefined.instance) {
                    castedArgs[i] = null;
                } else if (arg instanceof ConsString) {
                    castedArgs[i] = ((ConsString) arg).toString();
                } else if (arg instanceof Double
                        && (expectedType.equals(Integer.class) || expectedType.equals(int.class)
                                || expectedType.equals(Long.class) || expectedType.equals(long.class))) {
                    castedArgs[i] = ((Double) arg).intValue();
                } else {
                    castedArgs[i] = JavaScriptEngine.jsToJava(arg);
                    //castedArgs[i] = Context.jsToJava(castedArgs[i], expectedType);
                }

                castedArgs[i] = HostedJavaObject.wrap(expectedTypes[i], castedArgs[i]);
            }

            return castedArgs;
        }
    }

    return null;
}