Here you can find the source of getAnnotationAttributes(final Annotation annotation)
Parameter | Description |
---|---|
annotation | the annotation to retrieve the attributes for |
public static Map<String, Object> getAnnotationAttributes(final Annotation annotation)
//package com.java2s; /*/* ww w .j a v a 2s. co m*/ * Copyright 2002-2007 the original author or authors. * * 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 * * 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.Method; import java.util.HashMap; import java.util.Map; public class Main { /** * Retrieve the given annotation's attributes as a Map. * * @param annotation the annotation to retrieve the attributes for * * @return the Map of annotation attributes, with attribute names as keys * and corresponding attribute values as values */ public static Map<String, Object> getAnnotationAttributes(final Annotation annotation) { final Map<String, Object> attrs = new HashMap<String, Object>(); final Method[] methods = annotation.annotationType().getDeclaredMethods(); for (int j = 0; j < methods.length; j++) { final Method method = methods[j]; if (method.getParameterTypes().length == 0 && method.getReturnType() != void.class) { try { attrs.put(method.getName(), method.invoke(annotation)); } catch (final Exception ex) { throw new IllegalStateException("Could not obtain annotation attribute values", ex); } } } return attrs; } }