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.lunarray.model.descriptor.converter.def.DelegatingEnumConverterTool.java

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
// We're checking after the fact.
@Override//from  w  w w  .j  a v  a  2  s  . c o m
public <T> String convertToString(final Class<T> type, final T instance) throws ConverterException {
    Validate.notNull(type, DelegatingEnumConverterTool.TYPE_NULL);
    String result = null;
    if (type.isEnum()) {
        DelegatingEnumConverterTool.LOGGER.debug("Converting to string with type {} with value: {}", type,
                instance);
        @SuppressWarnings("rawtypes")
        // No other way?
        final Class<? extends Enum> enumType = (Class<? extends Enum>) type;
        final Enum<?> enumInstance = enumType.cast(instance);
        if (!CheckUtil.isNull(enumInstance)) {
            result = enumInstance.name();
        }
    } else {
        result = this.converterTool.convertToString(type, instance);
    }
    return result;
}

From source file:com.streamsets.pipeline.maven.rbgen.RBGenMojo.java

@SuppressWarnings("unchecked")
private LinkedHashMap<String, String> extractResources(Class klass) throws Exception {
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    if (klass.isEnum()) {
        Object[] enums = klass.getEnumConstants();
        for (Object e : enums) {
            String name = ((Enum) e).name();
            String text = name;/*  w w w  .j  a  va  2  s  . c om*/
            if (errorCodeClass.isAssignableFrom(klass)) {
                text = invokeMessageMethod(errorCodeClass, "getMessage", e);
            }
            if (labelClass.isAssignableFrom(klass)) {
                text = invokeMessageMethod(labelClass, "getLabel", e);
            }
            map.put(name, text);
        }
    } else if (stageClass.isAssignableFrom(klass)) {
        Annotation stageDef = klass.getAnnotation(stageDefClass);
        if (stageDef != null) {
            String labelText = invokeMessageMethod(stageDefClass, "label", stageDef);
            map.put("stageLabel", labelText);
            String descriptionText = invokeMessageMethod(stageDefClass, "description", stageDef);
            map.put("stageDescription", descriptionText);
            Annotation errorStage = klass.getAnnotation(errorStageClass);
            if (errorStage != null) {
                String errorLabelText = invokeMessageMethod(errorStageClass, "label", errorStage);
                if (errorLabelText.isEmpty()) {
                    errorLabelText = labelText;
                }
                map.put("errorStageLabel", errorLabelText);
                String errorDescriptionText = invokeMessageMethod(errorStageClass, "description", errorStage);
                if (!errorDescriptionText.isEmpty()) {
                    errorDescriptionText = descriptionText;
                }
                map.put("errorStageDescription", errorDescriptionText);

            }
        }
        for (Field field : klass.getFields()) {
            Annotation configDef = field.getAnnotation(configDefClass);
            if (configDef != null) {
                String labelText = invokeMessageMethod(configDefClass, "label", configDef);
                map.put("configLabel." + field.getName(), labelText);
                String descriptionText = invokeMessageMethod(configDefClass, "description", configDef);
                map.put("configDescription." + field.getName(), descriptionText);
            }
        }

    }
    return map;
}

From source file:com.tera.common.configuration.converter.FieldConverters.java

@Override
public FieldConverter getConverterFor(Class<?> clazz) {
    for (FieldConverters fConverter : FieldConverters.values()) {
        if (fConverter.matches(clazz)) {
            return fConverter;
        }//  w  w  w. j  a  v  a  2s . co  m
    }
    if (clazz.isEnum()) {
        return ENUM;
    }
    throw new NotImplementedException("No implementation available for converting: " + clazz.getName());
}

