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.obiba.magma.beans.BeanVariableValueSourceFactory.java

protected Variable doBuildVariable(Class<?> propertyType, String name) {
    ValueType type = ValueType.Factory.forClass(propertyType);

    Variable.Builder builder = Variable.Builder.newVariable(name, type, entityType);
    if (propertyType.isEnum()) {
        Enum<?>[] constants = (Enum<?>[]) propertyType.getEnumConstants();
        String[] names = Iterables.toArray(
                Iterables.transform(Arrays.asList(constants), Functions.toStringFunction()), String.class);
        builder.addCategories(names);// w w w . jav  a2  s .  c  om
    }

    if (occurrenceGroup != null) {
        builder.repeatable().occurrenceGroup(occurrenceGroup);
    }

    builder.accept(variableBuilderVisitors);

    // Allow extended classes to contribute to the builder
    return buildVariable(builder).build();
}

From source file:org.jsonschema2pojo.integration.EnumIT.java

@Test
@SuppressWarnings("unchecked")
public void enumWithNullValue() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/enumWithNullValue.json",
            "com.example");

    Class<Enum> nullEnumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.EnumWithNullValue");

    assertThat(nullEnumClass.isEnum(), is(true));
    assertThat(nullEnumClass.getEnumConstants().length, is(1));

}

From source file:org.jsonschema2pojo.integration.EnumIT.java

@Test
@SuppressWarnings("unchecked")
public void enumWithEmptyStringAsValue() throws ClassNotFoundException, NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/enum/enumWithEmptyString.json",
            "com.example");

    Class<Enum> emptyEnumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.EnumWithEmptyString");

    assertThat(emptyEnumClass.isEnum(), is(true));
    assertThat(emptyEnumClass.getEnumConstants()[0].name(), is("__EMPTY__"));

}

From source file:com.basistech.rosette.apimodel.ModelTest.java

private Object createObject(Class clazz)
        throws IllegalAccessException, InstantiationException, InvocationTargetException {
    if (Enum.class.isAssignableFrom(clazz)) {
        // pick a value, any value.
        return clazz.getEnumConstants()[0];
    } else {//from   ww w  .  j  av a2 s . c om
        Constructor<?>[] ctors = clazz.newInstance().getClass().getDeclaredConstructors();

        Object o = null;
        for (Constructor ctor : ctors) {
            if (ctor.getGenericParameterTypes().length == 1) {
                Object objectOfType = createObjectOfType(ctor.getGenericParameterTypes()[0]);
                if (objectOfType != null) {
                    o = ctor.newInstance(objectOfType);
                    break;
                }
            }
        }
        return o;
    }
}

From source file:com.mstiles92.plugins.stileslib.config.ConfigObject.java

@SuppressWarnings("rawtypes")
protected boolean isEnum(Class clazz, Object obj) {
    if (!clazz.isEnum())
        return false;
    for (Object constant : clazz.getEnumConstants()) {
        if (constant.equals(obj)) {
            return true;
        }//from  ww  w .j av a  2  s.  com
    }
    return false;
}

From source file:de.pixida.logtest.automatondefinitions.JsonAutomatonDefinition.java

private <E extends Enum<E>> E loadOptEnumAttributeFromJsonObject(final JSONObject jsonObject,
        final Class<E> enumClz, final JsonKey key) {
    final String value = this.loadOptStringAttributeFromJsonObject(jsonObject, key);
    if (value == null) {
        return null;
    }/*w  w w. j  a va 2s. c o  m*/
    final String valueLc = value.toLowerCase(Locale.US);
    return Arrays.stream(enumClz.getEnumConstants())
            .filter(enumConstant -> valueLc.equals(enumConstant.toString().toLowerCase(Locale.US))).findFirst()
            .orElseThrow(
                    () -> new AutomatonLoadingException("Invalid value for required conditions: " + value));
}

From source file:org.seedstack.seed.core.internal.application.ConfigurationMembersInjector.java

private Enum<? extends ErrorCode> findEnum() {
    Class<? extends Enum<? extends ErrorCode>> errorCodeClass = annotation.errorCodeClass();
    if (errorCodeClass.equals(org.seedstack.seed.core.api.Configuration.ConfigurationErrorCode.class)) {
        errorCodeClass = ApplicationErrorCode.class;
    }/*from  www  .j  ava 2s .c o m*/

    for (Enum<? extends ErrorCode> enumElement : errorCodeClass.getEnumConstants()) {
        if (enumElement.name().equals(annotation.errorCodeName())) {
            return enumElement;
        }
    }

    return null;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopOptionsGroup.java

@Override
public void setOptionsEnum(Class<? extends EnumClass> optionsEnum) {
    if (optionsInitialized)
        return;/*from www .  j  a va2  s.c  om*/

    super.setOptionsEnum(optionsEnum);
    if (optionsEnum != null) {
        List options = Arrays.asList(optionsEnum.getEnumConstants());
        for (Object obj : options) {
            addItem(new ObjectWrapper(obj));
        }

        if ((datasource != null) && (datasource.getState() == Datasource.State.VALID)) {
            Entity newValue = datasource.getItem();
            updateComponent(newValue);
            fireChangeListeners(newValue);
        }
        optionsInitialized = true;
    }
}

From source file:org.eclipse.smarthome.binding.homematic.internal.communicator.virtual.DisplayTextVirtualDatapoint.java

/**
 * Adds a Datapoint to the device with the values of the given enum.
 *//*www  .  j  ava 2s.com*/
private void addEnumDisplayDatapoint(HmDevice device, int channelNumber, String datapointName,
        Class<? extends Enum<?>> e) {
    HmDatapoint dpEnum = addDatapoint(device, channelNumber, datapointName, HmValueType.ENUM, null, false);
    dpEnum.setOptions(getEnumNames(e));
    dpEnum.setMinValue(0);
    dpEnum.setMaxValue(e.getEnumConstants().length);
}

From source file:org.apache.hyracks.control.common.config.ConfigManager.java

@Override
@SafeVarargs/*from   w w w.  ja  v a  2 s. co  m*/
public final void register(final Class<? extends IOption>... optionClasses) {
    for (Class<? extends IOption> optionClass : optionClasses) {
        register(optionClass.getEnumConstants());
    }
}