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.angkorteam.framework.swagger.factory.SwaggerFactory.java

public static boolean isSimpleScalar(Class<?> type) {
    return !type.isArray() && (type == byte.class || type == Byte.class || type == short.class
            || type == Short.class || type == int.class || type == Integer.class || type == float.class
            || type == Float.class || type == long.class || type == Long.class || type == double.class
            || type == Double.class || type == boolean.class || type == Boolean.class || type == char.class
            || type == Character.class || type == MultipartFile.class || type == String.class);
}

From source file:com.angkorteam.framework.swagger.factory.SwaggerFactory.java

public static boolean isModelArray(Class<?> type) {
    return type.isArray() && (type != byte[].class && type != Byte[].class && type != short[].class
            && type != Short[].class && type != int[].class && type != Integer[].class && type != float[].class
            && type != Float[].class && type != long[].class && type != Long[].class && type != double[].class
            && type != Double[].class && type != boolean[].class && type != Boolean[].class
            && type != char[].class && type != Character[].class && type != Date[].class
            && type != MultipartFile[].class && type != String[].class
            && type.getComponentType().getAnnotation(ApiModel.class) != null);
}

From source file:com.angkorteam.framework.swagger.factory.SwaggerFactory.java

public static boolean isModelScalar(Class<?> type) {
    return !type.isArray() && (type != byte.class && type != Byte.class && type != short.class
            && type != Short.class && type != int.class && type != Integer.class && type != float.class
            && type != Float.class && type != long.class && type != Long.class && type != double.class
            && type != Double.class && type != boolean.class && type != Boolean.class && type != char.class
            && type != Character.class && type != Date.class && type != MultipartFile.class
            && type != String.class && type.getAnnotation(ApiModel.class) != null);
}

From source file:com.liferay.wsrp.proxy.TypeConvertorUtil.java

private static void _convert(int sourceVersion, String sourcePackage, Object sourceClass, Object sourceChild,
        String sourceChildName, Object destination) throws Exception {

    Class<?> sourceChildClass = sourceChild.getClass();

    if (sourceChildClass == NavigationalContext.class) {
        sourceChildName = "navigationalState";

        NavigationalContext navigationalContext = (NavigationalContext) sourceChild;

        sourceChild = navigationalContext.getOpaqueValue();
    } else if (sourceChildClass == SessionParams.class) {
        sourceChildName = "sessionID";

        SessionParams sessionParams = (SessionParams) sourceChild;

        sourceChild = sessionParams.getSessionID();
    }// w w w . j  av a 2s. c  o m

    if (sourceChild == null) {
        return;
    }

    sourceChildClass = sourceChild.getClass();

    Object destinationChild = null;

    if (sourceChildClass.isArray()) {
        destinationChild = convert(sourceChild, sourceVersion);
    } else {
        destinationChild = sourceChild;

        sourceChildClass = sourceChild.getClass();

        if (sourceChildClass.getName().contains(sourcePackage)) {
            destinationChild = convert(sourceChild, sourceVersion);
        }
    }

    String destinationChildName = sourceChildName;

    if (sourceChildName.equals("itemBinary")) {
        destinationChildName = "markupBinary";
    } else if (sourceChildName.equals("itemString")) {
        destinationChildName = "markupString";
    } else if (sourceChildName.equals("markupBinary")) {
        destinationChildName = "itemBinary";
    } else if (sourceChildName.equals("markupString")) {
        destinationChildName = "itemString";
    } else if (sourceChildName.equals("name") && (sourceClass == Property.class)) {

        QName qName = (QName) destinationChild;

        destinationChild = qName.getLocalPart();
    } else if (sourceChildName.equals("name") && (sourceClass == PropertyDescription.class)) {

        String name = (String) destinationChild;

        destinationChild = new QName("namespace", name, "prefix");
    } else if (sourceChildName.equals("navigationalState") && (sourceClass == MarkupParams.class)) {

        String navigationalState = (String) sourceChild;

        NavigationalContext navigationalContext = new NavigationalContext();

        navigationalContext.setOpaqueValue(navigationalState);

        destinationChild = navigationalContext;

        destinationChildName = "navigationalContext";
    } else if (sourceChildName.equals("requiresRewriting")) {
        destinationChildName = "requiresUrlRewriting";
    } else if (sourceChildName.equals("requiresUrlRewriting")) {
        destinationChildName = "requiresRewriting";
    } else if (sourceChildName.equals("sessionID") && (sourceClass == RuntimeContext.class)) {

        String sessionID = (String) sourceChild;

        SessionParams sessionParams = new SessionParams();

        sessionParams.setSessionID(sessionID);

        destinationChild = sessionParams;

        destinationChildName = "sessionParams";
    }

    try {
        PropertyUtils.setProperty(destination, destinationChildName, destinationChild);
    } catch (NoSuchMethodException nsme) {
        if (_log.isWarnEnabled()) {
            _log.warn(nsme, nsme);
        }
    }
}

