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.apache.hadoop.hbase.io.HbaseObjectWritable.java

/**
 * Read a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding.//from   w  ww.ja  v  a  2  s  .c  o  m
 * @param in
 * @param objectWritable
 * @param conf
 * @return the object
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static Object readObject(DataInput in, HbaseObjectWritable objectWritable, Configuration conf)
        throws IOException {
    Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
    Object instance;
    if (declaredClass.isPrimitive()) { // primitive types
        if (declaredClass == Boolean.TYPE) { // boolean
            instance = Boolean.valueOf(in.readBoolean());
        } else if (declaredClass == Character.TYPE) { // char
            instance = Character.valueOf(in.readChar());
        } else if (declaredClass == Byte.TYPE) { // byte
            instance = Byte.valueOf(in.readByte());
        } else if (declaredClass == Short.TYPE) { // short
            instance = Short.valueOf(in.readShort());
        } else if (declaredClass == Integer.TYPE) { // int
            instance = Integer.valueOf(in.readInt());
        } else if (declaredClass == Long.TYPE) { // long
            instance = Long.valueOf(in.readLong());
        } else if (declaredClass == Float.TYPE) { // float
            instance = Float.valueOf(in.readFloat());
        } else if (declaredClass == Double.TYPE) { // double
            instance = Double.valueOf(in.readDouble());
        } else if (declaredClass == Void.TYPE) { // void
            instance = null;
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declaredClass);
        }
    } else if (declaredClass.isArray()) { // array
        if (declaredClass.equals(byte[].class)) {
            instance = Bytes.readByteArray(in);
        } else if (declaredClass.equals(Result[].class)) {
            instance = Result.readArray(in);
        } else {
            int length = in.readInt();
            instance = Array.newInstance(declaredClass.getComponentType(), length);
            for (int i = 0; i < length; i++) {
                Array.set(instance, i, readObject(in, conf));
            }
        }
    } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE
        Class<?> componentType = readClass(conf, in);
        int length = in.readInt();
        instance = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(instance, i, readObject(in, conf));
        }
    } else if (List.class.isAssignableFrom(declaredClass)) { // List
        int length = in.readInt();
        instance = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            ((ArrayList) instance).add(readObject(in, conf));
        }
    } else if (declaredClass == String.class) { // String
        instance = Text.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in));
    } else if (declaredClass == Message.class) {
        String className = Text.readString(in);
        try {
            declaredClass = getClassByName(conf, className);
            instance = tryInstantiateProtobuf(declaredClass, in);
        } catch (ClassNotFoundException e) {
            LOG.error("Can't find class " + className, e);
            throw new IOException("Can't find class " + className, e);
        }
    } else { // Writable or Serializable
        Class instanceClass = null;
        int b = (byte) WritableUtils.readVInt(in);
        if (b == NOT_ENCODED) {
            String className = Text.readString(in);
            try {
                instanceClass = getClassByName(conf, className);
            } catch (ClassNotFoundException e) {
                LOG.error("Can't find class " + className, e);
                throw new IOException("Can't find class " + className, e);
            }
        } else {
            instanceClass = CODE_TO_CLASS.get(b);
        }
        if (Writable.class.isAssignableFrom(instanceClass)) {
            Writable writable = WritableFactories.newInstance(instanceClass, conf);
            try {
                writable.readFields(in);
            } catch (Exception e) {
                LOG.error("Error in readFields", e);
                throw new IOException("Error in readFields", e);
            }
            instance = writable;
            if (instanceClass == NullInstance.class) { // null
                declaredClass = ((NullInstance) instance).declaredClass;
                instance = null;
            }
        } else {
            int length = in.readInt();
            byte[] objectBytes = new byte[length];
            in.readFully(objectBytes);
            ByteArrayInputStream bis = null;
            ObjectInputStream ois = null;
            try {
                bis = new ByteArrayInputStream(objectBytes);
                ois = new ObjectInputStream(bis);
                instance = ois.readObject();
            } catch (ClassNotFoundException e) {
                LOG.error("Class not found when attempting to deserialize object", e);
                throw new IOException("Class not found when attempting to " + "deserialize object", e);
            } finally {
                if (bis != null)
                    bis.close();
                if (ois != null)
                    ois.close();
            }
        }
    }
    if (objectWritable != null) { // store values
        objectWritable.declaredClass = declaredClass;
        objectWritable.instance = instance;
    }
    return instance;
}

From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java

/**
 * Read a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*from w ww.j  a va 2  s.  com*/
 * @param in
 * @param objectWritable
 * @param conf
 * @return the object
 * @throws IOException
 */
