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

public static String getMethodReturnOutVarType(Class returnClass, Class relClass) {
    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  v a 2s . c o  m*/
        return getCppType(returnClass, relClass);
    }
}

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

public static String getMethodReturnVarDeclareOut(Class returnClass, Class relClass) {
    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 {//w w  w  .j a v  a2 s.  c om
        return getCppType(returnClass, relClass) + " retVal((jobject)NULL,false);";
    }
}

From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java

/**
 * Returns true if it can convert the supplied
 * object to the specified class.//  w  ww.j  a va  2 s  . co  m
 * @param object to check
 * @param toType prospective destination class
 * @return boolean
 */
public boolean canConvert(Object object, final Class toType) {
    if (object == null) {
        return true;
    }
    final Class useType = TypeUtils.wrapPrimitive(toType);
    Class fromType = object.getClass();

    if (useType.isAssignableFrom(fromType)) {
        return true;
    }

    if (useType == String.class) {
        return true;
    }

    if (object instanceof Boolean && (Number.class.isAssignableFrom(useType)
            || "java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName()))) {
        return true;
    }
    if (object instanceof Number && (Number.class.isAssignableFrom(useType) || useType == Boolean.class)) {
        return true;
    }
    if (object instanceof String && (useType == Boolean.class || useType == Character.class
            || useType == Byte.class || useType == Short.class || useType == Integer.class
            || useType == Long.class || useType == Float.class || useType == Double.class)) {
        return true;
    }
    if (fromType.isArray()) {
        // Collection -> array
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            int length = Array.getLength(object);
            for (int i = 0; i < length; i++) {
                Object value = Array.get(object, i);
                if (!canConvert(value, cType)) {
                    return false;
                }
            }
            return true;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            return canCreateCollection(useType);
        }
        if (Array.getLength(object) > 0) {
            Object value = Array.get(object, 0);
            return canConvert(value, useType);
        }
        return canConvert("", useType);
    }
    if (object instanceof Collection) {
        // Collection -> array
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            Iterator it = ((Collection) object).iterator();
            while (it.hasNext()) {
                Object value = it.next();
                if (!canConvert(value, cType)) {
                    return false;
                }
            }
            return true;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            return canCreateCollection(useType);
        }
        if (((Collection) object).size() > 0) {
            Object value;
            if (object instanceof List) {
                value = ((List) object).get(0);
            } else {
                Iterator it = ((Collection) object).iterator();
                value = it.next();
            }
            return canConvert(value, useType);
        }
        return canConvert("", useType);
    }
    if (object instanceof NodeSet) {
        return canConvert(((NodeSet) object).getValues(), useType);
    }
    if (object instanceof Pointer) {
        return canConvert(((Pointer) object).getValue(), useType);
    }
    return ConvertUtils.lookup(useType) != null;
}

From source file:cz.lbenda.common.StringConverters.java

@SuppressWarnings("unchecked")
public static <T> StringConverter<T> converterForClass(Class<T> clazz) {
    if (Byte.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) BYTE_CONVERTER;
    }//from w ww . j a  v a  2s  .c o m
    if (Short.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) SHORT_CONVERTER;
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) INT_CONVERTER;
    }
    if (Long.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) LONG_CONVERTER;
    }
    if (Float.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) FLOAT_CONVERTER;
    }
    if (Double.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) DOUBLE_CONVERTER;
    }
    if (BigDecimal.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) DECIMAL_CONVERTER;
    }

    if (String.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) STRING_CONVERTER;
    }

    if (Boolean.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) BOOLEAN_CONVERTER;
    }

    if (java.sql.Date.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) SQL_DATE_CONVERTER;
    }
    if (java.sql.Time.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) SQL_TIME_CONVERTER;
    }
    if (java.sql.Timestamp.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) SQL_TIMESTAMP_CONVERTER;
    }
    if (LocalDate.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) LOCALDATE_CONVERTER;
    }
    if (LocalDateTime.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) LOCALDATETIME_CONVERTER;
    }
    if (LocalTime.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) LOCALTIME_CONVERTER;
    }
    if (BinaryData.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) BINARYDATA_CONVERTER;
    }
    if (Array.class.isAssignableFrom(clazz)) {
        return (StringConverter<T>) ARRAY_CONVERTER;
    }

    if (clazz.isArray()) {
        if (Byte.TYPE.isAssignableFrom(clazz.getComponentType())) {
            return (StringConverter<T>) BYTEARRAY_CONVERTER;
        } else {
            return (StringConverter<T>) OBJECTARRAY_CONVERTER;
        }
    }

    return (StringConverter<T>) OBJECT_CONVERTER;
}

