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.unboundid.scim2.common.utils.SchemaUtils.java

/**
 * Returns true if the supplied class is a collection or an array. This
 * is primarily used to determine if it's a multivalued attribute.
 *
 * @param cls the class to check./*ww  w . j  a  va 2s . c  om*/
 * @return true if the class is a collection or an array, or false if not.
 */
private static boolean isCollectionOrArray(final Class<?> cls) {
    return (cls.isArray() && byte[].class != cls) || Collection.class.isAssignableFrom(cls);

}

From source file:net.sf.json.JSONUtils.java

/**
 * DOCUMENT ME!/*from  w w  w  .jav a 2 s . com*/
 *
 * @param type DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public static Class getInnerComponentType(Class type) {
    if (!type.isArray()) {
        return type;
    }

    return getInnerComponentType(type.getComponentType());
}

From source file:com.amazon.carbonado.layout.LayoutFactory.java

private static int annValueHashCode(Object value) {
    Class type = value.getClass();
    if (!type.isArray()) {
        if (value instanceof String || type.isPrimitive()) {
            return value.hashCode();
        } else if (value instanceof Class) {
            // Use name for stable hash code.
            return ((Class) value).getName().hashCode();
        } else if (value instanceof Enum) {
            // Use name for stable hash code.
            return ((Enum) value).name().hashCode();
        } else if (value instanceof Annotation) {
            return annHashCode((Annotation) value);
        } else {//  ww  w . ja  v  a2 s  .  c  om
            return value.hashCode();
        }
    } else if (type == byte[].class) {
        return Arrays.hashCode((byte[]) value);
    } else if (type == char[].class) {
        return Arrays.hashCode((char[]) value);
    } else if (type == double[].class) {
        return Arrays.hashCode((double[]) value);
    } else if (type == float[].class) {
        return Arrays.hashCode((float[]) value);
    } else if (type == int[].class) {
        return Arrays.hashCode((int[]) value);
    } else if (type == long[].class) {
        return Arrays.hashCode((long[]) value);
    } else if (type == short[].class) {
        return Arrays.hashCode((short[]) value);
    } else if (type == boolean[].class) {
        return Arrays.hashCode((boolean[]) value);
    } else {
        return Arrays.hashCode((Object[]) value);
    }
}

From source file:com.mtgi.analytics.aop.BehaviorTrackingAdvice.java

/**
 * marshal the given value to a String.  Only java builtin types,
 * and arrays of those types, are converted; everything else is
 * represented as "[qualified.type.name]" to avoid invoking
 * costly (or buggy) toString() methods.
 *///from w  w w  .j a v  a2s  .co  m
private static final void logValue(EventDataElement parent, String elementName, Class<?> expectedType,
        Object arg) {
    EventDataElement element = parent.addElement(elementName);
    if (arg == null)
        return;

    Class<?> type = arg.getClass();
    //log the concrete type of the argument if it differs from the expected type (i.e. is a subclass)
    //the primitive type checks avoid logging redundant type info for autoboxed values
    if (type != expectedType && !(expectedType.isPrimitive() || type.isPrimitive()))
        element.add("type", type.getName());

    //TODO: use annotations or some other configuration for custom
    //parameter logging?
    String value = "{object}";
    if (type.isArray()) {
        if (shouldLog(type.getComponentType()))
            value = toStringArray(arg);
    } else {
        if (shouldLog(type))
            value = arg.toString();
    }
    element.setText(value);
}

From source file:com.github.juanmf.java2plant.Parser.java

protected static void addAggregation(Set<Relation> relations, Class<?> fromType, Field f) {
    Class<?> delegateType = f.getType();
    String varName = f.getName();
    String message = varName + ": " + TypesHelper.getSimpleName(f.getGenericType().toString());
    String toCardinal = "1";
    String toName = delegateType.getName();
    if (isMulti(delegateType)) {
        toCardinal = "*";
        if (!delegateType.isArray()) {
            Set<String> typeVars = getTypeParams(f);
            for (String type : typeVars) {
                Relation aggregation = new Aggregation(fromType, type, f, toCardinal, message);
                relations.add(aggregation);
            }//  www.ja v a 2 s . c o m
            return;
        }
        toName = CanonicalName.getClassName(delegateType.getName());
    }
    Relation aggregation = new Aggregation(fromType, toName, f, toCardinal, message);
    relations.add(aggregation);
}

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

