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.day.cq.wcm.foundation.forms.MergedValueMap.java

/**
 * Converts the object to the given type.
 * @param obj object//  www.j a v  a2s  .co m
 * @param type type
 * @return the converted object
 */
@SuppressWarnings("unchecked")
private <T> T convert(Object obj, Class<T> type) {
    try {
        if (obj == null) {
            return null;
        } else if (type.isAssignableFrom(obj.getClass())) {
            return (T) obj;
        } else if (type.isArray()) {
            return (T) convertToArray(obj, type.getComponentType());
        } else if (obj instanceof Calendar && type == String.class) {
            // simulate jcrValue.getString()
            return (T) ISO8601.format((Calendar) obj);
        } else if (type == String.class) {
            return (T) String.valueOf(obj);
        } else if (type == Integer.class) {
            return (T) (Integer) Integer.parseInt(obj.toString());
        } else if (type == Long.class) {
            return (T) (Long) Long.parseLong(obj.toString());
        } else if (type == Double.class) {
            return (T) (Double) Double.parseDouble(obj.toString());
        } else if (type == Boolean.class) {
            return (T) (Boolean) Boolean.parseBoolean(obj.toString());
        } else {
            return null;
        }
    } catch (NumberFormatException e) {
        return null;
    }
}

From source file:com.astamuse.asta4d.web.form.flow.base.BasicFormFlowSnippetTrait.java

/**
 * Sub classes could override this method to customize how to handle the injection trace data for type unmatch errors.
 * //from   w w  w  .  java2 s.  c o  m
 * @param fieldName
 * @param fieldDataType
 * @param rawTraceData
 * @return
 */
default Object convertRawInjectionTraceDataToRenderingData(String fieldName, Class<?> fieldDataType,
        Object rawTraceData) {
    if (fieldDataType.isArray() && rawTraceData.getClass().isArray()) {
        return rawTraceData;
    } else if (rawTraceData.getClass().isArray()) {// but field data type is
                                                   // not array
        if (Array.getLength(rawTraceData) > 0) {
            return Array.get(rawTraceData, 0);
        } else {
            return null;
        }
    } else {
        return rawTraceData;
    }
}

From source file:org.guiceyfruit.spring.support.AutowiredMemberProvider.java

protected Object provide(Autowired annotation, Member member, TypeLiteral<?> typeLiteral, Class<?> memberType,
        Annotation[] annotations) {
    Predicate<Binding> filter = createQualifierFilter(member, annotations);

    Class<?> type = typeLiteral.getRawType();
    if (type.isArray()) {
        return provideArrayValue(member, typeLiteral, memberType, filter);
    } else if (Collection.class.isAssignableFrom(type)) {
        Collection collection = createCollection(type);
        return provideCollectionValues(collection, member, typeLiteral, filter);
    } else if (Map.class.isAssignableFrom(type)) {
        Map map = createMap(type);
        return provideMapValues(map, member, typeLiteral, filter);
    } else {//from  w w w  .  j  av a  2  s.  co  m
        return provideSingleValue(member, type, annotation, filter);
    }
}

From source file:com.doitnext.http.router.DefaultInvoker.java

private void mapQueryParameter(QueryParameter parameterAnnotation, Object[] arguments, Class<?> parameterClass,
        Map<String, String[]> queryArgs, int paramIndex) throws TypeConversionException {
    String queryArg[] = queryArgs.get(parameterAnnotation.name());
    if (queryArg == null)
        return;//from  ww  w  . j  av a2s.  com
    if (!parameterClass.isArray()) {
        if (queryArg.length > 0) {
            arguments[paramIndex] = stringConverter.convert(queryArg[0], parameterClass);
        }
    } else {
        Object queryParam = Array.newInstance(parameterClass.getComponentType(), queryArg.length);
        for (int x = 0; x < queryArg.length; x++) {
            Array.set(queryParam, x, stringConverter.convert(queryArg[x], parameterClass.getComponentType()));
        }
        arguments[paramIndex] = queryParam;
    }
}

From source file:com.expressui.core.dao.query.EntityQuery.java

/**
 * Clear this query so that all filters (query parameters) and sort-criteria are removed (except for defaults).
 * The method uses reflection to clear any filters defined as bean properties by subclasses. Once cleared,
 * re-execution of query results in all records being found or a default list of default filters are specified.
 *
 * @see #initializeDefaults//w  ww  .j  a v  a  2 s.  co  m
 */
public void clear() {
    try {
        for (PropertyDescriptor descriptor : descriptors) {
            Method writeMethod = descriptor.getWriteMethod();
            Method readMethod = descriptor.getReadMethod();
            if (readMethod != null && writeMethod != null
                    && !writeMethod.getDeclaringClass().equals(EntityQuery.class)
                    && !writeMethod.getDeclaringClass().equals(Object.class)) {
                Class type = descriptor.getPropertyType();
                if (type.isPrimitive() && !type.isArray()) {
                    if (ReflectionUtil.isNumberType(type)) {
                        writeMethod.invoke(this, 0);
                    } else if (Boolean.class.isAssignableFrom(type)) {
                        writeMethod.invoke(this, false);
                    }
                } else {
                    writeMethod.invoke(this, new Object[] { null });
                }
            }
        }
    } catch (IllegalAccessException e) {
        Assert.PROGRAMMING.fail(e);
    } catch (InvocationTargetException e) {
        Assert.PROGRAMMING.fail(e);
    }

    initializeDefaults();
}

From source file:com.manydesigns.elements.forms.TableForm.java

public void writeToObject(Object obj) {
    Class clazz = obj.getClass();
    if (clazz.isArray()) { // Tratta obj come un array
        // Scorre tutti gli elementi dell'array obj,
        // indipendentemente da quante righe ci sono nel table form.
        // Eventualmente lancia Eccezione.
        final int arrayLength = Array.getLength(obj);
        for (int i = 0; i < arrayLength; i++) {
            Object currentObj = Array.get(obj, i);
            rows[i].writeToObject(currentObj);
        }/* w w  w. ja  v a 2s  . c om*/
    } else if (Collection.class.isAssignableFrom(clazz)) {
        // Tratta obj come collection
        Collection collection = (Collection) obj;

        int i = 0;
        for (Object currentObj : collection) {
            rows[i].writeToObject(currentObj);
            i++;
        }
    }
}

From source file:org.castor.jaxb.reflection.ClassInfoBuilder.java

private void handleMultivaluedness(Class<?> fieldType, JaxbFieldNature fieldNature, Type type) {
    if (fieldType.isArray() || fieldType.isAssignableFrom(Collection.class)
            || Collection.class.isAssignableFrom(fieldType)) {
        fieldNature.setGenericType(type);
        fieldNature.setMultivalued(true);
    }//from   w ww.ja va 2  s .  c o  m

}

From source file:com.cloudera.api.model.ApiModelTest.java

private static void compareChildObjects(Method getter, Class<?> type, Object expected, Object deserialized) {
    if (isApiType(type)) {
        if (expected != null) {
            assertNotNull("Missing deserialized value for getter " + getMethodName(getter), deserialized);
            compareObjects(expected, deserialized);
        }//  www  .  j a v  a 2 s.  c om
    } else {
        assertNotNull("Missing expected value for getter " + getMethodName(getter), expected);
        assertNotNull("Missing deserialized value for getter " + getMethodName(getter), deserialized);
        String errorMessage = String.format("Values for getter '%s' don't match: '%s' != '%s'.",
                getMethodName(getter), expected, deserialized);
        if (type.isArray()) {
            assertTrue(errorMessage, ArrayUtils.isEquals(expected, deserialized));
        } else {
            assertEquals(errorMessage, expected, deserialized);
        }
    }
}

From source file:org.castor.jaxb.reflection.ClassInfoBuilder.java

/**
 * Build the FieldInfo for a Field./*from   ww  w  .  j ava2 s .  c  o  m*/
 * 
 * @param classInfo
 *            the ClassInfo to check if this field already exists
 * @param field
 *            the Field to describe
 * @return the ClassInfo containing the FieldInfo build
 */
private void buildFieldInfo(final ClassInfo classInfo, final Field field) {
    if (classInfo == null) {
        String message = "Argument classInfo must not be null.";
        LOG.warn(message);
        throw new IllegalArgumentException(message);
    }
    if (field == null) {
        String message = "Argument field must not be null.";
        LOG.warn(message);
        throw new IllegalArgumentException(message);
    }
    String fieldName = javaNaming.extractFieldNameFromField(field);
    FieldInfo fieldInfo = classInfo.getFieldInfo(fieldName);
    if (fieldInfo == null) {
        fieldInfo = createFieldInfo(fieldName);
        classInfo.addFieldInfo(fieldInfo);
        fieldInfo.setParentClassInfo(classInfo);
    }
    JaxbFieldNature jaxbFieldNature = new JaxbFieldNature(fieldInfo);
    jaxbFieldNature.setField(field);
    Class<?> fieldType = field.getDeclaringClass();
    if (fieldType.isArray() || fieldType.isAssignableFrom(Collection.class)) {
        jaxbFieldNature.setMultivalued(true);
    }
    jaxbFieldNature.setGenericType(field.getGenericType());
    fieldAnnotationProcessingService.processAnnotations(jaxbFieldNature, field.getAnnotations());
}

From source file:com.manydesigns.elements.forms.TableForm.java

public void readFromObject(Object obj) {
    Class clazz = obj.getClass();
    if (clazz.isArray()) { // Tratta obj come un array
        // Scorre tutti gli ellementi dell'array obj,
        // indipendentemente da quante righe ci sono nell table form.
        // Eventualmente lancia Eccezione.
        final int arrayLength = Array.getLength(obj);
        for (int i = 0; i < arrayLength; i++) {
            Object currentObj = Array.get(obj, i);
            rows[i].readFromObject(currentObj);
        }/* w w w  . j a v a2 s .c  o m*/

        // Scorre le rimanenti righe del table form,
        // passano null come ottetto di bind.
        for (int i = arrayLength; i < rows.length; i++) {
            rows[i].readFromObject(null);
        }
    } else if (Collection.class.isAssignableFrom(clazz)) {
        // Tratta obj come collection
        Collection collection = (Collection) obj;

        int i = 0;
        for (Object currentObj : collection) {
            rows[i].readFromObject(currentObj);
            i++;
        }

        for (; i < rows.length; i++) {
            rows[i].readFromObject(null);
        }
    }
}