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:ca.oson.json.util.ObjectUtil.java

public static boolean isObject(Class valueType) {
    if (valueType == null) { // no idea, just assume
        return false;
    }// w  ww  .  j a va  2  s  .  c  om

    if (valueType.isPrimitive() || valueType.isEnum()) {
        return false;
    }

    if (Number.class.isAssignableFrom(valueType) || Date.class.isAssignableFrom(valueType)) {
        return false;
    }

    if (Map.class.isAssignableFrom(valueType)) {
        return false;
    }

    if (isArrayOrCollection(valueType)) {
        return false;
    }

    if (valueType == String.class || valueType == Character.class || valueType == Boolean.class) {
        return false;
    }

    return true;
}

From source file:capital.scalable.restdocs.constraints.ConstraintReaderImpl.java

private List<String> getEnumConstraintMessage(Class<?> javaBaseClass, String javaFieldName) {
    // could be getter actually
    Field field = findField(javaBaseClass, javaFieldName);
    if (field == null) {
        return emptyList();
    }//from w  w w  . j  av  a2 s.c  o m

    Class<?> rawClass = field.getType();
    if (!rawClass.isEnum()) {
        return emptyList();
    }

    Class<Enum> enumClass = (Class<Enum>) rawClass;

    String value = arrayToString(enumClass.getEnumConstants());
    String enumName = enumClass.getCanonicalName();
    String message = constraintDescriptionResolver
            .resolveDescription(new Constraint(enumName, singletonMap(VALUE, (Object) value)));

    // fallback
    if (isBlank(message) || message.equals(enumName)) {
        message = "Must be one of " + value;
    }
    return singletonList(message);
}

From source file:org.waveprotocol.box.server.util.ClientFlagsUtil.java

/**
 * Converts list of parameters to JSON.// w  w w .j a  va2 s . c o  m
 * 
 * @param params pairs (name, value) of parameters
 * @param checkParams true, if params should be checked for validity
 * @param log message log
 * @return resulting JSON object
 */
public static JSONObject convertParamsToJson(List<Pair<String, String>> params, boolean checkParams, Log log) {
    try {
        JSONObject result = new JSONObject();
        for (Pair<String, String> param : params) {
            String name = param.getFirst();
            String shortName = FlagConstants.getShortName(name);
            if (shortName != null) {
                String value = param.getSecond();
                if (checkParams) {
                    try {
                        checkParameter(name, value);
                    } catch (IllegalArgumentException e) {
                        warning(log, e.getMessage());
                    }
                }
                try {
                    Method getter = ClientFlags.class.getMethod(name);
                    Class<?> retType = getter.getReturnType();
                    try {
                        if (retType.equals(String.class)) {
                            result.put(shortName, ValueUtils.stripFrom(value, "\""));
                        } else if (retType.equals(int.class)) {
                            result.put(shortName, Integer.parseInt(value));
                        } else if (retType.equals(boolean.class)) {
                            if (!value.equals("true") && !value.equals("false")) {
                                throw new IllegalArgumentException();
                            }
                            result.put(shortName, Boolean.parseBoolean(value));
                        } else if (retType.equals(double.class)) {
                            result.put(shortName, Double.parseDouble(value));
                        } else if (retType.isEnum()) {
                            result.put(shortName, value);
                        } else {
                            warning(log, String.format("Ignoring flag %s with unknown return type %s.", name,
                                    retType));
                        }
                    } catch (IllegalArgumentException e) {
                        warning(log, String.format("Failed to parse parameter: name=%s, type=%s, value=%s",
                                name, retType, value));
                    }
                } catch (NoSuchMethodException e) {
                    warning(log, String.format("Failed to find the method %s() in ClientFlags.", name));
                }
            } else {
                warning(log, String.format("Failed to find the flag %s in ClientFlags.", name));
            }
        }
        return result;
    } catch (JSONException e) {
        severe(log, String.format("Failed to create flags JSON: %s", e.getMessage()));
        return new JSONObject();
    }
}

From source file:de.erdesignerng.dialect.ModelItemProperties.java

