Example usage for java.lang Enum getClass

List of usage examples for java.lang Enum getClass

Introduction

In this page you can find the example usage for java.lang Enum getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.joyent.manta.util.MantaUtils.java

/**
 * Serializes a specified value to a {@link String}.
 * @param value the value to be serialized
 * @return a serialized value as a {@link String}
 *///from  w ww.j a  v a  2  s.c om
public static String asString(final Object value) {
    if (value == null) {
        return null;
    } else if (value instanceof Enum<?>) {
        Enum<?> enumValue = (Enum<?>) value;

        try {
            /* In this case, we actually want the subclass of the enum type
             * because we are trying to read a property from it via
             * reflection. */
            @SuppressWarnings("GetClassOnEnum")
            Field field = enumValue.getClass().getField(enumValue.name());
            Validate.notNull(field, "A non-null field should always be returned. "
                    + "Enum constant missing @Value or @NullValue annotation: %s", enumValue);
        } catch (NoSuchFieldException e) {
            String msg = String.format("Could not find name field for enum: %s", value);
            LOGGER.warn(msg, e);
            return null;
        }
    } else if (value instanceof Iterable<?>) {
        StringBuilder sb = new StringBuilder();

        Iterator<?> itr = ((Iterable<?>) value).iterator();
        while (itr.hasNext()) {
            Object next = itr.next();

            if (next != null) {
                sb.append(next.toString());
            }

            if (itr.hasNext()) {
                sb.append(",");
            }
        }

        return sb.toString();
    } else if (value.getClass().isArray()) {
        Object[] array = (Object[]) value;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < array.length; i++) {
            Object next = array[i];

            if (next != null) {
                sb.append(next.toString());
            }

            if (i < array.length - 1) {
                sb.append(", ");
            }
        }

        return sb.toString();
    }

    return value.toString();
}

From source file:com.evolveum.midpoint.repo.sql.query2.restriction.Restriction.java

protected String nameOf(Enum e) {
    return e.getClass().getName() + "." + e.name();
}

From source file:com.evolveum.midpoint.repo.sql.helpers.mapper.EnumMapper.java

@Override
public SchemaEnum map(Enum input, MapperContext context) {
    String repoEnumClass = null;/*from w  ww  .  java 2 s. c o m*/
    try {
        String className = input.getClass().getSimpleName();
        Class clazz;
        if (input instanceof ExportType) {
            clazz = RExportType.class; // todo fix this brutal hack
        } else {
            className = StringUtils.left(className, className.length() - 4);
            repoEnumClass = "com.evolveum.midpoint.repo.sql.data.common.enums.R" + className;
            clazz = Class.forName(repoEnumClass);
        }

        if (!SchemaEnum.class.isAssignableFrom(clazz)) {
            throw new SystemException("Can't translate enum value " + input);
        }

        return RUtil.getRepoEnumValue(input, clazz);
    } catch (ClassNotFoundException ex) {
        throw new SystemException("Couldn't find class '" + repoEnumClass + "' for enum '" + input + "'", ex);
    }
}

From source file:com.mg.framework.utils.MiscUtils.java

/**
 *  ? ? ? ? //  ww w  .  ja v  a2s.  c o m
 *
 * @param value  ? ? 
 * @param locale 
 * @return ? ?
 * @throws NullPointerException ? <code>value == null</code>
 */
public static String getEnumTextRepresentation(Enum<?> value, Locale locale) {
    if (value == null)
        throw new NullPointerException();

    String result;
    EnumConstantText textAnnot = null;
    Field fld = ReflectionUtils.findDeclaredField(value.getClass(), value.name());
    textAnnot = fld.getAnnotation(EnumConstantText.class);
    if (textAnnot != null)
        result = UIUtils.loadL10nText(locale, textAnnot.value());
    else
        result = value.name();
    return result;
}

From source file:io.hops.ha.common.TransactionState.java

public synchronized void incCounter(Enum type) {
    counter++;/*from w w  w.j  ava2 s .  c o m*/
    LOG.debug("counter inc for rpc: " + this.rpcID + " count " + counter + " type: " + type + " classe:"
            + type.getClass());
}

From source file:edu.ksu.cis.santos.mdcf.dml.ast.AstNode.java

private void toString(final StringBuilder sb, final Object o) {
    if (o instanceof AstNode) {
        final AstNode n = (AstNode) o;
        final String name = n.getClass().getSimpleName();
        sb.append(Character.toLowerCase(name.charAt(0)));
        sb.append(name.substring(1));/*ww  w  .j  a v  a 2 s. c  om*/
        sb.append('(');
        final Object[] children = n.children();
        final int len = children.length;
        for (int i = 0; i < len; i++) {
            toString(sb, children[i]);
            if (i != (len - 1)) {
                sb.append(", ");
            }
        }
        sb.append(')');
    } else if (o instanceof List) {
        final List<?> l = (List<?>) o;
        sb.append("list(");
        final int size = l.size();
        for (int i = 0; i < size; i++) {
            toString(sb, l.get(i));
            if (i != (size - 1)) {
                sb.append(", ");
            }
        }
        sb.append(')');
    } else if (o instanceof Optional) {
        final Optional<?> opt = (Optional<?>) o;
        if (opt.isPresent()) {
            sb.append("some(");
            toString(sb, opt.get());
            sb.append(')');
        } else {
            sb.append("none()");
        }
    } else if (o instanceof String) {
        final String s = (String) o;
        sb.append('"');
        sb.append(StringEscapeUtils.escapeJava(s));
        sb.append('"');
    } else if (o instanceof Enum) {
        final Enum<?> e = (Enum<?>) o;
        sb.append(e.getClass().getSimpleName());
        sb.append('.');
        sb.append(e.toString());
    } else {
        sb.append(o);
    }
}