From source file:com.azure.webapi.MobileServiceClient.java

/**
 * Invokes a custom API/*  ww w.j a v  a2s  . co m*/
 * 
 * @param apiName
 *            The API name
 * @param body
 *            The object to send as the request body
 * @param httpMethod
 *            The HTTP Method used to invoke the API
 * @param parameters
 *            The query string parameters sent in the request
 * @param clazz
 *            The API result class
 * @param callback
 *            The callback to invoke after the API execution
 */
public <E> void invokeApi(String apiName, Object body, String httpMethod, List<Pair<String, String>> parameters,
        final Class<E> clazz, final ApiOperationCallback<E> callback) {
    if (clazz == null) {
        if (callback != null) {
            callback.onCompleted(null, new IllegalArgumentException("clazz cannot be null"), null);
        }
        return;
    }

    JsonElement json = null;
    if (body != null) {
        json = getGsonBuilder().create().toJsonTree(body);
    }

    invokeApi(apiName, json, httpMethod, parameters, new ApiJsonOperationCallback() {

        @SuppressWarnings("unchecked")
        @Override
        public void onCompleted(JsonElement jsonElement, Exception exception, ServiceFilterResponse response) {
            if (callback != null) {
                if (exception == null) {
                    Class<?> concreteClass = clazz;
                    if (clazz.isArray()) {
                        concreteClass = clazz.getComponentType();
                    }

                    List<?> entities = JsonEntityParser.parseResults(jsonElement, getGsonBuilder().create(),
                            concreteClass);

                    if (clazz.isArray()) {
                        E array = (E) Array.newInstance(concreteClass, entities.size());
                        for (int i = 0; i < entities.size(); i++) {
                            Array.set(array, i, entities.get(i));
                        }
                        callback.onCompleted(array, null, response);
                    } else {
                        callback.onCompleted((E) entities.get(0), exception, response);
                    }
                } else {
                    callback.onCompleted(null, exception, response);
                }
            }
        }
    });
}

From source file:org.apache.axis.encoding.TypeMappingImpl.java

/**
 * Gets the SerializerFactory registered for the specified pair
 * of Java type and XML data type./* www .j a v  a  2 s  .co  m*/
 *
 * @param javaType - Class of the Java type
 * @param xmlType - Qualified name of the XML data type
 *
 * @return Registered SerializerFactory
 *
 * @throws JAXRPCException - If there is no registered SerializerFactory
 * for this pair of Java type and XML data type
 * java.lang.IllegalArgumentException -
 * If invalid or unsupported XML/Java type is specified
 */
public javax.xml.rpc.encoding.SerializerFactory getSerializer(Class javaType, QName xmlType)
        throws JAXRPCException {

    javax.xml.rpc.encoding.SerializerFactory sf = null;

    // If the xmlType was not provided, get one
    if (xmlType == null) {
        xmlType = getTypeQName(javaType, null);
        // If we couldn't find one, we're hosed, since getTypeQName()
        // already asked all of our delegates.
        if (xmlType == null) {
            return null;
        }
    }

    // Try to get the serializer associated with this pair
    Pair pair = new Pair(javaType, xmlType);

    // Now get the serializer with the pair
    sf = (javax.xml.rpc.encoding.SerializerFactory) pair2SF.get(pair);

    // Need to look into hierarchy of component type.
    // ex) java.util.GregorianCalendar[]
    //     -> java.util.Calendar[]
    if (sf == null && javaType.isArray()) {
        int dimension = 1;
        Class componentType = javaType.getComponentType();
        while (componentType.isArray()) {
            dimension += 1;
            componentType = componentType.getComponentType();
        }
        int[] dimensions = new int[dimension];
        componentType = componentType.getSuperclass();
        Class superJavaType = null;
        while (componentType != null) {
            superJavaType = Array.newInstance(componentType, dimensions).getClass();
            pair = new Pair(superJavaType, xmlType);
            sf = (javax.xml.rpc.encoding.SerializerFactory) pair2SF.get(pair);
            if (sf != null) {
                break;
            }
            componentType = componentType.getSuperclass();
        }
    }

    // check if ArrayOfT(xml)->T[](java) conversion is possible
    if (sf == null && javaType.isArray() && xmlType != null) {
        Pair pair2 = (Pair) qName2Pair.get(xmlType);
        if (pair2 != null && pair2.javaType != null && !pair2.javaType.isPrimitive()
                && ArrayUtil.isConvertable(pair2.javaType, javaType)) {
            sf = (javax.xml.rpc.encoding.SerializerFactory) pair2SF.get(pair2);
        }
    }

    // find serializer with xmlType
    if (sf == null && !javaType.isArray() && !Constants.isSchemaXSD(xmlType.getNamespaceURI())
            && !Constants.isSOAP_ENC(xmlType.getNamespaceURI())) {
        Pair pair2 = (Pair) qName2Pair.get(xmlType);
        if (pair2 != null && pair2.javaType != null && !pair2.javaType.isArray() // for array
                && (javaType.isAssignableFrom(pair2.javaType) || (pair2.javaType.isPrimitive()
                        && javaType == JavaUtils.getWrapperClass(pair2.javaType)))) // for derived type (xsd:restriction) 
        {
            sf = (javax.xml.rpc.encoding.SerializerFactory) pair2SF.get(pair2);
        }
    }

    return sf;
}

