Example usage for java.lang Class isArray

List of usage examples for java.lang Class isArray

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isArray();

Source Link

Document

Determines if this Class object represents an array class.

Usage

From source file:com.link_intersystems.lang.Conversions.java

/**
 *
 * @param from//from ww w  .  ja  v  a2 s  .com
 * @param to
 * @return true if from is a subtype of t.
 * @since 1.0.0.0
 */
public static boolean isWideningReference(Class<?> from, Class<?> to) {
    Assert.notNull("from", from);
    Assert.notNull("to", to);
    if (from.isArray() && to.isArray()) {
        from = from.getComponentType();
        to = to.getComponentType();
    }
    return to.isAssignableFrom(from);
}

From source file:nl.ucan.navigate.NestedPath.java

private static Class getCollectionReturnType(String property, Class clasz) throws IntrospectionException {
    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(property, clasz);
    Method method = propertyDescriptor.getReadMethod();
    Class klass = GenericCollectionTypeResolver.getCollectionReturnType(method);
    if (klass == null) {
        klass = GenericTypeResolver.resolveReturnType(method, clasz);
        if (klass.isArray()) {
            return klass.getComponentType();
        } else {/*from   w  w w  .  java  2  s.c  o  m*/
            throw new IllegalStateException("unsupported return type");
        }
    } else
        return klass;
}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * Returns 1 if the type is a collection,
 * -1 if it is definitely not//from w  w w  .  j a va2s . c  o  m
 * and 0 if it may be a collection in some cases.
 * @param clazz to test
 * @return int
 */
public static int getCollectionHint(Class clazz) {
    if (clazz.isArray()) {
        return 1;
    }

    if (Collection.class.isAssignableFrom(clazz)) {
        return 1;
    }

    if (clazz.isPrimitive()) {
        return -1;
    }

    if (clazz.isInterface()) {
        return 0;
    }

    if (Modifier.isFinal(clazz.getModifiers())) {
        return -1;
    }

    return 0;
}

From source file:edu.usu.sdl.openstorefront.util.ReflectionUtil.java

/**
 * This check for Value Model Objects/*from w  ww  . jav  a2s . c  o  m*/
 *
 * @param fieldClass
 * @return
 */
