1 /* |
|
2 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.mirror.util; |
|
27 |
|
28 import com.sun.mirror.declaration.*; |
|
29 |
|
30 import java.util.SortedSet; |
|
31 import java.util.TreeSet; |
|
32 |
|
33 /** |
|
34 * A visitor for declarations that scans declarations contained within |
|
35 * the given declaration in source code order. For example, when |
|
36 * visiting a class, the methods, fields, constructors, and nested |
|
37 * types of the class are also visited. |
|
38 * |
|
39 * To control the processing done on a declaration, users of this |
|
40 * class pass in their own visitors for pre and post processing. The |
|
41 * preprocessing visitor is called before the contained declarations |
|
42 * are scanned; the postprocessing visitor is called after the |
|
43 * contained declarations are scanned. |
|
44 * |
|
45 * @deprecated All components of this API have been superseded by the |
|
46 * standardized annotation processing API. The replacement for the |
|
47 * functionality of this class is {@link |
|
48 * javax.lang.model.util.SimpleElementVisitor6}. |
|
49 * |
|
50 * @author Joseph D. Darcy |
|
51 * @author Scott Seligman |
|
52 * @since 1.5 |
|
53 */ |
|
54 @Deprecated |
|
55 @SuppressWarnings("deprecation") |
|
56 class SourceOrderDeclScanner extends DeclarationScanner { |
|
57 static class SourceOrderComparator implements java.util.Comparator<Declaration> { |
|
58 SourceOrderComparator(){} |
|
59 |
|
60 |
|
61 static boolean equals(Declaration d1, Declaration d2) { |
|
62 return d1 == d2 || (d1 != null && d1.equals(d2)); |
|
63 } |
|
64 |
|
65 private static class DeclPartialOrder extends com.sun.mirror.util.SimpleDeclarationVisitor { |
|
66 private int value = 1000; |
|
67 private static int staticAdjust(Declaration d) { |
|
68 return d.getModifiers().contains(Modifier.STATIC)?0:1; |
|
69 } |
|
70 |
|
71 DeclPartialOrder() {} |
|
72 |
|
73 public int getValue() { return value; } |
|
74 |
|
75 @Override |
|
76 public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {value = 0;} |
|
77 |
|
78 @Override |
|
79 public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {value = 1;} |
|
80 |
|
81 @Override |
|
82 public void visitClassDeclaration(ClassDeclaration d) {value = 2 + staticAdjust(d);} |
|
83 |
|
84 @Override |
|
85 public void visitInterfaceDeclaration(InterfaceDeclaration d) {value = 4;} |
|
86 |
|
87 @Override |
|
88 public void visitEnumDeclaration(EnumDeclaration d) {value = 6;} |
|
89 |
|
90 @Override |
|
91 public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {value = 8;} |
|
92 |
|
93 @Override |
|
94 public void visitFieldDeclaration(FieldDeclaration d) {value = 10 + staticAdjust(d);} |
|
95 |
|
96 @Override |
|
97 public void visitConstructorDeclaration(ConstructorDeclaration d) {value = 12;} |
|
98 |
|
99 @Override |
|
100 public void visitMethodDeclaration(MethodDeclaration d) {value = 14 + staticAdjust(d);} |
|
101 } |
|
102 @SuppressWarnings("cast") |
|
103 private int compareEqualPosition(Declaration d1, Declaration d2) { |
|
104 assert |
|
105 (d1.getPosition() == d2.getPosition()) || // Handles two null positions. |
|
106 (d1.getPosition().file().compareTo(d2.getPosition().file()) == 0 && |
|
107 d1.getPosition().line() == d2.getPosition().line() && |
|
108 d1.getPosition().column() == d2.getPosition().column()); |
|
109 |
|
110 DeclPartialOrder dpo1 = new DeclPartialOrder(); |
|
111 DeclPartialOrder dpo2 = new DeclPartialOrder(); |
|
112 |
|
113 d1.accept(dpo1); |
|
114 d2.accept(dpo2); |
|
115 |
|
116 int difference = dpo1.getValue() - dpo2.getValue(); |
|
117 if (difference != 0) |
|
118 return difference; |
|
119 else { |
|
120 int result = d1.getSimpleName().compareTo(d2.getSimpleName()); |
|
121 if (result != 0) |
|
122 return result; |
|
123 return (int)( Long.signum((long)System.identityHashCode(d1) - |
|
124 (long)System.identityHashCode(d2))); |
|
125 } |
|
126 } |
|
127 |
|
128 public int compare(Declaration d1, Declaration d2) { |
|
129 if (equals(d1, d2)) |
|
130 return 0; |
|
131 |
|
132 SourcePosition p1 = d1.getPosition(); |
|
133 SourcePosition p2 = d2.getPosition(); |
|
134 |
|
135 if (p1 == null && p2 != null) |
|
136 return 1; |
|
137 else if (p1 != null && p2 == null) |
|
138 return -1; |
|
139 else if(p1 == null && p2 == null) |
|
140 return compareEqualPosition(d1, d2); |
|
141 else { |
|
142 assert p1 != null && p2 != null; |
|
143 int fileComp = p1.file().compareTo(p2.file()) ; |
|
144 if (fileComp == 0) { |
|
145 long diff = (long)p1.line() - (long)p2.line(); |
|
146 if (diff == 0) { |
|
147 diff = Long.signum((long)p1.column() - (long)p2.column()); |
|
148 if (diff != 0) |
|
149 return (int)diff; |
|
150 else { |
|
151 // declarations may be two |
|
152 // compiler-generated members with the |
|
153 // same source position |
|
154 return compareEqualPosition(d1, d2); |
|
155 } |
|
156 } else |
|
157 return (diff<0)? -1:1; |
|
158 } else |
|
159 return fileComp; |
|
160 } |
|
161 } |
|
162 } |
|
163 |
|
164 final static java.util.Comparator<Declaration> comparator = new SourceOrderComparator(); |
|
165 |
|
166 SourceOrderDeclScanner(DeclarationVisitor pre, DeclarationVisitor post) { |
|
167 super(pre, post); |
|
168 } |
|
169 |
|
170 /** |
|
171 * Visits a type declaration. |
|
172 * |
|
173 * @param d the declaration to visit |
|
174 */ |
|
175 public void visitTypeDeclaration(TypeDeclaration d) { |
|
176 d.accept(pre); |
|
177 |
|
178 SortedSet<Declaration> decls = new |
|
179 TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ; |
|
180 |
|
181 for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) { |
|
182 decls.add(tpDecl); |
|
183 } |
|
184 |
|
185 for(FieldDeclaration fieldDecl: d.getFields()) { |
|
186 decls.add(fieldDecl); |
|
187 } |
|
188 |
|
189 for(MethodDeclaration methodDecl: d.getMethods()) { |
|
190 decls.add(methodDecl); |
|
191 } |
|
192 |
|
193 for(TypeDeclaration typeDecl: d.getNestedTypes()) { |
|
194 decls.add(typeDecl); |
|
195 } |
|
196 |
|
197 for(Declaration decl: decls ) |
|
198 decl.accept(this); |
|
199 |
|
200 d.accept(post); |
|
201 } |
|
202 |
|
203 /** |
|
204 * Visits a class declaration. |
|
205 * |
|
206 * @param d the declaration to visit |
|
207 */ |
|
208 public void visitClassDeclaration(ClassDeclaration d) { |
|
209 d.accept(pre); |
|
210 |
|
211 SortedSet<Declaration> decls = new |
|
212 TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ; |
|
213 |
|
214 for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) { |
|
215 decls.add(tpDecl); |
|
216 } |
|
217 |
|
218 for(FieldDeclaration fieldDecl: d.getFields()) { |
|
219 decls.add(fieldDecl); |
|
220 } |
|
221 |
|
222 for(MethodDeclaration methodDecl: d.getMethods()) { |
|
223 decls.add(methodDecl); |
|
224 } |
|
225 |
|
226 for(TypeDeclaration typeDecl: d.getNestedTypes()) { |
|
227 decls.add(typeDecl); |
|
228 } |
|
229 |
|
230 for(ConstructorDeclaration ctorDecl: d.getConstructors()) { |
|
231 decls.add(ctorDecl); |
|
232 } |
|
233 |
|
234 for(Declaration decl: decls ) |
|
235 decl.accept(this); |
|
236 |
|
237 d.accept(post); |
|
238 } |
|
239 |
|
240 public void visitExecutableDeclaration(ExecutableDeclaration d) { |
|
241 d.accept(pre); |
|
242 |
|
243 SortedSet<Declaration> decls = new |
|
244 TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ; |
|
245 |
|
246 for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) |
|
247 decls.add(tpDecl); |
|
248 |
|
249 for(ParameterDeclaration pDecl: d.getParameters()) |
|
250 decls.add(pDecl); |
|
251 |
|
252 for(Declaration decl: decls ) |
|
253 decl.accept(this); |
|
254 |
|
255 d.accept(post); |
|
256 } |
|
257 |
|
258 } |
|