Java examples for java.lang.annotation:Annotation Attribute
Accessor for the value for an annotation attribute.
/********************************************************************** Copyright (c) 2010 Andy Jefferson and others. All rights reserved. Licensed 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//from w w w .j a va 2s .c o m 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. Contributors: ... **********************************************************************/ //package com.java2s; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; public class Main { /** * Accessor for the value for an annotation attribute. * @param elem The element * @param annotCls Annotation class * @param attribute The attribute we're interested in * @return The value */ public static Object getValueForAnnotationAttribute(Element elem, Class annotCls, String attribute) { List<? extends AnnotationMirror> anns = elem.getAnnotationMirrors(); Iterator<? extends AnnotationMirror> annIter = anns.iterator(); while (annIter.hasNext()) { AnnotationMirror ann = annIter.next(); if (ann.getAnnotationType().toString() .equals(annotCls.getName())) { Map<? extends ExecutableElement, ? extends AnnotationValue> values = ann .getElementValues(); for (Map.Entry entry : values.entrySet()) { ExecutableElement ex = (ExecutableElement) entry .getKey(); if (ex.getSimpleName().toString().equals(attribute)) { return ((AnnotationValue) entry.getValue()) .getValue(); } } } } return null; } }