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.support;
018
019import java.util.regex.Matcher;
020import java.util.regex.Pattern;
021import java.util.regex.PatternSyntaxException;
022
023/**
024 * Regular expression pointcut based on the {@code java.util.regex} package.
025 * Supports the following JavaBean properties:
026 * <ul>
027 * <li>pattern: regular expression for the fully-qualified method names to match
028 * <li>patterns: alternative property taking a String array of patterns. The result will
029 * be the union of these patterns.
030 * </ul>
031 *
032 * <p>Note: the regular expressions must be a match. For example,
033 * {@code .*get.*} will match com.mycom.Foo.getBar().
034 * {@code get.*} will not.
035 *
036 * @author Dmitriy Kopylenko
037 * @author Rob Harrop
038 * @since 1.1
039 */
040@SuppressWarnings("serial")
041public class JdkRegexpMethodPointcut extends AbstractRegexpMethodPointcut {
042
043        /**
044         * Compiled form of the patterns.
045         */
046        private Pattern[] compiledPatterns = new Pattern[0];
047
048        /**
049         * Compiled form of the exclusion patterns.
050         */
051        private Pattern[] compiledExclusionPatterns = new Pattern[0];
052
053
054        /**
055         * Initialize {@link Pattern Patterns} from the supplied {@code String[]}.
056         */
057        @Override
058        protected void initPatternRepresentation(String[] patterns) throws PatternSyntaxException {
059                this.compiledPatterns = compilePatterns(patterns);
060        }
061
062        /**
063         * Initialize exclusion {@link Pattern Patterns} from the supplied {@code String[]}.
064         */
065        @Override
066        protected void initExcludedPatternRepresentation(String[] excludedPatterns) throws PatternSyntaxException {
067                this.compiledExclusionPatterns = compilePatterns(excludedPatterns);
068        }
069
070        /**
071         * Returns {@code true} if the {@link Pattern} at index {@code patternIndex}
072         * matches the supplied candidate {@code String}.
073         */
074        @Override
075        protected boolean matches(String pattern, int patternIndex) {
076                Matcher matcher = this.compiledPatterns[patternIndex].matcher(pattern);
077                return matcher.matches();
078        }
079
080        /**
081         * Returns {@code true} if the exclusion {@link Pattern} at index {@code patternIndex}
082         * matches the supplied candidate {@code String}.
083         */
084        @Override
085        protected boolean matchesExclusion(String candidate, int patternIndex) {
086                Matcher matcher = this.compiledExclusionPatterns[patternIndex].matcher(candidate);
087                return matcher.matches();
088        }
089
090
091        /**
092         * Compiles the supplied {@code String[]} into an array of
093         * {@link Pattern} objects and returns that array.
094         */
095        private Pattern[] compilePatterns(String[] source) throws PatternSyntaxException {
096                Pattern[] destination = new Pattern[source.length];
097                for (int i = 0; i < source.length; i++) {
098                        destination[i] = Pattern.compile(source[i]);
099                }
100                return destination;
101        }
102
103}