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:com.sparkplatform.api.core.PropertyAsserter.java

/**
 * See {@link #assertBasicGetterSetterBehavior(Object,String)} method. Only difference is that here we accept an
 * explicit argument for the setter method.
 *
 * @param target   the object on which to invoke the getter and setter
 * @param property the property name, e.g. "firstName"
 * @param argument the property value, i.e. the value the setter will be invoked with
 *//*from   w w w  .  j a  v a 2s.  c  o  m*/
public static void assertBasicGetterSetterBehavior(Object target, String property, Object argument) {
    try {
        PropertyDescriptor descriptor = new PropertyDescriptor(property, target.getClass());
        Object arg = argument;
        Class type = descriptor.getPropertyType();
        if (arg == null) {
            if (type.isArray()) {
                arg = Array.newInstance(type.getComponentType(), new int[] { TEST_ARRAY_SIZE });
            } else if (type.isEnum()) {
                arg = type.getEnumConstants()[0];
            } else if (TYPE_ARGUMENTS.containsKey(type)) {
                arg = TYPE_ARGUMENTS.get(type);
            } else {
                arg = invokeDefaultConstructorEvenIfPrivate(type);
            }
        }

        Method writeMethod = descriptor.getWriteMethod();
        Method readMethod = descriptor.getReadMethod();

        writeMethod.invoke(target, arg);
        Object propertyValue = readMethod.invoke(target);
        if (type.isPrimitive()) {
            assertEquals(property + " getter/setter failed test", arg, propertyValue);
        } else {
            assertSame(property + " getter/setter failed test", arg, propertyValue);
        }
    } catch (IntrospectionException e) {
        String msg = "Error creating PropertyDescriptor for property [" + property
                + "]. Do you have a getter and a setter?";
        log.error(msg, e);
        fail(msg);
    } catch (IllegalAccessException e) {
        String msg = "Error accessing property. Are the getter and setter both accessible?";
        log.error(msg, e);
        fail(msg);
    } catch (InvocationTargetException e) {
        String msg = "Error invoking method on target";
        log.error(msg, e);
        fail(msg);
    }
}

From source file:org.andromda.presentation.gui.JsfUtils.java

/**
 * Returns the messages.properties message of the enumeration value
 * @param prefix a String prefix to be used to load the name from the messages
 * @param enumValue the value// w  w  w.  j  a  v a  2  s .com
 * @return the String from the messages.properties
 */
@SuppressWarnings("rawtypes")
public static String getEnumMessage(final String prefix, final Object enumValue) {
    if (enumValue == null) {
        return StringUtils.EMPTY;
    }
    final Class<?> enumClass = enumValue.getClass();
    if (enumClass.isEnum()) {
        return Messages.get(prefix + ((Enum) enumValue).name());
    }
    try {
        final List values = (List) enumClass.getMethod("values", (Class<?>[]) null).invoke(null,
                (Object[]) null);
        final int sz = values.size();
        final List names = (List) enumClass.getMethod("names", (Class<?>[]) null).invoke(null, (Object[]) null);
        for (int i = 0; i < sz; i++) {
            if (values.get(i).equals(enumValue)) {
                return Messages.get(prefix + names.get(i));
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(
                enumValue.getClass().getCanonicalName() + " is not an Andromda generated enumeration.", e);
    }
    return null;
}

From source file:hu.javaforum.commons.ReflectionHelper.java

/**
 * Converts the string value to instance of the field class.
 *
 * @param fieldClass The field class/*w  ww . jav  a 2s . c o  m*/
 * @param stringValue The string value
 * @return The instance of the field class
 * @throws ParseException Throws when the string isn't parseable
 * @throws UnsupportedEncodingException If the Base64 stream contains non UTF-8 chars
 */
private static Object convertToOthers(final Class fieldClass, final String stringValue)
        throws ParseException, UnsupportedEncodingException {
    Object parameter = null;

    if (fieldClass.isEnum()) {
        parameter = Enum.valueOf((Class<Enum>) fieldClass, stringValue);
    } else if (fieldClass.equals(byte[].class)) {
        parameter = Base64.decodeBase64(stringValue.getBytes("UTF-8"));
    } else if (fieldClass.equals(Date.class)) {
        parameter = stringToDate(stringValue);
    } else if (fieldClass.equals(Calendar.class)) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringToDate(stringValue));
        parameter = cal;
    }

    return parameter;
}

From source file:com.mawujun.util.AnnotationUtils.java

/**
 * <p>Checks if the specified type is permitted as an annotation member.</p>
 *
 * <p>The Java language specification only permits certain types to be used
 * in annotations. These include {@link String}, {@link Class}, primitive
 * types, {@link Annotation}, {@link Enum}, and single-dimensional arrays of
 * these types.</p>// w w  w  .j a  v a2  s .c o  m
 *
 * @param type the type to check, {@code null}
 * @return {@code true} if the type is a valid type to use in an annotation
 */
