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.github.wshackle.java4cpp.J4CppMain.java

private static String classToParamNameDecl(Class<?> c, int index) {
    if (c.isArray()) {
        return classToParamName(c.getComponentType()) + "Array_" + index;
    }//from  w ww . ja v a 2s. com
    return c.getSimpleName().substring(0, 1).toLowerCase() + c.getSimpleName().substring(1) + "_" + index;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String getCppType(Class<?> clss, Class<?> relClass) {
    if (clss.isArray()) {
        Class<?> componentType = clss.getComponentType();
        return getCppArrayType(componentType);
    }/*from   w w w  .j a v a 2  s.  co  m*/
    if (clss.isPrimitive()) {
        return getCppPrimitiveType(clss);
    }
    if (Void.class.isAssignableFrom(clss)) {
        return "void";
    } else if (isString(clss)) {
        return "jstring";
    } else {
        return getCppRelativeName(clss, relClass);
    }
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String getEasyCallCppType(Class<?> clss, Class<?> relClass) {
    if (clss.isArray()) {
        Class<?> componentType = clss.getComponentType();
        return getCppEasyCallArrayType(componentType);
    }//from   w  ww  .java2s . co m
    if (clss.isPrimitive()) {
        return getCppPrimitiveType(clss);
    }
    if (Void.class.isAssignableFrom(clss)) {
        return "void";
    } else if (isString(clss)) {
        return "const char *";
    } else {
        return getCppRelativeName(clss, relClass);
    }
}

From source file:com.googlecode.jsonrpc4j.JsonRpcServer.java

/**
 * Determines whether or not the given {@link JsonNode} matches
 * the given type.  This method is limitted to a few java types
 * only and shouldn't be used to determine with great accuracy
 * whether or not the types match./* w  w w . j  a  v a2 s .  co  m*/
 *
 * @param node the {@link JsonNode}
 * @param type the {@link Class}
 * @return true if the types match, false otherwise
 */
private boolean isMatchingType(JsonNode node, Class<?> type) {

    if (node.isNull()) {
        return true;

    } else if (node.isTextual()) {
        return String.class.isAssignableFrom(type);

    } else if (node.isNumber()) {
        return Number.class.isAssignableFrom(type) || short.class.isAssignableFrom(type)
                || int.class.isAssignableFrom(type) || long.class.isAssignableFrom(type)
                || float.class.isAssignableFrom(type) || double.class.isAssignableFrom(type);

    } else if (node.isArray() && type.isArray()) {
        return (node.size() > 0) ? isMatchingType(node.get(0), type.getComponentType()) : false;

    } else if (node.isArray()) {
        return type.isArray() || Collection.class.isAssignableFrom(type);

    } else if (node.isBinary()) {
        return byte[].class.isAssignableFrom(type) || Byte[].class.isAssignableFrom(type)
                || char[].class.isAssignableFrom(type) || Character[].class.isAssignableFrom(type);

    } else if (node.isBoolean()) {
        return boolean.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type);

    } else if (node.isObject() || node.isPojo()) {
        return !type.isPrimitive() && !String.class.isAssignableFrom(type)
                && !Number.class.isAssignableFrom(type) && !Boolean.class.isAssignableFrom(type);
    }

    // not sure if it's a matching type
    return false;
}

From source file:com.nortal.jroad.typegen.xmlbeans.XteeSchemaCodePrinter.java

static void getTypeName(Class<?> c, StringBuffer sb) {
    int arrayCount = 0;
    while (c.isArray()) {
        c = c.getComponentType();
        arrayCount++;/*from  w  w  w .  j ava2s .com*/
    }

    sb.append(c.getName());

    for (int i = 0; i < arrayCount; i++)
        sb.append("[]");

}

From source file:javadz.beanutils.locale.LocaleBeanUtilsBean.java

/**
 *  Convert the specified value to the required type.
 *
 * @param type The Java type of target property
 * @param index The indexed subscript value (if any)
 * @param value The value to be converted
 * @return The converted value/*from   w ww  .  jav  a  2 s . com*/
 */
protected Object convert(Class type, int index, Object value) {

    Object newValue = null;

    if (type.isArray() && (index < 0)) { // Scalar value into array
        if (value instanceof String) {
            String[] values = new String[1];
            values[0] = (String) value;
            newValue = ConvertUtils.convert(values, type);
        } else if (value instanceof String[]) {
            newValue = ConvertUtils.convert((String[]) value, type);
        } else {
            newValue = value;
        }
    } else if (type.isArray()) { // Indexed value into array
        if (value instanceof String) {
            newValue = ConvertUtils.convert((String) value, type.getComponentType());
        } else if (value instanceof String[]) {
            newValue = ConvertUtils.convert(((String[]) value)[0], type.getComponentType());
        } else {
            newValue = value;
        }
    } else { // Value into scalar
        if (value instanceof String) {
            newValue = ConvertUtils.convert((String) value, type);
        } else if (value instanceof String[]) {
            newValue = ConvertUtils.convert(((String[]) value)[0], type);
        } else {
            newValue = value;
        }
    }
    return newValue;
}

From source file:com.trafficspaces.api.model.Resource.java

public void setJSONObject(JSONObject jsonObject) {
    Iterator itr = jsonObject.keys();
    while (itr.hasNext()) {
        String key = (String) itr.next();
        Object value = jsonObject.opt(key);

        try {/*w  ww.  j  av  a2s. c o m*/
            Field field = this.getClass().getField(key);
            Class type = field.getType();

            int fieldModifiers = field.getModifiers();
            //System.out.println("key=" + key + ", name=" + field.getName() + ", value=" + value + ", type=" +type + ", componentType=" +type.getComponentType() + 
            //      ", ispublic="+Modifier.isPublic(fieldModifiers) + ", isstatic="+Modifier.isStatic(fieldModifiers) + ", isnative="+Modifier.isNative(fieldModifiers) +
            //      ", isprimitive="+type.isPrimitive() + ", isarray="+type.isArray() + ", isResource="+Resource.class.isAssignableFrom(type));

            if (type.isPrimitive()) {
                if (type.equals(int.class)) {
                    field.setInt(this, jsonObject.getInt(key));
                } else if (type.equals(double.class)) {
                    field.setDouble(this, jsonObject.getDouble(key));
                }
            } else if (type.isArray()) {
                JSONArray jsonArray = null;
                if (value instanceof JSONArray) {
                    jsonArray = (JSONArray) value;
                } else if (value instanceof JSONObject) {
                    JSONObject jsonSubObject = (JSONObject) value;
                    jsonArray = jsonSubObject.optJSONArray(key.substring(0, key.length() - 1));
                }
                if (jsonArray != null && jsonArray.length() > 0) {
                    Class componentType = type.getComponentType();
                    Object[] values = (Object[]) Array.newInstance(componentType, jsonArray.length());
                    for (int j = 0; j < jsonArray.length(); j++) {
                        Resource resource = (Resource) componentType.newInstance();
                        resource.setJSONObject(jsonArray.getJSONObject(j));
                        values[j] = resource;
                    }
                    field.set(this, values);
                }
            } else if (Resource.class.isAssignableFrom(type) && value instanceof JSONObject) {
                Resource resource = (Resource) type.newInstance();
                resource.setJSONObject((JSONObject) value);
                field.set(this, resource);
            } else if (type.equals(String.class) && value instanceof String) {
                field.set(this, (String) value);
            }
        } catch (NoSuchFieldException nsfe) {
            System.err.println("warning: field does not exist. key=" + key + ",value=" + value);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("error: key=" + key + ",value=" + value + ", error=" + e.getMessage());
        }
    }
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

public static String getMethodReturnVarType(Class returnClass) {
    if (returnClass.isArray()) {
        return getMethodReturnVarArrayType(returnClass.getComponentType());
    } else if (boolean.class.isAssignableFrom(returnClass)) {
        return "jboolean";
    } else if (byte.class.isAssignableFrom(returnClass)) {
        return "jbyte";
    } else if (char.class.isAssignableFrom(returnClass)) {
        return "jchar";
    } else if (short.class.isAssignableFrom(returnClass)) {
        return "jshort";
    } else if (int.class.isAssignableFrom(returnClass)) {
        return "jint";
    } else if (long.class.isAssignableFrom(returnClass)) {
        return "jlong";
    } else if (float.class.isAssignableFrom(returnClass)) {
        return "jfloat";
    } else if (double.class.isAssignableFrom(returnClass)) {
        return "jdouble";
    } else if (void.class.isAssignableFrom(returnClass)) {
        return "";
    } else if (Void.class.isAssignableFrom(returnClass)) {
        return "";
    } else if (isString(returnClass)) {
        return "jstring";
    } else {// ww w  . j  a  va 2 s  .  c  om
        return "jobject";
    }
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

public static String getMethodReturnVarDeclare(Class returnClass) {
    if (returnClass.isArray()) {
        return getMethodReturnArrayVarDeclare(returnClass.getComponentType());
    } else if (boolean.class.isAssignableFrom(returnClass)) {
        return "jboolean retVal=false;";
    } else if (byte.class.isAssignableFrom(returnClass)) {
        return "jbyte retVal= (jbyte) -1;";
    } else if (char.class.isAssignableFrom(returnClass)) {
        return "jchar retVal= (jchar) -1;";
    } else if (short.class.isAssignableFrom(returnClass)) {
        return "jshort retVal=(jshort) -1;";
    } else if (int.class.isAssignableFrom(returnClass)) {
        return "jint retVal= (jint) -1;";
    } else if (long.class.isAssignableFrom(returnClass)) {
        return "jlong retVal= (jlong) -1;";
    } else if (float.class.isAssignableFrom(returnClass)) {
        return "jfloat retVal= (jfloat) -1.0;";
    } else if (double.class.isAssignableFrom(returnClass)) {
        return "jdouble retVal= (jdouble) -1.0;";
    } else if (void.class.isAssignableFrom(returnClass)) {
        return "";
    } else if (Void.class.isAssignableFrom(returnClass)) {
        return "";
    } else if (isString(returnClass)) {
        return "jstring retVal=NULL;";
    } else {/*from   w w w.j  a va 2  s  .c om*/
        return "jobject retVal=NULL;";
    }
}

From source file:com.astamuse.asta4d.data.DefaultDataTypeTransformer.java

private List<DataValueConvertor> extractConvertors(final Class<?> srcType, final Class<?> targetType) {

    List<DataValueConvertor> foundConvertorList = new LinkedList<DataValueConvertor>();

    // find in list as element to element
    for (DataValueConvertor convertor : DataTypeConvertorList) {
        Pair<Class, Class> typePair = extractConvertorTypeInfo(convertor);
        if (typePair == null) {
            continue;
        }/*  ww  w  .ja v  a 2 s. c o  m*/
        if (typePair.getLeft().isAssignableFrom(srcType)) {
            if (targetType.isAssignableFrom(typePair.getRight())) {// found one
                foundConvertorList.add(convertor);
            } else if (convertor instanceof DataValueConvertorTargetTypeConvertable
                    && typePair.getRight().isAssignableFrom(targetType)) {
                foundConvertorList
                        .add(((DataValueConvertorTargetTypeConvertable) convertor).convert(targetType));
            }
        }
        // @formatter:on
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    // find as array to array
    if (srcType.isArray() && targetType.isArray()) {

        List<DataValueConvertor> componentConvertorList = findConvertor(srcType.getComponentType(),
                targetType.getComponentType());
        List<DataValueConvertor> toArrayConvertorList = ListConvertUtil.transform(componentConvertorList,
                new RowConvertor<DataValueConvertor, DataValueConvertor>() {
                    @Override
                    public DataValueConvertor convert(int rowIndex,
                            final DataValueConvertor originalConvertor) {
                        return new DataValueConvertor() {
                            Pair<Class, Class> typePair = extractConvertorTypeInfo(originalConvertor);

                            @Override
                            public Object convert(Object obj) throws UnsupportedValueException {
                                if (typePair == null) {
                                    return null;
                                }

                                int length = Array.getLength(obj);
                                Object targetArray = Array.newInstance(targetType.getComponentType(), length);

                                for (int i = 0; i < length; i++) {
                                    Array.set(targetArray, i, originalConvertor.convert(Array.get(obj, i)));
                                }
                                return targetArray;
                            }
                        };
                    }
                });

        foundConvertorList.addAll(toArrayConvertorList);
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    // find as element to array
    if (targetType.isArray()) {

        List<DataValueConvertor> componentConvertorList = findConvertor(srcType, targetType.getComponentType());
        List<DataValueConvertor> toArrayConvertorList = ListConvertUtil.transform(componentConvertorList,
                new RowConvertor<DataValueConvertor, DataValueConvertor>() {
                    @Override
                    public DataValueConvertor convert(int rowIndex,
                            final DataValueConvertor originalConvertor) {
                        return new DataValueConvertor() {
                            private Pair<Class, Class> typePair = extractConvertorTypeInfo(originalConvertor);

                            @Override
                            public Object convert(Object obj) throws UnsupportedValueException {
                                if (typePair == null) {
                                    return null;
                                }
                                Object array = Array.newInstance(targetType.getComponentType(), 1);
                                Array.set(array, 0, originalConvertor.convert(obj));
                                return array;
                            }
                        };
                    }
                });

        foundConvertorList.addAll(toArrayConvertorList);
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    // find as array to element
    if (srcType.isArray()) {
        List<DataValueConvertor> componentConvertorList = findConvertor(srcType.getComponentType(), targetType);
        List<DataValueConvertor> toArrayConvertorList = ListConvertUtil.transform(componentConvertorList,
                new RowConvertor<DataValueConvertor, DataValueConvertor>() {
                    @Override
                    public DataValueConvertor convert(int rowIndex,
                            final DataValueConvertor originalConvertor) {
                        return new DataValueConvertor() {
                            @Override
                            public Object convert(Object obj) throws UnsupportedValueException {
                                int length = Array.getLength(obj);
                                if (length == 0) {
                                    return null;
                                } else {
                                    return originalConvertor.convert(Array.get(obj, 0));
                                }
                            }
                        };
                    }
                });

        foundConvertorList.addAll(toArrayConvertorList);
    }

    if (!foundConvertorList.isEmpty()) {
        return foundConvertorList;
    }

    return foundConvertorList;
}