KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > pointcut > CFlowMatcher


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.pointcut;
23
24 import org.jboss.aop.AspectManager;
25 import org.jboss.aop.joinpoint.Invocation;
26 import org.jboss.aop.pointcut.ast.ASTAndCFlow;
27 import org.jboss.aop.pointcut.ast.ASTCFlow;
28 import org.jboss.aop.pointcut.ast.ASTCFlowBoolean;
29 import org.jboss.aop.pointcut.ast.ASTCFlowExpression;
30 import org.jboss.aop.pointcut.ast.ASTCompositeCFlow;
31 import org.jboss.aop.pointcut.ast.ASTNotCFlow;
32 import org.jboss.aop.pointcut.ast.ASTOrCFlow;
33 import org.jboss.aop.pointcut.ast.ASTSubCFlow;
34 import org.jboss.aop.pointcut.ast.Node;
35
36 /**
37  * Comment
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 37406 $
41  */

42 public class CFlowMatcher extends MatcherHelper
43 {
44    StackTraceElement JavaDoc[] stack;
45    Invocation invocation;
46
47    public CFlowMatcher()
48    {
49       super(null, null);
50    }
51
52    public boolean matches(ASTCFlowExpression expr, Invocation invocation)
53    {
54       this.invocation = invocation;
55       Boolean JavaDoc rtn = (Boolean JavaDoc) expr.jjtAccept(this, null);
56       return rtn.booleanValue();
57    }
58
59    private StackTraceElement JavaDoc[] getStack()
60    {
61       stack = new Throwable JavaDoc().getStackTrace();
62       return stack;
63    }
64
65    protected Boolean JavaDoc resolvePointcut(Pointcut p)
66    {
67       throw new RuntimeException JavaDoc("SHOULD NOT BE REACHABLE");
68    }
69
70
71    public Object JavaDoc visit(ASTCFlowExpression node, Object JavaDoc data)
72    {
73       return node.jjtGetChild(0).jjtAccept(this, data);
74    }
75
76    public Object JavaDoc visit(ASTCFlowBoolean node, Object JavaDoc data)
77    {
78       return node.jjtGetChild(0).jjtAccept(this, data);
79    }
80
81    public Object JavaDoc visit(ASTNotCFlow node, Object JavaDoc data)
82    {
83       Boolean JavaDoc bool = (Boolean JavaDoc) node.jjtGetChild(0).jjtAccept(this, data);
84       boolean val = bool.booleanValue();
85       return val ? Boolean.FALSE : Boolean.TRUE;
86    }
87
88    public Object JavaDoc visit(ASTCompositeCFlow node, Object JavaDoc data)
89    {
90       return node.jjtGetChild(0).jjtAccept(this, data);
91    }
92
93    public Object JavaDoc visit(ASTSubCFlow node, Object JavaDoc data)
94    {
95       for (int i = 0; i < node.jjtGetNumChildren(); i++)
96       {
97          data = node.jjtGetChild(i).jjtAccept(this, data);
98       }
99       return data;
100    }
101
102    public Object JavaDoc visit(ASTAndCFlow node, Object JavaDoc left)
103    {
104       Node andChild = node.jjtGetChild(0); // should only have one child
105
boolean val = ((Boolean JavaDoc) left).booleanValue();
106       return new Boolean JavaDoc(val && ((Boolean JavaDoc) andChild.jjtAccept(this, Boolean.FALSE)).booleanValue());
107    }
108
109    public Object JavaDoc visit(ASTOrCFlow node, Object JavaDoc left)
110    {
111       Node orChild = node.jjtGetChild(0); // should only have one child
112
boolean val = ((Boolean JavaDoc) left).booleanValue();
113       return new Boolean JavaDoc(val || ((Boolean JavaDoc) orChild.jjtAccept(this, Boolean.FALSE)).booleanValue());
114    }
115
116    public Object JavaDoc visit(ASTCFlow node, Object JavaDoc data)
117    {
118       AspectManager manager = null;
119       if (invocation.getAdvisor() == null)
120          manager = AspectManager.instance();
121       else
122          manager = invocation.getAdvisor().getManager();
123       CFlowStack cflow = manager.getCFlowStack(node.getPointcutName());
124       if (cflow != null) return new Boolean JavaDoc(cflow.matches(getStack()));
125
126       DynamicCFlow dcflow = manager.getDynamicCFlow(node.getPointcutName());
127       return new Boolean JavaDoc(dcflow.shouldExecute(invocation));
128    }
129 }
130
Popular Tags