From source file:org.apache.axis.description.JavaServiceDesc.java

private QName getTypeQName(Class javaClass) {
    QName typeQName;//from www  .jav  a 2s.c  o  m
    TypeMapping tm = getTypeMapping();
    if (style == Style.RPC) {
        typeQName = tm.getTypeQName(javaClass);
    } else {
        typeQName = tm.getTypeQNameExact(javaClass);
        if (typeQName == null && javaClass.isArray()) {
            typeQName = tm.getTypeQName(javaClass.getComponentType());
        } else {
            typeQName = tm.getTypeQName(javaClass);
        }
    }
    return typeQName;
}

From source file:org.enerj.apache.commons.beanutils.locale.LocaleBeanUtilsBean.java

/**
 * Convert the specified value to the required type using the
 * specified convertion pattern./*  ww  w  .  j a  v a  2s  .c o m*/
 *
 * @param type The Java type of target property
 * @param index The indexed subscript value (if any)
 * @param value The value to be converted
 * @param pattern The convertion pattern
 */
protected Object convert(Class type, int index, Object value, String pattern) {

    if (log.isTraceEnabled()) {
        log.trace("Converting value '" + value + "' to type:" + type);
    }

    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 = getLocaleConvertUtils().convert((String[]) values, type, pattern);
        } else if (value instanceof String[]) {
            newValue = getLocaleConvertUtils().convert((String[]) value, type, pattern);
        } else {
            newValue = value;
        }
    } else if (type.isArray()) { // Indexed value into array
        if (value instanceof String) {
            newValue = getLocaleConvertUtils().convert((String) value, type.getComponentType(), pattern);
        } else if (value instanceof String[]) {
            newValue = getLocaleConvertUtils().convert(((String[]) value)[0], type.getComponentType(), pattern);
        } else {
            newValue = value;
        }
    } else { // Value into scalar
        if (value instanceof String) {
            newValue = getLocaleConvertUtils().convert((String) value, type, pattern);
        } else if (value instanceof String[]) {
            newValue = getLocaleConvertUtils().convert(((String[]) value)[0], type, pattern);
        } else {
            newValue = value;
        }
    }
    return newValue;
}

From source file:org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator.java

private QName generateSchemaForType(XmlSchemaSequence sequence, Class<?> type, String partName)
        throws Exception {

    boolean isArrayType = false;
    if (type != null) {
        isArrayType = type.isArray();//from www. java2  s .c  om
    }
    if (isArrayType) {
        if (type.getComponentType().isArray()) {
            // this is a double array element
            Class<?> simpleType = type.getComponentType();
            String simpleTypeName = "";
            while (simpleType.isArray()) {
                simpleTypeName += "ArrayOf";
                simpleType = simpleType.getComponentType();
            }
            simpleTypeName += getSimpleClassName(simpleType);

            return processParameterArrayTypes(sequence, type, partName, simpleTypeName);

        } else {
            type = type.getComponentType();
        }
    }
    if (AxisFault.class.getName().equals(type)) {
        return null;
    }
    String classTypeName;
    if (type == null) {
        classTypeName = "java.lang.Object";
    } else {
        classTypeName = type.getName();
    }
    if (isArrayType && "byte".equals(classTypeName)) {
        classTypeName = "base64Binary";
        isArrayType = false;
    }

    if (isDataHandler(type)) {
        classTypeName = "base64Binary";
    }

    return generateSchemaTypeforNameCommon(sequence, partName, isArrayType, type, classTypeName);
}

From source file:org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator.java

