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:org.opensaas.jaudit.test.BeanTest.java

/**
 * Returns test values based on the passed in value type. This method may be
 * overridden for types the base BeanTest class does not support. Currently
 * delegates to the Map returned by {@link #newClassToValues()}.
 * /*  w w  w . ja v a  2s.c  o  m*/
 * @param valueType
 *            Class to resolve values for.
 * 
 * @return Object[] matching values to test.
 */
protected Object[] getTestValues(final Class<?> valueType) {

    final Object[] values = CLASS_TO_VALUES.get(valueType);
    if (values == null) {

        // check if enum
        if (valueType.isEnum()) {
            final Object[] enumConsts = valueType.getEnumConstants();
            final List<Object> consts = new ArrayList<Object>(enumConsts.length + 1);
            consts.addAll(Arrays.asList(enumConsts));
            consts.add(null);
            return consts.toArray();
        }

        return EMPTY_ARRAY;
    }
    return values;
}

From source file:com.haulmont.cuba.core.sys.MetadataBuildSupport.java

private Object getXmlAnnotationAttributeValue(String value, String className, String datatypeName) {
    if (className == null && datatypeName == null)
        return inferMetaAnnotationType(value);
    if (className != null) {
        Class aClass = ReflectionHelper.getClass(className);
        if (aClass.isEnum()) {
            //noinspection unchecked
            return Enum.valueOf(aClass, value);
        } else {/*from w w w  .  j av  a 2s.  co m*/
            throw new UnsupportedOperationException("Class " + className + "  is not Enum");
        }
    } else {
        try {
            return datatypes.get(datatypeName).parse(value);
        } catch (ParseException e) {
            throw new RuntimeException("Unable to parse XML meta-annotation value", e);
        }
    }
}

From source file:de.alpharogroup.lang.ClassExtensions.java

/**
 * Gets the {@link ClassType} from the given class.
 *
 * @param clazz/*from   www . ja  va2s.  com*/
 *            The class.
 * @return the {@link ClassType} from the given class.
 */
public static ClassType getClassType(final Class<?> clazz) {
    if (clazz.isArray()) {
        return ClassType.ARRAY;
    }
    if (isCollection(clazz)) {
        return ClassType.COLLECTION;
    }
    if (isMap(clazz)) {
        return ClassType.MAP;
    }
    if (clazz.isLocalClass()) {
        return ClassType.LOCAL;
    }
    if (clazz.isMemberClass()) {
        return ClassType.MEMBER;
    }
    if (clazz.isPrimitive()) {
        return ClassType.PRIMITIVE;
    }
    if (clazz.isAnnotation()) {
        return ClassType.ANNOTATION;
    }
    if (clazz.isEnum()) {
        return ClassType.ENUM;
    }
    if (clazz.isInterface()) {
        return ClassType.INTERFACE;
    }
    if (clazz.isSynthetic()) {
        return ClassType.SYNTHETIC;
    }
    if (clazz.isAnonymousClass()) {
        return ClassType.ANONYMOUS;
    }
    return ClassType.DEFAULT;
}

From source file:org.unitils.core.util.ObjectFormatter.java

protected boolean formatPrimitiveOrEnum(Object object, Class<?> type, StringBuilder result) {
    if (type.isPrimitive() || type.isEnum()) {
        result.append(String.valueOf(object));
        return true;
    }//from  w w w.  j ava 2s .c o m
    return false;
}

From source file:com.yahoo.elide.jsonapi.serialization.KeySerializer.java

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    String str;//from   w  ww  . j a  va 2s  .com
    Class<?> cls = value.getClass();

    if (cls == String.class) {
        str = (String) value;
    } else if (Date.class.isAssignableFrom(cls)) {
        provider.defaultSerializeDateKey((Date) value, jgen);
        return;
    } else if (cls == Class.class) {
        str = ((Class<?>) value).getName();
    } else if (cls.isEnum()) {
        str = ((Enum<?>) value).name();
    } else {
        str = value.toString();
    }
    jgen.writeFieldName(str);
}

From source file:com.github.juanmf.java2plant.render.PlantRenderer.java

