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 java.util.List;
020
021import org.springframework.aop.Advisor;
022import org.springframework.aop.PointcutAdvisor;
023import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
024
025/**
026 * Utility methods for working with AspectJ proxies.
027 *
028 * @author Rod Johnson
029 * @author Ramnivas Laddad
030 * @since 2.0
031 */
032public abstract class AspectJProxyUtils {
033
034        /**
035         * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors.
036         * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching)
037         * and make available the current AspectJ JoinPoint. The call will have no effect if there are no
038         * AspectJ advisors in the advisor chain.
039         * @param advisors Advisors available
040         * @return {@code true} if any special {@link Advisor Advisors} were added, otherwise {@code false}.
041         */
042        public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> advisors) {
043                // Don't add advisors to an empty list; may indicate that proxying is just not required
044                if (!advisors.isEmpty()) {
045                        boolean foundAspectJAdvice = false;
046                        for (Advisor advisor : advisors) {
047                                // Be careful not to get the Advice without a guard, as
048                                // this might eagerly instantiate a non-singleton AspectJ aspect
049                                if (isAspectJAdvice(advisor)) {
050                                        foundAspectJAdvice = true;
051                                }
052                        }
053                        if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) {
054                                advisors.add(0, ExposeInvocationInterceptor.ADVISOR);
055                                return true;
056                        }
057                }
058                return false;
059        }
060
061        /**
062         * Determine whether the given Advisor contains an AspectJ advice.
063         * @param advisor the Advisor to check
064         */
065        private static boolean isAspectJAdvice(Advisor advisor) {
066                return (advisor instanceof InstantiationModelAwarePointcutAdvisor ||
067                                advisor.getAdvice() instanceof AbstractAspectJAdvice ||
068                                (advisor instanceof PointcutAdvisor &&
069                                                 ((PointcutAdvisor) advisor).getPointcut() instanceof AspectJExpressionPointcut));
070        }
071
072}