001/*
002 * Copyright 2002-2017 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.support;
018
019import java.io.Serializable;
020
021import org.aopalliance.aop.Advice;
022
023import org.springframework.aop.Pointcut;
024import org.springframework.lang.Nullable;
025
026/**
027 * Convenient Pointcut-driven Advisor implementation.
028 *
029 * <p>This is the most commonly used Advisor implementation. It can be used
030 * with any pointcut and advice type, except for introductions. There is
031 * normally no need to subclass this class, or to implement custom Advisors.
032 *
033 * @author Rod Johnson
034 * @author Juergen Hoeller
035 * @see #setPointcut
036 * @see #setAdvice
037 */
038@SuppressWarnings("serial")
039public class DefaultPointcutAdvisor extends AbstractGenericPointcutAdvisor implements Serializable {
040
041        private Pointcut pointcut = Pointcut.TRUE;
042
043
044        /**
045         * Create an empty DefaultPointcutAdvisor.
046         * <p>Advice must be set before use using setter methods.
047         * Pointcut will normally be set also, but defaults to {@code Pointcut.TRUE}.
048         */
049        public DefaultPointcutAdvisor() {
050        }
051
052        /**
053         * Create a DefaultPointcutAdvisor that matches all methods.
054         * <p>{@code Pointcut.TRUE} will be used as Pointcut.
055         * @param advice the Advice to use
056         */
057        public DefaultPointcutAdvisor(Advice advice) {
058                this(Pointcut.TRUE, advice);
059        }
060
061        /**
062         * Create a DefaultPointcutAdvisor, specifying Pointcut and Advice.
063         * @param pointcut the Pointcut targeting the Advice
064         * @param advice the Advice to run when Pointcut matches
065         */
066        public DefaultPointcutAdvisor(Pointcut pointcut, Advice advice) {
067                this.pointcut = pointcut;
068                setAdvice(advice);
069        }
070
071
072        /**
073         * Specify the pointcut targeting the advice.
074         * <p>Default is {@code Pointcut.TRUE}.
075         * @see #setAdvice
076         */
077        public void setPointcut(@Nullable Pointcut pointcut) {
078                this.pointcut = (pointcut != null ? pointcut : Pointcut.TRUE);
079        }
080
081        @Override
082        public Pointcut getPointcut() {
083                return this.pointcut;
084        }
085
086
087        @Override
088        public String toString() {
089                return getClass().getName() + ": pointcut [" + getPointcut() + "]; advice [" + getAdvice() + "]";
090        }
091
092}