KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > SemanticMap


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.base.ast;
26
27 import java.util.*;
28 import org.aspectj.compiler.base.*;
29
30 public class SemanticMap extends CompilerObject {
31     protected final Map map = new HashMap();
32     protected final Type type;
33
34     protected String JavaDoc kind;
35
36     public SemanticMap(Type type, String JavaDoc kind) {
37         super(type.getCompiler());
38         this.type = type;
39         this.kind = kind;
40     }
41
42     public SemanticObject get(String JavaDoc id, ASTObject fromWhere, Exprs params, boolean showError) {
43         //System.out.println(map);
44

45         SemanticGroup semanticGroup = (SemanticGroup)map.get(id);
46         if (semanticGroup == null) {
47             //System.out.println("no map for " + id);
48
if (showError) {
49                 showNotFoundError(id, fromWhere);
50             }
51             return getNotFoundSemanticObject();
52         }
53         return semanticGroup.resolve(fromWhere, params, showError);
54     }
55
56     public void addDeclared(SemanticObject objects) {
57         String JavaDoc id = objects.getId();
58         //System.out.println("adding " + dec + " id: " + id);
59
SemanticGroup semanticGroup = (SemanticGroup)map.get(id);
60         if (semanticGroup == null) {
61             map.put(id, makeSemanticGroup(objects));
62         } else {
63             semanticGroup.addDeclaredSO(objects);
64         }
65         //System.out.println(map);
66
}
67
68     public SemanticObject addIntroduced(SemanticObject objects) {
69         String JavaDoc id = objects.getId();
70         //System.out.println("adding " + dec + " id: " + id);
71
SemanticGroup semanticGroup = (SemanticGroup)map.get(id);
72         if (semanticGroup == null) {
73             map.put(id, makeSemanticGroup(objects));
74             return null;
75         } else {
76             return semanticGroup.addIntroducedSO(objects);
77         }
78         //System.out.println(map);
79
}
80
81     public boolean hasName(String JavaDoc id) {
82 // System.out.println("hasName? " + id + " in " + map);
83
// System.out.println(" for type " + type);
84

85         return map.get(id) != null;
86     }
87
88     public void addInherited(SemanticMap other) {
89         for (Iterator i = other.map.values().iterator(); i.hasNext(); ) {
90             SemanticGroup superGroup = (SemanticGroup)i.next();
91             String JavaDoc name = superGroup.getName();
92             SemanticGroup myGroup = (SemanticGroup)map.get(name);
93             if (myGroup == null) {
94                 myGroup = superGroup.makeInheritedCopy(this);
95             } else {
96                 myGroup.inheritFrom(superGroup);
97             }
98             
99             if (!myGroup.isEmpty()) map.put(name, myGroup);
100         }
101     }
102
103     public Collection getAllSemanticObjects() {
104         List ret = new LinkedList();
105         for (Iterator i = map.values().iterator(); i.hasNext(); ) {
106             SemanticGroup group = (SemanticGroup)i.next();
107             ret.addAll(group.members);
108         }
109         return ret;
110     }
111
112     public void ensureNoAbstracts() {
113         for (Iterator i = getAllSemanticObjects().iterator(); i.hasNext(); ) {
114             SemanticObject object = (SemanticObject)i.next();
115             if (object.isAbstract()) {
116                 if (object.getDeclaringType() == getType()) {
117                     object.getCorrespondingDec().showError("abstract " +
118                             getSemanticObjectTypeName() +
119                             " not allowed in concrete type " + getType().toShortString());
120                 } else {
121                     getType().getTypeDec().showError("concrete type " +
122                         getType().toShortString() + " must implement " +
123                         getSemanticObjectTypeName() + " " + object.toShortString());
124                 }
125             }
126         }
127     }
128
129     // onlys used by CodePattern
130
public SemanticObject findMatchingSemanticObject(SemanticObject matchSemanticObject) {
131         for (Iterator i = getAllSemanticObjects().iterator(); i.hasNext(); ) {
132             SemanticObject object = (SemanticObject)i.next();
133 // System.out.println(" matches? " + object + " == " + matchSemanticObject);
134
if (object.conflictsWith(matchSemanticObject)) return object;
135         }
136         return null;
137     }
138
139
140     protected SemanticObject getNotFoundSemanticObject() { return null; }
141     protected String JavaDoc getSemanticObjectTypeName() { return kind; }
142
143     public Type getType() { return type; }
144
145     protected SemanticGroup makeSemanticGroup(SemanticObject object) {
146         return new SemanticGroup(this, object);
147     }
148
149     protected void showNotFoundError(String JavaDoc id, ASTObject fromWhere) {
150         //System.out.println(map);
151
getCompiler().showError(fromWhere, "no " + getSemanticObjectTypeName() + " named "
152                         + id + " defined in " + type.toShortString());
153     }
154 }
155
Popular Tags