001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.cache.annotation;
018
019import java.lang.reflect.Method;
020import java.util.Collection;
021
022import org.springframework.cache.interceptor.CacheOperation;
023import org.springframework.lang.Nullable;
024
025/**
026 * Strategy interface for parsing known caching annotation types.
027 * {@link AnnotationCacheOperationSource} delegates to such parsers
028 * for supporting specific annotation types such as Spring's own
029 * {@link Cacheable}, {@link CachePut} and{@link CacheEvict}.
030 *
031 * @author Costin Leau
032 * @author Stephane Nicoll
033 * @author Juergen Hoeller
034 * @since 3.1
035 * @see AnnotationCacheOperationSource
036 * @see SpringCacheAnnotationParser
037 */
038public interface CacheAnnotationParser {
039
040        /**
041         * Determine whether the given class is a candidate for cache operations
042         * in the annotation format of this {@code CacheAnnotationParser}.
043         * <p>If this method returns {@code false}, the methods on the given class
044         * will not get traversed for {@code #parseCacheAnnotations} introspection.
045         * Returning {@code false} is therefore an optimization for non-affected
046         * classes, whereas {@code true} simply means that the class needs to get
047         * fully introspected for each method on the given class individually.
048         * @param targetClass the class to introspect
049         * @return {@code false} if the class is known to have no cache operation
050         * annotations at class or method level; {@code true} otherwise. The default
051         * implementation returns {@code true}, leading to regular introspection.
052         * @since 5.2
053         */
054        default boolean isCandidateClass(Class<?> targetClass) {
055                return true;
056        }
057
058        /**
059         * Parse the cache definition for the given class,
060         * based on an annotation type understood by this parser.
061         * <p>This essentially parses a known cache annotation into Spring's metadata
062         * attribute class. Returns {@code null} if the class is not cacheable.
063         * @param type the annotated class
064         * @return the configured caching operation, or {@code null} if none found
065         * @see AnnotationCacheOperationSource#findCacheOperations(Class)
066         */
067        @Nullable
068        Collection<CacheOperation> parseCacheAnnotations(Class<?> type);
069
070        /**
071         * Parse the cache definition for the given method,
072         * based on an annotation type understood by this parser.
073         * <p>This essentially parses a known cache annotation into Spring's metadata
074         * attribute class. Returns {@code null} if the method is not cacheable.
075         * @param method the annotated method
076         * @return the configured caching operation, or {@code null} if none found
077         * @see AnnotationCacheOperationSource#findCacheOperations(Method)
078         */
079        @Nullable
080        Collection<CacheOperation> parseCacheAnnotations(Method method);
081
082}