protected void addClass(StringBuilder sb, Class<?> aClass) {
    String classDeclaration = aClass.isEnum() ? "enum " + aClass.getName() : aClass.toString();
    sb.append(classDeclaration);/*  www  .  j  a  v a  2 s . co  m*/
    addClassTypeParams(sb, aClass);
    sb.append(" {\n");
    renderClassMembers(sb, aClass);
    sb.append("\n}\n");
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

protected boolean isStandardType(Class<?> type) {
    boolean standard = type.isPrimitive() || type.isEnum();
    if (!standard) {
        for (Class<?> standardType : this.standardTypes) {
            standard = standardType.isAssignableFrom(type);
            if (standard) {
                break;
            }/*  www  . java2 s. c o  m*/
        }
    }
    return standard;
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

protected void outputProperty(XMLWriter xmlWriter, PropertyTemplate property, OutputContext context)
        throws Exception {
    Element element = DocumentHelper.createElement("Prop");
    setElementAttributes(element, property,
            "name,defaultValue,highlight,fixed,enumValues,editor,clientTypes,deprecated,reserve");
    if (!property.isVisible()) {
        element.addAttribute("visible", "false");
    }/* ww w.ja va 2s  .c  om*/

    if (StringUtils.isNotEmpty(property.getType())) {
        try {
            Class<?> type = ClassUtils.forName(property.getType());
            if (!String.class.equals(type) && !type.isEnum()) {
                element.addAttribute("type", type.getName());
            }
        } catch (ClassNotFoundException e) {
            // do nothing
        }
    }

    CompositeType compositeType = property.getCompositeType();
    if (compositeType != CompositeType.Unsupport) {
        element.addAttribute("compositeType", compositeType.toString());
    }

    ReferenceTemplate reference = property.getReference();
    if (reference != null) {
        String referenceText = reference.getRuleTemplate().getName();
        if (StringUtils.isNotEmpty(reference.getProperty())) {
            referenceText += ':' + reference.getProperty();
        }
        element.addAttribute("reference", referenceText);
    }

    if (compositeType == CompositeType.Fixed || compositeType == CompositeType.Open) {
        xmlWriter.writeOpen(element);
        for (PropertyTemplate subProperty : property.getProperties().values()) {
            outputProperty(xmlWriter, subProperty, context);
        }
        xmlWriter.writeClose(element);
    } else {
        xmlWriter.write(element);
    }
}

From source file:com.thinkbiganalytics.metadata.modeshape.common.mixin.NodeEntityMixin.java

default <T> T getPropertyFromNode(Node node, String name, Class<T> type, boolean allowNotFound) {
    Object o = JcrPropertyUtil.getProperty(node, name, allowNotFound);
    if (allowNotFound && o == null) {
        return null;
    }//  w w w . jav  a2  s  . co  m
    if (type.isEnum()) {
        String savedType = o.toString();
        if (StringUtils.isNotBlank(savedType)) {
            Class<? extends Enum> x = (Class<? extends Enum>) type;
            return (T) Enum.valueOf(x, savedType);
        }
    }
    if (!o.getClass().isAssignableFrom(type)) {
        //if it cant be matched and it is a Node > JcrObject, do the conversion
        if (o instanceof Node && JcrObject.class.isAssignableFrom(type)) {
            return JcrUtil.constructNodeObject((Node) o, type, null);
        } else {
            throw new MetadataRepositoryException("Unable to convert Property " + name + " to type " + type);
        }
    } else {
        return (T) o;
    }
}

From source file:org.vulpe.view.tags.Functions.java

/**
 *
 * @param bean//w ww  . j av a2s . co  m
 * @param field
 * @param removeEnumItems
 * @return
 * @throws JspException
 */
public static List listInField(final Object bean, final String field, final String removeEnumItems)
        throws JspException {
    try {
        if (bean == null) {
            return null;
        }
        final List list = new ArrayList();
        final String[] fieldParts = field.replace(".id", "").split("\\.");
        Class<?> fieldClass = null;
        if (fieldParts.length == 1) {
            fieldClass = VulpeReflectUtil.getFieldClass(bean.getClass(), fieldParts[0]);
        } else {
            int count = 1;
            Class<?> parentClass = bean.getClass();
            for (final String fieldPart : fieldParts) {
                parentClass = VulpeReflectUtil.getFieldClass(parentClass, fieldPart);
                ++count;
                if (count == fieldParts.length) {
                    fieldClass = VulpeReflectUtil.getFieldClass(parentClass, fieldParts[fieldParts.length - 1]);
                }
            }
        }
        if (fieldClass.isEnum()) {
            int count = 0;
            for (final Object item : fieldClass.getEnumConstants()) {
                if (StringUtils.isNotEmpty(removeEnumItems)) {
                    boolean remove = false;
                    final String[] items = removeEnumItems.split(",");
                    for (final String eItem : items) {
                        if ((NumberUtils.isNumber(eItem) && Integer.valueOf(eItem) == count)
                                || item.toString().equals(eItem)) {
                            remove = true;
                            break;
                        }
                    }
                    ++count;
                    if (remove) {
                        continue;
                    }
                }
                list.add(new ValueBean(item.toString(),
                        findText(fieldClass.getName().concat(".").concat(item.toString()))));
            }
        }
        return list;
    } catch (Exception e) {
        throw new JspException(e);
    }
}