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.axelor.shell.core.Target.java

private Options getOptions() {

    if (options != null) {
        return options;
    }// w w w  .  ja v a  2s .  c  o  m

    options = new Options();
    int counter = 0;

    for (CliOption cliOption : cliOptions) {

        if (cliOption == null) { // variable arguments
            continue;
        }

        Class<?> type = method.getParameterTypes()[counter++];
        String name = cliOption.name();
        String shortName = "" + cliOption.shortName();

        Option option = new Option(shortName, cliOption.help());

        option.setType(type);
        option.setLongOpt(name);
        option.setRequired(cliOption.required());
        option.setArgs(1);

        if (!isBlank(cliOption.argName())) {
            option.setArgName(cliOption.argName());
            option.setArgs(1);
        }
        if (type == boolean.class) {
            option.setArgs(0);
        }

        if (type.isArray()) {
            option.setArgs(Option.UNLIMITED_VALUES);
        }

        options.addOption(option);
    }

    return options;
}

From source file:com.feilong.core.bean.ConvertUtil.java

/**
 * ? Primitive.//from  w  w w  . java  2  s  . c o  m
 *
 * @param o
 *            the o
 * @return true, if checks if is primitive array
 * 
 * @since 1.4.0
 */
private static boolean isPrimitiveArray(Object o) {
    // Allocate a new Array
    Class<? extends Object> klass = o.getClass();
    return !klass.isArray() ? false : klass.getComponentType().isPrimitive();//
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

private byte type(Class c) {
    Byte type = typeCodes.get(c);
    if (type == null) {
        if (c.isEnum())
            type = TYPE_ENUM;/* w  ww.  ja va  2 s  .co m*/
        else if (c.isArray())
            type = TYPE_ARRAY;
        else if (Collection.class.isAssignableFrom(c))
            type = TYPE_COLLECTION;
        else if (Map.class.isAssignableFrom(c))
            type = TYPE_MAP;
        else
            type = TYPE_OBJECT;
    }
    return type;
}

From source file:com.bstek.dorado.view.output.ClientOutputHelper.java

protected Map<String, PropertyConfig> doGetPropertyConfigs(Class<?> beanType) throws Exception {
    beanType = ProxyBeanUtils.getProxyTargetType(beanType);
    Map<String, PropertyConfig> propertyConfigs = new HashMap<String, PropertyConfig>();

    ClientObjectInfo clientObjectInfo = getClientObjectInfo(beanType);
    if (clientObjectInfo != null) {
        for (Map.Entry<String, ClientProperty> entry : clientObjectInfo.getPropertyConfigs().entrySet()) {
            String property = entry.getKey();
            ClientProperty clientProperty = entry.getValue();

            PropertyConfig propertyConfig = new PropertyConfig();
            if (clientProperty.ignored()) {
                propertyConfig.setIgnored(true);
            } else {
                if (StringUtils.isNotEmpty(clientProperty.outputter())) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(clientProperty.outputter(),
                            Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                }/*w w w.jav  a 2 s  .c o m*/

                if (clientProperty.alwaysOutput()) {
                    propertyConfig.setEscapeValue(FAKE_ESCAPE_VALUE);
                } else if (StringUtils.isNotEmpty(clientProperty.escapeValue())) {
                    propertyConfig.setEscapeValue(clientProperty.escapeValue());
                }
            }
            propertyConfigs.put(property, propertyConfig);
        }
    }

    boolean isAssembledComponent = (AssembledComponent.class.isAssignableFrom(beanType));
    Class<?> superComponentType = null;
    if (isAssembledComponent) {
        superComponentType = beanType;
        while (superComponentType != null && AssembledComponent.class.isAssignableFrom(superComponentType)) {
            superComponentType = superComponentType.getSuperclass();
            Assert.notNull(superComponentType);
        }
    }

    for (PropertyDescriptor propertyDescriptor : PropertyUtils.getPropertyDescriptors(beanType)) {
        String property = propertyDescriptor.getName();
        Method readMethod = propertyDescriptor.getReadMethod();
        if (readMethod.getDeclaringClass() != beanType) {
            try {
                readMethod = beanType.getMethod(readMethod.getName(), readMethod.getParameterTypes());
            } catch (NoSuchMethodException e) {
                // do nothing
            }
        }

        TypeInfo typeInfo;
        Class<?> propertyType = propertyDescriptor.getPropertyType();
        if (Collection.class.isAssignableFrom(propertyType)) {
            typeInfo = TypeInfo.parse((ParameterizedType) readMethod.getGenericReturnType(), true);
            if (typeInfo != null) {
                propertyType = typeInfo.getType();
            }
        } else if (propertyType.isArray()) {
            typeInfo = new TypeInfo(propertyType.getComponentType(), true);
        } else {
            typeInfo = new TypeInfo(propertyType, false);
        }

        PropertyConfig propertyConfig = null;
        ClientProperty clientProperty = readMethod.getAnnotation(ClientProperty.class);
        if (clientProperty != null) {
            propertyConfig = new PropertyConfig();
            if (clientProperty.ignored()) {
                propertyConfig.setIgnored(true);
            } else {
                if (StringUtils.isNotEmpty(clientProperty.outputter())) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(clientProperty.outputter(),
                            Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                } else if (Component.class.isAssignableFrom(propertyType)) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(COMPONENT_OUTPUTTER, Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                } else if (DataControl.class.isAssignableFrom(propertyType)
                        || FloatControl.class.isAssignableFrom(propertyType)) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(COMPONENT_OUTPUTTER, Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                } else if (DataType.class.isAssignableFrom(propertyType)) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(DATA_TYPE_PROPERTY_OUTPUTTER,
                            Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                } else if (Object.class.equals(propertyType)) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(DATA_OUTPUTTER, Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                } else if (!EntityUtils.isSimpleType(propertyType) && !propertyType.isArray()) {
                    BeanWrapper beanWrapper = BeanFactoryUtils.getBean(OBJECT_OUTPUTTER, Scope.instant);
                    propertyConfig.setOutputter(beanWrapper.getBean());
                }

                if (!clientProperty.evaluateExpression()) {
                    propertyConfig.setEvaluateExpression(false);
                }

                if (clientProperty.alwaysOutput()) {
                    propertyConfig.setEscapeValue(FAKE_ESCAPE_VALUE);
                } else if (StringUtils.isNotEmpty(clientProperty.escapeValue())) {
                    propertyConfig.setEscapeValue(clientProperty.escapeValue());
                }
            }
        } else if (isAssembledComponent && readMethod.getDeclaringClass() == beanType
                && EntityUtils.isSimpleType(propertyType)) {
            if (BeanUtils.getPropertyDescriptor(superComponentType, property) == null) {
                propertyConfig = new PropertyConfig();
                propertyConfig.setIgnored(true);
            }
        }

        ComponentReference componentReference = readMethod.getAnnotation(ComponentReference.class);
        if (componentReference != null && String.class.equals(propertyType)) {
            if (propertyConfig == null) {
                propertyConfig = new PropertyConfig();
            }
            if (propertyConfig.getOutputter() == null) {
                BeanWrapper beanWrapper = BeanFactoryUtils.getBean(COMPONENT_REFERENCE_OUTPUTTER,
                        Scope.instant);
                propertyConfig.setOutputter(beanWrapper.getBean());
            }
        }

        if (!propertyType.isPrimitive() && (Number.class.isAssignableFrom(propertyType)
                || Boolean.class.isAssignableFrom(propertyType))) {
            if (propertyConfig == null) {
                propertyConfig = new PropertyConfig();
            }
            if (propertyConfig.getEscapeValue() == PropertyConfig.NONE_VALUE) {
                propertyConfig.setEscapeValue(null);
            }
        }

        if (propertyConfig != null) {
            propertyConfigs.put(property, propertyConfig);
        }
    }
    return (propertyConfigs.isEmpty()) ? null : propertyConfigs;
}

From source file:javadz.beanutils.ConvertUtilsBean.java

/**
 * Convert an array of specified values to an array of objects of the
 * specified class (if possible).  If the specified Java class is itself
 * an array class, this class will be the type of the returned value.
 * Otherwise, an array will be constructed whose component type is the
 * specified class.//from   ww  w . j  ava2 s  .c om
 *
 * @param values Array of values to be converted
 * @param clazz Java array or element class to be converted to
 * @return The converted value
 *
 * @exception ConversionException if thrown by an underlying Converter
 */
public Object convert(String[] values, Class clazz) {

    Class type = clazz;
    if (clazz.isArray()) {
        type = clazz.getComponentType();
    }
    if (log.isDebugEnabled()) {
        log.debug("Convert String[" + values.length + "] to class '" + type.getName() + "[]'");
    }
    Converter converter = lookup(type);
    if (converter == null) {
        converter = lookup(String.class);
    }
    if (log.isTraceEnabled()) {
        log.trace("  Using converter " + converter);
    }
    Object array = Array.newInstance(type, values.length);
    for (int i = 0; i < values.length; i++) {
        Array.set(array, i, converter.convert(type, values[i]));
    }
    return (array);

}

From source file:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object array, Class type, int index, JSONArray jsonArray)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        if (type == Integer.TYPE) {
            Array.setInt(array, index, jsonArray.getInt(index));
        } else if (type == Long.TYPE) {
            Array.setLong(array, index, jsonArray.getInt(index));
        } else if (type == Short.TYPE) {
            Array.setShort(array, index, (short) jsonArray.getInt(index));
        } else if (type == Boolean.TYPE) {
            Array.setBoolean(array, index, jsonArray.getBoolean(index));
        } else if (type == Double.TYPE) {
            Array.setDouble(array, index, jsonArray.getDouble(index));
        } else if (type == Float.TYPE) {
            Array.setFloat(array, index, (float) jsonArray.getDouble(index));
        } else if (type == Character.TYPE) {
            char ch = jsonArray.getString(index).charAt(0);
            Array.setChar(array, index, ch);
        } else if (type == Byte.TYPE) {
            Array.setByte(array, index, (byte) jsonArray.getInt(index));
        } else {/*from  w  w  w.  j a  va 2s.  c om*/
            throw new JSONSerializationException("Unknown primitive: " + type);
        }
    } else if (type == String.class) {
        Array.set(array, index, jsonArray.getString(index));
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONObject jObj = jsonArray.getJSONObject(index);
        JSONSerializable serObj = deSerialize(type, jObj);
        Array.set(array, index, serObj);
    } else if (type.isArray()) {
        Class componentClass = type.getComponentType();
        JSONArray subArray = jsonArray.getJSONArray(index);
        int len = subArray.length();

        Object newArray = Array.newInstance(componentClass, len);
        for (int k = 0; k < len; ++k) {
            loadObject(newArray, componentClass, k, subArray);
        }
    }
}

