001/*
002 * Copyright 2002-2012 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.aop.aspectj;
018
019import org.aopalliance.aop.Advice;
020
021import org.springframework.aop.Advisor;
022import org.springframework.aop.AfterAdvice;
023import org.springframework.aop.BeforeAdvice;
024import org.springframework.lang.Nullable;
025
026/**
027 * Utility methods for dealing with AspectJ advisors.
028 *
029 * @author Adrian Colyer
030 * @author Juergen Hoeller
031 * @since 2.0
032 */
033public abstract class AspectJAopUtils {
034
035        /**
036         * Return {@code true} if the advisor is a form of before advice.
037         */
038        public static boolean isBeforeAdvice(Advisor anAdvisor) {
039                AspectJPrecedenceInformation precedenceInfo = getAspectJPrecedenceInformationFor(anAdvisor);
040                if (precedenceInfo != null) {
041                        return precedenceInfo.isBeforeAdvice();
042                }
043                return (anAdvisor.getAdvice() instanceof BeforeAdvice);
044        }
045
046        /**
047         * Return {@code true} if the advisor is a form of after advice.
048         */
049        public static boolean isAfterAdvice(Advisor anAdvisor) {
050                AspectJPrecedenceInformation precedenceInfo = getAspectJPrecedenceInformationFor(anAdvisor);
051                if (precedenceInfo != null) {
052                        return precedenceInfo.isAfterAdvice();
053                }
054                return (anAdvisor.getAdvice() instanceof AfterAdvice);
055        }
056
057        /**
058         * Return the AspectJPrecedenceInformation provided by this advisor or its advice.
059         * If neither the advisor nor the advice have precedence information, this method
060         * will return {@code null}.
061         */
062        @Nullable
063        public static AspectJPrecedenceInformation getAspectJPrecedenceInformationFor(Advisor anAdvisor) {
064                if (anAdvisor instanceof AspectJPrecedenceInformation) {
065                        return (AspectJPrecedenceInformation) anAdvisor;
066                }
067                Advice advice = anAdvisor.getAdvice();
068                if (advice instanceof AspectJPrecedenceInformation) {
069                        return (AspectJPrecedenceInformation) advice;
070                }
071                return null;
072        }
073
074}