protected void generateSchemaforFieldsandProperties(XmlSchema xmlSchema, XmlSchemaSequence sequence,
        Class<?> type, String name, boolean isArrayType) throws Exception {
    String propertyName;/* w  w w .  jav  a 2  s .  c  om*/
    if (isArrayType) {
        propertyName = getClassName(type.getComponentType());
        if (type.getComponentType().isArray()) {
            // this is a double array element
            Class<?> simpleType = type.getComponentType();
            String simpleTypeName = "";
            while (simpleType.isArray()) {
                simpleTypeName += "ArrayOf";
                simpleType = simpleType.getComponentType();
            }
            simpleTypeName += getSimpleClassName(simpleType);

            if (xmlSchema.getTypeByName(simpleTypeName) == null) {
                XmlSchemaComplexType xmlSchemaComplexType = new XmlSchemaComplexType(xmlSchema);
                XmlSchemaSequence xmlSchemaSequence = new XmlSchemaSequence();
                xmlSchemaComplexType.setParticle(xmlSchemaSequence);
                generateSchemaforFieldsandProperties(xmlSchema, xmlSchemaSequence, type.getComponentType(),
                        "array", true);

                xmlSchemaComplexType.setName(simpleTypeName);
                xmlSchema.getItems().add(xmlSchemaComplexType);
                xmlSchema.getSchemaTypes().add(new QName(xmlSchema.getTargetNamespace(), simpleTypeName),
                        xmlSchemaComplexType);
            }

            if (isGenerateWrappedArrayTypes) {
                XmlSchemaElement xmlSchemaElement = new XmlSchemaElement();
                xmlSchemaElement.setName(name + "Wrapper");
                xmlSchemaElement.setNillable(true);
                sequence.getItems().add(xmlSchemaElement);

                String complexTypeName = simpleTypeName + "Wrapper";

                XmlSchemaComplexType xmlSchemaComplexType = new XmlSchemaComplexType(xmlSchema);
                XmlSchemaSequence xmlSchemaSequence = new XmlSchemaSequence();
                xmlSchemaComplexType.setParticle(xmlSchemaSequence);
                xmlSchemaComplexType.setName(complexTypeName);

                xmlSchema.getItems().add(xmlSchemaComplexType);
                xmlSchema.getSchemaTypes().add(new QName(schemaTargetNameSpace, xmlSchemaComplexType.getName()),
                        xmlSchemaComplexType);
                addContentToMethodSchemaType(xmlSchemaSequence,
                        new QName(xmlSchema.getTargetNamespace(), simpleTypeName), "array", true);

                xmlSchemaElement.setSchemaType(xmlSchemaComplexType);
                xmlSchemaElement
                        .setSchemaTypeName(new QName(schemaTargetNameSpace, xmlSchemaComplexType.getName()));

            } else {
                addContentToMethodSchemaType(sequence,
                        new QName(xmlSchema.getTargetNamespace(), simpleTypeName), name, true);

            }
            return;
        }
    } else {
        propertyName = getClassName(type);
    }
    if (isArrayType && "byte".equals(propertyName)) {
        propertyName = "base64Binary";
    }
    if (isDataHandler(type)) {
        propertyName = "base64Binary";
    }
    if (typeTable.isSimpleType(propertyName)) {

        if (isGenerateWrappedArrayTypes && isArrayType) {

            processGenerateWrappedArrayTypes(xmlSchema, sequence, type, name, isArrayType, propertyName);

        } else {
            addElementToSequence(name, typeTable.getSimpleSchemaTypeName(propertyName), sequence,
                    propertyName.equals("base64Binary"), isArrayType, type.isPrimitive());
        }

    } else {
        if (isArrayType) {
            generateSchema(type.getComponentType());
        } else {
            generateSchema(type);
        }

        if (isGenerateWrappedArrayTypes && isArrayType) {

            processGenerateWrappedArrayTypes(xmlSchema, sequence, type, name, isArrayType, propertyName);

        } else {
            addElementToSequence(name, typeTable.getComplexSchemaType(propertyName), sequence, false,
                    isArrayType, type.isPrimitive());
        }

        if (typeTable.getComplexSchemaType(propertyName) != null
                && !((NamespaceMap) xmlSchema.getNamespaceContext()).values()
                        .contains(typeTable.getComplexSchemaType(propertyName).getNamespaceURI())) {
            XmlSchemaImport importElement = new XmlSchemaImport();
            importElement.setNamespace(typeTable.getComplexSchemaType(propertyName).getNamespaceURI());
            xmlSchema.getItems().add(importElement);
            ((NamespaceMap) xmlSchema.getNamespaceContext()).put(generatePrefix(),
                    typeTable.getComplexSchemaType(propertyName).getNamespaceURI());
        }
    }

}