@SuppressWarnings("unchecked")
static Object readObject(DataInput in, HbaseObjectWritableFor96Migration objectWritable, Configuration conf)
        throws IOException {
    Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
    Object instance;
    if (declaredClass.isPrimitive()) { // primitive types
        if (declaredClass == Boolean.TYPE) { // boolean
            instance = Boolean.valueOf(in.readBoolean());
        } else if (declaredClass == Character.TYPE) { // char
            instance = Character.valueOf(in.readChar());
        } else if (declaredClass == Byte.TYPE) { // byte
            instance = Byte.valueOf(in.readByte());
        } else if (declaredClass == Short.TYPE) { // short
            instance = Short.valueOf(in.readShort());
        } else if (declaredClass == Integer.TYPE) { // int
            instance = Integer.valueOf(in.readInt());
        } else if (declaredClass == Long.TYPE) { // long
            instance = Long.valueOf(in.readLong());
        } else if (declaredClass == Float.TYPE) { // float
            instance = Float.valueOf(in.readFloat());
        } else if (declaredClass == Double.TYPE) { // double
            instance = Double.valueOf(in.readDouble());
        } else if (declaredClass == Void.TYPE) { // void
            instance = null;
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declaredClass);
        }
    } else if (declaredClass.isArray()) { // array
        if (declaredClass.equals(byte[].class)) {
            instance = Bytes.readByteArray(in);
        } else {
            int length = in.readInt();
            instance = Array.newInstance(declaredClass.getComponentType(), length);
            for (int i = 0; i < length; i++) {
                Array.set(instance, i, readObject(in, conf));
            }
        }
    } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE
        Class<?> componentType = readClass(conf, in);
        int length = in.readInt();
        instance = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(instance, i, readObject(in, conf));
        }
    } else if (List.class.isAssignableFrom(declaredClass)) { // List
        int length = in.readInt();
        instance = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            ((ArrayList) instance).add(readObject(in, conf));
        }
    } else if (declaredClass == String.class) { // String
        instance = Text.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in));
    } else if (declaredClass == Message.class) {
        String className = Text.readString(in);
        try {
            declaredClass = getClassByName(conf, className);
            instance = tryInstantiateProtobuf(declaredClass, in);
        } catch (ClassNotFoundException e) {
            LOG.error("Can't find class " + className, e);
            throw new IOException("Can't find class " + className, e);
        }
    } else if (Scan.class.isAssignableFrom(declaredClass)) {
        int length = in.readInt();
        byte[] scanBytes = new byte[length];
        in.readFully(scanBytes);
        ClientProtos.Scan.Builder scanProto = ClientProtos.Scan.newBuilder();
        instance = ProtobufUtil.toScan(scanProto.mergeFrom(scanBytes).build());
    } else { // Writable or Serializable
        Class instanceClass = null;
        int b = (byte) WritableUtils.readVInt(in);
        if (b == NOT_ENCODED) {
            String className = Text.readString(in);
            try {
                instanceClass = getClassByName(conf, className);
            } catch (ClassNotFoundException e) {
                LOG.error("Can't find class " + className, e);
                throw new IOException("Can't find class " + className, e);
            }
        } else {
            instanceClass = CODE_TO_CLASS.get(b);
        }
        if (Writable.class.isAssignableFrom(instanceClass)) {
            Writable writable = WritableFactories.newInstance(instanceClass, conf);
            try {
                writable.readFields(in);
            } catch (Exception e) {
                LOG.error("Error in readFields", e);
                throw new IOException("Error in readFields", e);
            }
            instance = writable;
            if (instanceClass == NullInstance.class) { // null
                declaredClass = ((NullInstance) instance).declaredClass;
                instance = null;
            }
        } else {
            int length = in.readInt();
            byte[] objectBytes = new byte[length];
            in.readFully(objectBytes);
            ByteArrayInputStream bis = null;
            ObjectInputStream ois = null;
            try {
                bis = new ByteArrayInputStream(objectBytes);
                ois = new ObjectInputStream(bis);
                instance = ois.readObject();
            } catch (ClassNotFoundException e) {
                LOG.error("Class not found when attempting to deserialize object", e);
                throw new IOException("Class not found when attempting to " + "deserialize object", e);
            } finally {
                if (bis != null)
                    bis.close();
                if (ois != null)
                    ois.close();
            }
        }
    }
    if (objectWritable != null) { // store values
        objectWritable.declaredClass = declaredClass;
        objectWritable.instance = instance;
    }
    return instance;
}

