Example usage for java.lang Class isEnum

List of usage examples for java.lang Class isEnum

Introduction

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

Prototype

public boolean isEnum() 

Source Link

Document

Returns true if and only if this class was declared as an enum in the source code.

Usage

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

public static boolean isPrimitiveOrEnum(Type type) {
    if (type instanceof Class && !((Class) type).isArray()) {
        Class klass = (Class) type;
        if (isPrimitive(klass.getName()) || klass.isEnum()) {
            return true;
        }//from ww w .  j a  va 2s. c  o  m
    }
    return false;
}

From source file:net.radai.beanz.util.ReflectionUtil.java

public static boolean isEnum(Type type) {
    if (type instanceof ParameterizedType) {
        return false; //no such thing as Enum<Something>
    }//w ww  . ja v  a  2s . co  m
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        return clazz.isEnum();
    }
    if (type instanceof GenericArrayType) {
        return false;
    }
    throw new UnsupportedOperationException();
}

From source file:io.fabric8.forge.rest.dto.UICommands.java

public static void populateController(Map<String, Object> requestedInputs, CommandController controller,
        ConverterFactory converterFactory) {
    Map<String, InputComponent<?, ?>> inputs = controller.getInputs();
    Set<String> inputKeys = new HashSet<>(inputs.keySet());
    if (requestedInputs != null) {
        inputKeys.retainAll(requestedInputs.keySet());
        Set<Map.Entry<String, InputComponent<?, ?>>> entries = inputs.entrySet();
        for (Map.Entry<String, InputComponent<?, ?>> entry : entries) {
            String key = entry.getKey();
            InputComponent<?, ?> component = entry.getValue();
            Object value = requestedInputs.get(key);
            String textValue = null;
            if (value instanceof String || value instanceof Number || value instanceof Date) {
                textValue = value.toString();
            }// ww w.j av a2 s. c o  m
            /*
                            String textValue = requestedInputs.get(key);
                            Object value = textValue;
            */
            if (component != null && textValue != null) {
                Converter<String, ?> valueConverter = component.getValueConverter();
                if (valueConverter != null) {
                    value = valueConverter.convert(textValue);
                } else {
                    Class<?> valueType = component.getValueType();
                    if (valueType.isEnum()) {
                        Class<? extends Enum> enumType = (Class<? extends Enum>) valueType;
                        value = Enum.valueOf(enumType, textValue);
                    }
                }
                InputComponents.setValueFor(converterFactory, component, value);
            } else {
                controller.setValueFor(key, value);
            }

            Object actual = controller.getValueFor(key);
        }
    }
}

From source file:com.palantir.ptoss.cinch.core.BindingContext.java

/**
 * Returns the list of {@link ModelUpdate} types in this binding context.
 * @param modelClass/* w ww  .  jav a 2s  . c  o m*/
 * @return the of {@link Class}es that implement {@link ModelUpdate} in this binding context.
 */
public static List<Class<?>> findModelUpdateClass(final BindableModel modelClass) {
    List<Class<?>> classes = Reflections.getTypesOfTypeForClassHierarchy(modelClass.getClass(),
            ModelUpdate.class);
    Predicate<Class<?>> isEnum = new Predicate<Class<?>>() {
        public boolean apply(final Class<?> input) {
            return input.isEnum();
        }
    };
    // Look for ModelUpdate classes in implemented interfaces
    classes = Lists.newArrayList(Iterables.filter(classes, isEnum));
    for (Class<?> iface : modelClass.getClass().getInterfaces()) {
        classes.addAll(Lists.newArrayList(Iterables
                .filter(Reflections.getTypesOfTypeForClassHierarchy(iface, ModelUpdate.class), isEnum)));
    }
    if (classes.size() == 0) {
        return null;
    }
    return classes;
}

From source file:org.openspotlight.common.util.Conversion.java

/**
 * Returns a new converted type based on target type parameter.
 * /* ww w . j  av  a 2 s . c  om*/
 * @param <E>
 * @param rawValue
 * @param targetType
 * @return a new value from a converted type
 * @throws SLException
 */
