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