All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.core.annotation.AnnotationUtils Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * 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.
 */

package org.springframework.core.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.springframework.core.BridgeMethodResolver;

/**
 * General utility methods for working with annotations, handling bridge methods
 * (which the compiler generates for generic declarations) as well as super
 * methods (for optional "annotation inheritance"). Note that none of this is
 * provided by the JDK's introspection facilities themselves.
 *
 * 

As a general rule for runtime-retained annotations (e.g. for transaction * control, authorization or service exposure), always use the lookup methods * on this class instead of the plain annotation lookup methods in the JDK. * You can still explicitly choose between lookup on the given class level only * ({@link #getAnnotation}) and lookup in the entire inheritance hierarchy of * the given method ({@link #findAnnotation}). * * @author Rod Johnson * @author Rob Harrop * @author Juergen Hoeller * @since 2.0 * @see java.lang.reflect.Method#getAnnotations() * @see java.lang.reflect.Method#getAnnotation(Class) */ public abstract class AnnotationUtils { /** * Get all {@link Annotation Annotations} from the supplied {@link Method}. *

Correctly handles bridge {@link Method Methods} generated by the compiler. * @param method the method to look for annotations on * @return the annotations found * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) */ public static Annotation[] getAnnotations(Method method) { return BridgeMethodResolver.findBridgedMethod(method).getAnnotations(); } /** * Get a single {@link Annotation} of annotationType from the * supplied {@link Method}. *

Correctly handles bridge {@link Method Methods} generated by the compiler. * @param method the method to look for annotations on * @param annotationType the annotation class to look for * @return the annotations found * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) */ public static A getAnnotation(Method method, Class annotationType) { return BridgeMethodResolver.findBridgedMethod(method).getAnnotation(annotationType); } /** * Get a single {@link Annotation} of annotationType from the * supplied {@link Method}, traversing its super methods if no annotation * can be found on the given method. *

Annotations on methods are not inherited by default, so we need to * handle this explicitly. * @param method the method to look for annotations on * @param annotationType the annotation class to look for * @return the annotation of the given type found, or null */ public static A findAnnotation(Method method, Class annotationType) { if (!annotationType.isAnnotation()) { throw new IllegalArgumentException(annotationType + " is not an annotation"); } A annotation = getAnnotation(method, annotationType); Class cl = method.getDeclaringClass(); while (annotation == null) { cl = cl.getSuperclass(); if (cl == null || cl.equals(Object.class)) { break; } try { method = cl.getDeclaredMethod(method.getName(), method.getParameterTypes()); annotation = getAnnotation(method, annotationType); } catch (NoSuchMethodException ex) { // We're done... } } return annotation; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy