KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.aspectj.util.FuzzyBoolean;
29
30 import org.aspectj.compiler.base.JavaCompiler;
31
32 import java.util.*;
33
34 /**
35   * @grammar ???
36   * @child Modifiers modifiers
37   * @child GenTypeName declaringTypeName
38   */

39 public abstract class DecPattern extends ASTObject {
40     public abstract String JavaDoc toShortString();
41
42     protected String JavaDoc makeDeclaringTypeString() {
43         if (getDeclaringTypeName() == null) return "";
44         return getDeclaringTypeName().toShortString() + '.';
45     }
46
47     public abstract String JavaDoc getLookupId();
48
49     private boolean shownWarning = false;
50
51     public FuzzyBoolean matches(JoinPoint jp) {
52         if (!(modifiers.matches(jp.getTargetSO().getModifiers()))) {
53             return FuzzyBoolean.NO;
54         }
55
56         if (getDeclaringTypeName() != null) {
57             Type targetType = jp.getTargetType();
58             if (targetType == null) {
59                 for (Iterator i = jp.getTargetTypes().iterator(); i.hasNext(); ) {
60                     targetType = (Type)i.next();
61 // System.out.println("matching " + targetType + " with " +
62
// getDeclaringTypeName());
63
if (!matchDeclaringTypeName(jp, targetType, jp.getTargetSO())) {
64                         return FuzzyBoolean.NEVER;
65                     }
66                     //System.out.println(" MATCHED!");
67
}
68                 return FuzzyBoolean.YES;
69             } else {
70                 if(matchDeclaringTypeName(jp, jp.getTargetType(), jp.getTargetSO())) {
71                     return FuzzyBoolean.YES;
72                 } else {
73                     return FuzzyBoolean.NO;
74                 }
75             }
76         } else {
77             return FuzzyBoolean.YES;
78         }
79     }
80
81
82     protected boolean isStaticMatch(SemanticObject so) {
83         return so.isStatic();
84     }
85     
86     boolean matchDeclaringTypeName(JoinPoint joinPoint, Type inType, SemanticObject so) {
87         //System.out.println(" matching dec? " + inType + ": " + so.toShortString());
88

89         //XXX This design is very brittle and depends on the details of SemanticObjects
90
//XXX and the way that they are held in Types
91
SemanticObject matchingSO = inType.findMatchingSemanticObject(so);
92         if (matchingSO == null) return false;
93         
94         if (isStaticMatch(so)) {
95             if (matchingSO != so) return false;
96         }
97
98         if (getDeclaringTypeName().matches(inType)) {
99             return true;
100         }
101
102         //??? for fields, static methods and constructors this can probably be optimized
103
//??? to only go up the class hierarchy ignoring interfaces
104
for (Iterator i = inType.getDirectSuperTypes().iterator(); i.hasNext(); ) {
105             if(matchDeclaringTypeName(joinPoint, (Type)i.next(), so)) return true;
106         }
107
108         return false;
109     }
110     
111     //BEGIN: Generated from @child and @property
112
protected Modifiers modifiers;
113     public Modifiers getModifiers() { return modifiers; }
114     public void setModifiers(Modifiers _modifiers) {
115         if (_modifiers != null) _modifiers.setParent(this);
116         modifiers = _modifiers;
117     }
118     
119     protected GenTypeName declaringTypeName;
120     public GenTypeName getDeclaringTypeName() { return declaringTypeName; }
121     public void setDeclaringTypeName(GenTypeName _declaringTypeName) {
122         if (_declaringTypeName != null) _declaringTypeName.setParent(this);
123         declaringTypeName = _declaringTypeName;
124     }
125     
126     public DecPattern(SourceLocation location, Modifiers _modifiers, GenTypeName _declaringTypeName) {
127         super(location);
128         setModifiers(_modifiers);
129         setDeclaringTypeName(_declaringTypeName);
130     }
131     protected DecPattern(SourceLocation source) {
132         super(source);
133     }
134     
135     public ASTObject getChildAt(int childIndex) {
136         switch(childIndex) {
137         case 0: return modifiers;
138         case 1: return declaringTypeName;
139         default: return super.getChildAt(childIndex);
140         }
141     }
142      public String JavaDoc getChildNameAt(int childIndex) {
143         switch(childIndex) {
144         case 0: return "modifiers";
145         case 1: return "declaringTypeName";
146         default: return super.getChildNameAt(childIndex);
147         }
148     }
149      public void setChildAt(int childIndex, ASTObject child) {
150         switch(childIndex) {
151         case 0: setModifiers((Modifiers)child); return;
152         case 1: setDeclaringTypeName((GenTypeName)child); return;
153         default: super.setChildAt(childIndex, child); return;
154         }
155     }
156      public int getChildCount() {
157         return 2;
158     }
159     
160     public String JavaDoc getDefaultDisplayName() {
161         return "DecPattern()";
162     }
163     
164     //END: Generated from @child and @property
165
}
166
167
Popular Tags