Example usage for java.lang Class isPrimitive

List of usage examples for java.lang Class isPrimitive

Introduction

In this page you can find the example usage for java.lang Class isPrimitive.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isPrimitive();

Source Link

Document

Determines if the specified Class object represents a primitive type.

Usage

From source file:com.jsen.core.reflect.ClassFunction.java

/**
 * Tests whether are passed arguments assignable to the given types.
 * /*from w w  w.j  av  a2 s  . co m*/
 * @param args Arguments to be tested.
 * @param types Types.
 * @return True if are passed arguments assignable to the given types.
 */
public static boolean isAssignableTypes(Object[] args, Class<?>... types) {
    if (args.length != types.length) {
        return false;
    }

    for (int i = 0; i < args.length; i++) {
        Class<?> argClass = (args[i] == null) ? null : args[i].getClass();
        Class<?> paramClass = types[i];

        if (paramClass.isPrimitive() && argClass == null) {
            return false;
        } else if (!paramClass.isPrimitive() && argClass == null) {
            continue;
        } else if (!ClassUtils.isAssignable(argClass, paramClass, true)) {
            return false;
        }
    }

    return true;
}

From source file:foundation.stack.datamill.configuration.impl.Classes.java

public static Class<?> primitiveToWrapper(final Class<?> clazz) {
    Class<?> convertedClass = clazz;
    if (clazz != null && clazz.isPrimitive()) {
        convertedClass = primitiveWrapperMap.get(clazz);
    }//w  w w.ja  v a 2  s.c  o m

    return convertedClass;
}

From source file:Main.java

/**
 * Creates a class identifier of form Labc/abc;, from a Class.
 *//*from   w  w w  .  j  a v a 2s. c  om*/
public static String ci(Class n) {
    if (n.isArray()) {
        n = n.getComponentType();
        if (n.isPrimitive()) {
            if (n == Byte.TYPE) {
                return "[B";
            } else if (n == Boolean.TYPE) {
                return "[Z";
            } else if (n == Short.TYPE) {
                return "[S";
            } else if (n == Character.TYPE) {
                return "[C";
            } else if (n == Integer.TYPE) {
                return "[I";
            } else if (n == Float.TYPE) {
                return "[F";
            } else if (n == Double.TYPE) {
                return "[D";
            } else if (n == Long.TYPE) {
                return "[J";
            } else {
                throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
            }
        } else {
            return "[" + ci(n);
        }
    } else {
        if (n.isPrimitive()) {
            if (n == Byte.TYPE) {
                return "B";
            } else if (n == Boolean.TYPE) {
                return "Z";
            } else if (n == Short.TYPE) {
                return "S";
            } else if (n == Character.TYPE) {
                return "C";
            } else if (n == Integer.TYPE) {
                return "I";
            } else if (n == Float.TYPE) {
                return "F";
            } else if (n == Double.TYPE) {
                return "D";
            } else if (n == Long.TYPE) {
                return "J";
            } else if (n == Void.TYPE) {
                return "V";
            } else {
                throw new RuntimeException("Unrecognized type in compiler: " + n.getName());
            }
        } else {
            return "L" + p(n) + ";";
        }
    }
}

From source file:com.google.code.siren4j.util.ComponentUtils.java

public static boolean isNumeric(Object obj) {
    Class<?> clazz = obj.getClass();
    boolean numeric = false;
    if (clazz.isPrimitive()) {
        numeric = clazz.equals(byte.class) || clazz.equals(short.class) || clazz.equals(int.class)
                || clazz.equals(long.class) || clazz.equals(float.class) || clazz.equals(double.class);

    } else {//from  w  w w  . ja v  a2s. c o  m
        numeric = obj instanceof Number;
    }
    return numeric;
}

From source file:com.junly.common.util.ReflectUtils.java

/**
 * <p class="detail">//from  ww w.ja  v a  2s .  co m
 * 
 * </p>
 * @author wan.Dong
 * @date 20161112 
 * @param obj 
 * @param name    ??
 * @param value  (?)??false
 * @return
 */
