Java tutorial
//package com.java2s; /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; import java.security.AccessController; import java.security.PrivilegedAction; import javax.xml.bind.annotation.XmlEnumValue; public class Main { /** * @param clazz * @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; } /** * Get an annotation. This is wrappered to avoid a Java2Security violation. * @param cls Class that contains annotation * @param annotation Class of requrested Annotation * @return annotation or null */ private static <T extends Annotation> T getAnnotation(final AnnotatedElement element, final Class<T> annotation) { return AccessController.doPrivileged(new PrivilegedAction<T>() { public T run() { return element.getAnnotation(annotation); } }); } }