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:Classes.java

/**
 * This method acts equivalently to invoking classLoader.loadClass(className)
 * but it also supports primitive types and array classes of object types or
 * primitive types./*  ww  w.  ja  va2  s . co  m*/
 * 
 * @param className
 *          the qualified name of the class or the name of primitive type or
 *          array in the same format as returned by the
 *          java.lang.Class.getName() method.
 * @param classLoader
 *          the ClassLoader used to load classes
 * @return the Class object for the requested className
 * 
 * @throws ClassNotFoundException
 *           when the <code>classLoader</code> can not find the requested
 *           class
 */
public static Class loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
    // ClassLoader.loadClass() does not handle primitive types:
    //
    // B byte
    // C char
    // D double
    // F float
    // I int
    // J long
    // S short
    // Z boolean
    // V void
    //
    if (className.length() == 1) {
        char type = className.charAt(0);
        if (type == 'B')
            return Byte.TYPE;
        if (type == 'C')
            return Character.TYPE;
        if (type == 'D')
            return Double.TYPE;
        if (type == 'F')
            return Float.TYPE;
        if (type == 'I')
            return Integer.TYPE;
        if (type == 'J')
            return Long.TYPE;
        if (type == 'S')
            return Short.TYPE;
        if (type == 'Z')
            return Boolean.TYPE;
        if (type == 'V')
            return Void.TYPE;
        // else throw...
        throw new ClassNotFoundException(className);
    }

    // Check for a primative type
    if (isPrimitive(className) == true)
        return (Class) Classes.PRIMITIVE_NAME_TYPE_MAP.get(className);

    // Check for the internal vm format: Lclassname;
    if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';')
        return classLoader.loadClass(className.substring(1, className.length() - 1));

    // first try - be optimistic
    // this will succeed for all non-array classes and array classes that have
    // already been resolved
    //
    try {
        return classLoader.loadClass(className);
    } catch (ClassNotFoundException e) {
        // if it was non-array class then throw it
        if (className.charAt(0) != '[')
            throw e;
    }

    // we are now resolving array class for the first time

    // count opening braces
    int arrayDimension = 0;
    while (className.charAt(arrayDimension) == '[')
        arrayDimension++;

    // resolve component type - use recursion so that we can resolve primitive
    // types also
    Class componentType = loadClass(className.substring(arrayDimension), classLoader);

    // construct array class
    return Array.newInstance(componentType, new int[arrayDimension]).getClass();
}

From source file:org.acmsl.commons.utils.ConversionUtils.java

/**
 * Converts given String to byte, if given value is not null.
 * @param value the value to convert./*from   ww w  .  j  av a 2  s .c  o  m*/
 * @return the converted value.
 */
@Nullable
public Byte toByteIfNotNull(@Nullable final String value) {
    Byte result = null;

    @Nullable
    final Converter t_Converter = ConvertUtils.lookup(Byte.TYPE);

    if (t_Converter != null) {
        @Nullable
        final Object t_Result = t_Converter.convert(Byte.TYPE, value);

        if (t_Result instanceof Byte) {
            result = (Byte) t_Result;
        }
    }

    return result;
}

From source file:com.phoenixnap.oss.ramlapisync.naming.SchemaHelper.java

/**
 * Maps primitives and other simple Java types into simple types supported by RAML
 * //from  w  w  w  .jav a 2s. co  m
 * @param clazz The Class to map
 * @return The Simple RAML ParamType which maps to this class or null if one is not found
 */
public static ParamType mapSimpleType(Class<?> clazz) {
    Class<?> targetClazz = clazz;
    if (targetClazz.isArray() && clazz.getComponentType() != null) {
        targetClazz = clazz.getComponentType();
    }
    if (targetClazz.equals(Long.TYPE) || targetClazz.equals(Long.class) || targetClazz.equals(Integer.TYPE)
            || targetClazz.equals(Integer.class) || targetClazz.equals(Short.TYPE)
            || targetClazz.equals(Short.class) || targetClazz.equals(Byte.TYPE)
            || targetClazz.equals(Byte.class)) {
        return ParamType.INTEGER;
    } else if (targetClazz.equals(Float.TYPE) || targetClazz.equals(Float.class)
            || targetClazz.equals(Double.TYPE) || targetClazz.equals(Double.class)
            || targetClazz.equals(BigDecimal.class)) {
        return ParamType.NUMBER;
    } else if (targetClazz.equals(Boolean.class) || targetClazz.equals(Boolean.TYPE)) {
        return ParamType.BOOLEAN;
    } else if (targetClazz.equals(String.class)) {
        return ParamType.STRING;
    }
    return null; // default to string
}