From source file:org.lunarray.model.descriptor.converter.def.DelegatingEnumConverterTool.java

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
// We're checking after the fact.
@Override/*from   w w w .  j ava2s. c o m*/
public <T> T convertToInstance(final Class<T> type, final String stringValue) throws ConverterException {
    Validate.notNull(type, DelegatingEnumConverterTool.TYPE_NULL);
    T result = null;
    if (type.isEnum()) {
        DelegatingEnumConverterTool.LOGGER.debug("Converting to instance type {} with value: {}", type,
                stringValue);
        @SuppressWarnings("rawtypes")
        // No other way?
        final Class<? extends Enum> enumType = (Class<? extends Enum>) type;
        if (!StringUtil.isEmptyString(stringValue)) {
            result = (T) Enum.valueOf(enumType, stringValue);
        }
    } else {
        result = this.converterTool.convertToInstance(type, stringValue);
    }
    return result;
}

From source file:com.github.alexfalappa.nbspringboot.cfgprops.highlighting.DataTypeMismatchHighlightingTask.java

private boolean checkType(String type, String text, ClassLoader cl) throws IllegalArgumentException {
    Class<?> clazz = ClassUtils.resolveClassName(type, cl);
    if (clazz != null) {
        try {/*from  w ww  .java 2 s . co m*/
            Object parsed = parser.parseType(text, clazz);
        } catch (Exception e1) {
            if (clazz.isEnum()) {
                try {
                    Object parsed = parser.parseType(text.toUpperCase(), clazz);
                } catch (Exception e2) {
                    return false;
                }
            } else {
                return false;
            }
        }
    }
    return true;
}

From source file:com.liferay.portal.jsonwebservice.JSONWebServiceConfigurator.java

private boolean _isJSONWebServiceClass(Class<?> clazz) {
    if (!clazz.isAnonymousClass() && !clazz.isArray() && !clazz.isEnum() && !clazz.isLocalClass()
            && !clazz.isPrimitive() && !(clazz.isMemberClass() ^ Modifier.isStatic(clazz.getModifiers()))) {

        return true;
    }//from ww  w . j a v  a 2s.c o  m

    return false;
}

From source file:org.rhq.enterprise.gui.legacy.taglib.ConstantsTag.java

public int doEndTag() throws JspException {
    try {//from www .j a v a 2s.com
        JspWriter out = pageContext.getOut();
        if (className == null) {
            className = pageContext.getServletContext().getInitParameter(constantsClassNameParam);
        }

        if (validate(out)) {
            // we're misconfigured. getting this far
            // is a matter of what our failure mode is;
            // if we haven't thrown an Error, carry on
            log.debug("constants tag misconfigured");
            return EVAL_PAGE;
        }

        Map<String, String> fieldMap;
        if (constants.containsKey(className)) {
            // we cache the result of the constant's class
            // reflection field walk as a map
            fieldMap = (Map<String, String>) constants.get(className);
        } else {
            fieldMap = new HashMap<String, String>();
            Class typeClass = Class.forName(className);
            if (typeClass.isEnum()) {
                for (Object enumConstantObj : typeClass.getEnumConstants()) {
                    Enum enumConstant = (Enum) enumConstantObj;

                    // Set name *and* value to enum name (e.g. name of ResourceCategory.PLATFORM = "PLATFORM")
                    // NOTE: We do not set the value to enumConstant.ordinal(), because there is no way to
                    // convert the ordinal value back to an Enum (i.e. no Enum.valueOf(int ordinal) method).
                    fieldMap.put(enumConstant.name(), enumConstant.name());
                }
            } else {
                Object instance = typeClass.newInstance();
                Field[] fields = typeClass.getFields();
                for (Field field : fields) {
                    // string comparisons of class names should be cheaper
                    // than reflective Class comparisons, the asumption here
                    // is that most constants are Strings, ints and booleans
                    // but a minimal effort is made to accomadate all types
                    // and represent them as String's for our tag's output
                    String fieldType = field.getType().getName();
                    String strVal;
                    if (fieldType.equals("java.lang.String")) {
                        strVal = (String) field.get(instance);
                    } else if (fieldType.equals("int")) {
                        strVal = Integer.toString(field.getInt(instance));
                    } else if (fieldType.equals("boolean")) {
                        strVal = Boolean.toString(field.getBoolean(instance));
                    } else if (fieldType.equals("char")) {
                        strVal = Character.toString(field.getChar(instance));
                    } else if (fieldType.equals("double")) {
                        strVal = Double.toString(field.getDouble(instance));
                    } else if (fieldType.equals("float")) {
                        strVal = Float.toString(field.getFloat(instance));
                    } else if (fieldType.equals("long")) {
                        strVal = Long.toString(field.getLong(instance));
                    } else if (fieldType.equals("short")) {
                        strVal = Short.toString(field.getShort(instance));
                    } else if (fieldType.equals("byte")) {
                        strVal = Byte.toString(field.getByte(instance));
                    } else {
                        strVal = field.get(instance).toString();
                    }

                    fieldMap.put(field.getName(), strVal);
                }
            }

            // cache the result
            constants.put(className, fieldMap);
        }

        if ((symbol != null) && !fieldMap.containsKey(symbol)) {
            // tell the developer that he's being a dummy and what
            // might be done to remedy the situation
            // TODO: what happens if the constants change?
            // do we need to throw a JspException, here? - mtk
            String err1 = symbol + " was not found in " + className + "\n";
            String err2 = err1 + "use <constants:diag classname=\"" + className + "\"/>\n"
                    + "to figure out what you're looking for";
            log.error(err2);
            die(out, err1);
        }

        if (varSpecified) {
            doSet(fieldMap);
        } else {
            doOutput(fieldMap, out);
        }
    } catch (JspException e) {
        throw e;
    } catch (Exception e) {
        log.debug("doEndTag() failed: ", e);
        throw new JspException("Could not access constants tag", e);
    }

    return EVAL_PAGE;
}

