1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  import org.as2lib.aop.JoinPoint;
    19  import org.as2lib.aop.Pointcut;
    20  
    21  /**
    22   * {@code WithinPointcut} captures join points based one their lexical-structure, that
    23   * means on the scope of the code as it was written. This within-pointcut allows for
    24   * capturing join points inside the lexical scope of classes and interfaces.
    25   * 
    26   * @author Simon Wacker
    27   * @see <a href="http://www.simonwacker.com/blog/archives/000061.php">Lexical-Structure Based Pointcuts</a>
    28   */
    29  class org.as2lib.aop.pointcut.WithinPointcut extends BasicClass implements Pointcut {
    30  	
    31  	/** The type pattern used to determine whether specific join points are captured. */
    32  	private var typePattern:String;
    33  	
    34  	/**
    35  	 * Constructs a new {@code WithinPointcut} instance.
    36  	 * 
    37  	 * <p>A {@code typePattern} is for example:
    38  	 * <code>org.as2lib..*Aspect</code>
    39  	 * 
    40  	 * <p>The above pattern matches all join points within all types in the "org.as2lib"
    41  	 * package and any sub-package that have a name that ends with "Aspect".
    42  	 * 
    43  	 * @param typePattern the type pattern describing the lexical scope of join points
    44  	 * to capture
    45  	 * @see #captures
    46  	 */
    47  	public function WithinPointcut(typePattern:String) {
    48  		if (typePattern != null) {
    49  			this.typePattern = typePattern + ".*";
    50  		}
    51  	}
    52  	
    53  	/**
    54  	 * Checks if the given {@code joinPoint} is captured by this pointcut.
    55  	 * 
    56  	 * <p>{@code false} will be returned if:
    57  	 * <ul>
    58  	 *   <li>The passed-in {@code joinPoint} is {@code null} or {@code undefined}.</li>
    59  	 *   <li>The passed-in {@code joinPoint} does not match the given type pattern.</li>
    60  	 * </ul>
    61  	 * 
    62  	 * @param joinPoint the join point upon which to make the check
    63  	 * @return {@code true} if the given {@code joinPoint} is captured by this pointcut
    64  	 * else {@code false}
    65  	 * @see JoinPoint#matches
    66  	 */
    67  	public function captures(joinPoint:JoinPoint):Boolean {
    68  		if (!joinPoint) return false;
    69  		return joinPoint.matches(this.typePattern);
    70  	}
    71  	
    72  }