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