Example usage for java.lang Byte TYPE

List of usage examples for java.lang Byte TYPE

Introduction

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

Prototype

Class TYPE

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

Click Source Link

Document

The Class instance representing the primitive type byte .

Usage

From source file:com.ms.commons.summer.web.util.json.JsonNumberMorpher.java

public Object morph(Object value) {
    if (value != null && type.isAssignableFrom(value.getClass())) {
        // no conversion needed
        return value;
    }//from  w w  w.j  av  a 2  s. com

    String str = String.valueOf(value).trim();

    if (!type.isPrimitive() && (value == null || str.length() == 0 || "null".equalsIgnoreCase(str))) {
        // if empty string and class != primitive treat it like null
        return null;
    }

    try {
        if (isDecimalNumber(type)) {
            if (Float.class.isAssignableFrom(type) || Float.TYPE == type) {
                return morphToFloat(str);
            } else if (Double.class.isAssignableFrom(type) || Double.TYPE == type) {
                return morphToDouble(str);
            } else {
                return morphToBigDecimal(str);
            }
        } else {
            if (Byte.class.isAssignableFrom(type) || Byte.TYPE == type) {
                return morphToByte(str);
            } else if (Short.class.isAssignableFrom(type) || Short.TYPE == type) {
                return morphToShort(str);
            } else if (Integer.class.isAssignableFrom(type) || Integer.TYPE == type) {
                return morphToInteger(str);
            } else if (Long.class.isAssignableFrom(type) || Long.TYPE == type) {
                return morphToLong(str);
            } else {
                return morphToBigInteger(str);
            }
        }
    } catch (ConvertErrorException e) {
        // JsonPropertyConvertContext.setConvertError((ConvertErrorException) e);
        return null;
    }
}

From source file:com.exadel.flamingo.flex.messaging.amf.io.AMF3Serializer.java

public void writeObject(Object o) throws IOException {
    if (debugMore)
        debug("writeObject(o=", o, ")");

    if (o == null)
        write(AMF3_NULL);//from w w  w.j a  v  a  2s  . co m
    else if (!(o instanceof Externalizable)) {

        o = converter.convertForSerialization(o);

        if (o instanceof String || o instanceof Character)
            writeAMF3String(o.toString());
        else if (o instanceof Boolean)
            write(((Boolean) o).booleanValue() ? AMF3_BOOLEAN_TRUE : AMF3_BOOLEAN_FALSE);
        else if (o instanceof Number) {
            if (o instanceof Integer || o instanceof Short || o instanceof Byte)
                writeAMF3Integer(((Number) o).intValue());
            else
                writeAMF3Number(((Number) o).doubleValue());
        } else if (o instanceof Date)
            writeAMF3Date((Date) o);
        else if (o instanceof Calendar)
            writeAMF3Date(((Calendar) o).getTime());
        else if (o instanceof Document)
            writeAMF3Xml((Document) o);
        else if (o instanceof Collection)
            writeAMF3Collection((Collection<?>) o);
        else if (o.getClass().isArray()) {
            if (o.getClass().getComponentType() == Byte.TYPE)
                writeAMF3ByteArray((byte[]) o);
            else
                writeAMF3Array(o);
        } else
            writeAMF3Object(o);
    } else
        writeAMF3Object(o);
}

From source file:jef.tools.ArrayUtils.java

/**
 * ?//from  w  w  w  .j av a2  s . co  m
 * 
 * @param obj
 * @return
 */
public static Object[] toObject(Object obj) {
    Class<?> c = obj.getClass();
    Assert.isTrue(c.isArray());
    Class<?> priType = c.getComponentType();
    if (!priType.isPrimitive())
        return (Object[]) obj;
    if (priType == Boolean.TYPE) {
        return toObject((boolean[]) obj);
    } else if (priType == Byte.TYPE) {
        return toObject((byte[]) obj);
    } else if (priType == Character.TYPE) {
        return toObject((char[]) obj);
    } else if (priType == Integer.TYPE) {
        return toObject((int[]) obj);
    } else if (priType == Long.TYPE) {
        return toObject((long[]) obj);
    } else if (priType == Float.TYPE) {
        return toObject((float[]) obj);
    } else if (priType == Double.TYPE) {
        return toObject((double[]) obj);
    } else if (priType == Short.TYPE) {
        return toObject((short[]) obj);
    }
    throw new IllegalArgumentException();
}

