Example usage for java.lang Class getEnumConstants

List of usage examples for java.lang Class getEnumConstants

Introduction

In this page you can find the example usage for java.lang Class getEnumConstants.

Prototype

public T[] getEnumConstants() 

Source Link

Document

Returns the elements of this enum class or null if this Class object does not represent an enum type.

Usage

From source file:org.locationtech.udig.processingtoolbox.tools.AbstractGeoProcessingDialog.java

protected void setComboItems(Combo combo, Class<?> enumClass) {
    for (Object enumVal : enumClass.getEnumConstants()) {
        combo.add(enumVal.toString());//from www. j  av  a 2 s  .  c  o  m
    }
}

From source file:org.lightjason.trafficsimulation.simulation.stationary.trafficlight.IBaseTrafficLight.java

/**
 * ctor//  w  w  w. ja va 2s.c o m
 *
 * @param p_configuration agent configuration
 * @param p_environment environment reference
 * @param p_functor functor
 * @param p_light light class
 * @param p_position position
 * @param p_rotation rotation
 */
protected IBaseTrafficLight(final IAgentConfiguration<T> p_configuration, final IEnvironment p_environment,
        final String p_functor, final String p_name, final Class<L> p_light, final DoubleMatrix1D p_position,
        final int p_rotation) {
    super(p_configuration, p_environment, p_functor, p_name, p_position);
    m_rotation = p_rotation;
    m_color = new AtomicReference<L>(p_light.getEnumConstants()[0]);
}

From source file:org.apache.hadoop.mapreduce.counters.FrameworkCounterGroup.java

@SuppressWarnings("unchecked")
public FrameworkCounterGroup(Class<T> enumClass) {
    this.enumClass = enumClass;
    T[] enums = enumClass.getEnumConstants();
    counters = new Object[enums.length];
}

From source file:org.opensingular.form.STypeSimple.java

public <T extends Enum<T>> SType selectionOfEnum(Class<T> enumType) {
    this.selectionOf(Enum.class).id(Enum::name).display(Enum::toString).enumConverter(enumType)
            .simpleProvider(ins -> Arrays.asList(enumType.getEnumConstants()));
    return this;
}

From source file:org.jsonschema2pojo.integration.config.GsonIT.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test/*from   w w w .  j a v  a 2s  . c  o  m*/
public void enumValuesAreSerializedCorrectly()
        throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    ClassLoader resultsClassLoader = generateAndCompile("/schema/enum/typeWithEnumProperty.json", "com.example",
            config("annotationStyle", "gson", "propertyWordDelimiters", "_"));

    Class generatedType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty");
    Class enumType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty");
    Object instance = generatedType.newInstance();

    Method setter = generatedType.getMethod("setEnumProperty", enumType);
    setter.invoke(instance, enumType.getEnumConstants()[3]);

    String json = new Gson().toJson(instance);
    Map<String, String> jsonAsMap = new Gson().fromJson(json, Map.class);

    assertThat(jsonAsMap.get("enum_Property"), is("4 ! 1"));
}

From source file:de.unentscheidbar.validation.DefaultMessageTextGetterTest.java

@Test
public void testDefaultMessageTexts() {

    Locale[] locales = { Locale.ROOT, Locale.GERMAN };

    Reflections r = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage(ClassUtils.getPackageName(Validators.class)))
            .setScanners(new ResourcesScanner(), new SubTypesScanner(false)));

    Collection<Class<? extends Id>> defaultMessageClasses = Collections2.filter(
            r.getSubTypesOf(ValidationMessage.Id.class), Predicates.and(IS_NOT_ABSTRACT, IsTestClass.NO));
    Assert.assertTrue("Suspiciously few message ID classes found", defaultMessageClasses.size() > 10);

    MessageTextGetter mtg = Validation.defaultMessageTextGetter();

    for (Class<? extends Id> idClass : defaultMessageClasses) {
        /* only works with enums atm (all default message ids are enums) */
        if (!idClass.isEnum()) {
            Assert.fail("Cannot test " + idClass.getName());
        }/*from  ww w . ja v a 2s  . c  o m*/
        for (Id id : idClass.getEnumConstants()) {
            for (Locale locale : locales) {
                Assert.assertTrue("Missing default message text for " + locale + " -> "
                        + id.getClass().getName() + " -> " + id.name(), mtg.hasText(id, locale));
            }
        }
    }
}