From source file:io.hops.ha.common.TransactionState.java

public synchronized void decCounter(Enum type) throws IOException {
    counter--;/*  w  ww  .j a v a  2  s.c om*/
    LOG.debug("counter dec for rpc: " + this.rpcID + " count " + counter + " type: " + type + " classe:"
            + type.getClass());
    if (counter == 0) {
        commit();
    }
}

From source file:be.fedict.eid.dss.model.bean.ConfigurationBean.java

/**
 * {@inheritDoc}//  w ww .j a v  a2s  . co m
 */
@SuppressWarnings({ "unchecked" })
public <T> T getValue(ConfigProperty configProperty, String index, Class<T> type) {

    if (!type.equals(configProperty.getType())) {
        throw new IllegalArgumentException("incorrect type: " + type.getName());
    }

    String propertyName = getPropertyName(configProperty, index);

    ConfigPropertyEntity configPropertyEntity = this.entityManager.find(ConfigPropertyEntity.class,
            propertyName);
    if (null == configPropertyEntity) {
        if (Boolean.class == configProperty.getType()) {
            return (T) Boolean.FALSE;
        } else {
            return null;
        }
    }
    String value = configPropertyEntity.getValue();
    if (null == value || value.trim().length() == 0) {
        if (Boolean.class == configProperty.getType()) {
            return (T) Boolean.FALSE;
        } else {
            return null;
        }
    }

    if (String.class == configProperty.getType()) {
        return (T) value;
    }
    if (Boolean.class == configProperty.getType()) {
        Boolean booleanValue = Boolean.parseBoolean(value);
        return (T) booleanValue;
    }
    if (Integer.class == configProperty.getType()) {
        Integer integerValue = Integer.parseInt(value);
        return (T) integerValue;
    }
    if (Long.class == configProperty.getType()) {
        Long longValue = Long.parseLong(value);
        return (T) longValue;
    }
    if (configProperty.getType().isEnum()) {
        Enum<?> e = (Enum<?>) configProperty.getType().getEnumConstants()[0];
        return (T) Enum.valueOf(e.getClass(), value);
    }
    throw new RuntimeException("unsupported type: " + configProperty.getType().getName());
}

From source file:de.escalon.hypermedia.hydra.serialize.JacksonHydraSerializer.java

private Map<String, Object> getTerms(Object bean, Class<?> mixInClass)
        throws IntrospectionException, IllegalAccessException, NoSuchFieldException {
    // define terms from package or type in context
    final Class<?> beanClass = bean.getClass();
    Map<String, Object> termsMap = getAnnotatedTerms(beanClass.getPackage(), beanClass.getPackage().getName());
    Map<String, Object> classTermsMap = getAnnotatedTerms(beanClass, beanClass.getName());
    Map<String, Object> mixinTermsMap = getAnnotatedTerms(mixInClass, beanClass.getName());

    // class terms override package terms
    termsMap.putAll(classTermsMap);/*w w w  .  jav a  2 s  . c o m*/
    // mixin terms override class terms
    termsMap.putAll(mixinTermsMap);

    final Field[] fields = beanClass.getDeclaredFields();
    for (Field field : fields) {
        final Expose fieldExpose = field.getAnnotation(Expose.class);
        if (Enum.class.isAssignableFrom(field.getType())) {
            Map<String, String> map = new LinkedHashMap<String, String>();
            termsMap.put(field.getName(), map);
            if (fieldExpose != null) {
                map.put(AT_ID, fieldExpose.value());
            }
            map.put(AT_TYPE, AT_VOCAB);
            final Enum value = (Enum) field.get(bean);
            final Expose enumValueExpose = getAnnotation(value.getClass().getField(value.name()), Expose.class);
            // TODO redefine actual enum value to exposed on enum value definition
            if (enumValueExpose != null) {
                termsMap.put(value.toString(), enumValueExpose.value());
            } else {
                // might use upperToCamelCase if nothing is exposed
                final String camelCaseEnumValue = WordUtils
                        .capitalizeFully(value.toString(), new char[] { '_' }).replaceAll("_", "");
                termsMap.put(value.toString(), camelCaseEnumValue);
            }
        } else {
            if (fieldExpose != null) {
                termsMap.put(field.getName(), fieldExpose.value());
            }
        }
    }

    // TODO do this recursively for nested beans and collect as long as
    // nested beans have same vocab
    // expose getters in context
    final BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
    final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
        final Method method = propertyDescriptor.getReadMethod();

        final Expose expose = method.getAnnotation(Expose.class);
        if (expose != null) {
            termsMap.put(propertyDescriptor.getName(), expose.value());
        }
    }
    return termsMap;
}

From source file:microsoft.exchange.webservices.data.core.EwsUtilities.java

/**
 * Validate enum version value./* www. j a v  a  2s .  co  m*/
 *
 * @param enumValue      the enum value
 * @param requestVersion the request version
 * @throws ServiceVersionException the service version exception
 */
public static void validateEnumVersionValue(Enum<?> enumValue, ExchangeVersion requestVersion)
        throws ServiceVersionException {
    final Map<Class<?>, Map<String, ExchangeVersion>> member = ENUM_VERSION_DICTIONARIES.getMember();
    final Map<String, ExchangeVersion> enumVersionDict = member.get(enumValue.getClass());

    final ExchangeVersion enumVersion = enumVersionDict.get(enumValue.toString());
    if (enumVersion != null) {
        final int i = requestVersion.compareTo(enumVersion);
        if (i < 0) {
            throw new ServiceVersionException(String.format(
                    "Enumeration value %s in enumeration type %s is only valid for Exchange version %s or later.",
                    enumValue.toString(), enumValue.getClass().getName(), enumVersion));
        }
    }
}