KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > AJCClassNode


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger 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 package org.aspectj.debugger.gui;
23
24 import org.aspectj.tools.ide.*;
25 import com.sun.jdi.*;
26 import java.util.*;
27 import javax.swing.tree.*;
28
29 public class AJCClassNode extends ClassNode {
30
31     private String JavaDoc name;
32     private Declaration classDec;
33     private boolean isAspect;
34
35     public AJCClassNode(String JavaDoc name) {
36         this(null, name);
37     }
38
39     public AJCClassNode(Declaration classDec) {
40         super(null);
41         this.classDec = classDec;
42         init(null, classDec.getSignature());
43     }
44
45     public AJCClassNode(ReferenceType refType) {
46         this(refType, refType.name());
47     }
48
49     private AJCClassNode(ReferenceType refType, String JavaDoc name) {
50         super(refType);
51         init(refType, name);
52     }
53
54     private void init(ReferenceType refType, String JavaDoc name) {
55         this.name = name;
56         addKids();
57     }
58
59     protected String JavaDoc name() {
60         return name;
61     }
62
63     protected String JavaDoc packageName() {
64         return classDec.getPackageName();
65     }
66
67     protected boolean callsAddKids() {
68         return true;
69     }
70
71     public int getType() {
72         if (isAspect) return AJIcons.ASPECT_ICON;
73         if (kind().equals("class")) return AJIcons.CLASS_ICON;
74         return AJIcons.INTERFACE_ICON;
75     }
76
77     public void addMethods() {
78         if (name == null) return;
79         if (classDec == null) {
80             String JavaDoc fileName = AJLineMapper.getSourceFilePathFromAJCClass(name);
81             if (fileName == null) return;
82             Declaration[] decs = SymbolManager.getSymbolManager().getDeclarations(fileName);
83             classDec = classDec(decs);
84         }
85         if (classDec == null) return;
86         addMethods(classDec);
87         addAdvices(classDec);
88     }
89
90     public static AJCClassNode[] makeNodes(String JavaDoc fileName) {
91         Declaration[] decs = SymbolManager.getSymbolManager().getDeclarations(fileName);
92         if (decs == null) return new AJCClassNode[0];
93         List classNodes = new Vector();
94         for (int i = 0; i < decs.length; i++) {
95             Declaration dec = decs[i];
96             if (dec != null) classNodes.add(new AJCClassNode(dec));
97         }
98         int N = classNodes.size();
99         AJCClassNode[] nodes = new AJCClassNode[N];
100         int i = 0;
101         for (Iterator iter = classNodes.iterator(); iter.hasNext() && i < N; i++) {
102             nodes[i] = (AJCClassNode) iter.next();
103         }
104         return nodes;
105     }
106
107     public String JavaDoc getName() {
108         return name;
109     }
110
111     private void addAdvices(Declaration classDec) {
112         addAdvices(classDec.getDeclarations());
113     }
114
115     private void addAdvices(Declaration[] decs) {
116         if (decs == null) return;
117         for (int i = 0; i < decs.length; i++) {
118             Declaration dec = decs[i];
119             if (dec != null && dec.getKind().equals("advice")) {
120                 addAdvice(dec);
121             }
122         }
123     }
124
125     private void addMethods(Declaration classDec) {
126         addMethods(classDec.getDeclarations());
127     }
128
129     private void addMethods(Declaration[] decs) {
130         if (decs == null) return;
131         for (int i = 0; i < decs.length; i++) {
132             Declaration dec = decs[i];
133             if (dec != null && isMethod(dec)) {
134                 addMethod(dec);
135             }
136         }
137     }
138
139     private boolean isMethod(Declaration dec) {
140         return dec.getKind().equals("method") || dec.getKind().equals("constructor");
141     }
142
143     private void addAdvice(Declaration dec) {
144         AdviceNode adviceNode = new AdviceNode(dec);
145         add(adviceNode);
146         isAspect = true;
147         addMethods(adviceNode, dec);
148     }
149
150     private void addMethod(Declaration dec) {
151         if (dec == null) return;
152         MethodNode methodNode = new AdvisableMethodNode(dec);
153         add(methodNode);
154         addAdvice(methodNode, dec);
155     }
156
157     private void addMethods(AdviceNode adviceNode, Declaration dec) {
158         Declaration[] pointsTo = dec.getPointsTo();
159         if (pointsTo == null) return;
160         for (int i = 0; i < pointsTo.length; i++) {
161             Declaration methodDec = pointsTo[i];
162             if (methodDec != null) {
163                 MethodNode methodNode = new AdvisableMethodNode(methodDec);
164                 adviceNode.add(methodNode);
165             }
166         }
167     }
168
169     private void addAdvice(MethodNode methodNode, Declaration dec) {
170         Declaration[] pointedToBy = dec.getPointedToBy();
171         if (pointedToBy == null) return;
172         for (int i = 0; i < pointedToBy.length; i++) {
173             Declaration advice = pointedToBy[i];
174             if (advice != null) {
175                 AdviceNode adviceNode = new AdviceNode(advice);
176                 methodNode.add(adviceNode);
177             }
178         }
179     }
180
181     private Declaration classDec(Declaration[] decs) {
182         if (decs == null) return null;
183         List q = new Vector();
184         for (int i = 0; i < decs.length; i++) {
185             q.add(decs[i]);
186         }
187         while (!q.isEmpty()) {
188             Declaration dec = (Declaration) q.remove(0);
189             if (dec != null && isType(dec)) {
190                 String JavaDoc signature = dec.getSignature();
191                 String JavaDoc packageName = dec.getPackageName();
192                 String JavaDoc fullSignature;
193                 if (packageName != null && !"".equals(packageName)) {
194                     fullSignature = packageName + "." + signature;
195                 } else {
196                     fullSignature = signature;
197                 }
198                 fullSignature = fullSignature.trim();
199                 //if (dec.getSignature().equals(name)) return dec;
200
if (fullSignature.equals(name)) return dec;
201                 Declaration[] newDecs = dec.getDeclarations();
202                 if (newDecs != null) {
203                     for (int i = 0; i < newDecs.length; i++) {
204                         q.add(newDecs[i]);
205                     }
206                 }
207             }
208         } while (!q.isEmpty());
209         return null;
210     }
211
212     private boolean isType(Declaration dec) {
213         return dec.getKind().equals("class") || dec.getKind().equals("interface");
214     }
215
216     protected String JavaDoc kind() {
217         if (isAspect) return "aspect";
218         if (classDec == null) {
219             return "class";
220         }
221         return classDec.getKind();
222     }
223
224     public String JavaDoc getRelativePath() {
225         return classDec.getFilename();
226     }
227
228     public int getLine() {
229         return classDec.getBeginLine();
230     }
231
232     public List getAdviceStrings() {
233         if (!isAspect) return null;
234         List adviceStrings = new Vector();
235         if (children == null) return adviceStrings;
236         Iterator iter = children.iterator();
237         while (iter.hasNext()) {
238             TreeNode treeNode = (TreeNode) iter.next();
239             if (treeNode instanceof AdviceNode) {
240                 adviceStrings.add(((AdviceNode) treeNode).getBreakpoint());
241             }
242         }
243         return adviceStrings;
244     }
245 }
246
Popular Tags