From source file:javadz.beanutils.ConvertUtilsBean.java

/**
 * Register the converters for primitive types.
 * </p>//from   w  ww .  j  a  v a  2s . c  o m
 * This method registers the following converters:
 * <ul>
 *     <li><code>Boolean.TYPE</code> - {@link BooleanConverter}</li>
 *     <li><code>Byte.TYPE</code> - {@link ByteConverter}</li>
 *     <li><code>Character.TYPE</code> - {@link CharacterConverter}</li>
 *     <li><code>Double.TYPE</code> - {@link DoubleConverter}</li>
 *     <li><code>Float.TYPE</code> - {@link FloatConverter}</li>
 *     <li><code>Integer.TYPE</code> - {@link IntegerConverter}</li>
 *     <li><code>Long.TYPE</code> - {@link LongConverter}</li>
 *     <li><code>Short.TYPE</code> - {@link ShortConverter}</li>
 * </ul>
 * @param throwException <code>true</code> if the converters should
 * throw an exception when a conversion error occurs, otherwise <code>
 * <code>false</code> if a default value should be used.
 */
private void registerPrimitives(boolean throwException) {
    register(Boolean.TYPE, throwException ? new BooleanConverter() : new BooleanConverter(Boolean.FALSE));
    register(Byte.TYPE, throwException ? new ByteConverter() : new ByteConverter(ZERO));
    register(Character.TYPE, throwException ? new CharacterConverter() : new CharacterConverter(SPACE));
    register(Double.TYPE, throwException ? new DoubleConverter() : new DoubleConverter(ZERO));
    register(Float.TYPE, throwException ? new FloatConverter() : new FloatConverter(ZERO));
    register(Integer.TYPE, throwException ? new IntegerConverter() : new IntegerConverter(ZERO));
    register(Long.TYPE, throwException ? new LongConverter() : new LongConverter(ZERO));
    register(Short.TYPE, throwException ? new ShortConverter() : new ShortConverter(ZERO));
}

From source file:com.complexible.pinto.RDFMapper.java

private Object valueToObject(final Value theValue, final Model theGraph,
        final PropertyDescriptor theDescriptor) {
    if (theValue instanceof Literal) {
        final Literal aLit = (Literal) theValue;
        final IRI aDatatype = aLit.getDatatype() != null ? aLit.getDatatype() : null;

        if (aDatatype == null || XMLSchema.STRING.equals(aDatatype) || RDFS.LITERAL.equals(aDatatype)) {
            String aStr = aLit.getLabel();

            if (theDescriptor != null && Character.TYPE.isAssignableFrom(theDescriptor.getPropertyType())) {
                if (aStr.length() == 1) {
                    return aStr.charAt(0);
                } else {
                    throw new RDFMappingException("Bean type is char, but value is a a string.");
                }//from   w ww .j a v a  2 s  .c  o  m
            } else {
                return aStr;
            }
        } else if (XMLSchema.BOOLEAN.equals(aDatatype)) {
            return Boolean.valueOf(aLit.getLabel());
        } else if (INTEGER_TYPES.contains(aDatatype)) {
            return Integer.parseInt(aLit.getLabel());
        } else if (LONG_TYPES.contains(aDatatype)) {
            return Long.parseLong(aLit.getLabel());
        } else if (XMLSchema.DOUBLE.equals(aDatatype)) {
            return Double.valueOf(aLit.getLabel());
        } else if (FLOAT_TYPES.contains(aDatatype)) {
            return Float.valueOf(aLit.getLabel());
        } else if (SHORT_TYPES.contains(aDatatype)) {
            return Short.valueOf(aLit.getLabel());
        } else if (BYTE_TYPES.contains(aDatatype)) {
            return Byte.valueOf(aLit.getLabel());
        } else if (XMLSchema.ANYURI.equals(aDatatype)) {
            try {
                return new java.net.URI(aLit.getLabel());
            } catch (URISyntaxException e) {
                LOGGER.warn("URI syntax exception converting literal value which is not a valid URI {} ",
                        aLit.getLabel());
                return null;
            }
        } else if (XMLSchema.DATE.equals(aDatatype) || XMLSchema.DATETIME.equals(aDatatype)) {
            return Dates2.asDate(aLit.getLabel());
        } else if (XMLSchema.TIME.equals(aDatatype)) {
            return new Date(Long.parseLong(aLit.getLabel()));
        } else {
            throw new RuntimeException("Unsupported or unknown literal datatype: " + aLit);
        }
    } else if (theDescriptor != null && Enum.class.isAssignableFrom(theDescriptor.getPropertyType())) {
        IRI aURI = (IRI) theValue;
        Object[] aEnums = theDescriptor.getPropertyType().getEnumConstants();
        for (Object aObj : aEnums) {
            if (((Enum) aObj).name().equals(aURI.getLocalName())) {
                return aObj;
            }
        }

        for (Field aField : theDescriptor.getPropertyType().getFields()) {
            Iri aAnnotation = aField.getAnnotation(Iri.class);
            if (aAnnotation != null && aURI.equals(iri(aAnnotation.value()))) {
                for (Object aObj : aEnums) {
                    if (((Enum) aObj).name().equals(aField.getName())) {
                        return aObj;
                    }
                }

                // if the uri in the Iri annotation equals the value we're converting, but there was no field
                // match, something bad has happened
                throw new RDFMappingException("Expected enum value not found");
            }
        }

        LOGGER.info("{} maps to the enum {}, but does not correspond to any of the values of the enum.", aURI,
                theDescriptor.getPropertyType());

        return null;
    } else {
        Resource aResource = (Resource) theValue;

        final Class aClass = pinpointClass(theGraph, aResource, theDescriptor);

        RDFCodec aCodec = mCodecs.get(aClass);
        if (aCodec != null) {
            return aCodec.readValue(theGraph, aResource);
        } else {
            return readValue(theGraph, aClass, aResource);
        }
    }
}

