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:net.servicefixture.util.ReflectionUtils.java

public static String getDefaultValue(Class type) {
    if (isEnumarationPatternClass(type)) {
        StringBuilder sb = new StringBuilder();
        Field[] fields = type.getFields();
        for (int i = 0; i < fields.length; i++) {
            if (type.equals(fields[i].getType()) && Modifier.isFinal(fields[i].getModifiers())
                    && Modifier.isPublic(fields[i].getModifiers())
                    && Modifier.isStatic(fields[i].getModifiers())) {
                try {
                    sb.append(fields[i].getName()).append(";");
                } catch (IllegalArgumentException e) {
                }//from   w  w w  .j a v  a  2s .co  m
            }
        }
        return sb.toString();
    } else if (type.isEnum()) {
        StringBuilder sb = new StringBuilder();
        try {
            Object[] array = (Object[]) type.getMethod("values").invoke(null);
            for (int i = 0; i < array.length; i++) {
                sb.append(array[i]).append(";");
            }
        } catch (Exception e) {
        }
        return sb.toString();

    } else if (Calendar.class.isAssignableFrom(type) || XMLGregorianCalendar.class.isAssignableFrom(type)) {
        return "${calendar.after(0)}";
    } else if (Date.class.isAssignableFrom(type)) {
        return "${date.after(0)}";
    } else if (Number.class.isAssignableFrom(type) || "long".equals(type.getName())
            || "int".equals(type.getName()) || "short".equals(type.getName()) || "float".equals(type.getName())
            || "double".equals(type.getName())) {
        return "0";
    } else if (String.class.equals(type)) {
        return "String";
    } else if (Boolean.class.equals(type) || "boolean".equals(type.getName())) {
        return "true";
    }

    return type.getName();
}

From source file:com.tacitknowledge.noexcuses.EnumTypeHandler.java

@Override
@SuppressWarnings("unchecked")
public <T> T createInstance(Class<T> type) {
    T instance = null;/*from w  w w .j  a va 2 s .  c  om*/
    if (type.isEnum()) {
        Object[] enums = type.getEnumConstants();
        if (ArrayUtils.isNotEmpty(enums)) {
            instance = (T) enums[0];
        }
    }
    return instance;
}

From source file:net.erdfelt.android.sdkfido.configer.EnumConverter.java

@SuppressWarnings("rawtypes")
public Object convert(Class type, Object value) {
    if (type.isEnum()) {
        String key = String.valueOf(value).toUpperCase();
        return values.get(key);
    } else {//from ww  w .j  a va  2  s  .co m
        throw new ConversionException("Not an Enum class");
    }
}

From source file:pt.ua.tm.neji.context.ContextConfiguration.java

private static Module invokeModuleWithParams(final String moduleName,
        final Collection<Pair<String>> moduleParams, final Parser parser) throws ReflectiveOperationException {

    List<Class<?>> classes = new ArrayList<>();
    List<Object> objects = new ArrayList<>();

    for (Pair<String> pair : moduleParams) {

        Class paramClass = Class.forName(pair.getX());

        if (paramClass == Parser.class) {
            if (parser == null) {
                logger.warn(/*from w  w  w.  j a  v  a2 s.c o m*/
                        "Module that required a parser in constructor was skipped during ContextDescriptor fetching: no parser was provided.");
                continue;
            } else {
                objects.add(parser);
            }

        } else if (paramClass.isEnum()) {
            Enum paramEnum = Enum.valueOf(paramClass, pair.getY());
            objects.add(paramEnum);

        } else {
            objects.add(ConstructorUtils.invokeConstructor(paramClass, pair.getY()));

            if (Primitives.isWrapperType(paramClass)) {
                paramClass = Primitives.unwrap(paramClass);
            }
        }
        classes.add(paramClass);
    }

    return (Module) ConstructorUtils.invokeConstructor(Class.forName(moduleName), objects.toArray(),
            classes.toArray(new Class[classes.size()]));
}

