Example usage for java.lang Character TYPE

List of usage examples for java.lang Character TYPE

Introduction

In this page you can find the example usage for java.lang Character TYPE.

Prototype

Class TYPE

To view the source code for java.lang Character TYPE.

Click Source Link

Document

The Class instance representing the primitive type char .

Usage

From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.primitive.CharMorpher.java

public Class morphsTo() {
    return Character.TYPE;
}

From source file:org.yccheok.jstock.gui.POIUtils.java

private static void handlePrimitive(Method method, Class<?> clazz) {
    if (clazz == Boolean.TYPE) {
        parameterTypeMap.put(Boolean.class, method);
    } else if (clazz == Character.TYPE) {
        parameterTypeMap.put(Character.class, method);
    } else if (clazz == Byte.TYPE) {
        parameterTypeMap.put(Byte.class, method);
    } else if (clazz == Short.TYPE) {
        parameterTypeMap.put(Short.class, method);
    } else if (clazz == Integer.TYPE) {
        parameterTypeMap.put(Integer.class, method);
    } else if (clazz == Long.TYPE) {
        parameterTypeMap.put(Long.class, method);
    } else if (clazz == Float.TYPE) {
        parameterTypeMap.put(Float.class, method);
    } else if (clazz == Double.TYPE) {
        parameterTypeMap.put(Double.class, method);
    } // ... and so on for the other six primitive types (void doesn't matter)
}

From source file:net.yck.wkrdb.common.shared.PropertyConverter.java

/**
 * Performs a data type conversion from the specified value object to the
 * given target data class. If additional information is required for this
 * conversion, it is obtained from {@code DefaultConversionHandler.INSTANCE}
 * object. If the class is a primitive type (Integer.TYPE, Boolean.TYPE,
 * etc), the value returned will use the wrapper type (Integer.class,
 * Boolean.class, etc)./*from  ww w .  j  a va  2s .com*/
 *
 * @param cls
 *            the target class of the converted value
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             if the value is not compatible with the requested type
 */
public static Object to(Class<?> cls, Object value) throws ConversionException {
    if (cls.isInstance(value)) {
        return value; // no conversion needed
    }

    if (String.class.equals(cls)) {
        return String.valueOf(value);
    }
    if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) {
        return toBoolean(value);
    } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) {
        return toCharacter(value);
    } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) {
        if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) {
            return toInteger(value);
        } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) {
            return toLong(value);
        } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) {
            return toByte(value);
        } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) {
            return toShort(value);
        } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) {
            return toFloat(value);
        } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) {
            return toDouble(value);
        } else if (BigInteger.class.equals(cls)) {
            return toBigInteger(value);
        } else if (BigDecimal.class.equals(cls)) {
            return toBigDecimal(value);
        }
    } else if (Date.class.equals(cls)) {
        return toDate(value, DefaultConversionHandler.INSTANCE.getDateFormat());
    } else if (Calendar.class.equals(cls)) {
        return toCalendar(value, DefaultConversionHandler.INSTANCE.getDateFormat());
    } else if (URL.class.equals(cls)) {
        return toURL(value);
    } else if (Locale.class.equals(cls)) {
        return toLocale(value);
    } else if (isEnum(cls)) {
        return convertToEnum(cls, value);
    } else if (Color.class.equals(cls)) {
        return toColor(value);
    } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
        return toInternetAddress(value);
    } else if (InetAddress.class.isAssignableFrom(cls)) {
        return toInetAddress(value);
    }

    throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")"
            + " can't be converted to a " + cls.getName() + " object");
}

From source file:org.primeframework.mvc.parameter.convert.converters.CharacterConverterTest.java

@Test
public void toStrings() {
    GlobalConverter converter = new BooleanConverter(new MockConfiguration());
    String str = converter.convertToString(Character.class, null, "testExpr", null);
    assertNull(str);/*from ww  w  . j av a 2 s  . c o  m*/

    str = converter.convertToString(Character.class, null, "testExpr", 'c');
    assertEquals(str, "c");

    str = converter.convertToString(Character.TYPE, null, "testExpr", 'c');
    assertEquals(str, "c");
}