/**
 * recursively marshall [multidimensional? - of course!!! ] array
 *
 * @param array/*  w w  w.j  av a 2 s.  c om*/
 * @return
 */
public static JSONArray marshallArray(Object array)
        throws InvocationTargetException, NoSuchMethodException, JSONException, IllegalAccessException {
    if (array != null && array.getClass().isArray()) {
        Class<?> componentType = array.getClass().getComponentType();
        JSONArray retval = new JSONArray();
        final int arrayLength = Array.getLength(array);
        // stirngs and primitives must be marshalled directly
        if (componentType.isPrimitive() || String.class.equals(componentType)) {

            for (int i = 0; i < arrayLength; i++) {
                retval.put(Array.get(array, i));
            }
        } else if (componentType.isArray()) {
            // that's cool, nested array recurse
            for (int i = 0; i < arrayLength; i++) {
                retval.put(marshallArray(Array.get(array, i)));
            }
        } else {
            // treat component as a bean   if it got default constructor
            try {
                if (componentType.getConstructor() != null) {
                    for (int i = 0; i < arrayLength; i++) {
                        retval.put(marshall(Array.get(array, i)));
                    }
                }
            } catch (NoSuchMethodException ex) {
                // just ignore it here, it means no such constructor was found
            }
        }

        return retval;
    }

    return null;
}

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

/**
 * A factory method for creating a ConfigurationParameter object.
 */// ww  w  .j a va2  s  .  co  m
public static ConfigurationParameter createPrimitiveParameter(String name, Class<?> parameterClass,
        String parameterDescription, boolean isMandatory) {
    String parameterClassName;
    if (parameterClass.isArray()) {
        parameterClassName = parameterClass.getComponentType().getName();
    } else {
        parameterClassName = parameterClass.getName();
    }

    String parameterType = javaUimaTypeMap.get(parameterClassName);
    if (parameterType == null) {
        // If we cannot map the type, we'll try to convert it to a String
        parameterType = ConfigurationParameter.TYPE_STRING;
    }
    return createPrimitiveParameter(name, parameterType, parameterDescription, parameterClass.isArray(),
            isMandatory);
}

From source file:com.alibaba.doris.admin.service.impl.ValueParseUtil.java

