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:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.NativeArrayConverter.java

/**
 *
 * {@inheritDoc}/* w  w w  . j ava  2s .co m*/
 */
@Override
public int getForJavaConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) {
    final int confidence;
    if (NativeArray.class.isAssignableFrom(valueInstanceClass)
            && (expectedClass.isAssignableFrom(Collection.class) || expectedClass.isAssignableFrom(Map.class)
                    || expectedClass.isArray())) {
        confidence = HIGHEST_CONFIDENCE;
    } else {
        confidence = LOWEST_CONFIDENCE;
    }
    return confidence;
}

From source file:WeakIdentityMap.java

static boolean equals(Object a, Object b) {
    if (a == b) {
        return true;
    }//from www  .  ja  va  2s.  c  o  m
    if (a == null || b == null) {
        return false;
    }
    Class ac = a.getClass();
    if (!(ac.isArray())) {
        return a.equals(b);
    }
    if (ac != b.getClass()) {
        return false;
    }
    if (a instanceof Object[]) {
        return equals((Object[]) a, (Object[]) b);
    } else if (a instanceof int[]) {
        return Arrays.equals((int[]) a, (int[]) b);
    } else if (a instanceof float[]) {
        return Arrays.equals((float[]) a, (float[]) b);
    } else if (a instanceof long[]) {
        return Arrays.equals((long[]) a, (long[]) b);
    } else if (a instanceof double[]) {
        return Arrays.equals((double[]) a, (double[]) b);
    } else if (a instanceof byte[]) {
        return Arrays.equals((byte[]) a, (byte[]) b);
    } else if (a instanceof char[]) {
        return Arrays.equals((char[]) a, (char[]) b);
    } else if (a instanceof boolean[]) {
        return Arrays.equals((boolean[]) a, (boolean[]) b);
    } else if (a instanceof short[]) {
        return Arrays.equals((short[]) a, (short[]) b);
    } else {
        return a.equals(b);
    }
}

From source file:de.escalon.hypermedia.spring.SpringActionInputParameter.java

/**
 * Allows convenient access to multiple call values in case that this input parameter is an array or collection.
 * Make sure to check {@link #isArrayOrCollection()} before calling this method.
 *
 * @return call values or empty array/*from   w  ww. jav  a  2  s.c  o  m*/
 * @throws UnsupportedOperationException
 *         if this input parameter is not an array or collection
 */
public Object[] getValues() {
    Object[] callValues;
    if (!isArrayOrCollection()) {
        throw new UnsupportedOperationException("parameter is not an array or collection");
    }
    Object callValue = getValue();
    if (callValue == null) {
        callValues = new Object[0];
    } else {
        Class<?> parameterType = getParameterType();
        if (parameterType.isArray()) {
            callValues = (Object[]) callValue;
        } else {
            callValues = ((Collection<?>) callValue).toArray();
        }
    }
    return callValues;
}

From source file:com.google.feedserver.util.BeanUtil.java

/**
 * Applies a collection of properties to a JavaBean. Converts String and
 * String[] values to correct property types
 * /*from   w  ww. j a  va2  s.c  o m*/
 * @param properties A map of the properties to set on the JavaBean
 * @param bean The JavaBean to set the properties on
 */
public void convertPropertiesToBean(Map<String, Object> properties, Object bean) throws IntrospectionException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException, ParseException {
    BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
    for (PropertyDescriptor p : beanInfo.getPropertyDescriptors()) {
        String name = p.getName();
        Object value = properties.get(name);
        Method reader = p.getReadMethod();
        Method writer = p.getWriteMethod();
        // we only care about "complete" properties
        if (reader != null && writer != null && value != null) {
            Class<?> propertyType = writer.getParameterTypes()[0];
            if (isBean(propertyType)) {
                // this is a bean
                if (propertyType.isArray()) {
                    propertyType = propertyType.getComponentType();
                    Object beanArray = Array.newInstance(propertyType, 1);

                    if (value.getClass().isArray()) {
                        Object[] valueArrary = (Object[]) value;
                        int length = valueArrary.length;
                        beanArray = Array.newInstance(propertyType, length);
                        for (int index = 0; index < valueArrary.length; ++index) {
                            Object valueObject = valueArrary[index];
                            fillBeanInArray(propertyType, beanArray, index, valueObject);
                        }
                    } else {
                        fillBeanInArray(propertyType, beanArray, 0, value);
                    }
                    value = beanArray;
                } else if (propertyType == Timestamp.class) {
                    value = new Timestamp(TIMESTAMP_FORMAT.parse((String) value).getTime());
                } else {
                    Object beanObject = createBeanObject(propertyType, value);
                    value = beanObject;
                }
            } else {
                Class<?> valueType = value.getClass();
                if (!propertyType.isAssignableFrom(valueType)) {
                    // convert string input values to property type
                    try {
                        if (valueType == String.class) {
                            value = ConvertUtils.convert((String) value, propertyType);
                        } else if (valueType == String[].class) {
                            value = ConvertUtils.convert((String[]) value, propertyType);
                        } else if (valueType == Object[].class) {
                            // best effort conversion
                            Object[] objectValues = (Object[]) value;
                            String[] stringValues = new String[objectValues.length];
                            for (int i = 0; i < objectValues.length; i++) {
                                stringValues[i] = objectValues[i] == null ? null : objectValues[i].toString();
                            }
                            value = ConvertUtils.convert(stringValues, propertyType);
                        } else {
                        }
                    } catch (ConversionException e) {
                        throw new IllegalArgumentException(
                                "Conversion failed for " + "property '" + name + "' with value '" + value + "'",
                                e);
                    }
                }
            }
            // We only write values that are present in the map. This allows
            // defaults or previously set values in the bean to be retained.
            writer.invoke(bean, value);
        }
    }
}

From source file:com.datos.vfs.util.DelegatingFileSystemOptionsBuilder.java

/**
 * tries to convert the value and pass it to the given method
 *//* w  w  w  .jav a 2 s  .c om*/
private boolean convertValuesAndInvoke(final Method configSetter, final Context ctx)
        throws FileSystemException {
    final Class<?>[] parameters = configSetter.getParameterTypes();
    if (parameters.length < 2) {
        return false;
    }
    if (!parameters[0].isAssignableFrom(FileSystemOptions.class)) {
        return false;
    }

    final Class<?> valueParameter = parameters[1];
    Class<?> type;
    if (valueParameter.isArray()) {
        type = valueParameter.getComponentType();
    } else {
        if (ctx.values.length > 1) {
            return false;
        }

        type = valueParameter;
    }

    if (type.isPrimitive()) {
        final Class<?> objectType = PRIMATIVE_TO_OBJECT.get(type.getName());
        if (objectType == null) {
            log.warn(Messages.getString("vfs.provider/config-unexpected-primitive.error", type.getName()));
            return false;
        }
        type = objectType;
    }

    final Class<? extends Object> valueClass = ctx.values[0].getClass();
    if (type.isAssignableFrom(valueClass)) {
        // can set value directly
        invokeSetter(valueParameter, ctx, configSetter, ctx.values);
        return true;
    }
    if (valueClass != String.class) {
        log.warn(Messages.getString("vfs.provider/config-unexpected-value-class.error", valueClass.getName(),
                ctx.scheme, ctx.name));
        return false;
    }

    final Object convertedValues = Array.newInstance(type, ctx.values.length);

    Constructor<?> valueConstructor;
    try {
        valueConstructor = type.getConstructor(STRING_PARAM);
    } catch (final NoSuchMethodException e) {
        valueConstructor = null;
    }
    if (valueConstructor != null) {
        // can convert using constructor
        for (int iterValues = 0; iterValues < ctx.values.length; iterValues++) {
            try {
                Array.set(convertedValues, iterValues,
                        valueConstructor.newInstance(new Object[] { ctx.values[iterValues] }));
            } catch (final InstantiationException e) {
                throw new FileSystemException(e);
            } catch (final IllegalAccessException e) {
                throw new FileSystemException(e);
            } catch (final InvocationTargetException e) {
                throw new FileSystemException(e);
            }
        }

        invokeSetter(valueParameter, ctx, configSetter, convertedValues);
        return true;
    }

    Method valueFactory;
    try {
        valueFactory = type.getMethod("valueOf", STRING_PARAM);
        if (!Modifier.isStatic(valueFactory.getModifiers())) {
            valueFactory = null;
        }
    } catch (final NoSuchMethodException e) {
        valueFactory = null;
    }

    if (valueFactory != null) {
        // can convert using factory method (valueOf)
        for (int iterValues = 0; iterValues < ctx.values.length; iterValues++) {
            try {
                Array.set(convertedValues, iterValues,
                        valueFactory.invoke(null, new Object[] { ctx.values[iterValues] }));
            } catch (final IllegalAccessException e) {
                throw new FileSystemException(e);
            } catch (final InvocationTargetException e) {
                throw new FileSystemException(e);
            }
        }

        invokeSetter(valueParameter, ctx, configSetter, convertedValues);
        return true;
    }

    return false;
}

From source file:WeakIdentityMap.java

static int compare(Object a, Object b) {
    if (a == b) {
        return 0;
    }/*from  w  w  w  .  ja  v  a2  s .c  om*/
    if (a == null) {
        return 1;
    }
    if (b == null) {
        return -1;
    }
    Class ac = a.getClass();
    if (!(ac.isArray())) {
        return ((Comparable) a).compareTo(b);
    }
    if (ac != b.getClass()) {
        throw new ClassCastException();
    }
    if (a instanceof Object[]) {
        return compare((Object[]) a, (Object[]) b);
    } else if (a instanceof int[]) {
        return compare((int[]) a, (int[]) b);
    } else if (a instanceof float[]) {
        return compare((float[]) a, (float[]) b);
    } else if (a instanceof long[]) {
        return compare((long[]) a, (long[]) b);
    } else if (a instanceof double[]) {
        return compare((double[]) a, (double[]) b);
    } else if (a instanceof byte[]) {
        return compare((byte[]) a, (byte[]) b);
    } else if (a instanceof char[]) {
        return compare((char[]) a, (char[]) b);
    } else if (a instanceof boolean[]) {
        return compare((boolean[]) a, (boolean[]) b);
    } else if (a instanceof short[]) {
        return compare((short[]) a, (short[]) b);
    } else {
        throw new ClassCastException();
    }
}

From source file:com.datos.vfs.util.DelegatingFileSystemOptionsBuilder.java

/**
 * invokes the method with the converted values
 *///from   w  w  w  .  jav  a  2s.  co  m
private void invokeSetter(final Class<?> valueParameter, final Context ctx, final Method configSetter,
        final Object values) throws FileSystemException {
    Object[] args;
    if (valueParameter.isArray()) {
        args = new Object[] { ctx.fso, values };
    } else {
        args = new Object[] { ctx.fso, Array.get(values, 0) };
    }
    try {
        configSetter.invoke(ctx.fileSystemConfigBuilder, args);
    } catch (final IllegalAccessException e) {
        throw new FileSystemException(e);
    } catch (final InvocationTargetException e) {
        throw new FileSystemException(e);
    }
}

From source file:com.adf.bean.AbsBean.java

/**
 * IMPORT// w  w w  .  jav  a2 s.co m
 * */
public void setValueByColumn(String col, String val) throws IllegalArgumentException {
    try {
        Field f = getClass().getDeclaredField(col);
        Class<?> cls = f.getType();
        f.setAccessible(true);
        if (cls.isArray()) {
            JSONArray arr;
            try {
                arr = new JSONArray(val);
                setArrayColumn(col, arr);
            } catch (JSONException e) {
            }
            return;
        }
        if ((int.class == cls) || (Integer.class == cls)) {
            int ival = Integer.parseInt(val);
            f.set(this, ival);
        } else if ((long.class == cls) || (Long.class == cls)) {
            long lval = Long.parseLong(val);
            f.set(this, lval);
        } else if ((float.class == cls) || (Float.class == cls)) {
            float fval = Float.parseFloat(val);
            f.set(this, fval);
        } else if ((short.class == cls) || (Short.class == cls)) {
            short sval = Short.parseShort(val);
            f.set(this, sval);
        } else if ((double.class == cls) || (Double.class == cls)) {
            double dval = Double.parseDouble(val);
            f.set(this, dval);
        } else if ((byte.class == cls) || (Byte.class == cls)) {
            byte bval = Byte.parseByte(val);
            f.set(this, bval);
        } else if ((boolean.class == cls) || (Boolean.class == cls)) {
            boolean bval = Boolean.parseBoolean(val);
            f.set(this, bval);
        } else if (char.class == cls) {
            char cval = val.charAt(0);
            f.set(this, cval);
        } else {
            Constructor<?> cons = cls.getDeclaredConstructor(String.class);
            cons.setAccessible(true);
            Object obj = cons.newInstance(val);
            f.set(this, obj);
        }
    } catch (NoSuchFieldException e) {
        LogUtil.err("setValueByColumn NoSuchFieldException, col " + col + " not exist!!!");
    } catch (IllegalAccessException e) {
        LogUtil.err("setValueByColumn IllegalAccessException, col " + col + " :" + e.getMessage());
        //throw e;
    } catch (IllegalArgumentException e) {
        LogUtil.err("setValueByColumn IllegalArgumentException, col " + col + " :" + e.getMessage());
        //throw e;
    } catch (NoSuchMethodException e) {
        LogUtil.err("setValueByColumn NoSuchMethodException, col " + col + " :" + e.getMessage());
        //throw e;
    } catch (InstantiationException e) {
        LogUtil.err("setValueByColumn InstantiationException, col " + col + " :" + e.getMessage());
        //throw e;
    } catch (InvocationTargetException e) {
        LogUtil.err("setValueByColumn InvocationTargetException, col " + col + " :" + e.getMessage());
        //throw e;
    }
}

From source file:com.espertech.esper.event.xml.XPathPropertyGetter.java

/**
 * Ctor.//  w w  w. j a  va2s .  c o  m
 * @param propertyName is the name of the event property for which this getter gets values
 * @param expressionText is the property expression itself
 * @param xPathExpression is a compile XPath expression
 * @param resultType is the resulting type
 * @param optionalCastToType if non-null then the return value of the xpath expression is cast to this value
 * @param fragmentFactory for creating fragments, or null in none to be created
 */
public XPathPropertyGetter(String propertyName, String expressionText, XPathExpression xPathExpression,
        QName resultType, Class optionalCastToType, FragmentFactory fragmentFactory) {
    this.expression = xPathExpression;
    this.expressionText = expressionText;
    this.property = propertyName;
    this.resultType = resultType;
    this.fragmentFactory = fragmentFactory;

    if ((optionalCastToType != null) && (optionalCastToType.isArray())) {
        isCastToArray = true;
        if (!resultType.equals(XPathConstants.NODESET)) {
            throw new IllegalArgumentException(
                    "Array cast-to types require XPathConstants.NODESET as the XPath result type");
        }
        optionalCastToType = optionalCastToType.getComponentType();
    } else {
        isCastToArray = false;
    }

    if (optionalCastToType != null) {
        simpleTypeParser = SimpleTypeParserFactory.getParser(optionalCastToType);
    } else {
        simpleTypeParser = null;
    }
    if (optionalCastToType == Node.class) {
        this.optionalCastToType = null;
    } else {
        this.optionalCastToType = optionalCastToType;
    }
}

From source file:com.medallia.spider.api.DynamicInputImpl.java

/**
 * Method used to parse values for the methods declared in {@link Input}.
 *//*  w  w w . ja  v  a 2  s .c om*/
public <X> X getInput(String name, Class<X> type, AnnotatedElement anno) {
    // Special case for file uploads
    if (type.isAssignableFrom(UploadedFile.class))
        return type.cast(fileUploads.get(name));

    // Also support just getting the bytes from a file without the name
    if (type.isArray() && type.getComponentType() == Byte.TYPE) {
        UploadedFile uploadedFile = fileUploads.get(name);
        return uploadedFile != null ? type.cast(uploadedFile.getBytes()) : null;
    }

    if (type.isArray() && anno.isAnnotationPresent(Input.MultiValued.class)) {
        // return type is an array; grab all
        Object o = inputParams.get(name);
        return type.cast(parseMultiValue(type, o, anno));
    }

    String v = Strings.extract(inputParams.get(name));

    // boolean is used for checkboxes, and false is encoded as a missing value
    if (type == Boolean.class || type == Boolean.TYPE) {
        @SuppressWarnings("unchecked")
        X x = (X) Boolean.valueOf(v != null && !"false".equalsIgnoreCase(v));
        return x;
    }

    // the remaining types have proper null values
    if (v == null)
        return null;

    return cast(parseSingleValue(type, v, anno, inputArgParsers));
}