From source file:ClassUtils.java

/**
 * Helper for invoking an instance method that takes a single parameter.
 * This method also handles parameters of primitive type.
 * /*from   w  ww. j  a  v  a2 s.co m*/
 * @param cl
 *            The class that the instance belongs to
 * @param instance
 *            The object on which we will invoke the method
 * @param methodName
 *            The method name
 * @param param
 *            The parameter
 * @throws Throwable
 */
public static Object invokeMethod(Class cl, Object instance, String methodName, Object param) throws Throwable {
    Class paramClass;
    if (param instanceof Integer)
        paramClass = Integer.TYPE;
    else if (param instanceof Long)
        paramClass = Long.TYPE;
    else if (param instanceof Short)
        paramClass = Short.TYPE;
    else if (param instanceof Boolean)
        paramClass = Boolean.TYPE;
    else if (param instanceof Double)
        paramClass = Double.TYPE;
    else if (param instanceof Float)
        paramClass = Float.TYPE;
    else if (param instanceof Character)
        paramClass = Character.TYPE;
    else if (param instanceof Byte)
        paramClass = Byte.TYPE;
    else
        paramClass = param.getClass();
    Method method = cl.getMethod(methodName, new Class[] { paramClass });
    try {
        return method.invoke(instance, new Object[] { param });
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}

From source file:org.atemsource.atem.impl.common.attribute.primitive.PrimitiveTypeFactory.java

public PrimitiveType getPrimitiveType(Class primitiveType) {
    if (String.class.isAssignableFrom(primitiveType)) {
        return new SimpleTextType();
    } else if (Boolean.TYPE.isAssignableFrom(primitiveType)) {
        final BooleanTypeImpl booleanTypeImpl = new BooleanTypeImpl();
        booleanTypeImpl.setNullable(false);
        return booleanTypeImpl;
    } else if (Boolean.class.isAssignableFrom(primitiveType)) {
        return new BooleanTypeImpl();
    } else if (double.class.isAssignableFrom(primitiveType)) {
        final DoubleType doubleTypeImpl = new DoubleType();
        doubleTypeImpl.setNullable(false);
        return doubleTypeImpl;
    } else if (Double.class.isAssignableFrom(primitiveType)) {
        return new DoubleType();
    } else if (float.class.isAssignableFrom(primitiveType)) {
        final FloatTypeImpl typeImpl = new FloatTypeImpl();
        typeImpl.setNullable(false);//from   w ww  .  j ava  2 s  .  com
        return typeImpl;
    } else if (Float.class.isAssignableFrom(primitiveType)) {
        return new FloatTypeImpl();
    } else if (BigDecimal.class.isAssignableFrom(primitiveType)) {
        return new BigDecimalTypeImpl();
    } else if (int.class.isAssignableFrom(primitiveType)) {
        final IntegerType integerTypeImpl = new IntegerType();
        integerTypeImpl.setNullable(false);
        return integerTypeImpl;
    } else if (Integer.class.isAssignableFrom(primitiveType)) {
        return new IntegerType();
    } else if (long.class.isAssignableFrom(primitiveType)) {
        return new LongType(false);
    } else if (Long.class.isAssignableFrom(primitiveType)) {
        return new LongType(true);
    } else if (Number.class.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(Number.class, true);
    } else if (Character.TYPE.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(Character.TYPE, false);
    } else if (Character.class.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(Character.class, true);
    } else if (BigDecimal.class.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(BigDecimal.class, true);
    } else if (BigInteger.class.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(BigInteger.class, true);
    } else if (Byte.TYPE.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(Byte.TYPE, false);
    } else if (byte[].class.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(byte[].class, true);
    } else if (Byte.class.isAssignableFrom(primitiveType)) {
        return new SimplePrimitiveType(Byte.class, true);
    } else if (Enum.class.isAssignableFrom(primitiveType)) {
        return new SimpleEnumType(primitiveType);
    } else {
        PrimitiveType found = classToType.get(primitiveType);
        Class parentClass = primitiveType;
        while (found == null && parentClass.getSuperclass() != null) {
            found = classToType.get(parentClass);
            parentClass = parentClass.getSuperclass();
        }
        if (found == null) {
            for (Class<?> interfaze : primitiveType.getInterfaces()) {
                found = classToType.get(interfaze);
                if (found != null) {
                    return found;
                }
            }
        }
        return found;
    }
}

From source file:org.thingsplode.synapse.serializers.jackson.adapters.ParameterWrapperDeserializer.java

@Override
public ParameterWrapper deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonNode node = jp.readValueAsTree();
    if (node != null && node.size() > 0 && node.isContainerNode()) {
        ParameterWrapper pw = ParameterWrapper.create();
        ArrayNode paramsNode = (ArrayNode) node.get("params");
        Iterator<JsonNode> elemIterator = paramsNode.elements();
        while (elemIterator.hasNext()) {
            JsonNode currentNode = elemIterator.next();
            if (currentNode.getNodeType() == JsonNodeType.OBJECT) {
                try {
                    String paramid = ((ObjectNode) currentNode).get("paramid").asText();
                    String typeName = ((ObjectNode) currentNode).get("type").asText();
                    Class paramType = null;
                    if (null != typeName)
                        switch (typeName) {
                        case "long":
                            paramType = Long.TYPE;
                            break;
                        case "byte":
                            paramType = Byte.TYPE;
                            break;
                        case "short":
                            paramType = Short.TYPE;
                            break;
                        case "int":
                            paramType = Integer.TYPE;
                            break;
                        case "float":
                            paramType = Float.TYPE;
                            break;
                        case "double":
                            paramType = Double.TYPE;
                            break;
                        case "boolean":
                            paramType = Boolean.TYPE;
                            break;
                        case "char":
                            paramType = Character.TYPE;
                            break;
                        default:
                            paramType = Class.forName(typeName);
                            break;
                        }/*  w w  w.j  a v  a2  s  . c om*/
                    Object parameterObject = jp.getCodec().treeToValue(currentNode.get("value"), paramType);
                    pw.add(paramid, paramType, parameterObject);
                } catch (ClassNotFoundException ex) {
                    throw new JsonParseException(jp, ex.getMessage());
                }
            }
        }
        return pw;
    } else {
        return null;
    }
}

From source file:demo.config.PropertyConverter.java

/**
 * Converts the specified value to the target class. If the class is a
 * primitive type (Integer.TYPE, Boolean.TYPE, etc) the value returned will
 * use the wrapper type (Integer.class, Boolean.class, etc).
 * /*from w w  w.j a v a 2 s. c o m*/
 * @param cls
 *            the target class of the converted value
 * @param value
 *            the value to convert
 * @param params
 *            optional parameters used for the conversion
 * @return the converted value
 * @throws ConversionException
 *             if the value is not compatible with the requested type
 * 
 * @since 1.5
 */
static Object to(Class<?> cls, Object value, Object[] params) throws ConversionException {
    if (cls.isInstance(value)) {
        return value; // no conversion needed
    }

    if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) {
        return toBoolean(value);
    } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) {
        return toCharacter(value);
    } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) {
        if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) {
            return toInteger(value);
        } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) {
            return toLong(value);
        } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) {
            return toByte(value);
        } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) {
            return toShort(value);
        } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) {
            return toFloat(value);
        } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) {
            return toDouble(value);
        } else if (BigInteger.class.equals(cls)) {
            return toBigInteger(value);
        } else if (BigDecimal.class.equals(cls)) {
            return toBigDecimal(value);
        }
    } else if (Date.class.equals(cls)) {
        return toDate(value, (String) params[0]);
    } else if (Calendar.class.equals(cls)) {
        return toCalendar(value, (String) params[0]);
    } else if (URL.class.equals(cls)) {
        return toURL(value);
    } else if (Locale.class.equals(cls)) {
        return toLocale(value);
    } else if (isEnum(cls)) {
        return convertToEnum(cls, value);
    } else if (Color.class.equals(cls)) {
        return toColor(value);
    } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
        return toInternetAddress(value);
    } else if (InetAddress.class.isAssignableFrom(cls)) {
        return toInetAddress(value);
    }

    throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")"
            + " can't be converted to a " + cls.getName() + " object");
}