From source file:com.alibaba.citrus.service.requestcontext.support.ValueListSupport.java

private <T> T convert(Class<T> type, MethodParameter methodParameter, Object[] values, Object defaultValue) {
    if (values == null) {
        values = EMPTY_STRING_ARRAY;/*from  w  ww .j  a v a 2 s  . com*/
    }

    Object convertedValue = null;
    boolean requiresArray = type.isArray() || CollectionFactory.isApproximableCollectionType(type);

    if (values.length == 0) {
        if (!type.equals(String.class)) {
            try {
                convertedValue = converter.convertIfNecessary(values, type, methodParameter);
            } catch (TypeMismatchException e) {
                // ignored for empty value, just returns null
            }
        }
    } else {
        if (requiresArray) {
            convertedValue = converter.convertIfNecessary(values, type, methodParameter);
        } else {
            Object singleValue = values[0];

            if (isEmptyObject(singleValue)) {
                singleValue = defaultValue;
            }

            convertedValue = converter.convertIfNecessary(singleValue, type, methodParameter);
        }
    }

    return type.cast(convertedValue);
}

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

public RowMapper getRowMapper(Class<?> rowType) {

    // BUGFIX: SingleColumnRowMapper ?  Primitive Type 
    if (rowType.isPrimitive()) {
        rowType = ClassUtils.primitiveToWrapper(rowType);
    }//w  w  w. ja  v  a2s.c om

    // ?  RowMapper
    RowMapper rowMapper;

    // ?(?2Map)
    if (TypeUtils.isColumnType(rowType)) {
        rowMapper = new SingleColumnRowMapper(rowType);
    }
    // Bean??????
    else {
        if (rowType == Map.class) {
            rowMapper = new ColumnMapRowMapper();
        } else if (rowType.isArray()) {
            rowMapper = new ArrayRowMapper(rowType);
        } else if ((rowType == List.class) || (rowType == Collection.class) || (rowType == Set.class)) {
            rowMapper = null;
        } else {
            boolean checkColumns = false;
            boolean checkProperties = false;
            String key = rowType.getName() + "[checkColumns=" + checkColumns + "&checkProperties="
                    + checkProperties + "]";
            rowMapper = rowMappers.get(key);
            if (rowMapper == null) {
                rowMapper = new BeanPropertyRowMapper(rowType, checkColumns, checkProperties); // jade's BeanPropertyRowMapper here
                rowMappers.put(key, rowMapper);
            }
        }
    }
    if (rowMapper == null) {
        throw new IllegalArgumentException();
    }
    return rowMapper;
}

From source file:org.hdiv.webflow.mvc.servlet.ServletMvcViewHDIV.java

private Object getEmptyValue(Class fieldType) {
    if (fieldType != null && boolean.class.equals(fieldType) || Boolean.class.equals(fieldType)) {
        // Special handling of boolean property.
        return Boolean.FALSE;
    } else if (fieldType != null && fieldType.isArray()) {
        // Special handling of array property.
        return Array.newInstance(fieldType.getComponentType(), 0);
    } else {//from   ww  w.j  a  v a2s.  c  o m
        // Default value: try null.
        return null;
    }
}

From source file:com.sun.faces.renderkit.html_basic.MenuRenderer.java

public Object convertSelectManyValue(FacesContext context, UISelectMany uiSelectMany, String[] newValues)
        throws ConverterException {
    // if we have no local value, try to get the valueBinding.
    ValueBinding valueBinding = uiSelectMany.getValueBinding("value");

    Object result = newValues; // default case, set local value
    Class modelType = null;
    boolean throwException = false;

    // If we have a ValueBinding
    if (null != valueBinding) {
        modelType = valueBinding.getType(context);
        // Does the valueBinding resolve properly to something with
        // a type?
        if (null != modelType) {
            if (modelType.isArray()) {
                result = handleArrayCase(context, uiSelectMany, modelType, newValues);
            } else if (List.class.isAssignableFrom(modelType)) {
                result = handleListCase(context, newValues);
            } else {
                throwException = true;// w w w  . j a  va 2 s . c om
            }
        } else {
            throwException = true;
        }
    } else {
        // No ValueBinding, just use Object array.
        Object[] convertedValues = new Object[1];
        result = handleArrayCase(context, uiSelectMany, convertedValues.getClass(), newValues);
    }
    if (throwException) {
        String values = "";
        if (null != newValues) {
            for (int i = 0; i < newValues.length; i++) {
                values = values + " " + newValues[i];
            }
        }
        Object[] params = { values, valueBinding.getExpressionString() };
        throw new ConverterException(Util.getExceptionMessage(Util.CONVERSION_ERROR_MESSAGE_ID, params));
    }

    // At this point, result is ready to be set as the value
    if (log.isTraceEnabled()) {
        log.trace("SelectMany Component  " + uiSelectMany.getId() + " convertedValues " + result);
    }
    return result;
}