All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.core.context.rule.PointcutRule Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
/*
 * Copyright (c) 2008-2024 The Aspectran Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aspectran.core.context.rule;

import com.aspectran.core.context.rule.type.PointcutType;
import com.aspectran.utils.ToStringBuilder;
import com.aspectran.utils.annotation.jsr305.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Pointcut are expressions that is matched with join points to determine whether
 * advice needs to be executed or not. Pointcut uses different kinds of expressions
 * that are matched with the join points and Aspectran uses the Wildcard pattern
 * matching expression.
 */
public class PointcutRule {

    private final PointcutType pointcutType;

    private List pointcutPatternRuleList;

    public PointcutRule(PointcutType pointcutType) {
        this.pointcutType = pointcutType;
    }

    public PointcutType getPointcutType() {
        return pointcutType;
    }

    public List getPointcutPatternRuleList() {
        return pointcutPatternRuleList;
    }

    public void addPointcutPatternRule(@NonNull PointcutPatternRule pointcutPatternRule) {
        pointcutPatternRule.setPointcutType(pointcutType);
        touchPointcutPatternRuleList().add(pointcutPatternRule);
    }

    public List touchPointcutPatternRuleList() {
        if (pointcutPatternRuleList == null) {
            pointcutPatternRuleList = new ArrayList<>();
        }
        return pointcutPatternRuleList;
    }

    @Override
    public String toString() {
        ToStringBuilder tsb = new ToStringBuilder();
        tsb.append("type", pointcutType);
        tsb.append("patterns", pointcutPatternRuleList);
        return tsb.toString();
    }

    @NonNull
    public static PointcutRule newInstance(String type) throws IllegalRuleException {
        PointcutType pointcutType = null;
        if (type != null) {
            pointcutType = PointcutType.resolve(type);
            if (pointcutType == null) {
                throw new IllegalRuleException("Unrecognized pointcut type '" + type +
                        "'; Pointcut type for Translet must be 'wildcard' or 'regexp'");
            }
        }
        return new PointcutRule(pointcutType);
    }

    public static PointcutRule newInstance(String[] patterns) {
        if (patterns == null || patterns.length == 0) {
            return null;
        }
        PointcutRule pointcutRule = new PointcutRule(PointcutType.WILDCARD);
        List pointcutPatternRuleList = new ArrayList<>(patterns.length);
        List excludePointcutPatternRuleList = new ArrayList<>(patterns.length);
        for (String pattern : patterns) {
            if (pattern != null) {
                pattern = pattern.trim();
                if (pattern.startsWith("-")) {
                    if (pattern.startsWith("-:")) {
                        pattern = pattern.substring(2).trim();
                    } else {
                        pattern = pattern.substring(1).trim();
                    }
                    PointcutPatternRule pointcutPatternRule = PointcutPatternRule.newInstance(pattern);
                    excludePointcutPatternRuleList.add(pointcutPatternRule);
                } else {
                    if (pattern.startsWith("+:")) {
                        pattern = pattern.substring(2).trim();
                    } else if (pattern.startsWith("+")) {
                        pattern = pattern.substring(1).trim();
                    }
                    PointcutPatternRule pointcutPatternRule = PointcutPatternRule.newInstance(pattern);
                    pointcutPatternRuleList.add(pointcutPatternRule);
                }
            }
        }
        if (pointcutPatternRuleList.isEmpty() && excludePointcutPatternRuleList.isEmpty()) {
            return null;
        }
        if (pointcutPatternRuleList.isEmpty()) {
            PointcutPatternRule pointcutPatternRule = new PointcutPatternRule();
            pointcutPatternRule.setExcludePointcutPatternRuleList(excludePointcutPatternRuleList);
            pointcutRule.addPointcutPatternRule(pointcutPatternRule);
        } else {
            for (PointcutPatternRule pointcutPatternRule : pointcutPatternRuleList) {
                pointcutPatternRule.setPointcutType(pointcutRule.getPointcutType());
                pointcutPatternRule.setExcludePointcutPatternRuleList(excludePointcutPatternRuleList);
                pointcutRule.addPointcutPatternRule(pointcutPatternRule);
            }
        }
        return pointcutRule;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy