Example usage for java.lang Class getComponentType

List of usage examples for java.lang Class getComponentType

Introduction

In this page you can find the example usage for java.lang Class getComponentType.

Prototype

public Class<?> getComponentType() 

Source Link

Document

Returns the Class representing the component type of an array.

Usage

From source file:com.springframework.beans.BeanUtils.java

/**
 * Check if the given type represents a "simple" property:
 * a primitive, a String or other CharSequence, a Number, a Date,
 * a URI, a URL, a Locale, a Class, or a corresponding array.
 * <p>Used to determine properties to check for a "simple" dependency-check.
 * @param clazz the type to check//from  w ww. j a  va  2  s .  c o  m
 * @return whether the given type represents a "simple" property
 */
public static boolean isSimpleProperty(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType()));
}

From source file:org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils.java

public static Type getArrayElementType(Type t) {
    if (t instanceof Class && ((Class<?>) t).isArray()) {
        Class<?> arrayClass = (Class<?>) t;
        return arrayClass.getComponentType();
    } else if (t instanceof GenericArrayType) {
        GenericArrayType arrayType = (GenericArrayType) t;
        return arrayType.getGenericComponentType();
    }/*from www. j  a v  a  2s .co m*/
    return null;
}

From source file:org.apache.myfaces.shared_impl.renderkit._SharedRendererUtils.java

static Object getConvertedUISelectManyValue(FacesContext facesContext, UISelectMany component,
        String[] submittedValue) throws ConverterException {
    // Attention!
    // This code is duplicated in jsfapi component package.
    // If you change something here please do the same in the other class!

    if (submittedValue == null)
        throw new NullPointerException("submittedValue");

    ValueBinding vb = component.getValueBinding("value");
    Class valueType = null;
    Class arrayComponentType = null;
    if (vb != null) {
        valueType = vb.getType(facesContext);
        if (valueType != null && valueType.isArray()) {
            arrayComponentType = valueType.getComponentType();
        }/*from w  w  w  . j  a v a  2 s.  c o m*/
    }

    Converter converter = component.getConverter();
    if (converter == null) {
        if (valueType == null) {
            // No converter, and no idea of expected type
            // --> return the submitted String array
            return submittedValue;
        }

        if (List.class.isAssignableFrom(valueType)) {
            // expected type is a List
            // --> according to javadoc of UISelectMany we assume that the element type
            //     is java.lang.String, and copy the String array to a new List
            int len = submittedValue.length;
            List lst = new ArrayList(len);
            for (int i = 0; i < len; i++) {
                lst.add(submittedValue[i]);
            }
            return lst;
        }

        if (arrayComponentType == null) {
            throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array");
        }

        if (String.class.equals(arrayComponentType))
            return submittedValue; //No conversion needed for String type
        if (Object.class.equals(arrayComponentType))
            return submittedValue; //No conversion for Object class

        try {
            converter = facesContext.getApplication().createConverter(arrayComponentType);
        } catch (FacesException e) {
            log(facesContext, "No Converter for type " + arrayComponentType.getName() + " found", e);
            return submittedValue;
        }
        if (converter == null) {
            log(facesContext, "No Converter for type " + arrayComponentType.getName() + " found.", null);
            return submittedValue;
        }
    }

    // Now, we have a converter...
    // We determine the type of the component array after converting one of it's elements
    if (vb != null) {
        valueType = vb.getType(facesContext);
        if (valueType != null && valueType.isArray()) {
            if (submittedValue.length > 0) {
                arrayComponentType = converter.getAsObject(facesContext, component, submittedValue[0])
                        .getClass();
            }
        }
    }

    if (valueType == null) {
        // ...but have no idea of expected type
        // --> so let's convert it to an Object array
        int len = submittedValue.length;
        Object[] convertedValues = (Object[]) Array
                .newInstance(arrayComponentType == null ? Object.class : arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            convertedValues[i] = converter.getAsObject(facesContext, component, submittedValue[i]);
        }
        return convertedValues;
    }

    if (List.class.isAssignableFrom(valueType)) {
        // Curious case: According to specs we should assume, that the element type
        // of this List is java.lang.String. But there is a Converter set for this
        // component. Because the user must know what he is doing, we will convert the values.
        int len = submittedValue.length;
        List lst = new ArrayList(len);
        for (int i = 0; i < len; i++) {
            lst.add(converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return lst;
    }

    if (arrayComponentType == null) {
        throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array");
    }

    if (arrayComponentType.isPrimitive()) {
        //primitive array
        int len = submittedValue.length;
        Object convertedValues = Array.newInstance(arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            Array.set(convertedValues, i, converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return convertedValues;
    } else {
        //Object array
        int len = submittedValue.length;
        ArrayList convertedValues = new ArrayList(len);
        for (int i = 0; i < len; i++) {
            convertedValues.add(i, converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return convertedValues.toArray((Object[]) Array.newInstance(arrayComponentType, len));
    }
}

From source file:im.r_c.android.dbox.TableInfo.java

/**
 * Make a ObjectColumnInfo object from a instance field.
 *
 * @param field        field//from  ww  w .  j a  va2s. c o  m
 * @param objectColumn virtual object column
 * @return object column info
 */
static ObjectColumnInfo of(Field field, ObjectColumn objectColumn) {
    ObjectColumnInfo oci = new ObjectColumnInfo();

    Class<?> fieldType = field.getType();
    Class<?> elemType = objectColumn.value();
    if (fieldType == elemType) {
        // @ObjectColumn(Foo.class)
        // private Foo foo;
        oci.mType = TYPE_OBJECT;
    } else if (fieldType.isArray() && fieldType.getComponentType() == elemType) {
        // @ObjectColumn(Foo.class)
        // private Foo[] foos;
        oci.mType = TYPE_OBJECT_ARRAY;
    } else if (fieldType == List.class) {
        // @ObjectColumn(Foo.class)
        // private List<Foo> fooList;
        oci.mType = TYPE_OBJECT_LIST;
    } else {
        throw new IllegalArgumentException("Unsupported field type found: " + fieldType);
    }

    if (elemType.getAnnotation(Table.class) == null) {
        // No "Table" annotation found
        throw new IllegalArgumentException(
                "The element type \"" + elemType + "\" of object field \"" + field + "\" is not a table.");
    }

    oci.mElemClass = elemType;
    oci.mField = field;

    if (!oci.mField.isAccessible()) {
        oci.mField.setAccessible(true);
    }

    return oci;
}

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

public static JSONObject serializeJSONObject(JSONSerializable obj)
        throws JSONSerializationException, JSONException {
    if (obj instanceof JSONExternalizable) {
        return ((JSONExternalizable) obj).toJSON();
    }//from w  w w  . j av  a 2s .  c o  m

    JSONObject jsonObj = new JSONObject();
    Class clz = obj.getClass();
    Field[] fields = clz.getDeclaredFields();

    for (int i = 0; i < fields.length; ++i) {
        fields[i].setAccessible(true);
        int modifiers = fields[i].getModifiers();
        if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) {
            String name = fields[i].getName();
            Class type = fields[i].getType();

            try {
                Object value = fields[i].get(obj);

                if (type.isArray() && value != null) {
                    int len = Array.getLength(value);

                    Class cls = type.getComponentType();

                    JSONArray array = new JSONArray();

                    for (int k = 0; k < len; ++k) {
                        dumpObject(array, cls, value, k);
                    }
                    jsonObj.put(name, array);
                } else {
                    dumpObject(jsonObj, fields[i], obj);
                }
            } catch (Exception e) {
                throw new JSONSerializationException(e.getMessage(), e);
            }
        }
    }

    return jsonObj;
}

From source file:org.istsms.util.Javabean2JSON.java

public static Object fromJSONArray(JSONArray jArr, Class arrayType) {
    if (jArr != null && arrayType != null) {
        Class baseType = arrayType.getComponentType();
        if (baseType == null)
            return null; //it was not an array!      
        String baseTypeName = baseType.getCanonicalName();
        int length = jArr.length();
        Object argArray = Array.newInstance(baseType, length);
        try {/* w ww.  j  a  va2s .  com*/
            Vector v = new Vector();
            for (int j = 0; j < jArr.length(); j++) {
                //four cases: the json Array element may be:
                //a. a JSON Array (in case of array of arrays)
                //b. a calendar
                //c. a JSON Object
                //d. a primitive value (serialized as a String)                           
                Object jElement = jArr.get(j);
                if (jElement.equals(null))
                    continue;
                if (jElement instanceof JSONArray) {
                    Object element = fromJSONArray((JSONArray) jElement, baseType);
                    if (element != null)
                        Array.set(argArray, j, element);
                } else if (isCalendar(baseTypeName)) {
                    Object element = toJavaCalendar((JSONObject) jElement);
                    if (element != null)
                        Array.set(argArray, j, element);
                } else if (jElement instanceof JSONObject) {
                    Object element = fromJSONObject((JSONObject) jElement, baseType);
                    if (element != null)
                        Array.set(argArray, j, element);
                } else {
                    Object primitive = getPrimitive(jArr.getString(j), baseTypeName);
                    if (primitive != null)
                        Array.set(argArray, j, primitive);
                }
            }

            return argArray;
        } catch (NegativeArraySizeException e) {
        } catch (JSONException e) {
        }
    }
    return null;
}

From source file:org.fit.cssbox.scriptbox.script.javascript.java.HostedJavaMethod.java

/**
 * Casts the given arguments into given expected types.
 * //from  ww  w .  ja v a2s  .c o m
 * @param expectedTypes Types into which should be casted the given arguments.
 * @param args Arguments to be casted.
 * @return Array of the casted arguments if casting was successful, otherwise null.
 */
public static Object[] castArgs(Class<?>[] expectedTypes, Object... args) {
    if (expectedTypes != null && args != null) {

        if (expectedTypes.length <= args.length + 1 && expectedTypes.length > 0) {
            Class<?> lastType = expectedTypes[expectedTypes.length - 1];
            if (lastType.isArray()) {
                Class<?> arrayType = lastType.getComponentType();

                boolean maybeVarargs = true;
                if (expectedTypes.length == args.length) {
                    Object lastArg = args[args.length - 1];
                    Class<?> lastArgClass = (lastArg != null) ? lastArg.getClass() : null;
                    maybeVarargs = lastArgClass != null && !ClassUtils.isAssignable(lastArgClass, lastType);
                }

                if (maybeVarargs) {
                    for (int i = expectedTypes.length - 1; i < args.length; i++) {
                        if (args[i] == null) {
                            continue;
                        }
                        Class<?> argType = args[i].getClass();

                        if (!ClassUtils.isAssignable(argType, arrayType)) {
                            maybeVarargs = false;
                            break;
                        }
                    }

                    if (maybeVarargs) {
                        Object[] oldArgs = args;
                        args = new Object[expectedTypes.length];

                        for (int i = 0; i < expectedTypes.length - 1; i++) {
                            args[i] = oldArgs[i];
                        }

                        Object[] varargs = new Object[oldArgs.length - expectedTypes.length + 1];

                        for (int i = expectedTypes.length - 1; i < oldArgs.length; i++) {
                            varargs[i - expectedTypes.length + 1] = oldArgs[i];
                        }

                        args[expectedTypes.length - 1] = varargs;
                    }
                }
            }
        }

        if (expectedTypes.length == args.length) {
            Object[] castedArgs = new Object[args.length];
            for (int i = 0; i < args.length; i++) {
                Object arg = args[i];
                Class<?> expectedType = expectedTypes[i];
                if (arg == null) {
                    castedArgs[i] = null;
                } else if (arg == Undefined.instance) {
                    castedArgs[i] = null;
                } else if (arg instanceof ConsString) {
                    castedArgs[i] = ((ConsString) arg).toString();
                } else if (arg instanceof Double
                        && (expectedType.equals(Integer.class) || expectedType.equals(int.class)
                                || expectedType.equals(Long.class) || expectedType.equals(long.class))) {
                    castedArgs[i] = ((Double) arg).intValue();
                } else {
                    castedArgs[i] = WindowJavaScriptEngine.jsToJava(arg);
                    //castedArgs[i] = Context.jsToJava(castedArgs[i], expectedType);
                }

                castedArgs[i] = ClassField.wrap(expectedTypes[i], castedArgs[i]);
            }

            return castedArgs;
        }
    }

    return null;
}

From source file:org.jfree.chart.demo.SymbolicXYPlotDemo.java

/**
 * Transform an primitive array to an object array.
 * //from  ww w.j a v a2s  . c o m
 * @param arr
 *           the array.
 * @return an array.
 */
private static Object toArray(final Object arr) {

    if (arr == null) {
        return arr;
    }

    final Class cls = arr.getClass();
    if (!cls.isArray()) {
        return arr;
    }

    Class compType = cls.getComponentType();
    int dim = 1;
    while (!compType.isPrimitive()) {
        if (!compType.isArray()) {
            return arr;
        } else {
            dim++;
            compType = compType.getComponentType();
        }
    }

    final int[] length = new int[dim];
    length[0] = Array.getLength(arr);
    Object[] newarr = null;

    try {
        if (compType.equals(Integer.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Integer"), length);
        } else if (compType.equals(Double.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Double"), length);
        } else if (compType.equals(Long.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Long"), length);
        } else if (compType.equals(Float.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Float"), length);
        } else if (compType.equals(Short.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Short"), length);
        } else if (compType.equals(Byte.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Byte"), length);
        } else if (compType.equals(Character.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Character"), length);
        } else if (compType.equals(Boolean.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Boolean"), length);
        }
    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
    }

    for (int i = 0; i < length[0]; i++) {
        if (dim != 1) {
            newarr[i] = toArray(Array.get(arr, i));
        } else {
            newarr[i] = Array.get(arr, i);
        }
    }
    return newarr;
}

From source file:org.ajax4jsf.util.SelectUtils.java

/**
 * Converts UISelectMany submitted value to converted value
 * /*from   ww w.  j a va2  s  .c om*/
 * @author Manfred Geiler
 * @param facesContext
 * @param component
 * @param submittedValue
 * @return
 * @throws ConverterException
 */
public static Object getConvertedUISelectManyValue(FacesContext facesContext, UISelectMany component,
        String[] submittedValue) throws ConverterException {
    // Attention!
    // This code is duplicated in jsfapi component package.
    // If you change something here please do the same in the other class!

    if (submittedValue == null)
        throw new NullPointerException("submittedValue");

    ELContext elContext = facesContext.getELContext();
    ValueExpression vb = component.getValueExpression("value");
    Class<?> valueType = null;
    Class<?> arrayComponentType = null;
    if (vb != null) {
        valueType = vb.getType(elContext);
        if (valueType != null && valueType.isArray()) {
            arrayComponentType = valueType.getComponentType();
        }
    }

    Converter converter = component.getConverter();
    if (converter == null) {
        if (valueType == null) {
            // No converter, and no idea of expected type
            // --> return the submitted String array
            return submittedValue;
        }

        if (List.class.isAssignableFrom(valueType)) {
            // expected type is a List
            // --> according to javadoc of UISelectMany we assume that the
            // element type
            // is java.lang.String, and copy the String array to a new List
            List<String> lst = Arrays.asList(submittedValue);
            return lst;
        }

        if (arrayComponentType == null) {
            throw new IllegalArgumentException(Messages.getMessage(Messages.VALUE_BINDING_TYPE_ERROR));
        }

        if (String.class.equals(arrayComponentType))
            return submittedValue; // No conversion needed for String type
        if (Object.class.equals(arrayComponentType))
            return submittedValue; // No conversion for Object class

        try {
            converter = facesContext.getApplication().createConverter(arrayComponentType);
        } catch (FacesException e) {
            log.error(Messages.getMessage(Messages.NO_CONVERTER_FOUND_ERROR, arrayComponentType.getName()), e);
            return submittedValue;
        }
    }

    // Now, we have a converter...
    if (valueType == null) {
        // ...but have no idea of expected type
        // --> so let's convert it to an Object array
        int len = submittedValue.length;
        Object[] convertedValues = (Object[]) Array
                .newInstance(arrayComponentType == null ? Object.class : arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            convertedValues[i] = converter.getAsObject(facesContext, component, submittedValue[i]);
        }
        return convertedValues;
    }

    if (List.class.isAssignableFrom(valueType)) {
        // Curious case: According to specs we should assume, that the
        // element type
        // of this List is java.lang.String. But there is a Converter set
        // for this
        // component. Because the user must know what he is doing, we will
        // convert the values.
        int len = submittedValue.length;
        List<Object> lst = new ArrayList<Object>(len);
        for (int i = 0; i < len; i++) {
            lst.add(converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return lst;
    }

    if (arrayComponentType == null) {
        throw new IllegalArgumentException(Messages.getMessage(Messages.VALUE_BINDING_TYPE_ERROR));
    }

    if (arrayComponentType.isPrimitive()) {
        // primitive array
        int len = submittedValue.length;
        Object convertedValues = Array.newInstance(arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            Array.set(convertedValues, i, converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return convertedValues;
    } else {
        // Object array
        int len = submittedValue.length;
        ArrayList<Object> convertedValues = new ArrayList<Object>(len);
        for (int i = 0; i < len; i++) {
            convertedValues.add(i, converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return convertedValues.toArray((Object[]) Array.newInstance(arrayComponentType, len));
    }
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for checking whether two objects of the given type are
 * equal. This method is used to compare the parameters of two annotation
 * instances.//w  w w . ja  v  a  2s. com
 *
 * @param type the type of the objects to be compared
 * @param o1 the first object
 * @param o2 the second object
 * @return a flag whether these objects are equal
 */
private static boolean memberEquals(Class<?> type, Object o1, Object o2) {
    if (o1 == o2) {
        return true;
    }
    if (o1 == null || o2 == null) {
        return false;
    }
    if (type.isArray()) {
        return arrayMemberEquals(type.getComponentType(), o1, o2);
    }
    if (type.isAnnotation()) {
        return equals((Annotation) o1, (Annotation) o2);
    }
    return o1.equals(o2);
}