KickJava   Java API By Example, From Geeks To Geeks.

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


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
31 import java.util.*;
32 import org.aspectj.util.FuzzyBoolean;
33
34 /**
35   * @grammar modifiers returnTypeName [declaringTypeName].id(formals) throws
36   * @child GenTypeName returnTypeName
37   * @property NamePattern id
38   * @child FormalsPattern formalsPattern
39   * @child NameTypeDsPattern throwsPattern
40   */

41 public class MethodPattern extends CodePattern {
42     public String JavaDoc toShortString() {
43         //XXX this ignores throws for now
44
String JavaDoc mods = modifiers.toShortString();
45         if (mods.length() > 0) mods += " ";
46         mods += returnTypeName.toShortString()+" ";
47         String JavaDoc ret = mods + makeDeclaringTypeString() + id.toShortString() + formalsPattern.toShortString();
48
49         if (throwsPattern != null) {
50             ret += " throws ...";
51         }
52
53         return ret;
54     }
55
56     public void checkSpec() {
57         if (id.getSimpleName() != null && id.getSimpleName().equals("new")) {
58             this.showError("constructor patterns don't have a return type");
59         }
60     }
61
62     public String JavaDoc getLookupId() { return id.getLookupString(); }
63
64     public FuzzyBoolean matches(JoinPoint jp) {
65         if (! (jp.getTargetSO() instanceof Method) ) {
66             return FuzzyBoolean.NO;
67         }
68         
69         Method m = (Method)jp.getTargetSO();
70         
71         if (!id.matches(m.getName())) return FuzzyBoolean.NO;
72         if (!returnTypeName.matches(m.getReturnType())) return FuzzyBoolean.NO;
73         if (!formalsPattern.matches(m.getFormals())) return FuzzyBoolean.NO;
74         if (throwsPattern != null && !throwsPattern.matches(m.getThrows())) return FuzzyBoolean.NO;
75         
76         return super.matches(jp);
77     }
78     
79     //BEGIN: Generated from @child and @property
80
protected GenTypeName returnTypeName;
81     public GenTypeName getReturnTypeName() { return returnTypeName; }
82     public void setReturnTypeName(GenTypeName _returnTypeName) {
83         if (_returnTypeName != null) _returnTypeName.setParent(this);
84         returnTypeName = _returnTypeName;
85     }
86     
87     protected NamePattern id;
88     public NamePattern getId() { return id; }
89     public void setId(NamePattern _id) { id = _id; }
90     
91     protected FormalsPattern formalsPattern;
92     public FormalsPattern getFormalsPattern() { return formalsPattern; }
93     public void setFormalsPattern(FormalsPattern _formalsPattern) {
94         if (_formalsPattern != null) _formalsPattern.setParent(this);
95         formalsPattern = _formalsPattern;
96     }
97     
98     protected NameTypeDsPattern throwsPattern;
99     public NameTypeDsPattern getThrowsPattern() { return throwsPattern; }
100     public void setThrowsPattern(NameTypeDsPattern _throwsPattern) {
101         if (_throwsPattern != null) _throwsPattern.setParent(this);
102         throwsPattern = _throwsPattern;
103     }
104     
105     public MethodPattern(SourceLocation location, Modifiers _modifiers, GenTypeName _declaringTypeName, GenTypeName _returnTypeName, NamePattern _id, FormalsPattern _formalsPattern, NameTypeDsPattern _throwsPattern) {
106         super(location, _modifiers, _declaringTypeName);
107         setReturnTypeName(_returnTypeName);
108         setId(_id);
109         setFormalsPattern(_formalsPattern);
110         setThrowsPattern(_throwsPattern);
111     }
112     protected MethodPattern(SourceLocation source) {
113         super(source);
114     }
115     
116     public ASTObject copyWalk(CopyWalker walker) {
117         MethodPattern ret = new MethodPattern(getSourceLocation());
118         ret.preCopy(walker, this);
119         if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) );
120         if (declaringTypeName != null) ret.setDeclaringTypeName( (GenTypeName)walker.process(declaringTypeName) );
121         if (returnTypeName != null) ret.setReturnTypeName( (GenTypeName)walker.process(returnTypeName) );
122         ret.id = id;
123         if (formalsPattern != null) ret.setFormalsPattern( (FormalsPattern)walker.process(formalsPattern) );
124         if (throwsPattern != null) ret.setThrowsPattern( (NameTypeDsPattern)walker.process(throwsPattern) );
125         return ret;
126     }
127     
128     public ASTObject getChildAt(int childIndex) {
129         switch(childIndex) {
130         case 2: return returnTypeName;
131         case 3: return formalsPattern;
132         case 4: return throwsPattern;
133         default: return super.getChildAt(childIndex);
134         }
135     }
136      public String JavaDoc getChildNameAt(int childIndex) {
137         switch(childIndex) {
138         case 2: return "returnTypeName";
139         case 3: return "formalsPattern";
140         case 4: return "throwsPattern";
141         default: return super.getChildNameAt(childIndex);
142         }
143     }
144      public void setChildAt(int childIndex, ASTObject child) {
145         switch(childIndex) {
146         case 2: setReturnTypeName((GenTypeName)child); return;
147         case 3: setFormalsPattern((FormalsPattern)child); return;
148         case 4: setThrowsPattern((NameTypeDsPattern)child); return;
149         default: super.setChildAt(childIndex, child); return;
150         }
151     }
152      public int getChildCount() {
153         return 5;
154     }
155     
156     public String JavaDoc getDefaultDisplayName() {
157         return "MethodPattern(id: "+id+")";
158     }
159     
160     //END: Generated from @child and @property
161
}
162
163
Popular Tags