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.transaction.annotation;
018
019import java.lang.reflect.AnnotatedElement;
020
021import org.springframework.lang.Nullable;
022import org.springframework.transaction.interceptor.TransactionAttribute;
023
024/**
025 * Strategy interface for parsing known transaction annotation types.
026 * {@link AnnotationTransactionAttributeSource} delegates to such
027 * parsers for supporting specific annotation types such as Spring's own
028 * {@link Transactional}, JTA 1.2's {@link javax.transaction.Transactional}
029 * or EJB3's {@link javax.ejb.TransactionAttribute}.
030 *
031 * @author Juergen Hoeller
032 * @since 2.5
033 * @see AnnotationTransactionAttributeSource
034 * @see SpringTransactionAnnotationParser
035 * @see Ejb3TransactionAnnotationParser
036 * @see JtaTransactionAnnotationParser
037 */
038public interface TransactionAnnotationParser {
039
040        /**
041         * Determine whether the given class is a candidate for transaction attributes
042         * in the annotation format of this {@code TransactionAnnotationParser}.
043         * <p>If this method returns {@code false}, the methods on the given class
044         * will not get traversed for {@code #parseTransactionAnnotation} 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 transaction
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 transaction attribute for the given method or class,
060         * based on an annotation type understood by this parser.
061         * <p>This essentially parses a known transaction annotation into Spring's metadata
062         * attribute class. Returns {@code null} if the method/class is not transactional.
063         * @param element the annotated method or class
064         * @return the configured transaction attribute, or {@code null} if none found
065         * @see AnnotationTransactionAttributeSource#determineTransactionAttribute
066         */
067        @Nullable
068        TransactionAttribute parseTransactionAnnotation(AnnotatedElement element);
069
070}