From source file:candr.yoclip.option.OptionSetter.java

@Override
public void setOption(final T bean, final String value) {

    final Class<?> setterParameterType = getType();
    try {/*from  w  ww .ja  v a  2s . com*/

        if (Boolean.TYPE.equals(setterParameterType) || Boolean.class.isAssignableFrom(setterParameterType)) {
            setBooleanOption(bean, value);

        } else if (Byte.TYPE.equals(setterParameterType) || Byte.class.isAssignableFrom(setterParameterType)) {
            setByteOption(bean, value);

        } else if (Short.TYPE.equals(setterParameterType)
                || Short.class.isAssignableFrom(setterParameterType)) {
            setShortOption(bean, value);

        } else if (Integer.TYPE.equals(setterParameterType)
                || Integer.class.isAssignableFrom(setterParameterType)) {
            setIntegerOption(bean, value);

        } else if (Long.TYPE.equals(setterParameterType) || Long.class.isAssignableFrom(setterParameterType)) {
            setLongOption(bean, value);

        } else if (Character.TYPE.equals(setterParameterType)
                || Character.class.isAssignableFrom(setterParameterType)) {
            setCharacterOption(bean, value);

        } else if (Float.TYPE.equals(setterParameterType)
                || Float.class.isAssignableFrom(setterParameterType)) {
            setFloatOption(bean, value);

        } else if (Double.TYPE.equals(setterParameterType)
                || Double.class.isAssignableFrom(setterParameterType)) {
            setDoubleOption(bean, value);

        } else if (String.class.isAssignableFrom(setterParameterType)) {
            setStringOption(bean, value);

        } else {
            final String supportedTypes = "boolean, byte, short, int, long, char, float, double, and String";
            throw new OptionsParseException(
                    String.format("OptionParameter only supports %s types", supportedTypes));
        }

    } catch (NumberFormatException e) {
        final String error = String.format("Error converting '%s' to %s", value, setterParameterType);
        throw new OptionsParseException(error, e);
    }
}

From source file:org.evosuite.regression.ObjectFields.java

private static Object getFieldValue(Field field, Object p) {
    try {//from ww w.  j  a  va 2s.com
        /*Class objClass = p.getClass();
        if(p instanceof java.lang.String){
           ((String) p).hashCode();
        }*/
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (fieldType.isPrimitive()) {
            if (fieldType.equals(Boolean.TYPE)) {
                return field.getBoolean(p);
            }
            if (fieldType.equals(Integer.TYPE)) {
                return field.getInt(p);
            }
            if (fieldType.equals(Byte.TYPE)) {
                return field.getByte(p);
            }
            if (fieldType.equals(Short.TYPE)) {
                return field.getShort(p);
            }
            if (fieldType.equals(Long.TYPE)) {
                return field.getLong(p);
            }
            if (fieldType.equals(Double.TYPE)) {
                return field.getDouble(p);
            }
            if (fieldType.equals(Float.TYPE)) {
                return field.getFloat(p);
            }
            if (fieldType.equals(Character.TYPE)) {
                return field.getChar(p);
            }
            throw new UnsupportedOperationException("Primitive type " + fieldType + " not implemented!");
        }
        return field.get(p);
    } catch (IllegalAccessException exc) {
        throw new RuntimeException(exc);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (MAX_RECURSION != 0)
            MAX_RECURSION = 0;
        else
            throw new RuntimeErrorException(e);
        return getFieldValue(field, p);
    }
}

From source file:candr.yoclip.option.OptionField.java

