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:java2typescript.jackson.module.StaticFieldExporter.java

public void export(List<Class<?>> classesToConvert) throws IllegalArgumentException {
    for (Class<?> clazz : classesToConvert) {
        if (clazz.isEnum()) {
            continue;
        }//ww w . j a  va 2 s.co m
        StaticClassType staticClass = new StaticClassType(clazz.getSimpleName() + CLASS_NAME_EXTENSION, clazz);

        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            if (isPublicStaticFinal(field.getModifiers())) {
                Value value;
                try {
                    value = constructValue(module, field.getType(), field.get(null));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException("Failed to get value of field " + field, e);
                }
                if (value != null) {
                    staticClass.getStaticFields().put(field.getName(), value);
                }
            }
        }
        if (staticClass.getStaticFields().size() > 0) {
            module.getNamedTypes().put(staticClass.getName(), staticClass);
        }
    }
}

From source file:org.apache.bval.util.KeyedAccess.java

/**
 * {@inheritDoc}//from  w w  w  .j a v  a2s . c  om
 */
@Override
public Object get(Object instance) {
    if (instance instanceof Map<?, ?>) {
        Map<?, ?> map = (Map<?, ?>) instance;
        Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(containerType, Map.class);
        Type keyType = TypeUtils.unrollVariables(typeArguments, MAP_TYPEVARS[0]);
        if (key == null || keyType == null || TypeUtils.isInstance(key, keyType)) {
            return map.get(key);
        }
        if (key instanceof String) {
            String name = (String) key;
            Class<?> rawKeyType = TypeUtils.getRawType(keyType, containerType);
            if (rawKeyType.isEnum()) {
                @SuppressWarnings({ "unchecked", "rawtypes" })
                final Object result = map.get(Enum.valueOf((Class<? extends Enum>) rawKeyType, name));
                return result;
            }
            for (Map.Entry<?, ?> e : map.entrySet()) {
                if (name.equals(e.getKey())) {
                    return e.getValue();
                }
            }
        }
    }
    return null;
}

From source file:com.bacoder.parser.core.DumpVisitor.java