From source file:org.apache.struts.action.DynaActionForm.java

/**
 * <p>Indicates if an object of the source class is assignable to the
 * destination class.</p>//from  w ww.  jav  a2  s.c o  m
 *
 * @param dest   Destination class
 * @param source Source class
 * @return <code>true</code> if the source is assignable to the
 *         destination; <code>false</code> otherwise.
 */
protected boolean isDynaAssignable(Class dest, Class source) {
    if (dest.isAssignableFrom(source) || ((dest == Boolean.TYPE) && (source == Boolean.class))
            || ((dest == Byte.TYPE) && (source == Byte.class))
            || ((dest == Character.TYPE) && (source == Character.class))
            || ((dest == Double.TYPE) && (source == Double.class))
            || ((dest == Float.TYPE) && (source == Float.class))
            || ((dest == Integer.TYPE) && (source == Integer.class))
            || ((dest == Long.TYPE) && (source == Long.class))
            || ((dest == Short.TYPE) && (source == Short.class))) {
        return (true);
    } else {
        return (false);
    }
}

From source file:ResultSetIterator.java

/**
 * ResultSet.getObject() returns an Integer object for an INT column.  The
 * setter method for the property might take an Integer or a primitive int.
 * This method returns true if the value can be successfully passed into
 * the setter method.  Remember, Method.invoke() handles the unwrapping
 * of Integer into an int.//from w ww.j  a va2s.c  o m
 * 
 * @param value The value to be passed into the setter method.
 * @param type The setter's parameter type.
 * @return boolean True if the value is compatible.
 */
private boolean isCompatibleType(Object value, Class type) {
    // Do object check first, then primitives
    if (value == null || type.isInstance(value)) {
        return true;

    } else if (type.equals(Integer.TYPE) && Integer.class.isInstance(value)) {
        return true;

    } else if (type.equals(Long.TYPE) && Long.class.isInstance(value)) {
        return true;

    } else if (type.equals(Double.TYPE) && Double.class.isInstance(value)) {
        return true;

    } else if (type.equals(Float.TYPE) && Float.class.isInstance(value)) {
        return true;

    } else if (type.equals(Short.TYPE) && Short.class.isInstance(value)) {
        return true;

    } else if (type.equals(Byte.TYPE) && Byte.class.isInstance(value)) {
        return true;

    } else if (type.equals(Character.TYPE) && Character.class.isInstance(value)) {
        return true;

    } else if (type.equals(Boolean.TYPE) && Boolean.class.isInstance(value)) {
        return true;

    } else {
        return false;
    }

}

From source file:com.sun.faces.config.ManagedBeanFactory.java

