View Javadoc

1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.aspect.aspectj;
4   
5   import org.aspectj.lang.JoinPoint;
6   import org.aspectj.lang.reflect.CodeSignature;
7   import org.throwableutil.ThrowableUtil;
8   import org.virtualmock.aspect.AspectUtils;
9   import org.virtualmock.call.InvokedCall;
10  import org.virtualmock.call.Signature;
11  
12  
13  /***
14   * This is a subclass of AspectUtils which implements behavior specific to
15   * AspectJ.
16   *
17   * @author Chad Woolley
18   * @version $Revision: 1.11 $
19   *
20   * @todo Write unit test for this class.
21   */
22  public class AspectjAspectUtils {
23      private AspectUtils aspectUtils = null;
24  
25      /***
26       * Creates a new AspectjAspectUtils object.
27       *
28       * @param aspectUtils an AspectUtils which will be used internally
29       */
30      public AspectjAspectUtils(AspectUtils aspectUtils) {
31          this.aspectUtils = aspectUtils;
32      }
33  
34      /***
35       * Determines if the joinpoint defined by the specified
36       * JoinPoint.StaticPart has been dynamically excluded.
37       *
38       * @param joinPointStaticPart JoinPoint.StaticPart
39       *
40       * @return true if excluded, false if not
41       *
42       * @todo expose some API on VM to allow user to dynamically define multiple
43       *       class exclusion patterns
44       */
45      public boolean isJoinPointExcluded(JoinPoint.StaticPart joinPointStaticPart) {
46          org.aspectj.lang.Signature signature =
47              joinPointStaticPart.getSignature();
48          Class targetClass = signature.getDeclaringType();
49          boolean isExcluded = aspectUtils.isClassExcluded(targetClass);
50  
51          return isExcluded;
52      }
53  
54      /***
55       * Uses information obtained from a JoinPoint to build an array of argument
56       * values.
57       *
58       * @param joinPoint the JoinPoint from which to obtain the argument values
59       *
60       * @return the array of argument values
61       */
62      protected Object[] getArgValuesFromJoinPoint(JoinPoint joinPoint) {
63          Object[] argValues = joinPoint.getArgs();
64  
65          return argValues;
66      }
67  
68      /***
69       * Uses information obtained from a JoinPoint to build an InvokedCall.
70       *
71       * @param joinPoint the JoinPoint from which to obtain an InvokedCall
72       *
73       * @return an InvokedCall instance, created from the information in the
74       *         joinPoint
75       */
76      protected InvokedCall getInvokedCallFromJoinPoint(JoinPoint joinPoint) {
77          Signature signature = getSignatureFromJoinPoint(joinPoint);
78          Object[] argValues = getArgValuesFromJoinPoint(joinPoint);
79  
80          InvokedCall invokedCall = new InvokedCall(signature, argValues);
81  
82          return invokedCall;
83      }
84  
85      /***
86       * Uses information obtained from a JoinPoint to build a Signature.
87       *
88       * @param joinPoint the JoinPoint from which to obtain a Signature
89       *
90       * @return a Signature
91       */
92      protected Signature getSignatureFromJoinPoint(JoinPoint joinPoint) {
93          // get the classname and method name for the call we are processing
94          CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
95          Class classType = codeSignature.getDeclaringType();
96          String methodName = codeSignature.getName();
97          Class[] argTypes = codeSignature.getParameterTypes();
98  
99          Signature signature = new Signature(classType, methodName, argTypes);
100 
101         return signature;
102     }
103 
104     /***
105      * invokes the custom BCEL-modified class to throw any Throwable as a
106      * RuntimeException.
107      *
108      * @param throwable the Throwable to throw as a RuntimeException
109      */
110     protected void throwAsRuntime(Throwable throwable) {
111         ThrowableUtil.throwAsRuntime(throwable);
112     }
113 }