KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mirror > util > SourceOrderDeclScanner


1 /*
2  * @(#)SourceOrderDeclScanner.java 1.5 04/09/16
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.mirror.util;
9
10 import com.sun.mirror.declaration.*;
11
12 import java.util.SortedSet JavaDoc;
13 import java.util.TreeSet JavaDoc;
14
15 /**
16  * A visitor for declarations that scans declarations contained within
17  * the given declaration in source code order. For example, when
18  * visiting a class, the methods, fields, constructors, and nested
19  * types of the class are also visited.
20  *
21  * To control the processing done on a declaration, users of this
22  * class pass in their own visitors for pre and post processing. The
23  * preprocessing visitor is called before the contained declarations
24  * are scanned; the postprocessing visitor is called after the
25  * contained declarations are scanned.
26  *
27  * @author Joseph D. Darcy
28  * @author Scott Seligman
29  * @version 1.5 04/09/16
30  * @since 1.5
31  */

32 class SourceOrderDeclScanner extends DeclarationScanner {
33     static class SourceOrderComparator implements java.util.Comparator JavaDoc<Declaration> {
34     SourceOrderComparator(){}
35
36     
37     static boolean equals(Declaration d1, Declaration d2) {
38         return d1 == d2 || (d1 != null && d1.equals(d2));
39     }
40
41     private static class DeclPartialOrder extends com.sun.mirror.util.SimpleDeclarationVisitor {
42         private int value = 1000;
43         private static int staticAdjust(Declaration d) {
44         return d.getModifiers().contains(Modifier.STATIC)?0:1;
45         }
46
47         DeclPartialOrder() {}
48         
49         public int getValue() { return value; }
50
51         @Override JavaDoc
52         public void visitTypeParameterDeclaration(TypeParameterDeclaration d) {value = 0;}
53
54         @Override JavaDoc
55         public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {value = 1;}
56
57         @Override JavaDoc
58         public void visitClassDeclaration(ClassDeclaration d) {value = 2 + staticAdjust(d);}
59
60         @Override JavaDoc
61         public void visitInterfaceDeclaration(InterfaceDeclaration d) {value = 4;}
62
63         @Override JavaDoc
64         public void visitEnumDeclaration(EnumDeclaration d) {value = 6;}
65
66         @Override JavaDoc
67         public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {value = 8;}
68
69         @Override JavaDoc
70         public void visitFieldDeclaration(FieldDeclaration d) {value = 10 + staticAdjust(d);}
71
72         @Override JavaDoc
73         public void visitConstructorDeclaration(ConstructorDeclaration d) {value = 12;}
74
75         @Override JavaDoc
76         public void visitMethodDeclaration(MethodDeclaration d) {value = 14 + staticAdjust(d);}
77     }
78     
79     private int compareEqualPosition(Declaration d1, Declaration d2) {
80         assert d1.getPosition() == d2.getPosition();
81
82         DeclPartialOrder dpo1 = new DeclPartialOrder();
83         DeclPartialOrder dpo2 = new DeclPartialOrder();
84
85         d1.accept(dpo1);
86         d2.accept(dpo2);
87
88         int difference = dpo1.getValue() - dpo2.getValue();
89         if (difference != 0)
90         return difference;
91         else {
92         int result = d1.getSimpleName().compareTo(d2.getSimpleName());
93         if (result != 0)
94             return result;
95         return (int)( Long.signum((long)System.identityHashCode(d1) -
96                       (long)System.identityHashCode(d2)));
97         }
98     }
99
100     public int compare(Declaration d1, Declaration d2) {
101         if (equals(d1, d2))
102         return 0;
103
104         SourcePosition p1 = d1.getPosition();
105         SourcePosition p2 = d2.getPosition();
106
107         if (p1 == null && p2 != null)
108         return 1;
109         else if (p1 != null && p2 == null)
110         return -1;
111         else if(p1 == null && p2 == null)
112         return compareEqualPosition(d1, d2);
113         else {
114         assert p1 != null && p2 != null;
115         int fileComp = p1.file().compareTo(p2.file()) ;
116         if (fileComp == 0) {
117             long diff = (long)p1.line() - (long)p2.line();
118             if (diff == 0) {
119             diff = Long.signum((long)p1.column() - (long)p2.column());
120             if (diff != 0)
121                 return (int)diff;
122             else {
123                 // declarations may be two
124
// compiler-generated members with the
125
// same source position
126
return compareEqualPosition(d1, d2);
127             }
128             } else
129             return (diff<0)? -1:1;
130         } else
131             return fileComp;
132         }
133     }
134     }
135
136     final static java.util.Comparator JavaDoc<Declaration> comparator = new SourceOrderComparator();
137
138     SourceOrderDeclScanner(DeclarationVisitor pre, DeclarationVisitor post) {
139     super(pre, post);
140     }
141
142     /**
143      * Visits a type declaration.
144      *
145      * @param d the declaration to visit
146      */

147     public void visitTypeDeclaration(TypeDeclaration d) {
148     d.accept(pre);
149
150     SortedSet JavaDoc<Declaration> decls = new
151         TreeSet JavaDoc<Declaration>(SourceOrderDeclScanner.comparator) ;
152
153     for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
154         decls.add(tpDecl);
155     }
156     
157     for(FieldDeclaration fieldDecl: d.getFields()) {
158         decls.add(fieldDecl);
159     }
160     
161     for(MethodDeclaration methodDecl: d.getMethods()) {
162         decls.add(methodDecl);
163     }
164     
165     for(TypeDeclaration typeDecl: d.getNestedTypes()) {
166         decls.add(typeDecl);
167     }
168
169     for(Declaration decl: decls )
170         decl.accept(this);
171
172     d.accept(post);
173     }
174
175     /**
176      * Visits a class declaration.
177      *
178      * @param d the declaration to visit
179      */

180     public void visitClassDeclaration(ClassDeclaration d) {
181     d.accept(pre);
182
183     SortedSet JavaDoc<Declaration> decls = new
184         TreeSet JavaDoc<Declaration>(SourceOrderDeclScanner.comparator) ;
185
186     for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
187         decls.add(tpDecl);
188     }
189     
190     for(FieldDeclaration fieldDecl: d.getFields()) {
191         decls.add(fieldDecl);
192     }
193     
194     for(MethodDeclaration methodDecl: d.getMethods()) {
195         decls.add(methodDecl);
196     }
197     
198     for(TypeDeclaration typeDecl: d.getNestedTypes()) {
199         decls.add(typeDecl);
200     }
201
202     for(ConstructorDeclaration ctorDecl: d.getConstructors()) {
203         decls.add(ctorDecl);
204     }
205
206     for(Declaration decl: decls )
207         decl.accept(this);
208
209     d.accept(post);
210     }
211     
212     public void visitExecutableDeclaration(ExecutableDeclaration d) {
213     d.accept(pre);
214
215     SortedSet JavaDoc<Declaration> decls = new
216         TreeSet JavaDoc<Declaration>(SourceOrderDeclScanner.comparator) ;
217     
218     for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters())
219         decls.add(tpDecl);
220
221     for(ParameterDeclaration pDecl: d.getParameters())
222         decls.add(pDecl);
223
224     for(Declaration decl: decls )
225         decl.accept(this);
226
227     d.accept(post);
228     }
229
230 }
231
Popular Tags