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

/**
 * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
 * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
 *
 * @param type//  w w w.  j a v a 2  s.com
 *            The class to query or null.
 * @return true if the given {@code type} is a primitive or primitive wrapper ({@link Boolean}, {@link Byte}, {@link Character},
 *         {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
 * @since 3.1
 */
public static boolean isPrimitiveOrWrapper(final Class<?> type) {
    if (type == null) {
        return false;
    }
    return type.isPrimitive() || isPrimitiveWrapper(type);
}

From source file:Main.java

/**
 * Takes a class type and constructs it. If the class does not have an empty constructor this
 * will// w w  w . j  a va 2  s. co m
 * find the parametrized constructor and use nulls and default values to construct the class.
 *
 * @param clazz The class to construct.
 * @param <T> The type of the class to construct.
 * @return The constructed object.
 */
public static <T> T createInstance(Class<T> clazz) {
    T created = null;
    Constructor<?>[] constructors = clazz.getConstructors();
    for (Constructor<?> constructor : constructors) {
        if (!Modifier.isPrivate(constructor.getModifiers())) {
            Class<?>[] parameterTypes = constructor.getParameterTypes();
            List<Object> params = new ArrayList<Object>();
            for (Class<?> parameterType : parameterTypes)
                if (!parameterType.isPrimitive()) {
                    params.add(null);
                } else {
                    if (parameterType == boolean.class) {
                        params.add(false);
                    } else {
                        params.add(0);
                    }
                }

            try {
                @SuppressWarnings("unchecked")
                T newObject = (T) constructor.newInstance(params.toArray());
                created = newObject;
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            break;
        }
    }
    return created;
}

From source file:Main.java

/**
 * <p>Converts the specified primitive Class object to its corresponding
 * wrapper Class object.</p>//from w  w w  .  j  av  a2s.  co m
 *
 * <p>NOTE: From v2.2, this method handles {@code Void.TYPE},
 * returning {@code Void.TYPE}.</p>
 *
 * @param cls  the class to convert, may be null
 * @return the wrapper class for {@code cls} or {@code cls} if
 * {@code cls} is not a primitive. {@code null} if null input.
 * @since 2.1
 */
public static Class<?> primitiveToWrapper(final Class<?> cls) {
    Class<?> convertedClass = cls;
    if (cls != null && cls.isPrimitive()) {
        convertedClass = primitiveWrapperMap.get(cls);
    }
    return convertedClass;
}

From source file:Main.java

private static boolean isCompatible(Class<?> type0, Class<?> type1) {
    if (type0.getName().equals(type1.getName())) {
        return true;
    }// w w w . j  a va2  s  . co  m
    if (type0.isPrimitive()) {
        return isCompatible(PRIMITIVE_TO_WRAPPER.get(type0), type1);
    }
    if (type1.isPrimitive()) {
        return isCompatible(type0, PRIMITIVE_TO_WRAPPER.get(type1));
    }
    return type0.isAssignableFrom(type1);
}

From source file:com.mtgi.analytics.aop.BehaviorTrackingAdvice.java

private static final boolean shouldLog(Class<?> type) {
    return (type.isPrimitive()) || type.isEnum() || type.getName().startsWith("java.lang");
}

From source file:Main.java

public static boolean containsIntArray(final Object src, final Object tgt, final Class<?> type)
        throws Exception {
    int value = Integer.parseInt((String) tgt);
    if (type.isPrimitive()) {
        int[] array = (int[]) src;
        for (int val : array) {
            if (val == value)
                return true;
        }//from   www . ja va 2  s.c  o m
    } else {
        Integer[] array = (Integer[]) src;
        for (int val : array) {
            if (val == value)
                return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean containsFloatArray(final Object src, final Object tgt, final Class<?> type)
        throws Exception {
    float value = Float.parseFloat((String) tgt);
    if (type.isPrimitive()) {
        float[] array = (float[]) src;
        for (float val : array) {
            if (val == value)
                return true;
        }//from   www  .j  ava 2  s . c o  m
    } else {
        Float[] array = (Float[]) src;
        for (float val : array) {
            if (val == value)
                return true;
        }
    }
    return false;
}

From source file:Main.java

private static boolean isAssignableFrom(Class<?> parameterType, Object value) {
    if (parameterType.isPrimitive()) {
        if (value == null) {
            return false;
        }/*from w  w w.ja v  a 2s  . c o m*/
        Class<?> valueClass = value.getClass();

        if (parameterType == Boolean.TYPE) {
            return valueClass == Boolean.class;
        } else if (parameterType == Byte.TYPE) {
            return valueClass == Byte.class;
        } else if (parameterType == Character.TYPE) {
            return valueClass == Character.class;
        } else if (parameterType == Short.TYPE) {
            return valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Integer.TYPE) {
            return valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                    || valueClass == Byte.class;
        } else if (parameterType == Long.TYPE) {
            return valueClass == Long.class || valueClass == Integer.class || valueClass == Character.class
                    || valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Float.TYPE) {
            return valueClass == Float.class || valueClass == Long.class || valueClass == Integer.class
                    || valueClass == Character.class || valueClass == Short.class || valueClass == Byte.class;
        } else if (parameterType == Double.TYPE) {
            return valueClass == Double.class || valueClass == Float.class || valueClass == Long.class
                    || valueClass == Integer.class || valueClass == Character.class || valueClass == Short.class
                    || valueClass == Byte.class;
        } else {
            return false;
        }
    } else {
        return value == null || parameterType.isAssignableFrom(value.getClass());
    }
}

From source file:it.greenvulcano.util.ArrayUtils.java

public static final List<?> arrayToList(Object arr) {
    List<?> list = null;//from w  w w.j a  va  2s.  c o m

    Class<?> ac = arr.getClass();
    if (!ac.isArray()) {
        throw new IllegalArgumentException("The input parameter isn't an array");
    }

    Class<?> act = ac.getComponentType();
    if (act.isPrimitive()) {
        if (act.equals(boolean.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((boolean[]) arr));
        } else if (act.equals(byte.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((byte[]) arr));
        } else if (act.equals(char.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((char[]) arr));
        } else if (act.equals(short.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((short[]) arr));
        } else if (act.equals(int.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((int[]) arr));
        } else if (act.equals(long.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((long[]) arr));
        } else if (act.equals(float.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((float[]) arr));
        } else if (act.equals(double.class)) {
            list = Arrays.asList(org.apache.commons.lang3.ArrayUtils.toObject((double[]) arr));
        }
    } else {
        list = arrayToList((Object[]) arr);
    }

    return list;
}

From source file:Main.java

private static boolean isTypeMatch(Class<?> paramClass, Class<? extends Object> paramClass1) {
    if (paramClass.equals(paramClass1))
        return true;
    if (paramClass.isPrimitive()) {
        if ((paramClass.getName().equals("int")) && (paramClass1.getName().equals("java.lang.Integer")))
            return true;
        if ((paramClass.getName().equals("long")) && (paramClass1.getName().equals("java.lang.Long")))
            return true;
        if ((paramClass.getName().equals("float")) && (paramClass1.getName().equals("java.lang.Float")))
            return true;
        if ((paramClass.getName().equals("double")) && (paramClass1.getName().equals("java.lang.Double")))
            return true;
        if ((paramClass.getName().equals("char")) && (paramClass1.getName().equals("java.lang.Character")))
            return true;
        if ((paramClass.getName().equals("byte")) && (paramClass1.getName().equals("java.lang.Byte")))
            return true;
        if ((paramClass.getName().equals("short")) && (paramClass1.getName().equals("java.lang.Short")))
            return true;
        if ((paramClass.getName().equals("boolean")) && (paramClass1.getName().equals("java.lang.Boolean")))
            return true;
    }/*from w w w  .  j a v  a  2s.  com*/
    return false;
}