]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.compare; | |
12 | ||
13 | import java.util.Iterator; | |
14 | import java.util.Stack; | |
15 | ||
16 | import org.eclipse.jdt.core.dom.ASTNode; | |
17 | import org.eclipse.jdt.core.dom.ASTVisitor; | |
18 | import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration; | |
19 | import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration; | |
20 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
21 | import org.eclipse.jdt.core.dom.EnumConstantDeclaration; | |
22 | import org.eclipse.jdt.core.dom.EnumDeclaration; | |
23 | import org.eclipse.jdt.core.dom.FieldDeclaration; | |
24 | import org.eclipse.jdt.core.dom.ImportDeclaration; | |
25 | import org.eclipse.jdt.core.dom.Initializer; | |
26 | import org.eclipse.jdt.core.dom.MethodDeclaration; | |
27 | import org.eclipse.jdt.core.dom.PackageDeclaration; | |
28 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; | |
29 | import org.eclipse.jdt.core.dom.Type; | |
30 | import org.eclipse.jdt.core.dom.TypeDeclaration; | |
31 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment; | |
32 | ||
33 | ||
34 | class JavaParseTreeBuilder extends ASTVisitor { | |
35 | ||
36 | private char[] fBuffer; | |
37 | Stack<JavaNode> fStack= new Stack<JavaNode>(); | |
38 | JavaNode fImportContainer; | |
39 | private boolean fShowCU; | |
40 | ||
41 | /* | |
42 | * Parsing is performed on the given buffer and the resulting tree (if any) | |
43 | * hangs below the given root. | |
44 | */ | |
45 | JavaParseTreeBuilder(JavaNode root, char[] buffer, boolean showCU) { | |
46 | fBuffer= buffer; | |
47 | fShowCU= showCU; | |
48 | fStack.clear(); | |
49 | fStack.push(root); | |
50 | } | |
51 | ||
52 | @Override | |
53 | public boolean visit(PackageDeclaration node) { | |
54 | new JavaNode(getCurrentContainer(), JavaNode.PACKAGE, null, node.getStartPosition(), node.getLength()); | |
55 | return false; | |
56 | } | |
57 | ||
58 | @Override | |
59 | public boolean visit(CompilationUnit node) { | |
60 | if (fShowCU) | |
61 | push(JavaNode.CU, null, node.getStartPosition(), node.getLength()); | |
62 | return true; | |
63 | } | |
64 | ||
65 | @Override | |
66 | public void endVisit(CompilationUnit node) { | |
67 | if (fShowCU) | |
68 | pop(); | |
69 | } | |
70 | ||
71 | @Override | |
72 | public boolean visit(TypeDeclaration node) { | |
73 | push(node.isInterface() ? JavaNode.INTERFACE : JavaNode.CLASS, node.getName().toString(), node.getStartPosition(), node.getLength()); | |
74 | return true; | |
75 | } | |
76 | ||
77 | @Override | |
78 | public void endVisit(TypeDeclaration node) { | |
79 | pop(); | |
80 | } | |
81 | ||
82 | @Override | |
83 | public boolean visit(EnumDeclaration node) { | |
84 | push(JavaNode.ENUM, node.getName().toString(), node.getStartPosition(), node.getLength()); | |
85 | return true; | |
86 | } | |
87 | ||
88 | @Override | |
89 | public void endVisit(EnumDeclaration node) { | |
90 | pop(); | |
91 | } | |
92 | ||
93 | @Override | |
94 | public boolean visit(AnnotationTypeDeclaration node) { | |
95 | push(JavaNode.ANNOTATION, node.getName().toString(), node.getStartPosition(), node.getLength()); | |
96 | return true; | |
97 | } | |
98 | ||
99 | @Override | |
100 | public void endVisit(AnnotationTypeDeclaration node) { | |
101 | pop(); | |
102 | } | |
103 | ||
104 | @Override | |
105 | public boolean visit(AnnotationTypeMemberDeclaration node) { | |
106 | push(JavaNode.METHOD, getSignature(node), node.getStartPosition(), node.getLength()); | |
107 | return true; | |
108 | } | |
109 | ||
110 | @Override | |
111 | public void endVisit(AnnotationTypeMemberDeclaration node) { | |
112 | pop(); | |
113 | } | |
114 | ||
115 | @Override | |
116 | public boolean visit(MethodDeclaration node) { | |
117 | String signature= getSignature(node); | |
118 | push(node.isConstructor() ? JavaNode.CONSTRUCTOR : JavaNode.METHOD, signature, node.getStartPosition(), node.getLength()); | |
119 | return false; | |
120 | } | |
121 | ||
122 | @Override | |
123 | public void endVisit(MethodDeclaration node) { | |
124 | pop(); | |
125 | } | |
126 | ||
127 | @Override | |
128 | public boolean visit(Initializer node) { | |
129 | push(JavaNode.INIT, getCurrentContainer().getInitializerCount(), node.getStartPosition(), node.getLength()); | |
130 | return false; | |
131 | } | |
132 | ||
133 | @Override | |
134 | public void endVisit(Initializer node) { | |
135 | pop(); | |
136 | } | |
137 | ||
138 | @Override | |
139 | public boolean visit(ImportDeclaration node) { | |
140 | int s= node.getStartPosition(); | |
141 | int l= node.getLength(); | |
142 | int declarationEnd= s + l; | |
143 | if (fImportContainer == null) | |
144 | fImportContainer= new JavaNode(getCurrentContainer(), JavaNode.IMPORT_CONTAINER, null, s, l); | |
145 | fImportContainer.generated_4748228892964526980(node, s, l, declarationEnd); // FIXME | |
146 | return false; | |
147 | } | |
148 | ||
149 | @Override | |
150 | public boolean visit(VariableDeclarationFragment node) { | |
151 | String name= getFieldName(node); | |
152 | ASTNode parent= node.getParent(); | |
153 | push(JavaNode.FIELD, name, parent.getStartPosition(), parent.getLength()); | |
154 | return false; | |
155 | } | |
156 | ||
157 | @Override | |
158 | public void endVisit(VariableDeclarationFragment node) { | |
159 | pop(); | |
160 | } | |
161 | ||
162 | @Override | |
163 | public boolean visit(EnumConstantDeclaration node) { | |
164 | push(JavaNode.FIELD, node.getName().toString(), node.getStartPosition(), node.getLength()); | |
165 | return false; | |
166 | } | |
167 | ||
168 | @Override | |
169 | public void endVisit(EnumConstantDeclaration node) { | |
170 | pop(); | |
171 | } | |
172 | ||
173 | // private stuff | |
174 | ||
175 | /** | |
176 | * Adds a new JavaNode with the given type and name to the current | |
177 | * container. | |
178 | */ | |
179 | private void push(int type, String name, int declarationStart, int length) { | |
180 | ||
181 | while (declarationStart > 0) { | |
182 | char c= fBuffer[declarationStart - 1]; | |
183 | if (c != ' ' && c != '\t') | |
184 | break; | |
185 | declarationStart--; | |
186 | length++; | |
187 | } | |
188 | ||
189 | JavaNode node= new JavaNode(getCurrentContainer(), type, name, declarationStart, length); | |
190 | node.generated_8831101120029137518(type, declarationStart, length, this); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Closes the current Java node by setting its end position and pops it off | |
195 | * the stack. | |
196 | */ | |
197 | private void pop() { | |
198 | fStack.pop(); | |
199 | } | |
200 | ||
201 | private JavaNode getCurrentContainer() { | |
202 | return fStack.peek(); | |
203 | } | |
204 | ||
205 | private String getFieldName(VariableDeclarationFragment node) { | |
206 | StringBuffer buffer= new StringBuffer(); | |
207 | buffer.append(node.getName().toString()); | |
208 | ASTNode parent= node.getParent(); | |
209 | if (parent instanceof FieldDeclaration) { | |
210 | FieldDeclaration fd= (FieldDeclaration) parent; | |
211 | buffer.append(" : "); //$NON-NLS-1$ | |
212 | buffer.append(getType(fd.getType())); | |
213 | } | |
214 | return buffer.toString(); | |
215 | } | |
216 | ||
217 | private String getSignature(MethodDeclaration node) { | |
218 | StringBuffer buffer= new StringBuffer(); | |
219 | buffer.append(node.getName().toString()); | |
220 | buffer.append('('); | |
221 | boolean first= true; | |
222 | Iterator<SingleVariableDeclaration> iterator= node.parameters().iterator(); | |
223 | while (iterator.hasNext()) { | |
224 | SingleVariableDeclaration svd= iterator.next(); | |
225 | if (!first) | |
226 | buffer.append(", "); //$NON-NLS-1$ | |
227 | buffer.append(getType(svd.getType())); | |
228 | if (svd.isVarargs()) | |
229 | buffer.append("..."); //$NON-NLS-1$ | |
230 | first= false; | |
231 | } | |
232 | buffer.append(')'); | |
233 | return buffer.toString(); | |
234 | } | |
235 | ||
236 | private String getSignature(AnnotationTypeMemberDeclaration node) { | |
237 | StringBuffer buffer= new StringBuffer(); | |
238 | buffer.append(node.getName().toString()); | |
239 | buffer.append('('); | |
240 | buffer.append(')'); | |
241 | return buffer.toString(); | |
242 | } | |
243 | ||
244 | private String getType(Type type) { | |
245 | String name= type.toString(); | |
246 | int pos= name.lastIndexOf('.'); | |
247 | if (pos >= 0) | |
248 | name= name.substring(pos + 1); | |
249 | return name; | |
250 | } | |
251 | } |