public static boolean setProperty(Object obj, String name, Object value) {
    if (obj != null) {
        Class<?> clazz = obj.getClass();
        while (clazz != null) {
            Field field = null;
            try {
                field = clazz.getDeclaredField(name);
            } catch (Exception e) {
                clazz = clazz.getSuperclass();
                continue;
            }
            try {
                Class<?> type = field.getType();
                if (type.isPrimitive() == true && value != null) {
                    if (value instanceof String) {
                        if (type.equals(int.class) == true) {
                            value = Integer.parseInt((String) value);
                        } else if (type.equals(double.class) == true) {
                            value = Double.parseDouble((String) value);
                        } else if (type.equals(boolean.class) == true) {
                            value = Boolean.parseBoolean((String) value);
                        } else if (type.equals(long.class) == true) {
                            value = Long.parseLong((String) value);
                        } else if (type.equals(byte.class) == true) {
                            value = Byte.parseByte((String) value);
                        } else if (type.equals(char.class) == true) {
                            value = Character.valueOf(((String) value).charAt(0));
                        } else if (type.equals(float.class) == true) {
                            value = Float.parseFloat((String) value);
                        } else if (type.equals(short.class) == true) {
                            value = Short.parseShort((String) value);
                        }
                    }
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                if (value == null || type.equals(value.getClass()) == true) {
                    field.setAccessible(true);
                    field.set(obj, value);
                    field.setAccessible(false);
                }
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }
    return false;
}

From source file:me.hurel.hqlbuilder.internal.ProxyUtil.java

public static Constructor<?> findBestConstructor(Constructor<?>[] constructors) {
    // First try : find a constructor with only primitive arguments
    for (Constructor<?> c : constructors) {
        Class<?>[] parameterTypes = c.getParameterTypes();
        if (parameterTypes.length == 0) {
            return c;
        } else {/*from www. j a va  2 s  .c  om*/
            boolean useConstructor = true;
            for (Class<?> parameterType : parameterTypes) {
                if (!parameterType.isPrimitive()) {
                    useConstructor = false;
                    break;
                }
            }
            if (useConstructor) {
                return c;
            }
        }
    }
    // Second try : Okay, let's give a chance to String parameters too
    for (Constructor<?> c : constructors) {
        Class<?>[] parameterTypes = c.getParameterTypes();
        if (parameterTypes.length == 0) {
            return c;
        } else {
            boolean useConstructor = true;
            for (Class<?> parameterType : parameterTypes) {
                if (!parameterType.isPrimitive() && !parameterType.equals(String.class)) {
                    useConstructor = false;
                    break;
                }
            }
            if (useConstructor) {
                return c;
            }
        }
    }
    return null;
}

From source file:Main.java

public static String canBeABeanType(Class<?> paramClass) {
    if (paramClass.isAnnotation())
        return "annotation";
    if (paramClass.isArray())
        return "array";
    if (paramClass.isEnum())
        return "enum";
    if (paramClass.isPrimitive())
        return "primitive";
    return null;/*from  w  w  w.  j av  a2 s . com*/
}

From source file:Main.java

private static boolean isBaseType(Class<?> o) {

    if (o == null)
        return true;

    String className = o.getName();
    if (className == null)
        return false;
    if (o.isPrimitive())
        return true;
    if (o == Byte.class)
        return true;
    if (o == Character.class)
        return true;
    if (o == Short.class)
        return true;
    if (o == Integer.class)
        return true;
    if (o == Long.class)
        return true;
    if (o == Float.class)
        return true;
    if (o == Double.class)
        return true;
    if (o == Boolean.class)
        return true;
    if (o == String.class)
        return true;
    if (o == StringBuffer.class)
        return true;
    if (o == StringBuilder.class)
        return true;
    if (o == BigInteger.class)
        return true;
    if (o == BigDecimal.class)
        return true;
    return false;
}

From source file:TypeUtil.java

/**
 * Converts primitive types to their wrapper counterparts. 
 * @param type the class type to convert
 * @return the wrapper equivalent for the primitive type, or if <tt>type</tt>
 *   was not a primitive, its returned as is
 *//*from   ww w.j av  a 2s. c  om*/
public static Class<?> toWrapperClass(Class<?> type) {
    if (!type.isPrimitive())
        return type;
    else if (int.class.equals(type))
        return Integer.class;
    else if (double.class.equals(type))
        return Double.class;
    else if (char.class.equals(type))
        return Character.class;
    else if (boolean.class.equals(type))
        return Boolean.class;
    else if (long.class.equals(type))
        return Long.class;
    else if (float.class.equals(type))
        return Float.class;
    else if (short.class.equals(type))
        return Short.class;
    else if (byte.class.equals(type))
        return Byte.class;
    else
        throw new IllegalArgumentException("Primitive type not supported: " + type.getName());
}

From source file:nl.strohalm.cyclos.utils.ClassHelper.java

/**
 * Returns if the given object is an instance of the specified class, handling primitives as objects
 *//*from w  w  w. ja  va 2  s.c o m*/
public static boolean isInstance(Class<?> clazz, final Object object) {
    if (clazz.isPrimitive()) {
        clazz = ClassUtils.primitiveToWrapper(clazz);
    }
    return clazz.isInstance(object);
}