public void initializeFrom(T aObject) {
    ModelProperties theProperties = aObject.getProperties();

    try {//from w w  w . j  a v  a2 s.c  o  m
        for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) {
            if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) {
                String theValue = theProperties.getProperty(theDescriptor.getName());
                if (!StringUtils.isEmpty(theValue)) {
                    Class theType = theDescriptor.getPropertyType();

                    if (theType.isEnum()) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Enum.valueOf(theType, theValue));
                    }
                    if (String.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), theValue);
                    }
                    if (Long.class.equals(theType) || long.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Long.parseLong(theValue));
                    }
                    if (Integer.class.equals(theType) || int.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(), Integer.parseInt(theValue));
                    }
                    if (Boolean.class.equals(theType) || boolean.class.equals(theType)) {
                        PropertyUtils.setProperty(this, theDescriptor.getName(),
                                Boolean.parseBoolean(theValue));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

From source file:org.thelq.stackexchange.api.model.types.EntryFormatTest.java

@DataProvider
public Object[][] noExtraEnumMethods() throws IOException {
    List<Class<?>> enumClasses = new ArrayList<Class<?>>();
    for (Class<?> curClass : TestUtils.loadClasses(PostEntry.class, null)) {
        if (curClass.isEnum())
            enumClasses.add(curClass);//from   w ww. ja va 2s  .c o  m
        //Load subtypes
        for (Class<?> curSubClass : curClass.getDeclaredClasses())
            if (curSubClass.isEnum())
                enumClasses.add(curSubClass);
    }
    return TestUtils.toTestParameters(enumClasses);
}

From source file:org.carrot2.workbench.editors.impl.EnumEditor.java

@SuppressWarnings("unchecked")
@Override/*from  w  w w . j a va  2 s .c  o m*/
public AttributeEditorInfo init(Map<String, Object> defaultValues) {
    Class<? extends Enum<?>> clazz = (Class<? extends Enum<?>>) descriptor.type;
    if (clazz.isEnum()) {
        valueRequired = (descriptor.getAnnotation(Required.class) != null);
        anyValueAllowed = false;

        super.setMappedValues(ValueHintMappingUtils.getValueToFriendlyName(clazz),
                new ArrayList<Object>(ValueHintMappingUtils.getValidValuesMap(clazz).keySet()));
    } else if (String.class.equals(clazz)) {
        final ValueHintEnum hint = descriptor.getAnnotation(ValueHintEnum.class);

        if (hint == null) {
            throw new IllegalArgumentException("Editor applicable to Strings with "
                    + ValueHintEnum.class.getName() + " annotation: " + descriptor);
        }

        clazz = hint.values();

        valueRequired = (descriptor.getAnnotation(Required.class) != null);
        anyValueAllowed = true;

        super.setMappedValues(ValueHintMappingUtils.getValueToFriendlyName(clazz),
                new ArrayList<Object>(ValueHintMappingUtils.getValidValuesMap(clazz).keySet()));
    } else {
        throw new IllegalArgumentException("Attribute type not supported: " + descriptor);
    }

    return new AttributeEditorInfo(1, false);
}

From source file:org.apache.empire.spring.EmpireReader.java

@Override
protected void getBeanProperty(Object bean, String property, Object value) {
    try {//from   w  w w  .j a v  a 2 s  . c o m
        if (bean == null)
            throw new InvalidArgumentException("bean", bean);
        if (StringUtils.isEmpty(property))
            throw new InvalidArgumentException("property", property);

        // Get descriptor
        PropertyDescriptor descriptor = BeanUtilsBean.getInstance().getPropertyUtils()
                .getPropertyDescriptor(bean, property);
        if (descriptor == null) {
            return; // Skip this property setter
        }
        // Check enum
        Class<?> type = descriptor.getPropertyType();
        if (type.isEnum()) {
            // Enum<?> ev = Enum.valueOf(type, value);
            boolean found = false;
            Enum<?>[] items = (Enum[]) type.getEnumConstants();
            for (int i = 0; i < items.length; i++) {
                Enum<?> item = items[i];
                if (ObjectUtils.compareEqual(item.name(), value)) {
                    value = item;
                    found = true;
                    break;
                }
            }
            // Enumeration value not found
            if (!found)
                throw new ItemNotFoundException(value);
        }

        // Set Property Value
        if (value != null) { // Bean utils will convert if necessary
            BeanUtils.setProperty(bean, property, value);
        } else { // Don't convert, just set
            PropertyUtils.setProperty(bean, property, null);
        }
    } catch (IllegalAccessException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    } catch (InvocationTargetException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    } catch (NoSuchMethodException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    } catch (NullPointerException e) {
        log.error(bean.getClass().getName() + ": unable to set property '" + property + "'");
        throw new BeanPropertySetException(bean, property, e);
    }
}

From source file:net.kaczmarzyk.spring.data.jpa.domain.EqualEnum.java

private Class<? extends Enum<?>> getEnumClass(Root<T> root) {
    Class<? extends Enum<?>> enumClass = this.<Enum<?>>path(root).getJavaType();
    if (!enumClass.isEnum()) {
        throw new IllegalArgumentException("Type of field with path " + super.path + " is not enum!");
    }//from   w w w. jav a 2s  . c  o  m
    return enumClass;
}

From source file:adalid.core.XS1.java

private static boolean isRestrictedFieldType(Class<?> fieldType) {
    int modifiers = fieldType.getModifiers();
    boolean b = fieldType.isPrimitive();
    b |= Modifier.isAbstract(modifiers) || !Modifier.isPublic(modifiers);
    b |= fieldType.isAnnotation();/* ww  w  .  ja v  a2s.  c  o m*/
    b |= fieldType.isAnonymousClass();
    b |= fieldType.isArray();
    b |= fieldType.isEnum();
    b |= fieldType.isLocalClass();
    b |= fieldType.isInterface();
    return b;
}

From source file:org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter.java

private boolean isGetDeclaringClassEnumGetter(final Method getter, final Class<?> clazz) {
    return clazz.isEnum() && getter.getName().equals("getDeclaringClass");
}