Example usage for org.apache.commons.lang3 ClassUtils isPrimitiveOrWrapper

List of usage examples for org.apache.commons.lang3 ClassUtils isPrimitiveOrWrapper

Introduction

In this page you can find the example usage for org.apache.commons.lang3 ClassUtils isPrimitiveOrWrapper.

Prototype

public static boolean isPrimitiveOrWrapper(final Class<?> type) 

Source Link

Document

Returns whether the given type is a primitive or primitive wrapper ( Boolean , Byte , Character , Short , Integer , Long , Double , Float ).

Usage

From source file:com.flipkart.foxtrot.client.util.TypeChecker.java

public static boolean isPrimitive(Object object) {
    return ClassUtils.isPrimitiveOrWrapper(object.getClass());
}

From source file:com.microsoft.rest.Validator.java

/**
 * Validates a user provided required parameter to be not null.
 * A {@link ServiceException} is thrown if a property fails the validation.
 *
 * @param parameter the parameter to validate
 * @throws ServiceException failures wrapped in {@link ServiceException}
 *///from   w ww.  ja v  a  2  s .c  o m
public static void validate(Object parameter) throws ServiceException {
    // Validation of top level payload is done outside
    if (parameter == null) {
        return;
    }

    Class parameterType = parameter.getClass();
    if (ClassUtils.isPrimitiveOrWrapper(parameterType) || parameterType.isEnum()
            || ClassUtils.isAssignable(parameterType, LocalDate.class)
            || ClassUtils.isAssignable(parameterType, DateTime.class)
            || ClassUtils.isAssignable(parameterType, String.class)
            || ClassUtils.isAssignable(parameterType, DateTimeRfc1123.class)
            || ClassUtils.isAssignable(parameterType, Period.class)) {
        return;
    }

    Field[] fields = FieldUtils.getAllFields(parameterType);
    for (Field field : fields) {
        field.setAccessible(true);
        JsonProperty annotation = field.getAnnotation(JsonProperty.class);
        Object property;
        try {
            property = field.get(parameter);
        } catch (IllegalAccessException e) {
            throw new ServiceException(e);
        }
        if (property == null) {
            if (annotation != null && annotation.required()) {
                throw new ServiceException(
                        new IllegalArgumentException(field.getName() + " is required and cannot be null."));
            }
        } else {
            try {
                Class propertyType = property.getClass();
                if (ClassUtils.isAssignable(propertyType, List.class)) {
                    List<?> items = (List<?>) property;
                    for (Object item : items) {
                        Validator.validate(item);
                    }
                } else if (ClassUtils.isAssignable(propertyType, Map.class)) {
                    Map<?, ?> entries = (Map<?, ?>) property;
                    for (Map.Entry<?, ?> entry : entries.entrySet()) {
                        Validator.validate(entry.getKey());
                        Validator.validate(entry.getValue());
                    }
                } else if (parameter.getClass().getDeclaringClass() != propertyType) {
                    Validator.validate(property);
                }
            } catch (ServiceException ex) {
                IllegalArgumentException cause = (IllegalArgumentException) (ex.getCause());
                if (cause != null) {
                    // Build property chain
                    throw new ServiceException(
                            new IllegalArgumentException(field.getName() + "." + cause.getMessage()));
                } else {
                    throw ex;
                }
            }
        }
    }
}

From source file:io.appform.nautilus.funnel.utils.AttributeUtils.java

public static boolean isValid(Map<String, Object> attributes) {
    if (null == attributes) {
        return true;
    }// ww w.j a  v a  2s. c o  m
    final boolean[] valid = { true };
    attributes.entrySet().forEach(entry -> {
        if (!entry.getKey().equals("location")) {
            Object value = entry.getValue();
            final Class<?> clazz = value.getClass();
            valid[0] &= (!entry.getKey().matches(Constants.FIELD_REPLACEMENT_VALUE)
                    && !ClassUtils.isPrimitiveOrWrapper(clazz) && !(value instanceof String));
        }

    });
    return valid[0];
}

From source file:edu.umich.flowfence.common.ParceledPayload.java

public static boolean canParcelType(Class<?> clazz, boolean allowVoid) {
    // All primitives and wrapper types are parcelable, except Character and Void.
    if (ClassUtils.isPrimitiveOrWrapper(clazz)) {
        return (clazz != char.class && clazz != Character.class
                && (allowVoid || (clazz != void.class && clazz != Void.class)));
    }/* ww  w  .  j a  va  2s.c o m*/
    // String and CharSequence are parcelable.
    if (clazz == String.class || ClassUtils.isAssignable(clazz, CharSequence.class)) {
        return true;
    }
    // Object arrays are parcelable if their component type is parcelable.
    // Primitive boolean[], byte[], int[], and long[] arrays are parcelable.
    if (clazz.isArray()) {
        Class<?> componentType = clazz.getComponentType();
        if (componentType.isPrimitive()) {
            return (componentType == int.class || componentType == long.class || componentType == byte.class
                    || componentType == boolean.class);
        } else {
            return canParcelType(componentType, false);
        }
    }
    // Parcelable, obviously, is parcelable.
    // This covers Bundle as well.
    if (ClassUtils.isAssignable(clazz, Parcelable.class)) {
        return true;
    }
    // Map, List, and SparseArray are all parcelable, with restrictions on their component type
    // that we can't check here.
    if (ClassUtils.isAssignable(clazz, Map.class) || ClassUtils.isAssignable(clazz, List.class)
            || ClassUtils.isAssignable(clazz, SparseArray.class)) {
        return true;
    }
    // IBinder is parcelable.
    if (ClassUtils.isAssignable(clazz, IBinder.class)) {
        return true;
    }
    // Serializable is parcelable.
    if (ClassUtils.isAssignable(clazz, Serializable.class)) {
        return true;
    }
    return false;
}

