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.springframework.lang.Nullable;
022
023/**
024 * Abstract superclass for expression pointcuts,
025 * offering location and expression properties.
026 *
027 * @author Rod Johnson
028 * @author Rob Harrop
029 * @since 2.0
030 * @see #setLocation
031 * @see #setExpression
032 */
033@SuppressWarnings("serial")
034public abstract class AbstractExpressionPointcut implements ExpressionPointcut, Serializable {
035
036        @Nullable
037        private String location;
038
039        @Nullable
040        private String expression;
041
042
043        /**
044         * Set the location for debugging.
045         */
046        public void setLocation(@Nullable String location) {
047                this.location = location;
048        }
049
050        /**
051         * Return location information about the pointcut expression
052         * if available. This is useful in debugging.
053         * @return location information as a human-readable String,
054         * or {@code null} if none is available
055         */
056        @Nullable
057        public String getLocation() {
058                return this.location;
059        }
060
061        public void setExpression(@Nullable String expression) {
062                this.expression = expression;
063                try {
064                        onSetExpression(expression);
065                }
066                catch (IllegalArgumentException ex) {
067                        // Fill in location information if possible.
068                        if (this.location != null) {
069                                throw new IllegalArgumentException("Invalid expression at location [" + this.location + "]: " + ex);
070                        }
071                        else {
072                                throw ex;
073                        }
074                }
075        }
076
077        /**
078         * Called when a new pointcut expression is set.
079         * The expression should be parsed at this point if possible.
080         * <p>This implementation is empty.
081         * @param expression the expression to set
082         * @throws IllegalArgumentException if the expression is invalid
083         * @see #setExpression
084         */
085        protected void onSetExpression(@Nullable String expression) throws IllegalArgumentException {
086        }
087
088        /**
089         * Return this pointcut's expression.
090         */
091        @Override
092        @Nullable
093        public String getExpression() {
094                return this.expression;
095        }
096
097}