From source file:net.ymate.platform.plugin.impl.DefaultPluginFactory.java

@SuppressWarnings("unchecked")
private List<Class<? extends IBeanHandler>> __doLoadBeanHandles() throws Exception {
    List<Class<? extends IBeanHandler>> _returnValues = new ArrayList<Class<? extends IBeanHandler>>();
    IBeanLoader _loader = new DefaultBeanLoader();
    ///* w w  w  . j  a v a  2 s  .c  o  m*/
    IBeanFilter _beanFilter = new IBeanFilter() {
        public boolean filter(Class<?> targetClass) {
            return !(targetClass.isInterface() || targetClass.isAnnotation() || targetClass.isEnum())
                    && (targetClass.isAnnotationPresent(Handler.class)
                            && ClassUtils.isInterfaceOf(targetClass, IBeanHandler.class));
        }
    };
    //
    for (String _package : __config.getAutoscanPackages()) {
        for (Class<?> _targetClass : _loader.load(_package, _beanFilter)) {
            _returnValues.add((Class<? extends IBeanHandler>) _targetClass);
        }
    }
    return _returnValues;
}

From source file:name.ikysil.beanpathdsl.codegen.Context.java

private boolean isExcluded(Class<?> clazz) {
    if (clazz.isPrimitive() || clazz.isAnonymousClass() || clazz.isLocalClass() || clazz.isInterface()
            || clazz.isSynthetic() || clazz.isEnum() || !Modifier.isPublic(clazz.getModifiers())
            || (clazz.getPackage() == null)) {
        return true;
    }/*from   w w  w .j  a v  a  2 s  .  com*/
    IncludedClass includedConfig = includedClasses.get(clazz);
    if (includedConfig != null) {
        return false;
    }
    ExcludedClass excludedConfig = excludedClasses.get(clazz);
    if (excludedConfig != null) {
        return true;
    }
    for (ExcludedPackage excludedPackage : excludedPackages) {
        if (excludedPackage.match(clazz.getPackage())) {
            return true;
        }
    }
    return false;
}

From source file:com.github.dozermapper.core.converters.PrimitiveOrWrapperConverter.java

public boolean accepts(Class<?> aClass) {
    return aClass.isPrimitive() || Number.class.isAssignableFrom(aClass) || String.class.equals(aClass)
            || Character.class.equals(aClass) || Boolean.class.equals(aClass)
            || java.util.Date.class.isAssignableFrom(aClass)
            || java.util.Calendar.class.isAssignableFrom(aClass) || aClass.isEnum();
}