From source file:hu.bme.mit.sette.common.model.runner.ParameterType.java

public static ParameterType primitiveFromJavaClass(Class<?> javaClass) {
    Validate.notNull(javaClass, "The Java class must not be null");
    Validate.isTrue(ClassUtils.isPrimitiveOrWrapper(javaClass),
            "The represented type is not primitive [javaClass: %s]", javaClass.getName());

    Class<?> primitiveClass;
    if (javaClass.isPrimitive()) {
        primitiveClass = javaClass;/*from www .  j  av  a  2  s . c  om*/
    } else {
        primitiveClass = ClassUtils.wrapperToPrimitive(javaClass);
    }

    Validate.isTrue(primitiveClass != void.class, "the parameter type must not be void [javaClass: %s]",
            javaClass.getName());

    return fromString(primitiveClass.getCanonicalName());
}

From source file:com.erudika.para.utils.filters.FieldFilter.java

private void filterObject(List<String> fields, Object entity) {
    if (fields == null || fields.isEmpty()) {
        return;/*w ww  .  j  a  v  a2  s .  c o m*/
    }
    if (entity instanceof List) {
        for (Object obj : (List) entity) {
            filterObject(fields, obj);
        }
    } else if (entity instanceof Map) {
        for (Object obj : ((Map) entity).values()) {
            filterObject(fields, obj);
        }
    } else if (entity instanceof Object[]) {
        for (Object obj : (Object[]) entity) {
            filterObject(fields, obj);
        }
    } else if (!ClassUtils.isPrimitiveOrWrapper(entity.getClass()) && !(entity instanceof String)) {
        for (Field field : entity.getClass().getDeclaredFields()) {
            try {
                String fieldName = field.getName();
                field.setAccessible(true);
                if (!fields.contains(fieldName)) {
                    field.set(entity, null);
                }
            } catch (Exception e) {
                LoggerFactory.getLogger(this.getClass()).warn(null, e);
            }
        }
    }
}

From source file:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java

@Override
public boolean canConvert(ArgumentConverter parser, Type type) {
    return ClassUtils.isPrimitiveOrWrapper(ReflectionUtils.toClass(type));
}

From source file:com.wavemaker.tools.apidocs.tools.parser.impl.PropertyParserImpl.java

private Property parseType(Type type) {
    Property property;/*  w ww. j a  va 2  s .co  m*/
    TypeInformation typeInfo = TypeUtil.extractTypeInformation(type);

    if (!typeInfo.getTypeArguments().isEmpty()) {
        if (isArray(typeInfo)) {
            property = feedArrayProperty(typeInfo);
        } else if (Map.class.isAssignableFrom(typeInfo.getActualType())) {
            property = feedMapProperty(typeInfo);
        } else {
            property = feedObjectProperty(typeInfo);
        }
    } else {
        Class<?> actualType = typeInfo.getActualType();
        if (DataTypeUtil.isEnum(actualType) || String.class.equals(actualType)) {
            property = feedStringProperty(actualType);
        } else if (ClassUtils.isPrimitiveOrWrapper(actualType) || Number.class.isAssignableFrom(actualType)) {
            property = feedPrimitiveProperty(actualType);
        } else if (File.class.equals(actualType) || File.class.isAssignableFrom(actualType)) {
            property = new FileProperty();
        } else if (UUID.class.equals(actualType)) {
            property = new UUIDProperty();
        } else if (URI.class.equals(actualType)) {
            property = new StringProperty(StringProperty.Format.URI);
        } else if (URL.class.equals(actualType)) {
            property = new StringProperty(StringProperty.Format.URL);
        } else if (Date.class.isAssignableFrom(actualType)) {
            property = new DateProperty();
            ((DateProperty) property).setSubFormat(actualType.getSimpleName().toLowerCase());
        } else if (LOCAL_DATE_TIME_CLASS_NAME.equals(actualType.getName())) {
            property = new DateProperty();
            ((DateProperty) property).setSubFormat("date-time");
        } else {
            property = feedObjectProperty(typeInfo);
        }
    }

    return property;
}

From source file:com.wavemaker.tools.apidocs.tools.parser.scanner.FilterableModelScanner.java

@Override
public boolean filter(final Class<?> type) {
    Objects.requireNonNull(type, "Type cannot be null");
    return !ClassUtils.isPrimitiveOrWrapper(type) && apply(type.getName() + CLASS_EXT);
}

From source file:it.andreascarpino.ansible.inventory.type.AnsibleVariable.java

public String valueToString(Object value) {
    if (value == null) {
        return "";
    }/*from  w  w  w  .  j  a  va2s  .c  om*/

    final Class<?> vClass = value.getClass();

    String str;
    if (Collection.class.isAssignableFrom(vClass)) {
        str = listToString((Collection<?>) value);
    } else if (Map.class.isAssignableFrom(vClass)) {
        str = mapToString((Map<?, ?>) value);
    } else if (ClassUtils.isPrimitiveOrWrapper(vClass) || value instanceof String) {
        str = value.toString();
    } else {
        str = objToString(value);
    }

    // Use double backslash because of YAML syntax
    return str.replace("\\", "\\\\");
}