@Override
public void setOption(final T bean, final String value) {

    final Class<?> fieldType = getField().getType();
    try {//from  ww w. j  a  v  a  2  s  . c  o m

        if (Boolean.TYPE.equals(fieldType) || Boolean.class.isAssignableFrom(fieldType)) {
            setBooleanOption(bean, value);

        } else if (Byte.TYPE.equals(fieldType) || Byte.class.isAssignableFrom(fieldType)) {
            setByteOption(bean, value);

        } else if (Short.TYPE.equals(fieldType) || Short.class.isAssignableFrom(fieldType)) {
            setShortOption(bean, value);

        } else if (Integer.TYPE.equals(fieldType) || Integer.class.isAssignableFrom(fieldType)) {
            setIntegerOption(bean, value);

        } else if (Long.TYPE.equals(fieldType) || Long.class.isAssignableFrom(fieldType)) {
            setLongOption(bean, value);

        } else if (Character.TYPE.equals(fieldType) || Character.class.isAssignableFrom(fieldType)) {
            setCharacterOption(bean, value);

        } else if (Float.TYPE.equals(fieldType) || Float.class.isAssignableFrom(fieldType)) {
            setFloatOption(bean, value);

        } else if (Double.TYPE.equals(fieldType) || Double.class.isAssignableFrom(fieldType)) {
            setDoubleOption(bean, value);

        } else if (String.class.isAssignableFrom(fieldType)) {
            setStringOption(bean, value);

        } else {
            final String supportedTypes = "boolean, byte, short, int, long, char, float, double, and String";
            throw new OptionsParseException(
                    String.format("OldOptionsParser only support %s types", supportedTypes));
        }

    } catch (NumberFormatException e) {
        final String error = String.format("Error converting '%s' to %s", value, fieldType);
        throw new OptionsParseException(error, e);
    }
}

From source file:com.nonninz.robomodel.RoboModel.java

private void loadField(Field field, Cursor query) throws DatabaseNotUpToDateException {
    final Class<?> type = field.getType();
    final boolean wasAccessible = field.isAccessible();
    final int columnIndex = query.getColumnIndex(field.getName());
    field.setAccessible(true);// ww w.  j av  a 2  s  . c  o  m

    /*
     * TODO: There is the potential of a problem here:
     * What happens if the developer changes the type of a field between releases?
     *
     * If he saves first, then the column type will be changed (In the future).
     * If he loads first, we don't know if an Exception will be thrown if the
     * types are incompatible, because it's undocumented in the Cursor documentation.
     */

    try {
        if (type == String.class) {
            field.set(this, query.getString(columnIndex));
        } else if (type == Boolean.TYPE) {
            final boolean value = query.getInt(columnIndex) == 1 ? true : false;
            field.setBoolean(this, value);
        } else if (type == Byte.TYPE) {
            field.setByte(this, (byte) query.getShort(columnIndex));
        } else if (type == Double.TYPE) {
            field.setDouble(this, query.getDouble(columnIndex));
        } else if (type == Float.TYPE) {
            field.setFloat(this, query.getFloat(columnIndex));
        } else if (type == Integer.TYPE) {
            field.setInt(this, query.getInt(columnIndex));
        } else if (type == Long.TYPE) {
            field.setLong(this, query.getLong(columnIndex));
        } else if (type == Short.TYPE) {
            field.setShort(this, query.getShort(columnIndex));
        } else if (type.isEnum()) {
            final String string = query.getString(columnIndex);
            if (string != null && string.length() > 0) {
                final Object[] constants = type.getEnumConstants();
                final Method method = type.getMethod("valueOf", Class.class, String.class);
                final Object value = method.invoke(constants[0], type, string);
                field.set(this, value);
            }
        } else {
            // Try to de-json it (db column must be of type text)
            try {
                final Object value = mMapper.readValue(query.getString(columnIndex), field.getType());
                field.set(this, value);
            } catch (final Exception e) {
                final String msg = String.format("Type %s is not supported for field %s", type,
                        field.getName());
                Ln.w(e, msg);
                throw new IllegalArgumentException(msg);
            }
        }
    } catch (final IllegalAccessException e) {
        final String msg = String.format("Field %s is not accessible", type, field.getName());
        throw new IllegalArgumentException(msg);
    } catch (final NoSuchMethodException e) {
        // Should not happen
        throw new RuntimeException(e);
    } catch (final InvocationTargetException e) {
        // Should not happen
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        // This is when there is no column in db, but there is in the model
        throw new DatabaseNotUpToDateException(e);
    } finally {
        field.setAccessible(wasAccessible);
    }
}

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