From source file:com.facebook.stetho.json.ObjectMapper.java

/**
 * In this case we know that there is an {@link Enum} decorated with {@link JsonValue}. This means
 * that we need to iterate through all of the values of the {@link Enum} returned by the given
 * {@link Method} to check the given value.
 * @param value/*from w ww.jav  a 2s . c om*/
 * @param clazz
 * @param method
 * @return
 */
private Enum getEnumByMethod(String value, Class<? extends Enum> clazz, Method method) {
    Enum[] enumValues = clazz.getEnumConstants();
    // Start at the front to ensure first always wins
    for (int i = 0; i < enumValues.length; ++i) {
        Enum enumValue = enumValues[i];
        try {
            Object o = method.invoke(enumValue);
            if (o != null) {
                if (o.toString().equals(value)) {
                    return enumValue;
                }
            }
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    throw new IllegalArgumentException("No enum constant " + clazz.getName() + "." + value);
}

From source file:org.locationtech.udig.processingtoolbox.tools.AbstractGeoProcessingDialog.java

protected void fillEnum(Combo combo, Class<?> enumType) {
    combo.removeAll();// w w w . j  a va 2s . c o m
    for (Object enumVal : enumType.getEnumConstants()) {
        combo.add(enumVal.toString());
    }
    combo.select(0);
}

From source file:org.hdiv.web.servlet.tags.form.OptionsTagHDIV.java

@Override
protected int writeTagContent(TagWriter tagWriter) throws JspException {

    // make sure we are under a '<code>select</code>' tag before proceeding.
    assertUnderSelectTag();// w  w  w . j  av  a 2  s.c  o  m

    IDataComposer dataComposer = (IDataComposer) this.pageContext.getRequest()
            .getAttribute(org.hdiv.web.util.TagUtils.DATA_COMPOSER);
    SelectTagHDIV selectTag = (SelectTagHDIV) org.hdiv.web.util.TagUtils.getAncestorOfType(this,
            SelectTagHDIV.class);

    Object items = getItems();
    Object itemsObject = null;
    if (items != null) {
        itemsObject = (items instanceof String ? evaluate("items", (String) items) : items);
    } else {
        Class<?> selectTagBoundType = ((SelectTagHDIV) findAncestorWithClass(this, SelectTagHDIV.class))
                .getBindStatus().getValueType();
        if (selectTagBoundType != null && selectTagBoundType.isEnum()) {
            itemsObject = selectTagBoundType.getEnumConstants();
        }
    }

    if (itemsObject != null) {
        String itemValue = getItemValue();
        String itemLabel = getItemLabel();

        String valueProperty = (itemValue != null
                ? ObjectUtils.getDisplayString(evaluate("itemValue", itemValue))
                : null);
        String labelProperty = (itemLabel != null
                ? ObjectUtils.getDisplayString(evaluate("itemLabel", itemLabel))
                : null);

        OptionsWriter optionWriter = new OptionsWriter(dataComposer, selectTag.getName(), itemsObject,
                valueProperty, labelProperty);
        optionWriter.writeOptions(tagWriter);
    }

    return SKIP_BODY;
}

From source file:org.jsonschema2pojo.integration.config.Moshi1IT.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test// w ww .  j  av  a  2s. c om
public void enumValuesAreSerializedCorrectly()
        throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/typeWithEnumProperty.json",
            "com.example", config("annotationStyle", "moshi1", "propertyWordDelimiters", "_"));

    Class generatedType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty");
    Class enumType = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty");
    Object instance = generatedType.newInstance();

    Method setter = generatedType.getMethod("setEnumProperty", enumType);
    setter.invoke(instance, enumType.getEnumConstants()[3]);

    JsonAdapter jsonAdapter = moshi.adapter(generatedType);
    String json = jsonAdapter.toJson(instance);

    Map<String, String> jsonAsMap = new Gson().fromJson(json, Map.class);
    assertThat(jsonAsMap.get("enum_Property"), is("4 ! 1"));
}