From source file:com.bstek.dorado.data.entity.EntityUtils.java

/**
 * ???//from w  ww. j  a v a  2 s .  com
 */
public static boolean isSimpleType(Class<?> cl) {
    boolean b = (String.class.equals(cl) || cl.isPrimitive() || Boolean.class.equals(cl)
            || Number.class.isAssignableFrom(cl) || cl.isEnum() || Date.class.isAssignableFrom(cl)
            || Character.class.isAssignableFrom(cl));
    if (!b && cl.isArray()) {
        b = isSimpleType(cl.getComponentType());
    }
    return b;
}

From source file:de.escalon.hypermedia.spring.uber.UberUtils.java

/**
 * Renders input fields for bean properties of bean to add or update or patch.
 *
 * @param uberFields//from  w w  w .  j a  v  a2 s  .c  o  m
 *         to add to
 * @param beanType
 *         to render
 * @param annotatedParameters
 *         which describes the method
 * @param annotatedParameter
 *         which requires the bean
 * @param currentCallValue
 *         sample call value
 */
private static void recurseBeanCreationParams(List<UberField> uberFields, Class<?> beanType,
        ActionDescriptor annotatedParameters, ActionInputParameter annotatedParameter, Object currentCallValue,
        String parentParamName, Set<String> knownFields) {
    // TODO collection, map and object node creation are only describable by an annotation, not via type reflection
    if (ObjectNode.class.isAssignableFrom(beanType) || Map.class.isAssignableFrom(beanType)
            || Collection.class.isAssignableFrom(beanType) || beanType.isArray()) {
        return; // use @Input(include) to list parameter names, at least? Or mix with hdiv's form builder?
    }
    try {
        Constructor[] constructors = beanType.getConstructors();
        // find default ctor
        Constructor constructor = PropertyUtils.findDefaultCtor(constructors);
        // find ctor with JsonCreator ann
        if (constructor == null) {
            constructor = PropertyUtils.findJsonCreator(constructors, JsonCreator.class);
        }
        Assert.notNull(constructor,
                "no default constructor or JsonCreator found for type " + beanType.getName());
        int parameterCount = constructor.getParameterTypes().length;

        if (parameterCount > 0) {
            Annotation[][] annotationsOnParameters = constructor.getParameterAnnotations();

            Class[] parameters = constructor.getParameterTypes();
            int paramIndex = 0;
            for (Annotation[] annotationsOnParameter : annotationsOnParameters) {
                for (Annotation annotation : annotationsOnParameter) {
                    if (JsonProperty.class == annotation.annotationType()) {
                        JsonProperty jsonProperty = (JsonProperty) annotation;

                        // TODO use required attribute of JsonProperty for required fields
                        String paramName = jsonProperty.value();
                        Class parameterType = parameters[paramIndex];
                        Object propertyValue = PropertyUtils.getPropertyOrFieldValue(currentCallValue,
                                paramName);
                        MethodParameter methodParameter = new MethodParameter(constructor, paramIndex);

                        addUberFieldsForMethodParameter(uberFields, methodParameter, annotatedParameter,
                                annotatedParameters, parentParamName, paramName, parameterType, propertyValue,
                                knownFields);
                        paramIndex++; // increase for each @JsonProperty
                    }
                }
            }
            Assert.isTrue(parameters.length == paramIndex, "not all constructor arguments of @JsonCreator "
                    + constructor.getName() + " are annotated with @JsonProperty");
        }

        Set<String> knownConstructorFields = new HashSet<String>(uberFields.size());
        for (UberField sirenField : uberFields) {
            knownConstructorFields.add(sirenField.getName());
        }

        // TODO support Option provider by other method args?
        Map<String, PropertyDescriptor> propertyDescriptors = PropertyUtils.getPropertyDescriptors(beanType);

        // add input field for every setter
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors.values()) {
            final Method writeMethod = propertyDescriptor.getWriteMethod();
            String propertyName = propertyDescriptor.getName();

            if (writeMethod == null || knownFields.contains(parentParamName + propertyName)) {
                continue;
            }
            final Class<?> propertyType = propertyDescriptor.getPropertyType();

            Object propertyValue = PropertyUtils.getPropertyOrFieldValue(currentCallValue, propertyName);
            MethodParameter methodParameter = new MethodParameter(propertyDescriptor.getWriteMethod(), 0);

            addUberFieldsForMethodParameter(uberFields, methodParameter, annotatedParameter,
                    annotatedParameters, parentParamName, propertyName, propertyType, propertyValue,
                    knownConstructorFields);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to write input fields for constructor", e);
    }
}

