KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > hcr > JavaParseTreeBuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.core.hcr;
12
13  
14 import java.util.Stack JavaDoc;
15
16 import org.eclipse.jdt.internal.compiler.SourceElementRequestorAdapter;
17 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
18
19 class JavaParseTreeBuilder extends SourceElementRequestorAdapter implements ICompilationUnit {
20     
21     private static final boolean SHOW_COMPILATIONUNIT= true;
22
23     private char[] fBuffer;
24     private JavaNode fImportContainer;
25     private Stack JavaDoc fStack= new Stack JavaDoc();
26     
27     /**
28      * Parsing is performed on the given buffer and the resulting tree
29      * (if any) hangs below the given root.
30      */

31     JavaParseTreeBuilder(JavaNode root, char[] buffer) {
32         fImportContainer= null;
33         fStack.clear();
34         fStack.push(root);
35         fBuffer= buffer;
36     }
37             
38     //---- ICompilationUnit
39
/* (non Java doc)
40      * @see ICompilationUnit#getContents
41      */

42     public char[] getContents() {
43         return fBuffer;
44     }
45     
46     /* (non Java doc)
47      * @see ICompilationUnit#getFileName
48      */

49     public char[] getFileName() {
50         return new char[0];
51     }
52     
53     /* (non Java doc)
54      * @see ICompilationUnit#getMainTypeName
55      */

56     public char[] getMainTypeName() {
57         return new char[0];
58     }
59     
60     /* (non Java doc)
61      * @see ICompilationUnit#getMainTypeName
62      */

63     public char[][] getPackageName() {
64         return null;
65     }
66     
67     //---- ISourceElementRequestor
68

69     public void enterCompilationUnit() {
70         if (SHOW_COMPILATIONUNIT)
71             push(JavaNode.CU, null, 0);
72     }
73     
74     public void exitCompilationUnit(int declarationEnd) {
75         if (SHOW_COMPILATIONUNIT)
76             pop(declarationEnd);
77     }
78     
79     public void acceptPackage(int declarationStart, int declarationEnd, char[] p3) {
80         push(JavaNode.PACKAGE, null, declarationStart);
81         pop(declarationEnd);
82     }
83     
84     public void acceptImport(int declarationStart, int declarationEnd, char[] name, boolean onDemand, int modifiers) {
85         int length= declarationEnd-declarationStart+1;
86         if (fImportContainer == null)
87             fImportContainer= new JavaNode(getCurrentContainer(), JavaNode.IMPORT_CONTAINER, null, declarationStart, length);
88         String JavaDoc nm= new String JavaDoc(name);
89         if (onDemand)
90             nm+= ".*"; //$NON-NLS-1$
91
new JavaNode(fImportContainer, JavaNode.IMPORT, nm, declarationStart, length);
92         fImportContainer.setLength(declarationEnd-fImportContainer.getStart()+1);
93     }
94     
95     public void enterClass(int declarationStart, int p2, char[] name, int p4, int p5, char[] p6, char[][] p7) {
96         push(JavaNode.CLASS, new String JavaDoc(name), declarationStart);
97     }
98     
99     public void exitClass(int declarationEnd) {
100         pop(declarationEnd);
101     }
102
103     public void enterInterface(int declarationStart, int p2, char[] name, int p4, int p5, char[][] p6) {
104         push(JavaNode.INTERFACE, new String JavaDoc(name), declarationStart);
105     }
106     
107     public void exitInterface(int declarationEnd) {
108         pop(declarationEnd);
109     }
110     
111     public void enterInitializer(int declarationSourceStart, int modifiers) {
112         push(JavaNode.INIT, getCurrentContainer().getInitializerCount(), declarationSourceStart);
113     }
114     
115     public void enterConstructor(int declarationStart, int p2, char[] name, int p4, int p5, char[][] parameterTypes, char[][] p7, char[][] p8) {
116         push(JavaNode.CONSTRUCTOR, getSignature(name, parameterTypes), declarationStart);
117     }
118     
119     public void exitConstructor(int declarationEnd) {
120         pop(declarationEnd);
121     }
122     
123     public void enterMethod(int declarationStart, int p2, char[] p3, char[] name, int p5, int p6, char[][] parameterTypes, char[][] p8, char[][] p9){
124         push(JavaNode.METHOD, getSignature(name, parameterTypes), declarationStart);
125     }
126     
127     public void exitMethod(int declarationEnd) {
128         pop(declarationEnd);
129     }
130     
131     public void enterField(int declarationStart, int p2, char[] p3, char[] name, int p5, int p6) {
132         push(JavaNode.FIELD, new String JavaDoc(name), declarationStart);
133     }
134     
135     public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
136         pop(declarationEnd);
137     }
138
139     private JavaNode getCurrentContainer() {
140         return (JavaNode) fStack.peek();
141     }
142     
143     /**
144      * Adds a new JavaNode with the given type and name to the current container.
145      */

146     private void push(int type, String JavaDoc name, int declarationStart) {
147                         
148         while (declarationStart > 0) {
149             char c= fBuffer[declarationStart-1];
150             if (c != ' ' && c != '\t')
151                 break;
152             declarationStart--;
153         }
154                     
155         fStack.push(new JavaNode(getCurrentContainer(), type, name, declarationStart, 0));
156     }
157     
158     /**
159      * Closes the current Java node by setting its end position
160      * and pops it off the stack.
161      */

162     private void pop(int declarationEnd) {
163         JavaNode current= getCurrentContainer();
164         current.setLength(declarationEnd - current.getStart() + 1);
165         fStack.pop();
166     }
167     
168     /**
169      * Builds a signature string from the given name and parameter types.
170      */

171     public String JavaDoc getSignature(char[] name, char[][] parameterTypes) {
172         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
173         buffer.append(name);
174         buffer.append('(');
175         if (parameterTypes != null) {
176             for (int p= 0; p < parameterTypes.length; p++) {
177                 String JavaDoc parameterType= new String JavaDoc(parameterTypes[p]);
178                 
179                 // PR 1GF9WH7: ITPJUI:WINNT - Cannot replace main from local history
180
// we only use the last component of a type name
181
int pos= parameterType.lastIndexOf('.');
182                 if (pos >= 0)
183                     parameterType= parameterType.substring(pos+1);
184                 // end fix
185

186                 buffer.append(parameterType);
187                 if (p < parameterTypes.length-1)
188                     buffer.append(", "); //$NON-NLS-1$
189
}
190         }
191         buffer.append(')');
192         return buffer.toString();
193     }
194     
195     /**
196      * @see org.eclipse.jdt.internal.compiler.ISourceElementRequestor#exitInitializer(int)
197      */

198     public void exitInitializer(int declarationEnd) {
199         pop(declarationEnd);
200     }
201
202 }
203
Popular Tags