/**
 * <li><p> Call the property getter, if it exists.</p></li>
 * <p/>/* w  w  w  .  j a  va 2s  . co m*/
 * <li><p>If the getter returns null or doesn't exist, create a
 * java.util.ArrayList(), otherwise use the returned Object (an array or
 * a java.util.List).</p></li>
 * <p/>
 * <li><p>If a List was returned or created in step 2), add all
 * elements defined by nested &lt;value&gt; elements in the order
 * they are listed, converting values defined by nested
 * &lt;value&gt; elements to the type defined by
 * &lt;value-class&gt;. If a &lt;value-class&gt; is not defined, use
 * the value as-is (i.e., as a java.lang.String). Add null for each
 * &lt;null-value&gt; element.</p></li>
 * <p/>
 * <li><p> If an array was returned in step 2), create a
 * java.util.ArrayList and copy all elements from the returned array to
 * the new List, auto-boxing elements of a primitive type. Add all
 * elements defined by nested &lt;value&gt; elements as described in step
 * 3).</p></li>
 * <p/>
 * <li><p> If a new java.util.List was created in step 2) and the
 * property is of type List, set the property by calling the setter
 * method, or log an error if there is no setter method.</p></li>
 * <p/>
 * <li><p> If a new java.util.List was created in step 4), convert
 * the * List to array of the same type as the property and set the
 * property by * calling the setter method, or log an error if there
 * is no setter * method.</p></li>
 */

private void setArrayOrListPropertiesIntoBean(Object bean, ManagedPropertyBean property) throws Exception {
    Object result = null;
    boolean getterIsNull = true, getterIsArray = false;
    List valuesForBean = null;
    Class valueType = java.lang.String.class, propertyType = null;

    String propertyName = property.getPropertyName();

    try {
        // see if there is a getter
        result = PropertyUtils.getProperty(bean, propertyName);
        getterIsNull = (null == result) ? true : false;

        propertyType = PropertyUtils.getPropertyType(bean, propertyName);
        getterIsArray = propertyType.isArray();

    } catch (NoSuchMethodException nsme) {
        // it's valid to not have a getter.
    }

    // the property has to be either a List or Array
    if (!getterIsArray) {
        if (null != propertyType && !java.util.List.class.isAssignableFrom(propertyType)) {
            throw new FacesException(
                    Util.getExceptionMessageString(Util.MANAGED_BEAN_CANNOT_SET_LIST_ARRAY_PROPERTY_ID,
                            new Object[] { propertyName, managedBean.getManagedBeanName() }));
        }
    }

    //
    // Deal with the possibility of the getter returning existing
    // values.
    //

    // if the getter returned non-null
    if (!getterIsNull) {
        // if what it returned was an array
        if (getterIsArray) {
            valuesForBean = new ArrayList();
            for (int i = 0, len = Array.getLength(result); i < len; i++) {
                // add the existing values
                valuesForBean.add(Array.get(result, i));
            }
        } else {
            // if what it returned was not a List
            if (!(result instanceof List)) {
                // throw an exception                    
                throw new FacesException(
                        Util.getExceptionMessageString(Util.MANAGED_BEAN_EXISTING_VALUE_NOT_LIST_ID,
                                new Object[] { propertyName, managedBean.getManagedBeanName() }));
            }
            valuesForBean = (List) result;
        }
    } else {

        // getter returned null
        result = valuesForBean = new ArrayList();
    }

    // at this point valuesForBean contains the existing values from
    // the bean, or no values if the bean had no values.  In any
    // case, we can proceed to add values from the config file.
    valueType = copyListEntriesFromConfigToList(property.getListEntries(), valuesForBean);

    // at this point valuesForBean has the values to be set into the
    // bean.

    if (getterIsArray) {
        // convert back to Array
        result = Array.newInstance(valueType, valuesForBean.size());
        for (int i = 0, len = valuesForBean.size(); i < len; i++) {
            if (valueType == Boolean.TYPE) {
                Array.setBoolean(result, i, ((Boolean) valuesForBean.get(i)).booleanValue());
            } else if (valueType == Byte.TYPE) {
                Array.setByte(result, i, ((Byte) valuesForBean.get(i)).byteValue());
            } else if (valueType == Double.TYPE) {
                Array.setDouble(result, i, ((Double) valuesForBean.get(i)).doubleValue());
            } else if (valueType == Float.TYPE) {
                Array.setFloat(result, i, ((Float) valuesForBean.get(i)).floatValue());
            } else if (valueType == Integer.TYPE) {
                Array.setInt(result, i, ((Integer) valuesForBean.get(i)).intValue());
            } else if (valueType == Character.TYPE) {
                Array.setChar(result, i, ((Character) valuesForBean.get(i)).charValue());
            } else if (valueType == Short.TYPE) {
                Array.setShort(result, i, ((Short) valuesForBean.get(i)).shortValue());
            } else if (valueType == Long.TYPE) {
                Array.setLong(result, i, ((Long) valuesForBean.get(i)).longValue());
            } else {
                Array.set(result, i, valuesForBean.get(i));
            }
        }
    } else {
        result = valuesForBean;
    }

    if (getterIsNull || getterIsArray) {
        PropertyUtils.setProperty(bean, propertyName, result);
    }

}