From source file:net.buffalo.protocal.util.ClassUtil.java

public static Object convertValue(Object value, Class targetType) {

    if (value.getClass().equals(targetType))
        return value;

    if (targetType.isPrimitive()) {
        targetType = getWrapperClass(targetType);
    }/*from w w w .j a  v a 2 s .c om*/

    if (targetType.isAssignableFrom(value.getClass()))
        return value;

    if ((value instanceof String || value instanceof Number) && Number.class.isAssignableFrom(targetType)) {
        try {
            Constructor ctor = targetType.getConstructor(new Class[] { String.class });
            return ctor.newInstance(new Object[] { value.toString() });
        } catch (Exception e) {
            LOGGER.error("convert type error", e);
            throw new RuntimeException(
                    "Cannot convert from " + value.getClass().getName() + " to " + targetType, e);
        }
    }

    if (targetType.isArray() && Collection.class.isAssignableFrom(value.getClass())) {
        Collection collection = (Collection) value;
        Object array = Array.newInstance(targetType.getComponentType(), collection.size());
        int i = 0;
        for (Iterator iter = collection.iterator(); iter.hasNext();) {
            Object val = iter.next();
            Array.set(array, i++, val);
        }

        return array;
    }

    if (Collection.class.isAssignableFrom(targetType) && value.getClass().isArray()) {
        return Arrays.asList((Object[]) value);
    }

    throw new IllegalArgumentException(
            "Cannot convert from " + value.getClass().getName() + " to " + targetType);
}

From source file:com.laxser.blitz.web.paramresolver.ResolverFactoryImpl.java

private static boolean addIndexAliasParamNameIfNeccessary(ParamMetaData metaData) {
    Class<?>[] paramTypes = metaData.getMethod().getParameterTypes();
    int index = metaData.getIndex(); // index0
    int uriParamIndex = 0; // uriParamIndex**1**
    int breakIndex = 0;// breakIndex0
    for (; breakIndex < paramTypes.length && breakIndex <= index; breakIndex++) {
        Class<?> type = paramTypes[breakIndex];
        if (type.isArray()) {
            type = type.getComponentType();
        } else if (Collection.class.isAssignableFrom(type)) {
            Class<?>[] generics = compileGenericParameterTypesDetail(metaData.getMethod(), breakIndex);
            if (generics == null) {
                return false;
            }// ww w  .  j av a  2  s .  co  m
            Assert.isTrue(generics.length > 0);
            type = generics[0];
        }
        if (ClassUtils.isPrimitiveOrWrapper(type) || type == String.class
                || Date.class.isAssignableFrom(type)) {
            uriParamIndex++;
        }
    }
    if ((breakIndex - 1) == index && uriParamIndex > 0) {
        String alias = "$" + uriParamIndex;
        metaData.addAliasParamName(alias);
        if (logger.isDebugEnabled()) {
            logger.debug("add index alias paramName: '" + alias + "' for "
                    + metaData.getControllerClass().getName() + "." + metaData.getMethod().getName() + "(..."
                    + metaData.getParamType() + "[index=" + breakIndex + "] ...)");
        }
        return true;
    }
    return false;
}

From source file:ca.oson.json.util.ObjectUtil.java

public static boolean isArrayOrCollection(Class valueType) {
    if (valueType == null) { // no idea, just assume
        return true;
    }/* w  w w.  j  a v a 2  s.  c o  m*/

    if (valueType.isArray()) {
        return true;
    }

    if (Collection.class.isAssignableFrom(valueType)) {
        return true;
    }

    if (Iterable.class.isAssignableFrom(valueType)) {
        return true;
    }

    return false;
}

From source file:com.silverwrist.dynamo.app.ApplicationContainer.java

private static final Class getUltimateComponent(Class klass) {
    while (klass.isArray())
        klass = klass.getComponentType();
    return klass;

}