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

/**
 * Check if the given class is a primitive class or a primitive wrapper class.
 * //from  w  ww  .j  av  a  2 s.  c o m
 * @param type
 *          Class to check.
 * @return True if the class is a primitive or primitive wrapper.
 */
public static boolean isPrimitive(final Class type) {
    if (type.isPrimitive() || isPrimitiveWrapper(type)) {
        return true;
    }

    return false;
}

From source file:com.wavemaker.commons.util.TypeConversionUtils.java

/**
 * Returns true iff the Class clazz represents a primitive (boolean, int) or a primitive wrapper (Integer),
 * including Big{Integer,Decimal} and Atomic{Integer,Long}. Also, Strings and Dates are included.
 *
 * @param clazz/*www.ja v a  2s .  c om*/
 * @return
 */
public static boolean isPrimitiveOrWrapper(Class<?> clazz) {

    if (clazz.isPrimitive()) {
        return true;
    }

    if (clazz.equals(String.class)) {
        return true;
    }

    if (Date.class.isAssignableFrom(clazz)) {
        return true;
    }

    if (LocalDateTime.class.isAssignableFrom(clazz)) {
        return true;
    }

    if (PRIMITIVE_WRAPPERS.contains(clazz)) {
        return true;
    }

    return false;
}

From source file:com.ksmpartners.ernie.util.TestUtil.java

/**
 * Creates a test value to be used for a setter call. If primitive, it will
 * use Mockito. If not, will check the testValues hash specified for
 * pre-wired classes. If not found, will create an empty instance.
 * <p>//from  w  w w .  jav  a  2  s.  c o  m
 * @param qName the qualified name of the property in the event of failure
 * @param fieldType the target field type
 * @return a new value to be used in setter calls
 */
private static Object createSetValue(final String qName, final Class<?> fieldType) {
    createTestValuesIfNeeded();
    // create a set value
    Object setValue = null;
    try {
        if (fieldType.isPrimitive())
            setValue = Mockito.any(fieldType);
        else {
            // look to see if we have a mock object already for this type
            setValue = testValues.get(fieldType);

            // if still null, create instance
            if (setValue == null)
                setValue = fieldType.newInstance();
        }
    } catch (Exception e) {
        Assert.fail("Unable to mock parameter of type [" + fieldType.getName() + "] for field " + qName + ": "
                + e.getMessage());
    }
    return setValue;
}

From source file:gov.nih.nci.caarray.security.SecurityPolicy.java

private static void clearDisallowedProperty(Object entity, PropertyAccessor propAccessor)
        throws IllegalAccessException, InvocationTargetException {
    Class<?> type = propAccessor.getType();
    if (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)) {
        propAccessor.set(entity, CaArrayUtils.emptyCollectionOrMapFor(type));
    } else if (!type.isPrimitive()) {
        propAccessor.set(entity, null);/*from  w  w  w . java 2s . co m*/
    } else {
        LOG.warn("Could not null out primitive property " + propAccessor.getter().getName());
    }
}

From source file:Classes.java

/**
 * Get the wrapper class for the given primitive type.
 * /* w  w w .  ja va2 s.c  o  m*/
 * @param type
 *          Primitive class.
 * @return Wrapper class for primitive.
 * 
 * @exception IllegalArgumentException
 *              Type is not a primitive class
 */
public static Class getPrimitiveWrapper(final Class type) {
    if (!type.isPrimitive()) {
        throw new IllegalArgumentException("type is not a primitive class");
    }

    for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) {
        if (type.equals(PRIMITIVE_WRAPPER_MAP[i]))
            return PRIMITIVE_WRAPPER_MAP[i + 1];
    }

    // should never get here, if we do then PRIMITIVE_WRAPPER_MAP
    // needs to be updated to include the missing mapping
    System.out.println("Unreachable Statement Exception");
    return null;
}

From source file:com.jkoolcloud.tnt4j.streams.matchers.StringMatcher.java