From source file:org.brekka.stillingar.spring.bpp.ConfigurationBeanPostProcessorTest.java

@Test
public void primitiveDefaultByte() {
    assertSame(ConfigurationBeanPostProcessor.primitiveDefault(Byte.TYPE), Byte.valueOf((byte) 0));
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * Helper method for generating a hash code for an array.
 *
 * @param componentType the component type of the array
 * @param o the array/*from   w  w w  . jav a 2s .  c  o  m*/
 * @return a hash code for the specified array
 */
private static int arrayMemberHash(Class<?> componentType, Object o) {
    if (componentType.equals(Byte.TYPE)) {
        return Arrays.hashCode((byte[]) o);
    }
    if (componentType.equals(Short.TYPE)) {
        return Arrays.hashCode((short[]) o);
    }
    if (componentType.equals(Integer.TYPE)) {
        return Arrays.hashCode((int[]) o);
    }
    if (componentType.equals(Character.TYPE)) {
        return Arrays.hashCode((char[]) o);
    }
    if (componentType.equals(Long.TYPE)) {
        return Arrays.hashCode((long[]) o);
    }
    if (componentType.equals(Float.TYPE)) {
        return Arrays.hashCode((float[]) o);
    }
    if (componentType.equals(Double.TYPE)) {
        return Arrays.hashCode((double[]) o);
    }
    if (componentType.equals(Boolean.TYPE)) {
        return Arrays.hashCode((boolean[]) o);
    }
    return Arrays.hashCode((Object[]) o);
}

From source file:android.reflect.ClazzLoader.java

/**
 * ????//from w w  w.j  a  va 2  s  .c o  m
 */
public static <O, V> void setFieldValue(O o, Field field, V v) {
    if (field != null) {
        try {
            field.setAccessible(true);

            if (v == null) {
                field.set(o, null);
            } else {
                Class<?> vType = v.getClass();
                if (vType == Integer.TYPE) {
                    field.setInt(o, (Integer) v);
                } else if (vType == Long.TYPE) {
                    field.setLong(o, (Long) v);
                } else if (vType == Boolean.TYPE) {
                    field.setBoolean(o, (Boolean) v);
                } else if (vType == Float.TYPE) {
                    field.setFloat(o, (Float) v);
                } else if (vType == Short.TYPE) {
                    field.setShort(o, (Short) v);
                } else if (vType == Byte.TYPE) {
                    field.setByte(o, (Byte) v);
                } else if (vType == Double.TYPE) {
                    field.setDouble(o, (Double) v);
                } else if (vType == Character.TYPE) {
                    field.setChar(o, (Character) v);
                } else {
                    field.set(o, v);
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, t);
        }
    }
}

From source file:net.sf.json.JSONDynaBean.java

/**
 * DOCUMENT ME!/*from w  w  w .  j  a v a  2 s  .  c  om*/
 *
 * @param clazz DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
private boolean isByte(Class clazz) {
    return Byte.class.isAssignableFrom(clazz) || (clazz == Byte.TYPE);
}

From source file:com.examples.with.different.packagename.testcarver.AbstractConverter.java

/**
 * Change primitve Class types to the associated wrapper class.
 * @param type The class type to check./*from w w w  . j  ava 2s.c om*/
 * @return The converted type.
 */
Class primitive(Class type) {
    if (type == null || !type.isPrimitive()) {
        return type;
    }

    if (type == Integer.TYPE) {
        return Integer.class;
    } else if (type == Double.TYPE) {
        return Double.class;
    } else if (type == Long.TYPE) {
        return Long.class;
    } else if (type == Boolean.TYPE) {
        return Boolean.class;
    } else if (type == Float.TYPE) {
        return Float.class;
    } else if (type == Short.TYPE) {
        return Short.class;
    } else if (type == Byte.TYPE) {
        return Byte.class;
    } else if (type == Character.TYPE) {
        return Character.class;
    } else {
        return type;
    }
}

From source file:org.cloudata.core.common.io.CObjectWritable.java

/** Read a {@link CWritable}, {@link String}, primitive type, or an array of
 * the preceding. *///ww  w  . ja va 2  s  . co  m
@SuppressWarnings("unchecked")
public static Object readObject(DataInput in, CObjectWritable objectWritable, CloudataConf conf,
        boolean arrayComponent, Class componentClass) throws IOException {
    String className;
    if (arrayComponent) {
        className = componentClass.getName();
    } else {
        className = CUTF8.readString(in);
        //SANGCHUL
        //   System.out.println("SANGCHUL] className:" + className);
    }

    Class<?> declaredClass = PRIMITIVE_NAMES.get(className);
    if (declaredClass == null) {
        try {
            declaredClass = conf.getClassByName(className);
        } catch (ClassNotFoundException e) {
            //SANGCHUL
            e.printStackTrace();
            throw new RuntimeException("readObject can't find class[className=" + className + "]", e);
        }
    }

    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
        //System.out.println("SANGCHUL] is array");
        int length = in.readInt();
        //System.out.println("SANGCHUL] array length : " + length);
        //System.out.println("Read:in.readInt():" + length);
        if (declaredClass.getComponentType() == Byte.TYPE) {
            byte[] bytes = new byte[length];
            in.readFully(bytes);
            instance = bytes;
        } else if (declaredClass.getComponentType() == ColumnValue.class) {
            instance = readColumnValue(in, conf, declaredClass, length);
        } else {
            Class componentType = declaredClass.getComponentType();

            // SANGCHUL
            //System.out.println("SANGCHUL] componentType : " + componentType.getName());

            instance = Array.newInstance(componentType, length);
            for (int i = 0; i < length; i++) {
                Object arrayComponentInstance = readObject(in, null, conf, !componentType.isArray(),
                        componentType);
                Array.set(instance, i, arrayComponentInstance);
                //Array.set(instance, i, readObject(in, conf));
            }
        }
    } else if (declaredClass == String.class) { // String
        instance = CUTF8.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, CUTF8.readString(in));
    } else if (declaredClass == ColumnValue.class) {
        //ColumnValue?  ?? ?? ?  ? ?.
        //? ?   ? ? ? ? . 
        Class instanceClass = null;
        try {
            short typeDiff = in.readShort();
            if (typeDiff == TYPE_DIFF) {
                instanceClass = conf.getClassByName(CUTF8.readString(in));
            } else {
                instanceClass = declaredClass;
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("readObject can't find class", e);
        }
        ColumnValue columnValue = new ColumnValue();
        columnValue.readFields(in);
        instance = columnValue;
    } else { // Writable

        Class instanceClass = null;
        try {
            short typeDiff = in.readShort();
            // SANGCHUL
            //System.out.println("SANGCHUL] typeDiff : " + typeDiff);
            //System.out.println("Read:in.readShort():" + typeDiff);
            if (typeDiff == TYPE_DIFF) {
                // SANGCHUL
                String classNameTemp = CUTF8.readString(in);
                //System.out.println("SANGCHUL] typeDiff : " + classNameTemp);
                instanceClass = conf.getClassByName(classNameTemp);
                //System.out.println("Read:UTF8.readString(in):" + instanceClass.getClass());
            } else {
                instanceClass = declaredClass;
            }
        } catch (ClassNotFoundException e) {

            // SANGCHUL
            e.printStackTrace();
            throw new RuntimeException("readObject can't find class", e);
        }

        CWritable writable = CWritableFactories.newInstance(instanceClass, conf);
        writable.readFields(in);
        //System.out.println("Read:writable.readFields(in)");
        instance = writable;

        if (instanceClass == NullInstance.class) { // null
            declaredClass = ((NullInstance) instance).declaredClass;
            instance = null;
        }
    }

    if (objectWritable != null) { // store values
        objectWritable.declaredClass = declaredClass;
        objectWritable.instance = instance;
    }

    return instance;
}

From source file:com.gdcn.modules.db.jdbc.processor.CamelBeanProcessor.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.// w  w  w .j a va2s  .co  m
 *
 * @param value The value to be passed into the setter method.
 * @param type The setter's parameter type (non-null)
 * @return boolean True if the value is compatible (null => true)
 */
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;

    }
    return false;

}