From source file:it.unibo.alchemist.language.EnvironmentBuilder.java

@SuppressWarnings("unchecked")
private static Object parseAndCreate(final Class<?> clazz, final String val, final Map<String, Object> env,
        final RandomGenerator random)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    if (clazz.isAssignableFrom(RandomGenerator.class) && val.equalsIgnoreCase("random")) {
        L.debug("Random detected! Class " + clazz.getSimpleName() + ", param: " + val);
        if (random == null) {
            L.error("Random instatiation required, but RandomGenerator not yet defined.");
        }/*from w w  w  . j  a  va 2s . c o  m*/
        return random;
    }
    if (clazz.isPrimitive() || PrimitiveUtils.classIsWrapper(clazz)) {
        L.debug(val + " is a primitive or a wrapper: " + clazz);
        if ((clazz.isAssignableFrom(Boolean.TYPE) || clazz.isAssignableFrom(Boolean.class))
                && (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false"))) {
            return Boolean.parseBoolean(val);
        }
        /*
         * If Number is in clazz's hierarchy
         */
        if (PrimitiveUtils.classIsNumber(clazz)) {
            final Optional<Number> num = extractNumber(val);
            if (num.isPresent()) {
                final Optional<Number> castNum = PrimitiveUtils.castIfNeeded(clazz, num.get());
                /*
                 * If method requires Object or unsupported Number, return
                 * what was parsed.
                 */
                return castNum.orElse(num.get());
            }
        }
        if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {
            return val.charAt(0);
        }
    }
    if (List.class.isAssignableFrom(clazz) && val.startsWith("[") && val.endsWith("]")) {
        final List<Constructor<List<?>>> l = unsafeExtractConstructors(clazz);
        @SuppressWarnings("rawtypes")
        final List list = tryToBuild(l, new ArrayList<String>(0), env, random);
        final StringTokenizer strt = new StringTokenizer(val.substring(1, val.length() - 1), ",; ");
        while (strt.hasMoreTokens()) {
            final String sub = strt.nextToken();
            final Object o = tryToParse(sub, env, random);
            if (o == null) {
                L.debug("WARNING: list elemnt skipped: " + sub);
            } else {
                list.add(o);
            }
        }
        return list;
    }
    L.debug(val + " is not a primitive: " + clazz + ". Searching it in the environment...");
    final Object o = env.get(val);
    if (o != null && clazz.isInstance(o)) {
        return o;
    }
    if (Time.class.isAssignableFrom(clazz)) {
        return new DoubleTime(Double.parseDouble(val));
    }
    if (clazz.isAssignableFrom(String.class)) {
        L.debug("String detected! Passing " + val + " back.");
        return val;
    }
    L.debug(val + " not found or class not compatible, unable to go further.");
    return null;
}

From source file:javadz.beanutils.LazyDynaBean.java

/**
 * Create a new Instance of a 'Primitive' Property.
 * @param name The name of the property/* w w w .  j a  v a 2  s.co  m*/
 * @param type The class of the property
 * @return The new value
 */
protected Object createPrimitiveProperty(String name, Class type) {

    if (type == Boolean.TYPE) {
        return Boolean.FALSE;
    } else if (type == Integer.TYPE) {
        return Integer_ZERO;
    } else if (type == Long.TYPE) {
        return Long_ZERO;
    } else if (type == Double.TYPE) {
        return Double_ZERO;
    } else if (type == Float.TYPE) {
        return Float_ZERO;
    } else if (type == Byte.TYPE) {
        return Byte_ZERO;
    } else if (type == Short.TYPE) {
        return Short_ZERO;
    } else if (type == Character.TYPE) {
        return Character_SPACE;
    } else {
        return null;
    }

}