@Override
public void visitBefore(Node node) {
    String tag = String.format("%s<%s sl=\"%d\" sc=\"%d\" el=\"%d\" ec=\"%d\">\n",
            Strings.repeat(indent, level), node.getClass().getSimpleName(), node.getStartLine(),
            node.getStartColumn(), node.getEndLine(), node.getEndColumn());
    try {//from   w  w  w  .j  a va 2 s . co m
        outputStream.write(tag.getBytes());

        Class<? extends Node> clazz = node.getClass();
        if (clazz.getAnnotation(DumpTextWithToString.class) != null) {
            String property = String.format("%s<text>%s</text>\n", Strings.repeat(indent, level + 1),
                    node.toString());
            try {
                outputStream.write(property.getBytes());
            } catch (IOException e) {
                throw new RuntimeException("Unable to write \'" + property + "\'", e);
            }
        } else {
            Field[] fields = node.getClass().getDeclaredFields();
            for (Field field : fields) {
                Class<?> type = field.getType();
                if (type.isPrimitive() || type.isEnum() || String.class.isAssignableFrom(type)) {
                    String propertyName = field.getName();
                    Object value;
                    try {
                        value = PropertyUtils.getSimpleProperty(node, propertyName);
                        String property = String.format("%s<%s>%s</%s>\n", Strings.repeat(indent, level + 1),
                                propertyName,
                                value == null ? "" : StringEscapeUtils.escapeXml(value.toString()),
                                propertyName);
                        try {
                            outputStream.write(property.getBytes());
                        } catch (IOException e) {
                            throw new RuntimeException("Unable to write \'" + property + "\'", e);
                        }
                    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                        // Ignore the field.
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Unable to write \'" + tag + "\'", e);
    }
    level++;
}

From source file:edu.utah.further.core.util.context.EnumAliasService.java

/**
 * @return/*w ww . j a v a  2 s  .c om*/
 */
@SuppressWarnings("unchecked")
private Set<Class<? extends Enum<?>>> initializeLabeledClasses() {
    final Set<Class<? extends Enum<?>>> set = CollectionUtil.newSet();
    for (final Class<?> annotatedClass : this.getObject()) {
        if (annotatedClass.isEnum() && ReflectionUtil.classIsInstanceOf(annotatedClass, Labeled.class)) {
            set.add((Class<? extends Enum<?>>) annotatedClass);
        }
    }
    return set;
}

From source file:com.ejisto.modules.factory.impl.EnumFactory.java

@Override
public Enum<T> create(MockedField m, Enum<T> actualValue) {
    try {/* w ww. j a v a  2 s.  c  o m*/
        String name = m.getFieldValue();
        if (StringUtils.isEmpty(name)) {
            return actualValue;
        }
        @SuppressWarnings("unchecked")
        Class<Enum<T>> clazz = (Class<Enum<T>>) Class.forName(m.getFieldType());
        if (!clazz.isEnum()) {
            return actualValue;
        }
        Enum<T>[] enums = clazz.getEnumConstants();
        for (Enum<T> en : enums) {
            if (en.name().equals(name)) {
                return en;
            }
        }
        if (actualValue != null) {
            return actualValue;
        }
        return enums.length > 0 ? enums[0] : null;
    } catch (Exception ex) {
        log.warn(String.format("enum value not found for %s.", m), ex);
    }
    return actualValue;
}

From source file:se.vgregion.portal.innovatinosslussen.domain.TypesBeanTest.java

void doGetterSetterValuesMatch(Class... typeInPackage)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    for (Class item : typeInPackage)
        for (Class type : getTypes(item)) {
            if (!type.isEnum()) {
                if (type.getDeclaredConstructors()[0].getModifiers() == Modifier.PUBLIC) {
                    doGetterSetterValuesMatch(type.newInstance());
                }/* w w  w.j a v a  2s. c  o m*/
            }
        }
}

From source file:eu.squadd.testing.objectspopulator.RandomValuePopulator.java

private boolean isSimpleType(final Class<?> fieldType) {
    return fieldType.isPrimitive() || fieldType.isEnum() || String.class.isAssignableFrom(fieldType)
            || Date.class.isAssignableFrom(fieldType);
}

From source file:edu.utah.further.core.util.context.EnumAliasService.java

/**
 * Set the static enum alias map field.//from  w w  w  .  j  a  v a2s . c  om
 * <p>
 * Warning: do not call this method outside this class. It is meant to be called by
 * Spring when instantiating the singleton instance of this bean.
 */
@SuppressWarnings("unchecked")
private BidiMap<String, Class<? extends Enum<?>>> initializeEnumAliases() {
    final Class<Alias> annotationClass = Alias.class;
    // TODO: move to CollectionUtil as a generic factory method
    final BidiMap<String, Class<? extends Enum<?>>> map = new DualHashBidiMap<>();
    for (final Class<?> annotatedClass : this.getObject()) {
        if (annotatedClass.isEnum()) {
            final String alias = annotatedClass.getAnnotation(annotationClass).value();
            final Class<? extends Enum<?>> otherClass = map.get(alias);
            if (otherClass != null) {
                // This is the second time that we encountered the alias
                // key, signal name collision
                final String errorMessage = "Enum alias collision!!! Both " + otherClass + ", " + annotatedClass
                        + " have the same alias: " + quote(alias) + ". Rename the @"
                        + annotationClass.getSimpleName() + " annotation value one of them.";
                // Log message before throwing, otherwise Spring will
                // show an incomprehensible TargetInvocationException
                // without its cause here.
                log.error(errorMessage);
                throw new ApplicationException(errorMessage);
            }
            map.put(alias, (Class<? extends Enum<?>>) annotatedClass);
        }
    }
    return map;
}

From source file:ca.oson.json.util.ObjectUtil.java

public static boolean isMapOrObject(Class valueType) {
    if (valueType == null) { // no idea, just assume
        return true;
    }/*ww  w .ja  v  a2  s  . c  o  m*/

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

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

    if (Number.class.isAssignableFrom(valueType) || Date.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:org.mule.module.wsdlproc.WSDLProcModule.java

public Object createArgumentObject(Map<String, Object> inputParams)
        throws InstantiationException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, SecurityException, NoSuchMethodException {
    Object inputObject = type.newInstance();

    BeanUtilsBean beanUtilsBean = new BeanUtilsBean(new ConvertUtilsBean() {//This is to make BeanUtils handle Enums
        @Override/*from w ww.  j a  v  a2 s. c om*/
        public Object convert(String value, Class clazz) {
            if (clazz.isEnum()) {
                return Enum.valueOf(clazz, camelCaseToEnumFormat(value));
            } else {
                return super.convert(value, clazz);
            }
        }
    });

    beanUtilsBean.populate(inputObject, inputParams);
    //BeanUtils.populateWithoutFail(inputObject, inputParams, false);
    return inputObject;

}