@PostConstruct
public void initialize() {
    classes = new ArrayList<Class>();
    classes.add(String.class);
    classes.add(Boolean.class);
    classes.add(Boolean.TYPE);// ww  w.java2  s  .  c  o  m
    classes.add(Long.class);
    classes.add(Long.TYPE);
    classes.add(Integer.class);
    classes.add(Integer.TYPE);
    classes.add(Float.class);
    classes.add(Float.TYPE);
    classes.add(Double.TYPE);
    classes.add(Double.class);
    classes.add(Character.TYPE);
    classes.add(Character.TYPE);
    classes.add(Character.class);
    classes.add(Byte.TYPE);
    classes.add(Byte.class);
    classes.add(Enum.class);
    classes.add(BigInteger.class);
    classes.add(BigDecimal.class);
    Collection<PrimitiveTypeRegistrar> registrars = beanLocator.getInstances(PrimitiveTypeRegistrar.class);
    for (PrimitiveTypeRegistrar registrar : registrars) {
        PrimitiveType<?>[] types = registrar.getTypes();
        for (PrimitiveType<?> primitiveType : types) {
            classToType.put(primitiveType.getJavaType(), primitiveType);
            classes.add(primitiveType.getJavaType());
        }
    }
}

From source file:com.medallia.spider.api.DynamicInputImpl.java

/**
 * Method used to parse values for the methods declared in {@link Input}.
 *///  w  ww. ja v  a 2  s  .co m
public <X> X getInput(String name, Class<X> type, AnnotatedElement anno) {
    // Special case for file uploads
    if (type.isAssignableFrom(UploadedFile.class))
        return type.cast(fileUploads.get(name));

    // Also support just getting the bytes from a file without the name
    if (type.isArray() && type.getComponentType() == Byte.TYPE) {
        UploadedFile uploadedFile = fileUploads.get(name);
        return uploadedFile != null ? type.cast(uploadedFile.getBytes()) : null;
    }

    if (type.isArray() && anno.isAnnotationPresent(Input.MultiValued.class)) {
        // return type is an array; grab all
        Object o = inputParams.get(name);
        return type.cast(parseMultiValue(type, o, anno));
    }

    String v = Strings.extract(inputParams.get(name));

    // boolean is used for checkboxes, and false is encoded as a missing value
    if (type == Boolean.class || type == Boolean.TYPE) {
        @SuppressWarnings("unchecked")
        X x = (X) Boolean.valueOf(v != null && !"false".equalsIgnoreCase(v));
        return x;
    }

    // the remaining types have proper null values
    if (v == null)
        return null;

    return cast(parseSingleValue(type, v, anno, inputArgParsers));
}

From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java

@SuppressWarnings("squid:S3776")
private static Object convertPrimitiveValue(String value, Class<?> type) throws ParseException {
    if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
        return value.toLowerCase().trim().equals("true");
    } else {//from ww w .  j  a va  2 s.c om
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        Number num = numberFormat.parse(value);
        if (type.equals(Byte.class) || type.equals(Byte.TYPE)) {
            return num.byteValue();
        } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
            return num.doubleValue();
        } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
            return num.floatValue();
        } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
            return num.intValue();
        } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
            return num.longValue();
        } else if (type.equals(Short.class) || type.equals(Short.TYPE)) {
            return num.shortValue();
        } else {
            return null;
        }
    }
}