public static boolean isComplexClass(Class fieldClass) {
    Objects.requireNonNull(fieldClass, "Class is required");

    boolean complex = false;
    if (!fieldClass.isPrimitive() && !fieldClass.isArray()
            && !fieldClass.getSimpleName().equalsIgnoreCase(String.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Long.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Integer.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Boolean.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Double.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Float.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(BigDecimal.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Date.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(List.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Map.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Collection.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Queue.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(Set.class.getSimpleName())
            && !fieldClass.getSimpleName().equalsIgnoreCase(BigInteger.class.getSimpleName())) {
        complex = true;
    }
    return complex;
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

private static boolean assignPropertyValue(PropertyDescriptor sourceProp, Object sourceValue,
        PropertyDescriptor destProp, Object destInstance) {
    if (sourceValue == null) {
        System.out.println("Null value found, assignment skipped");
        return true;
    }//from  w  w  w .j a  v  a2s  . c  o m
    boolean result = false;
    Class<?> sourceType = sourceProp.getPropertyType();
    Method getter = sourceProp.getReadMethod();
    Class<?> destType = destProp.getPropertyType();
    Method setter = destProp.getWriteMethod();
    try {
        if (destType.isInterface() || destType.isArray() || destType.isEnum()) {
            if (Collection.class.isAssignableFrom(sourceType) && Collection.class.isAssignableFrom(destType))
                assignCollectionValue(getter, sourceValue, destType, setter, destInstance);
            else if (Map.class.isAssignableFrom(sourceType) && Map.class.isAssignableFrom(destType))
                assignMapValue(getter, sourceValue, setter, destInstance);
            else
                assignMixedTypesValue(sourceType, getter, sourceValue, destType, setter, destInstance);
        } else if (destType.isInstance(sourceValue) || destType.isPrimitive())
            setter.invoke(destInstance, sourceValue);

        else if (destType.isMemberClass())
            setter.invoke(destInstance, sourceValue);

        else if (destType.isAssignableFrom(sourceType))
            setter.invoke(destInstance, destType.cast(sourceValue));

        else { //if (ClassUtils.isInnerClass(destType)) {
            Object member = transposeModel(sourceType, destType, sourceValue);
            member = destType.cast(member);
            setter.invoke(destInstance, member);
        }
        result = true;
    } catch (IllegalArgumentException ex) {
        System.out.println("Looks like type mismatch, source type: " + sourceType + ", dest type: " + destType);
        Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvocationTargetException | IllegalAccessException ex) {
        Logger.getLogger(ServiceModelTranslator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

From source file:org.uimafit.factory.ConfigurationParameterFactory.java

private static String getConfigurationParameterType(Field field) {
    Class<?> parameterClass = field.getType();
    String parameterClassName;// w  ww .  j ava2 s . c  o  m
    if (parameterClass.isArray()) {
        parameterClassName = parameterClass.getComponentType().getName();
    } else if (Collection.class.isAssignableFrom(parameterClass)) {
        ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
        parameterClassName = ((Class<?>) (collectionType.getActualTypeArguments()[0])).getName();
    } else {
        parameterClassName = parameterClass.getName();
    }
    String parameterType = javaUimaTypeMap.get(parameterClassName);
    if (parameterType == null) {
        return ConfigurationParameter.TYPE_STRING;
    }
    return parameterType;
}

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

/**
 * recursively marshall to JSON/*w  w w .  j a  va  2s .c  om*/
 *
 * @param sink
 * @param object
 */
static void marshallRecursive(JSONObject sink, Object object)
        throws JSONException, InvocationTargetException, IllegalAccessException, NoSuchMethodException {
    // nothing to marshall
    if (object == null)
        return;
    // primitive object is a field and does not interes us here
    if (object.getClass().isPrimitive())
        return;
    // object not null,  and is not primitive - iterate through getters
    for (Method method : object.getClass().getMethods()) {
        // our getters are parameterless and start with "get"
        if ((method.getName().startsWith(GETTER_PREFIX) && method.getName().length() > BEGIN_INDEX
                || method.getName().startsWith(IS_PREFIX) && method.getName().length() > IS_LENGTH)
                && (method.getModifiers() & Modifier.PUBLIC) != 0 && method.getParameterTypes().length == 0
                && method.getReturnType() != void.class) {
            // is return value primitive?
            Class<?> type = method.getReturnType();
            if (type.isPrimitive() || String.class.equals(type)) {
                // it is, marshall it
                Object val = method.invoke(object);
                if (val != null) {
                    sink.put(propertize(method.getName()), val);
                }
                continue;
            } else if (type.isArray()) {
                Object val = marshallArray(method.invoke(object));
                if (val != null) {
                    sink.put(propertize(method.getName()), val);
                }
                continue;
            } else if (type.isAssignableFrom(Date.class)) {
                Date date = (Date) method.invoke(object);
                if (date != null) {
                    sink.put(propertize(method.getName()), date.getTime());
                }
                continue;
            } else if (type.isAssignableFrom(Boolean.class)) {
                Boolean b = (Boolean) method.invoke(object);
                if (b != null) {
                    sink.put(propertize(method.getName()), b.booleanValue());
                }
                continue;
            } else if (type.isAssignableFrom(Integer.class)) {
                Integer i = (Integer) method.invoke(object);
                if (i != null) {
                    sink.put(propertize(method.getName()), i.intValue());
                }
                continue;
            } else if (type.isAssignableFrom(Long.class)) {
                Long l = (Long) method.invoke(object);
                if (l != null) {
                    sink.put(propertize(method.getName()), l.longValue());
                }
                continue;
            } else {
                // does it have default constructor?
                try {
                    if (method.getReturnType().getConstructor() != null) {
                        Object val = marshall(method.invoke(object));
                        if (val != null) {
                            sink.put(propertize(method.getName()), val);
                        }
                        continue;
                    }
                } catch (NoSuchMethodException ex) {
                    // just ignore it here, it means no such constructor was found
                }
            }
        }
    }
}

From source file:jp.furplag.util.commons.ObjectUtils.java

/**
 * substitute for {@code instanceof}.//w  w w  .j  av  a  2  s .c  om
 *
 * @param clazz the Object, return false if null.
 * @param classes array of {@link java.lang.Class}.
 * @return if true, {@code o.getClass()} (or Class<?> o) contains given Classes.
 */
public static boolean isAny(final Object o, final Class<?>... classes) {
    if (classes == null)
        return o == null;
    if (classes.length < 1)
        return false;
    Class<?> type = o == null ? null
            : (o instanceof Class) ? ((Class<?>) o)
                    : o.getClass().isArray() ? o.getClass().getComponentType() : o.getClass();
    for (Class<?> clazz : classes) {
        if (o == null && clazz == null)
            return true;
        if (o == null || clazz == null)
            continue;
        if (clazz.equals(type))
            return true;
        if (!(o instanceof Class)
                && ClassUtils.primitiveToWrapper(type).equals(ClassUtils.primitiveToWrapper(clazz)))
            return true;
        if (clazz.isArray() && type.equals(clazz.getComponentType()))
            return true;
    }

    return false;
}

From source file:com.sinosoft.one.data.jade.rowmapper.DefaultRowMapperFactory.java

private static Class<?> getRowType(StatementMetaData statementMetaData) {
    Class<?> returnClassType = statementMetaData.getMethod().getReturnType();
    if (Collection.class.isAssignableFrom(returnClassType) || Page.class.isAssignableFrom(returnClassType)) {
        return getRowTypeFromCollectionType(statementMetaData, returnClassType);
    } else if (Map.class == returnClassType) {
        return getRowTypeFromMapType(statementMetaData, returnClassType);
    } else if (returnClassType.isArray() && returnClassType != byte[].class) {
        // , ??/*from www.j  a  v  a2  s .  c o m*/
        return returnClassType.getComponentType();
    }

    // DAO?
    return returnClassType;
}

From source file:Mopex.java

/**
 * Returns a syntactically correct name for a class object. If the class
 * object represents an array, the proper number of square bracket pairs are
 * appended to the component type./*from   w  ww. j  av  a  2  s. c  o m*/
 * 
 * @return java.lang.String
 * @param cls
 *            java.lang.Class
 */
//start extract classNameToString
public static String getTypeName(Class cls) {
    if (!cls.isArray()) {
        return cls.getName();
    } else {
        return getTypeName(cls.getComponentType()) + "[]";
    }
}