public static boolean isValidAnnotationMemberType(Class<?> type) {
    if (type == null) {
        return false;
    }
    if (type.isArray()) {
        type = type.getComponentType();
    }
    return type.isPrimitive() || type.isEnum() || type.isAnnotation() || String.class.equals(type)
            || Class.class.equals(type);
}

From source file:net.servicefixture.util.ReflectionUtils.java

public static boolean isLowestLevelType(Class type) {
    type = CollectionBuilderFactory.getElementType(type);
    if (type.isPrimitive() || type.getName().startsWith("java.") || type.getName().startsWith("javax.")
            || isEnumarationPatternClass(type) || type.isEnum()) {
        return true;
    }//  www  . j  av a  2s  . com
    return false;
}

From source file:cop.raml.mocks.MockUtils.java

private static TypeElementMock createArrayElement(@NotNull Class<?> cls) throws ClassNotFoundException {
    TypeElementMock element = new TypeElementMock("Array", ElementKind.CLASS);
    TypeMirrorMock type = new TypeMirrorMock(element, TypeKind.ARRAY);

    if (cls.isPrimitive())
        type.setElementType(createPrimitiveElement(cls).asType());
    else if (cls.isEnum())
        type.setElementType(createEnumElement(cls).asType());
    else//from   ww  w  .ja v a 2 s . c o m
        type.setElementType(createClassElement(cls).asType());

    element.setType(type);

    return element;
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

public static boolean isEnum(Class<?> type) {
    return type.isEnum();
}

From source file:org.andromda.presentation.gui.JsfUtils.java

/**
 * Returns an array of SelectItem from the values/names of the enumeration
 * @param prefix a String prefix to be used to load the name from the messages
 * @param enumClassName the enumeration class name
 * @return the array of SelectItem//from  www . j  a  va2  s  .co m
 */
@SuppressWarnings("rawtypes")
public static SelectItem[] getEnumSelectItems(final String prefix, final String enumClassName) {
    try {
        final SelectItem[] result;
        final Class<?> enumClass = JsfUtils.class.getClassLoader().loadClass(enumClassName);
        if (enumClass.isEnum()) {
            final Enum[] values = (Enum[]) enumClass.getMethod("values", (Class<?>[]) null).invoke(null,
                    (Object[]) null);
            result = new SelectItem[values.length];
            int i = 0;
            for (final Enum value : values) {
                result[i] = new SelectItem(value, Messages.get(prefix + value.name()));
                i++;
            }
        } else {
            final List values = (List) enumClass.getMethod("values", (Class<?>[]) null).invoke(null,
                    (Object[]) null);
            final int sz = values.size();
            final List names = (List) enumClass.getMethod("names", (Class<?>[]) null).invoke(null,
                    (Object[]) null);
            result = new SelectItem[sz];
            for (int i = 0; i < sz; i++) {
                result[i] = new SelectItem(values.get(i), Messages.get(prefix + names.get(i)));
            }
        }

        return result;
    } catch (Exception e) {
        throw new RuntimeException(enumClassName + " is not an Andromda generated enumeration.", e);
    }
}

From source file:com.github.reinert.jjschema.v1.SchemaWrapperFactory.java

public static SchemaWrapper createWrapper(Class<?> type, Set<ManagedReference> managedReferences,
        String relativeId) {/*  www  .  j ava 2s.  c  o m*/
    // If it is void then return null
    if (type == Void.class || type == void.class || type == null) {
        return new NullSchemaWrapper(type);
    }
    // If it is a simple type, then just put the type
    else if (SimpleTypeMappings.isSimpleType(type)) {
        return new SimpleSchemaWrapper(type);
    }
    // If it is an Enum than process like enum
    else if (type.isEnum()) {
        return new EnumSchemaWrapper(type);
    }
    // If none of the above possibilities were true, then it is a custom object
    else {
        if (managedReferences != null)
            if (relativeId != null)
                return new CustomSchemaWrapper(type, managedReferences, relativeId);
            else
                return new CustomSchemaWrapper(type, managedReferences);
        else
            return new CustomSchemaWrapper(type);
    }
}

From source file:io.apiman.manager.api.es.EsMarshallingTest.java

/**
 * Populate the given set with one or two items of the given type.
 * @param collection// w w w.jav a  2  s  . co  m
 * @param typeClass
 */
private static void populateSet(Set collection, Class<?> typeClass)
        throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException,
        SecurityException, ClassNotFoundException {
    if (typeClass.isEnum()) {
        collection.add(typeClass.getEnumConstants()[0]);
        collection.add(typeClass.getEnumConstants()[1]);
    } else if (typeClass == String.class) {
        collection.add("VALUE_1");
        collection.add("VALUE_2");
    } else if (typeClass.getPackage().getName().startsWith("io.apiman.manager.api.beans")) {
        Object bean1 = createBean(typeClass);
        Object bean2 = createBean(typeClass);
        collection.add(bean1);
        collection.add(bean2);
    } else {
        throw new IllegalAccessException("Failed to populate Set of type: " + typeClass.getSimpleName());
    }
}