private static Method findMatchingMethodAndConvertArgs(Object methodName, String[] arguments,
        Object[] convertedArguments, Method[] methods) {
    for (Method method : methods) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == arguments.length + 1) {
            boolean methodMatch = true;
            Class<?>[] parameterTypes = method.getParameterTypes();
            for (int i = 1; i < parameterTypes.length; i++) {
                Class<?> parameterType = parameterTypes[i];
                if (CharSequence.class.isAssignableFrom(parameterType)) {
                    convertedArguments[i - 1] = arguments[i - 1];
                } else {
                    try {
                        if (parameterType.isPrimitive()) {
                            parameterType = ClassUtils.primitiveToWrapper(parameterType);
                        }// w  ww.j  a  v  a 2s.  co m
                        Method converterMethod = parameterType.getMethod("valueOf", String.class);
                        convertedArguments[i - 1] = converterMethod.invoke(null, arguments[i - 1]);
                    } catch (Exception e) {
                        methodMatch = false;
                        break;
                    }
                }
            }

            if (methodMatch) {
                return method;
            }
        }
    }
    return null;
}

From source file:cop.raml.mocks.MockUtils.java

private static TypeElementMock createArrayElement(@NotNull Class<?> cls) throws ClassNotFoundException {
    TypeElementMock element = new TypeElementMock("Array", ElementKind.CLASS);
    TypeMirrorMock type = new TypeMirrorMock(element, TypeKind.ARRAY);

    if (cls.isPrimitive())
        type.setElementType(createPrimitiveElement(cls).asType());
    else if (cls.isEnum())
        type.setElementType(createEnumElement(cls).asType());
    else//  w w w .  j  av  a  2 s.com
        type.setElementType(createClassElement(cls).asType());

    element.setType(type);

    return element;
}

From source file:ReflectUtil.java

/**
 * Returns an appropriate default value for the class supplied. Mirrors the defaults used
 * when the JVM initializes instance variables.
 *
 * @param clazz the class for which to find the default value
 * @return null for non-primitive types and an appropriate wrapper instance for primitives
 */// w  ww  .  ja va2  s. c  o  m
public static Object getDefaultValue(Class<?> clazz) {
    if (clazz.isPrimitive()) {
        return primitiveDefaults.get(clazz);
    } else {
        return null;
    }
}

From source file:com.wrmsr.search.dsl.util.DerivedSuppliers.java

private static void compileGetter(ClassDefinition classDefinition,
        Map<String, FieldDefinition> classFieldDefinitionMap, java.lang.reflect.Method target,
        List<TargetParameter> targetParameters) {
    java.lang.reflect.Type targetReturnType = target.getGenericReturnType();
    Class<?> targetReturnClass = (Class<?>) targetReturnType;
    Class<?> boxedTargetReturnClass = requireNonNull(ClassUtils.primitiveToWrapper(targetReturnClass));
    checkArgument(targetReturnClass == boxedTargetReturnClass || targetReturnClass.isPrimitive());

    MethodDefinition methodDefinition = classDefinition.declareMethod(a(PUBLIC, FINAL), "get",
            type(Object.class));
    methodDefinition.declareAnnotation(Override.class);
    Scope scope = methodDefinition.getScope();
    BytecodeBlock body = methodDefinition.getBody();

    if (targetReturnClass.isPrimitive()) {
        body.newObject(boxedTargetReturnClass).dup();
    }/*from  w ww. j  a v a2 s  .c  o m*/
    for (TargetParameter targetParameter : targetParameters) {
        body.getVariable(scope.getThis()).getField(classFieldDefinitionMap.get(targetParameter.name))
                .invokeInterface(Supplier.class, "get", Object.class)
                .checkCast(targetParameter.parameterizedBoxedType);
        Class<?> targetParameterClass = (Class<?>) targetParameter.type;
        unboxValue(body, targetParameterClass);
    }
    body.invokeStatic(target);
    if (targetReturnClass.isPrimitive()) {
        body.invokeConstructor(boxedTargetReturnClass, targetReturnClass);
    }
    body.retObject();
}

From source file:ClassReader.java

private static void addDescriptor(StringBuffer b, Class c) {
    if (c.isPrimitive()) {
        if (c == void.class) {
            b.append('V');
        } else if (c == int.class) {
            b.append('I');
        } else if (c == boolean.class) {
            b.append('Z');
        } else if (c == byte.class) {
            b.append('B');
        } else if (c == short.class) {
            b.append('S');
        } else if (c == long.class) {
            b.append('J');
        } else if (c == char.class) {
            b.append('C');
        } else if (c == float.class) {
            b.append('F');
        } else if (c == double.class) {
            b.append('D');
        }/*from   w ww. j  a  v  a 2s  .co  m*/
    } else if (c.isArray()) {
        b.append('[');
        addDescriptor(b, c.getComponentType());
    } else {
        b.append('L').append(c.getName().replace('.', '/')).append(';');
    }
}