@SuppressWarnings("unchecked")
public static <E> E convert(final Object rawValue, final Class<E> targetType) {
    Assertions.checkNotNull("targetType", targetType); //$NON-NLS-1$
    Assertions.checkCondition("validTargetType:" + targetType.getName(), //$NON-NLS-1$
            Conversion.CONVERTERS.containsKey(targetType) || targetType.isEnum());
    if (rawValue == null) {
        return null;
    }

    try {
        if (targetType.isEnum()) {
            final String rawValueAsString = rawValue.toString();
            final Field[] flds = targetType.getDeclaredFields();
            for (final Field f : flds) {
                if (f.isEnumConstant()) {
                    if (f.getName().equals(rawValueAsString)) {
                        final Object value = f.get(null);
                        return (E) value;

                    }
                }
            }
            throw new IllegalStateException(MessageFormat.format("Invalid enum constant:{0} for type {1}",
                    rawValueAsString, targetType));
        }
        final Converter converter = Conversion.CONVERTERS.get(targetType);
        final E converted = (E) converter.convert(targetType, rawValue);
        return converted;
    } catch (final Exception e) {
        throw Exceptions.logAndReturnNew(e, SLRuntimeException.class);
    }
}

From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java

private static Object convertValue(String value, Class<?> type) throws ParseException {
    Class clazz = type.isArray() ? type.getComponentType() : type;
    if (clazz.isPrimitive() || Number.class.isAssignableFrom(clazz) || clazz == Boolean.class) {
        return convertPrimitiveValue(value, clazz);
    } else if (clazz == String.class) {
        return value;
    } else if (clazz.isEnum()) {
        return Enum.valueOf((Class<Enum>) clazz, value);
    }//from   w  w w. j  ava  2  s.co  m

    return null;
}

From source file:org.jaffa.util.BeanHelper.java

/** Iterates through the defined constants for the input Enum class.
 * Returns the constant that matches the input value.
 * @param enumClass The Enum class.//from   w w w .  j  a  v a 2  s  .c  o  m
 * @param value The value whose corresponding Enum constant is to be returned.
 * @return the Enum constant that matches the input value. A null is returned if no match is found or if the input class is not an Enum.
 */
private static Object findEnum(Class enumClass, String value) {
    if (enumClass.isEnum()) {
        Object[] enumConstants = enumClass.getEnumConstants();
        if (enumConstants != null) {
            for (Object enumConstant : enumConstants) {
                if (enumConstant.toString().equals(value))
                    return enumConstant;
            }
        }
    }
    return null;
}

From source file:org.libreplan.business.util.deepcopy.DeepCopy.java

private static boolean isEnum(Class<?> klass) {
    Class<?> currentClass = klass;
    do {//from   www .j  a v a2  s .co  m
        if (currentClass.isEnum()) {
            return true;
        }
        currentClass = currentClass.getSuperclass();
    } while (currentClass != null);

    return false;
}

From source file:com.netflix.simianarmy.aws.SimpleDBRecorder.java

/**
 * Value to enum. Converts a "name|type" string back to an enum.
 *
 * @param value//w w w  .  j av  a2s .  c  o  m
 *            the value
 * @return the enum
 */
private static <T extends NamedType> T valueToEnum(Class<T> type, String value) {
    // parts = [enum value, enum class type]
    String[] parts = value.split("\\|", 2);
    if (parts.length < 2) {
        throw new RuntimeException("value " + value + " does not appear to be an internal enum format");
    }

    Class<?> enumClass;
    try {
        enumClass = Class.forName(parts[1]);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("class for enum value " + value + " not found");
    }
    if (!enumClass.isEnum()) {
        throw new RuntimeException("value " + value + " does not appear to be of an enum type");
    }
    if (!type.isAssignableFrom(enumClass)) {
        throw new RuntimeException("value " + value + " cannot be assigned to a variable of this type: "
                + type.getCanonicalName());
    }
    @SuppressWarnings("rawtypes")
    Class<? extends Enum> enumType = enumClass.asSubclass(Enum.class);
    @SuppressWarnings("unchecked")
    T enumValue = (T) Enum.valueOf(enumType, parts[0]);
    return enumValue;
}

From source file:org.jdto.impl.ValueConversionHelper.java

/**
 * Make the target value compatible with the target type.
 * @param targetValue the target value to be converted.
 * @param targetType the target type for the value.
 * @return the value converted to the target type or the same value if no
 * conversion is available./* w w w  . j  a v  a 2 s  . c o m*/
 */
static Object compatibilize(Object targetValue, Class targetType) {

    //the f(null) = null case
    if (targetValue == null) {
        return null;
    }

    //if the target type is String then call toString
    if (String.class.equals(targetType)) {
        return targetValue.toString();
    }

    //if the target type is enum and the source is a string
    //try to convert it.
    if (targetType.isEnum() && String.class.equals(targetValue.getClass())) {
        return readEnumConstant((String) targetValue, targetType);
    }

    if (Date.class.equals(targetType) || Calendar.class.equals(targetType)) {
        return convertToDateOrCalendar(targetValue, targetType);
    }

    //enought of compatibility.
    return targetValue;
}