KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > WithinCodePcd


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.crosscuts.ast;
26 import org.aspectj.compiler.base.ast.*;
27 import org.aspectj.compiler.crosscuts.joinpoints.*;
28
29 import org.aspectj.compiler.base.JavaCompiler;
30 import org.aspectj.util.FuzzyBoolean;
31
32 import java.util.*;
33
34 /**
35   * @grammar withincode(codePattern)
36   * @child CodePattern codePattern
37   */

38 public class WithinCodePcd extends Pcd {
39     public String JavaDoc toShortString() {
40         return "withincode(" + getCodePattern().toShortString() + ")";
41     }
42     public void checkStatic() { }
43
44     /**
45      * loop to find enclosing code decs of inner types
46      */

47     private CodeDec matchingEnclosingCodeDec(CodeDec codeDec) {
48         while (codeDec != null) {
49             //System.out.println("within? " + codeDec.toShortString() + " in " + point);
50
JoinPoint jp;
51             if (codeDec instanceof ConstructorDec) {
52                 jp = new ConstructorExecutionJp((ConstructorDec)codeDec);
53             } else if (codeDec instanceof MethodDec) {
54                 jp = new MethodExecutionJp((MethodDec)codeDec);
55             } else {
56                 ///???
57
return null;
58             }
59             
60             
61             if (getCodePattern().matches(jp).alwaysTrue()) return codeDec;
62             
63             if (!codeDec.getDeclaringType().getTypeDec().isLocal()) break;
64             codeDec = codeDec.getEnclosingCodeDec();
65         }
66         
67         return null;
68     }
69     
70
71     public JpPlanner makePlanner(PlanData planData) {
72         return new JpPlanner() {
73             public FuzzyBoolean fastMatch(JoinPoint jp) {
74                 ASTObject sourceLocation = jp.getSourceLocation();
75                 if (sourceLocation == null) return FuzzyBoolean.NEVER;
76                 
77                 CodeDec codeDec;
78                 if (sourceLocation instanceof CodeDec) {
79                     codeDec = (CodeDec)sourceLocation;
80                 } else {
81                     codeDec = sourceLocation.getEnclosingCodeDec();
82                 }
83                 if (codeDec == null) return FuzzyBoolean.NO;
84                 
85                 codeDec = matchingEnclosingCodeDec(codeDec);
86                 if (codeDec == null) return FuzzyBoolean.NO;
87                 else return FuzzyBoolean.YES;
88             }
89         };
90     }
91     
92     //BEGIN: Generated from @child and @property
93
protected CodePattern codePattern;
94     public CodePattern getCodePattern() { return codePattern; }
95     public void setCodePattern(CodePattern _codePattern) {
96         if (_codePattern != null) _codePattern.setParent(this);
97         codePattern = _codePattern;
98     }
99     
100     public WithinCodePcd(SourceLocation location, CodePattern _codePattern) {
101         super(location);
102         setCodePattern(_codePattern);
103     }
104     protected WithinCodePcd(SourceLocation source) {
105         super(source);
106     }
107     
108     public ASTObject copyWalk(CopyWalker walker) {
109         WithinCodePcd ret = new WithinCodePcd(getSourceLocation());
110         ret.preCopy(walker, this);
111         if (codePattern != null) ret.setCodePattern( (CodePattern)walker.process(codePattern) );
112         return ret;
113     }
114     
115     public ASTObject getChildAt(int childIndex) {
116         switch(childIndex) {
117         case 0: return codePattern;
118         default: return super.getChildAt(childIndex);
119         }
120     }
121      public String JavaDoc getChildNameAt(int childIndex) {
122         switch(childIndex) {
123         case 0: return "codePattern";
124         default: return super.getChildNameAt(childIndex);
125         }
126     }
127      public void setChildAt(int childIndex, ASTObject child) {
128         switch(childIndex) {
129         case 0: setCodePattern((CodePattern)child); return;
130         default: super.setChildAt(childIndex, child); return;
131         }
132     }
133      public int getChildCount() {
134         return 1;
135     }
136     
137     public String JavaDoc getDefaultDisplayName() {
138         return "WithinCodePcd()";
139     }
140     
141     //END: Generated from @child and @property
142
}
143
144
Popular Tags