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.jaspersoft.jasperserver.ws.axis2.RepositoryHelper.java

protected Object getMultiParameterValues(JRParameter parameter, Collection values) {
    Object parameterValue;//  www .j  a va  2s  . c om
    Class parameterType = parameter.getValueClass();
    if (parameterType.equals(Object.class) || parameterType.equals(Collection.class)
            || parameterType.equals(Set.class) || parameterType.equals(List.class)) {
        Collection paramValues;
        if (parameterType.equals(List.class)) {
            //if the parameter type is list, use a list
            paramValues = new ArrayList(values.size());
        } else {
            //else use an ordered set
            paramValues = new ListOrderedSet();
        }

        Class componentType = parameter.getNestedType();
        for (Iterator it = values.iterator(); it.hasNext();) {
            Object val = (Object) it.next();
            Object paramValue;
            if (componentType == null || !(val instanceof String)) {
                //no conversion if no nested type set for the parameter
                paramValue = val;
            } else {
                paramValue = stringToValue((String) val, componentType);
            }
            paramValues.add(paramValue);
        }
        parameterValue = paramValues;
    } else if (parameterType.isArray()) {
        Class componentType = parameterType.getComponentType();
        parameterValue = Array.newInstance(componentType, values.size());
        int idx = 0;
        for (Iterator iter = values.iterator(); iter.hasNext(); ++idx) {
            Object val = iter.next();
            Object paramValue;
            if (val instanceof String) {
                paramValue = stringToValue((String) val, componentType);
            } else {
                paramValue = val;
            }
            Array.set(parameterValue, idx, paramValue);
        }
    } else {
        parameterValue = values;
    }
    return parameterValue;
}

From source file:com.jaspersoft.jasperserver.ws.axis2.scheduling.ReportJobBeanTraslator.java

protected Object toCollectionValue(Class parameterType, Object valueArray) {
    Object reportValue;/* w  w  w .  j ava2 s  .  com*/
    int valueCount = Array.getLength(valueArray);
    if (parameterType.equals(Object.class) || parameterType.equals(Collection.class)
            || parameterType.equals(Set.class)) {
        Collection values = new ListOrderedSet();
        for (int i = 0; i < valueCount; ++i) {
            values.add(Array.get(valueArray, i));
        }
        reportValue = values;
    } else if (parameterType.equals(List.class)) {
        Collection values = new ArrayList(valueCount);
        for (int i = 0; i < valueCount; ++i) {
            values.add(Array.get(valueArray, i));
        }
        reportValue = values;
    } else if (parameterType.isArray()) {
        Class componentType = parameterType.getComponentType();
        if (componentType.equals(valueArray.getClass().getComponentType())) {
            reportValue = valueArray;
        } else {
            reportValue = Array.newInstance(componentType, valueCount);
            for (int i = 0; i < valueCount; ++i) {
                Array.set(reportValue, i, Array.get(valueArray, i));
            }
        }
    } else {
        throw new JSException("report.scheduling.ws.collection.parameter.type.not.supported",
                new Object[] { parameterType.getName() });
    }
    return reportValue;
}

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

/**
 * Returns true if it can convert the supplied
 * object to the specified class.//from w  ww  . jav  a  2 s .c  o m
 * @param object to check
 * @param toType prospective destination class
 * @return boolean
 */
public boolean canConvert(Object object, final Class toType) {
    if (object == null) {
        return true;
    }
    final Class useType = TypeUtils.wrapPrimitive(toType);
    Class fromType = object.getClass();

    if (useType.isAssignableFrom(fromType)) {
        return true;
    }

    if (useType == String.class) {
        return true;
    }

    if (object instanceof Boolean && (Number.class.isAssignableFrom(useType)
            || "java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName()))) {
        return true;
    }
    if (object instanceof Number && (Number.class.isAssignableFrom(useType) || useType == Boolean.class)) {
        return true;
    }
    if (object instanceof String && (useType == Boolean.class || useType == Character.class
            || useType == Byte.class || useType == Short.class || useType == Integer.class
            || useType == Long.class || useType == Float.class || useType == Double.class)) {
        return true;
    }
    if (fromType.isArray()) {
        // Collection -> array
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            int length = Array.getLength(object);
            for (int i = 0; i < length; i++) {
                Object value = Array.get(object, i);
                if (!canConvert(value, cType)) {
                    return false;
                }
            }
            return true;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            return canCreateCollection(useType);
        }
        if (Array.getLength(object) > 0) {
            Object value = Array.get(object, 0);
            return canConvert(value, useType);
        }
        return canConvert("", useType);
    }
    if (object instanceof Collection) {
        // Collection -> array
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            Iterator it = ((Collection) object).iterator();
            while (it.hasNext()) {
                Object value = it.next();
                if (!canConvert(value, cType)) {
                    return false;
                }
            }
            return true;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            return canCreateCollection(useType);
        }
        if (((Collection) object).size() > 0) {
            Object value;
            if (object instanceof List) {
                value = ((List) object).get(0);
            } else {
                Iterator it = ((Collection) object).iterator();
                value = it.next();
            }
            return canConvert(value, useType);
        }
        return canConvert("", useType);
    }
    if (object instanceof NodeSet) {
        return canConvert(((NodeSet) object).getValues(), useType);
    }
    if (object instanceof Pointer) {
        return canConvert(((Pointer) object).getValue(), useType);
    }
    return ConvertUtils.lookup(useType) != null;
}

From source file:com.astamuse.asta4d.data.DefaultDataTypeTransformer.java

private List<DataValueConvertor> extractConvertors(final Class<?> srcType, final Class<?> targetType) {

    List<DataValueConvertor> foundConvertorList = new LinkedList<DataValueConvertor>();

    // find in list as element to element
    for (DataValueConvertor convertor : DataTypeConvertorList) {
        Pair<Class, Class> typePair = extractConvertorTypeInfo(convertor);
        if (typePair == null) {
            continue;
        }/*from  w w  w .  ja  va 2s  .co m*/
        if (typePair.getLeft().isAssignableFrom(srcType)) {
            if (targetType.isAssignableFrom(typePair.getRight())) {// found one
                foundConvertorList.add(convertor);
            } else if (convertor instanceof DataValueConvertorTargetTypeConvertable
                    && typePair.getRight().isAssignableFrom(targetType)) {
                foundConvertorList
                        .add(((DataValueConvertorTargetTypeConvertable) convertor).convert(targetType));
            }
        }
        // @formatter:on
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    // find as array to array
    if (srcType.isArray() && targetType.isArray()) {

        List<DataValueConvertor> componentConvertorList = findConvertor(srcType.getComponentType(),
                targetType.getComponentType());
        List<DataValueConvertor> toArrayConvertorList = ListConvertUtil.transform(componentConvertorList,
                new RowConvertor<DataValueConvertor, DataValueConvertor>() {
                    @Override
                    public DataValueConvertor convert(int rowIndex,
                            final DataValueConvertor originalConvertor) {
                        return new DataValueConvertor() {
                            Pair<Class, Class> typePair = extractConvertorTypeInfo(originalConvertor);

                            @Override
                            public Object convert(Object obj) throws UnsupportedValueException {
                                if (typePair == null) {
                                    return null;
                                }

                                int length = Array.getLength(obj);
                                Object targetArray = Array.newInstance(targetType.getComponentType(), length);

                                for (int i = 0; i < length; i++) {
                                    Array.set(targetArray, i, originalConvertor.convert(Array.get(obj, i)));
                                }
                                return targetArray;
                            }
                        };
                    }
                });

        foundConvertorList.addAll(toArrayConvertorList);
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    // find as element to array
    if (targetType.isArray()) {

        List<DataValueConvertor> componentConvertorList = findConvertor(srcType, targetType.getComponentType());
        List<DataValueConvertor> toArrayConvertorList = ListConvertUtil.transform(componentConvertorList,
                new RowConvertor<DataValueConvertor, DataValueConvertor>() {
                    @Override
                    public DataValueConvertor convert(int rowIndex,
                            final DataValueConvertor originalConvertor) {
                        return new DataValueConvertor() {
                            private Pair<Class, Class> typePair = extractConvertorTypeInfo(originalConvertor);

                            @Override
                            public Object convert(Object obj) throws UnsupportedValueException {
                                if (typePair == null) {
                                    return null;
                                }
                                Object array = Array.newInstance(targetType.getComponentType(), 1);
                                Array.set(array, 0, originalConvertor.convert(obj));
                                return array;
                            }
                        };
                    }
                });

        foundConvertorList.addAll(toArrayConvertorList);
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    // find as array to element
    if (srcType.isArray()) {
        List<DataValueConvertor> componentConvertorList = findConvertor(srcType.getComponentType(), targetType);
        List<DataValueConvertor> toArrayConvertorList = ListConvertUtil.transform(componentConvertorList,
                new RowConvertor<DataValueConvertor, DataValueConvertor>() {
                    @Override
                    public DataValueConvertor convert(int rowIndex,
                            final DataValueConvertor originalConvertor) {
                        return new DataValueConvertor() {
                            @Override
                            public Object convert(Object obj) throws UnsupportedValueException {
                                int length = Array.getLength(obj);
                                if (length == 0) {
                                    return null;
                                } else {
                                    return originalConvertor.convert(Array.get(obj, 0));
                                }
                            }
                        };
                    }
                });

        foundConvertorList.addAll(toArrayConvertorList);
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    return foundConvertorList;
}

From source file:net.sf.jrf.domain.PersistentObjectDynaProperty.java

/** Constructs instance and property name and class.
* @param name property name.//ww w .  j  a va 2 s  .com
* @param cls value object <code>Class</code>.
* @param readMethodName name of the read <code>Method</code>
* @param writeMethodName name of the write <code>Method</code>
*/
public PersistentObjectDynaProperty(String name, Class cls, String readMethodName, String writeMethodName) {
    super(name, cls);
    this.readMethodName = readMethodName;
    this.writeMethodName = writeMethodName;
    if (cls.isPrimitive()) {
        if (cls.equals(Boolean.TYPE))
            primitiveWrapperClass = Boolean.class;
        else if (cls.equals(Byte.TYPE))
            primitiveWrapperClass = Byte.class;
        else if (cls.equals(Character.TYPE))
            primitiveWrapperClass = Character.class;
        else if (cls.equals(Double.TYPE))
            primitiveWrapperClass = Double.class;
        else if (cls.equals(Float.TYPE))
            primitiveWrapperClass = Float.class;
        else if (cls.equals(Integer.TYPE))
            primitiveWrapperClass = Integer.class;
        else if (cls.equals(Long.TYPE))
            primitiveWrapperClass = Long.class;
        else if (cls.equals(Short.TYPE))
            primitiveWrapperClass = Short.class;

    } else if (java.util.List.class.isAssignableFrom(cls) || cls.isArray()) {
        indexed = true;
    } else if (java.util.Map.class.isAssignableFrom(cls)) {
        mapped = true;
    }
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String classToParamName(Class<?> c) {
    if (c.isArray()) {
        return classToParamName(c.getComponentType()) + "Array";
    }/*  w w w.j ava  2 s . c  o m*/
    return c.getSimpleName().substring(0, 1).toLowerCase() + c.getSimpleName().substring(1);
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static boolean isPrimitiveArray(Class clss) {
    return clss.isArray() && clss.getComponentType().isPrimitive();
}

From source file:com.googlecode.jsonrpc4j.JsonRpcServer.java

/**
 * Determines whether or not the given {@link JsonNode} matches
 * the given type.  This method is limitted to a few java types
 * only and shouldn't be used to determine with great accuracy
 * whether or not the types match./*from   ww w. j a  va2 s  . com*/
 *
 * @param node the {@link JsonNode}
 * @param type the {@link Class}
 * @return true if the types match, false otherwise
 */
private boolean isMatchingType(JsonNode node, Class<?> type) {

    if (node.isNull()) {
        return true;

    } else if (node.isTextual()) {
        return String.class.isAssignableFrom(type);

    } else if (node.isNumber()) {
        return Number.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)
                || int.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)
                || float.class.isAssignableFrom(type) || double.class.isAssignableFrom(type);

    } else if (node.isArray() && type.isArray()) {
        return (node.size() > 0) ? isMatchingType(node.get(0), type.getComponentType()) : false;

    } else if (node.isArray()) {
        return type.isArray() || Collection.class.isAssignableFrom(type);

    } else if (node.isBinary()) {
        return byte[].class.isAssignableFrom(type) || Byte[].class.isAssignableFrom(type)
                || char[].class.isAssignableFrom(type) || Character[].class.isAssignableFrom(type);

    } else if (node.isBoolean()) {
        return boolean.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type);

    } else if (node.isObject() || node.isPojo()) {
        return !type.isPrimitive() && !String.class.isAssignableFrom(type)
                && !Number.class.isAssignableFrom(type) && !Boolean.class.isAssignableFrom(type);
    }

    // not sure if it's a matching type
    return false;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String classToParamNameDecl(Class<?> c, int index) {
    if (c.isArray()) {
        return classToParamName(c.getComponentType()) + "Array_" + index;
    }/*from  ww  w  .  j  av  a2s.  co m*/
    return c.getSimpleName().substring(0, 1).toLowerCase() + c.getSimpleName().substring(1) + "_" + index;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String addCppJThis(Class c) {
    if (c.isPrimitive() || c.isArray() || isString(c)) {
        return "";
    } else {/*from w ww . ja va  2 s.c  o  m*/
        return ".jthis";
    }
}