From source file:org.bitsofinfo.util.address.usps.ais.loader.GenericEnumConverter.java

@Override
public Object convert(Class clazz, Object value) {

    if (clazz.isEnum() && value instanceof String) {
        String toParse = (String) value;
        if (toParse != null && toParse.trim().length() > 0) {
            try {
                return Enum.valueOf(clazz, toParse);
            } catch (Exception e) {
                System.out.println("error parsing..." + toParse);
                return null;

            }/*w w w.java2  s. c  om*/
        }
    }

    return null;
}

From source file:com.formkiq.core.form.bean.CustomConvertUtilsBean.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from w  w  w.j a  va2 s .co m*/
public Object convert(final String value, final Class clazz) {
    if (clazz.isEnum()) {

        String val = Strings.extractLabelAndValue(value).getRight();
        try {
            Method method = clazz.getMethod("find", String.class);
            Object result = method.invoke(method, val);
            if (result instanceof Optional) {
                Optional<Object> op = (Optional<Object>) result;
                if (op.isPresent()) {
                    return op.get();
                }
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            // ignore error
        }

        return Enum.valueOf(clazz, val);
    }

    return super.convert(value, clazz);
}

From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java

public static boolean isSimpleType(Type type) {
    Class<?> cl = toClass(type);
    return (String.class.equals(cl) || cl.isPrimitive() || Boolean.class.equals(cl)
            || Number.class.isAssignableFrom(cl) || cl.isEnum());
}

From source file:com.vmware.photon.controller.deployer.helpers.TestHelper.java

public static Object[][] getInvalidStartStages(@Nullable Class<? extends Enum> subStages) {

    if (subStages == null) {

        ///*from   w  w  w. ja  v a  2 s  . c o m*/
        // N.B. Tasks without defined sub-stages must accept all default start stages.
        //

        throw new IllegalArgumentException("subStages");
    }

    if (!subStages.isEnum() || subStages.getEnumConstants().length == 0) {
        throw new IllegalStateException("Class " + subStages.getName() + " is not a valid enum");
    }

    Enum[] enumConstants = subStages.getEnumConstants();
    List<Object[]> invalidStartStages = new ArrayList<>();
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.CREATED, enumConstants[i] });
    }
    invalidStartStages.add(new Object[] { TaskState.TaskStage.STARTED, null });
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.FINISHED, enumConstants[i] });
    }
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.FAILED, enumConstants[i] });
    }
    for (int i = 0; i < enumConstants.length; i++) {
        invalidStartStages.add(new Object[] { TaskState.TaskStage.CANCELLED, enumConstants[i] });
    }

    Object[][] returnValue = new Object[invalidStartStages.size()][2];
    for (int i = 0; i < invalidStartStages.size(); i++) {
        returnValue[i][0] = invalidStartStages.get(i)[0];
        returnValue[i][1] = invalidStartStages.get(i)[1];
    }

    return returnValue;
}

From source file:org.apache.bval.jsr.util.EnumerationConverter.java

/**
 * {@inheritDoc}/*from   www .ja va  2 s .c  o m*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object convert(Class type, Object value) {
    if (!type.isEnum()) {
        throw new RuntimeException("Only enum types supported in this version!");
    }

    if (value == null) {
        throw new RuntimeException("Null values not supported in this version!");
    }

    if (String.class != value.getClass()) {
        throw new RuntimeException("Only java.lang.String values supported in this version!");
    }

    String stringValue = (String) value;

    final Class<Enum> enumClass = (Class<Enum>) type;
    return Enum.valueOf(enumClass, stringValue);
}

From source file:cn.lambdalib.s11n.SerializationHelper.java

public boolean isS11nType(Class type) {
    return type.isEnum() || type.isAnnotationPresent(SerializeType.class) || serializeTypes.contains(type)
            || serializeTypes.stream().anyMatch(c -> c.isAssignableFrom(type));
}