From source file:nl.strohalm.cyclos.utils.conversion.CoercionHelper.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object convert(Class toType, Object value) {
    if ("".equals(value)) {
        value = null;/*w w  w  .  j av a 2s  .com*/
    }
    // If we do not want a collection, but the value is one, use the first value
    if (value != null && !(Collection.class.isAssignableFrom(toType) || toType.isArray())
            && (value.getClass().isArray() || value instanceof Collection)) {
        final Iterator it = IteratorUtils.getIterator(value);
        if (!it.hasNext()) {
            value = null;
        } else {
            value = it.next();
        }
    }

    // Check for null values
    if (value == null) {
        if (toType.isPrimitive()) {
            // On primitives, use the default value
            if (toType == Boolean.TYPE) {
                value = Boolean.FALSE;
            } else if (toType == Character.TYPE) {
                value = '\0';
            } else {
                value = 0;
            }
        } else {
            // For objects, return null
            return null;
        }
    }

    // Check if the value is already of the expected type
    if (toType.isInstance(value)) {
        return value;
    }

    // If the class is primitive, use the wrapper, so we have an easier work of testing instances
    if (toType.isPrimitive()) {
        toType = ClassUtils.primitiveToWrapper(toType);
    }

    // Convert to well-known types
    if (String.class.isAssignableFrom(toType)) {
        if (value instanceof Entity) {
            final Long entityId = ((Entity) value).getId();
            return entityId == null ? null : entityId.toString();
        }
        return value.toString();
    } else if (Number.class.isAssignableFrom(toType)) {
        if (!(value instanceof Number)) {
            if (value instanceof String) {
                value = new BigDecimal((String) value);
            } else if (value instanceof Date) {
                value = ((Date) value).getTime();
            } else if (value instanceof Calendar) {
                value = ((Calendar) value).getTimeInMillis();
            } else if (value instanceof Entity) {
                value = ((Entity) value).getId();
                if (value == null) {
                    return null;
                }
            } else {
                throw new ConversionException("Invalid number: " + value);
            }
        }
        final Number number = (Number) value;
        if (Byte.class.isAssignableFrom(toType)) {
            return number.byteValue();
        } else if (Short.class.isAssignableFrom(toType)) {
            return number.shortValue();
        } else if (Integer.class.isAssignableFrom(toType)) {
            return number.intValue();
        } else if (Long.class.isAssignableFrom(toType)) {
            return number.longValue();
        } else if (Float.class.isAssignableFrom(toType)) {
            return number.floatValue();
        } else if (Double.class.isAssignableFrom(toType)) {
            return number.doubleValue();
        } else if (BigInteger.class.isAssignableFrom(toType)) {
            return new BigInteger(number.toString());
        } else if (BigDecimal.class.isAssignableFrom(toType)) {
            return new BigDecimal(number.toString());
        }
    } else if (Boolean.class.isAssignableFrom(toType)) {
        if (value instanceof Number) {
            return ((Number) value).intValue() != 0;
        } else if ("on".equalsIgnoreCase(value.toString())) {
            return true;
        } else {
            return Boolean.parseBoolean(value.toString());
        }
    } else if (Character.class.isAssignableFrom(toType)) {
        final String str = value.toString();
        return (str.length() == 0) ? null : str.charAt(0);
    } else if (Calendar.class.isAssignableFrom(toType)) {
        if (value instanceof Date) {
            final Calendar cal = new GregorianCalendar();
            cal.setTime((Date) value);
            return cal;
        }
    } else if (Date.class.isAssignableFrom(toType)) {
        if (value instanceof Calendar) {
            final long millis = ((Calendar) value).getTimeInMillis();
            try {
                return ConstructorUtils.invokeConstructor(toType, millis);
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }
    } else if (Enum.class.isAssignableFrom(toType)) {
        Object ret;
        try {
            ret = Enum.valueOf(toType, value.toString());
        } catch (final Exception e) {
            ret = null;
        }
        if (ret == null) {
            Object[] possible;
            try {
                possible = (Object[]) toType.getMethod("values").invoke(null);
            } catch (final Exception e) {
                throw new IllegalStateException(
                        "Couldn't invoke the 'values' method for enum " + toType.getName());
            }
            if (StringValuedEnum.class.isAssignableFrom(toType)) {
                final String test = coerce(String.class, value);
                for (final Object item : possible) {
                    if (((StringValuedEnum) item).getValue().equals(test)) {
                        ret = item;
                        break;
                    }
                }
            } else if (IntValuedEnum.class.isAssignableFrom(toType)) {
                final int test = coerce(Integer.TYPE, value);
                for (final Object item : possible) {
                    if (((IntValuedEnum) item).getValue() == test) {
                        ret = item;
                        break;
                    }
                }
            } else {
                throw new ConversionException("Invalid enum: " + value);
            }
        }
        return ret;
    } else if (Entity.class.isAssignableFrom(toType)) {
        final Long id = coerce(Long.class, value);
        return EntityHelper.reference(toType, id);
    } else if (Locale.class.isAssignableFrom(toType)) {
        return LocaleConverter.instance().valueOf(value.toString());
    } else if (Collection.class.isAssignableFrom(toType)) {
        final Collection collection = (Collection) ClassHelper.instantiate(toType);
        final Iterator iterator = IteratorUtils.getIterator(value);
        while (iterator.hasNext()) {
            collection.add(iterator.next());
        }
        return collection;
    } else if (toType.isArray()) {
        final Collection collection = coerceCollection(toType.getComponentType(), value);
        final Object[] array = (Object[]) Array.newInstance(toType.getComponentType(), collection.size());
        return collection.toArray(array);
    }

    // We don't know how to convert the value
    throw new ConversionException("Cannot coerce value to: " + toType.getName());
}

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

private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException {
    String key = f.getName();/*from www. j  av  a2s  . c om*/
    Class type = f.getType();
    try {
        if (type.isPrimitive()) {
            if (type == Integer.TYPE) {
                f.setInt(retObj, jsonObj.getInt(key));
            } else if (type == Long.TYPE) {
                f.setLong(retObj, jsonObj.getInt(key));
            } else if (type == Short.TYPE) {
                f.setShort(retObj, (short) jsonObj.getInt(key));
            } else if (type == Boolean.TYPE) {
                f.setBoolean(retObj, jsonObj.getBoolean(key));
            } else if (type == Double.TYPE) {
                f.setDouble(retObj, jsonObj.getDouble(key));
            } else if (type == Float.TYPE) {
                f.setFloat(retObj, (float) jsonObj.getDouble(key));
            } else if (type == Character.TYPE) {
                char ch = jsonObj.getString(key).charAt(0);
                f.setChar(retObj, ch);
            } else if (type == Byte.TYPE) {
                f.setByte(retObj, (byte) jsonObj.getInt(key));
            } else {
                throw new JSONSerializationException("Unknown primitive: " + type);
            }
        } else if (type == String.class) {
            f.set(retObj, jsonObj.getString(key));
        } else if (JSONSerializable.class.isAssignableFrom(type)) {
            JSONObject jObj = jsonObj.getJSONObject(key);
            JSONSerializable serObj = deSerialize(type, jObj);
            f.set(retObj, serObj);
        }
    } catch (Exception e) {
        throw new JSONSerializationException(e.getMessage(), e);
    }
}