/**
 * ?String ?://from  w w w.jav a  2  s  . com
 * 
 * <pre>
 * short, int, long, float : 0
 * char, byte: 0
 * String: null
 * Map, List: null
 * Integer, Long, Float : null
 * Date: null
 * array: null
 * </pre>
 * 
 * @param strValue
 * @param clazz
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> T parseStringValue(String strValue, Class<T> clazz, boolean autoDefault) {

    if (DEF_NULL.equals(strValue)) {
        if (!clazz.isPrimitive()) {
            return null;
        }
        if (autoDefault) {
            return (T) getInternalDefaultValue(clazz);
        } else {
            return null;
        }
    }

    if (DEF_EMPTY.equals(strValue)) {
        if (clazz.isArray()) {
            return (T) Array.newInstance(clazz.getComponentType(), 0);
        }

        if (Map.class.isAssignableFrom(clazz)) {
            return (T) Collections.EMPTY_MAP;
        }

        if (List.class.isAssignableFrom(clazz)) {
            return (T) new ArrayList<Object>();
        }

        if (Set.class.isAssignableFrom(clazz)) {
            return (T) new HashSet<Object>();
        }

        if (String.class.equals(clazz)) {
            return (T) StringUtils.EMPTY;
        }

        if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
            return (T) Character.valueOf(' ');
        }

        if (autoDefault) {
            return (T) getInternalDefaultValue(clazz);
        } else {
            return null;
        }
    }

    if (StringUtils.isBlank(strValue)) {// ?
        if (autoDefault) {
            return (T) getInternalDefaultValue(clazz);
        } else {
            return null;
        }
    } else {

        if (String.class.equals(clazz)) {
            return (T) strValue;
        }

        if (Short.TYPE.equals(clazz) || Short.class.equals(clazz)) {
            return (T) Short.valueOf(strValue);
        }

        if (Integer.TYPE.equals(clazz) || Integer.class.equals(clazz)) {
            return (T) Integer.valueOf(strValue);
        }
        if (Long.TYPE.equals(clazz) || Long.class.equals(clazz)) {
            return (T) Long.valueOf(strValue);
        }
        if (Boolean.TYPE.equals(clazz) || Boolean.class.equals(clazz)) {
            return (T) Boolean.valueOf(strValue);
        }
        if (Float.TYPE.equals(clazz) || Float.class.equals(clazz)) {
            return (T) Float.valueOf(strValue);
        }
        if (Double.TYPE.equals(clazz) || Double.class.equals(clazz)) {
            return (T) Double.valueOf(strValue);
        }
        if (Byte.TYPE.equals(clazz) || Byte.class.equals(clazz)) {
            return (T) Byte.valueOf(strValue);
        }
        if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
            return (T) Character.valueOf(strValue.charAt(0));
        }

        if (clazz.isArray()) {
            final Class<?> componentType = clazz.getComponentType();
            // String[]
            if (String.class.equals(componentType)) {
                return (T) StringUtils.split(strValue, ',');
            }
            // ?char[]
            if (Character.TYPE.equals(componentType)) {
                return (T) strValue.toCharArray();
            }

            if (Character.class.equals(componentType)) {
                final char[] tmp = strValue.toCharArray();
                final Character[] result = new Character[tmp.length];
                for (int i = 0; i < result.length; i++) {
                    result[i] = tmp[i];
                }
                return (T) result;
            }

            if (Byte.TYPE.equals(componentType) || Byte.class.equals(componentType)) {
                return (T) (strValue == null ? null : strValue.getBytes());
            }
        }
    }

    return null;

}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Creates a parameter from the value. If the value instance of String then
 * this method calls the converter methods.
 *
 * @param fieldClass The class of field/*from  w  ww .j a v a  2 s.  co  m*/
 * @param value The value
 * @return The converted instance
 * @throws ClassNotFoundException If class not found
 * @throws ParseException If the pattern cannot be parseable
 * @throws UnsupportedEncodingException If the Base64 stream contains non UTF-8 chars
 */
protected static Object createParameterFromValue(final Class fieldClass, final Object value)
        throws ClassNotFoundException, ParseException, UnsupportedEncodingException {
    Object parameter = value;

    if (!value.getClass().equals(fieldClass) && !PRIMITIVE_WRAPPER_CLASSES.contains(value.getClass())) {
        if (fieldClass.isArray() && value instanceof List) {
            String arrayElementTypeName = fieldClass.getName().substring(2, fieldClass.getName().length() - 1);
            Class arrayElementType = Class.forName(arrayElementTypeName);
            Object[] at = (Object[]) Array.newInstance(arrayElementType, 0);
            parameter = ((List<Object>) value).toArray(at);
        } else {
            String stringValue = createStringFromValue(value);
            if (stringValue != null) {
                parameter = convertToPrimitive(fieldClass, stringValue);
                parameter = parameter == null ? convertToPrimitiveWrapper(fieldClass, stringValue) : parameter;
                parameter = parameter == null ? convertToBigNumbers(fieldClass, stringValue) : parameter;
                parameter = parameter == null ? convertToOthers(fieldClass, stringValue) : parameter;
            }
        }
    }

    return parameter;
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

public static final boolean canConstructTarget(Class _class) {
    boolean isFinal = Modifier.isFinal(_class.getModifiers());
    boolean isArray = _class.isArray();
    boolean isEnum = _class.isEnum();

    return (isFinal == false && isArray == false && isEnum == false);
}