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:Main.java

/** Read the {@link XmlEnumValue} string off of an enum. */
public static String enumToXml(Enum<?> input) {
    try {//from w w  w  .  j  a  v a2 s  .c o  m
        return input.getClass().getField(input.name()).getAnnotation(XmlEnumValue.class).value();
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String serializedName(Enum anEnum) {
    String name = anEnum.name();//from w w  w  .  j a  va2  s .c  o  m

    try {
        SerializedName serializedName = anEnum.getClass().getField(name).getAnnotation(SerializedName.class);

        if (serializedName != null) {
            name = serializedName.value();
        }
    } catch (NoSuchFieldException ignored) {

    }

    return name;
}

From source file:Main.java

/**
 * @param clazz/*from  ww  w.ja v a  2  s.  c  o m*/
 * @return namespace of root element qname or null if this is not object does not represent a root element
 */
public static String getEnumValue(Enum<?> myEnum) {
    Field f;
    String value;
    try {
        f = myEnum.getClass().getField(myEnum.name());

        f.setAccessible(true);

        XmlEnumValue xev = (XmlEnumValue) getAnnotation(f, XmlEnumValue.class);
        if (xev == null) {
            value = f.getName();
        } else {
            value = xev.value();
        }
    } catch (SecurityException e) {
        value = null;
    } catch (NoSuchFieldException e) {
        value = null;
    }

    return value;
}

From source file:com.flexive.faces.converter.EnumConverter.java

/**
 * Encode an Enum value to a string that can be decoded using {@link EnumConverter#getValue(String)}.
 * <p>This method is exposed in JSF-EL as <code>fx:encodeEnum</code>.</p>
 *
 * @param value the Enum value to be encoded
 * @return  the encoded string representation
 *//*from w  w w  .  ja v  a  2  s  . com*/
public static String encodeEnum(Enum value) {
    // replace inner class suffixes of class name and append enum name
    return value.getClass().getName().replaceFirst("\\$\\d+$", "") + "::" + value.name();
}

From source file:net.sourceforge.fenixedu.util.BundleUtil.java

@Deprecated // remove on move to major version 4.0.0
public static String getEnumName(final Enum<?> enumeration, final String moduleName) {
    String enumFullName = enumeration.getClass().getName();
    if (enumFullName.indexOf('$') > -1) {
        enumFullName = enumFullName.substring(0, enumFullName.indexOf('$'));
    }//from w ww  .j av a 2  s .  co m

    String enumSimpleName = enumeration.getClass().getSimpleName();
    if (enumSimpleName.isEmpty()) {
        enumSimpleName = enumFullName.substring(enumFullName.lastIndexOf('.') + 1);
    }

    enumFullName = enumFullName + "." + enumeration.name();
    enumSimpleName = enumSimpleName + "." + enumeration.name();
    try {
        return getResourceBundleByModuleName(moduleName).getString(enumFullName);
    } catch (MissingResourceException e) {
        try {
            return getResourceBundleByModuleName(moduleName).getString(enumSimpleName);
        } catch (MissingResourceException ex) {
            try {
                return getResourceBundleByModuleName(moduleName).getString(enumeration.name());
            } catch (MissingResourceException exc) {
                return enumFullName;
            }
        }
    }
}

From source file:com.netflix.simianarmy.aws.AbstractRecorder.java

/**
 * Enum to value. Converts an enum to "name|type" string
 *
 * @param e//from   www .  j  ava 2 s  .c o  m
 *            the e
 * @return the string
 */
protected static String enumToValue(Enum e) {
    return String.format("%s|%s", e.name(), e.getClass().getName());
}

From source file:com.oembedler.moon.graphql.engine.dfs.ResolvableTypeAccessor.java

public static ResolvableTypeAccessor forEnumField(Enum en) {
    Field field = ReflectionUtils.findField(en.getClass(), ((Enum) en).name());
    return new ResolvableTypeAccessor(en.toString(), null, Lists.newArrayList(field.getAnnotations()),
            en.getClass());//  ww w  .  ja  va  2s. co  m
}

From source file:org.agiso.core.i18n.util.I18nUtils.java

public static String getCode(Enum<?> e) {
    try {//from  ww  w .j  a va  2s .c  om
        return getCode(e.getClass().getField(e.name()));
    } catch (SecurityException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (NoSuchFieldException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    }
    return e.getDeclaringClass().getName() + CODE_SEPARATOR + e.name();
}

From source file:gdv.xport.feld.Feld.java

/**
 * Ermittelt die FeldInfo aus dem uebergebenen Enum.
 *
 * @param feldX the feld x// ww w  . ja v a  2  s. c om
 * @return the feld info
 */
protected static FeldInfo getFeldInfo(final Enum<?> feldX) {
    try {
        Field field = feldX.getClass().getField(feldX.name());
        return field.getAnnotation(FeldInfo.class);
    } catch (NoSuchFieldException nsfe) {
        throw new InternalError("no field " + feldX + " (" + nsfe + ")");
    }
}

From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java

/**
 * ?Value/* www .  ja v  a2s  .  com*/
 * @param context 
 * @param enumObject 
 */
public static String parseValueAnnotationFromEnum(Context context, Enum<?> enumObject) {
    Value annotation = null;
    try {
        annotation = enumObject.getClass().getField(enumObject.name()).getAnnotation(Value.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (annotation == null) {
        return null;
    }
    String annotationValue = annotation.value();
    if (annotationValue != null && !"".equals(annotationValue)) {
        return annotationValue;
    } else if (context != null && annotation.resId() > 0) {
        return context.getString(annotation.resId());
    } else {
        return null;
    }
}