1 /*
   2  * Copyright 2004-2005 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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 d1.getPosition() == d2.getPosition();
 105 
 106             DeclPartialOrder dpo1 = new DeclPartialOrder();
 107             DeclPartialOrder dpo2 = new DeclPartialOrder();
 108 
 109             d1.accept(dpo1);
 110             d2.accept(dpo2);
 111 
 112             int difference = dpo1.getValue() - dpo2.getValue();
 113             if (difference != 0)
 114                 return difference;
 115             else {
 116                 int result = d1.getSimpleName().compareTo(d2.getSimpleName());
 117                 if (result != 0)
 118                     return result;
 119                 return (int)( Long.signum((long)System.identityHashCode(d1) -
 120                                           (long)System.identityHashCode(d2)));
 121             }
 122         }
 123 
 124         public int compare(Declaration d1, Declaration d2) {
 125             if (equals(d1, d2))
 126                 return 0;
 127 
 128             SourcePosition p1 = d1.getPosition();
 129             SourcePosition p2 = d2.getPosition();
 130 
 131             if (p1 == null && p2 != null)
 132                 return 1;
 133             else if (p1 != null && p2 == null)
 134                 return -1;
 135             else if(p1 == null && p2 == null)
 136                 return compareEqualPosition(d1, d2);
 137             else {
 138                 assert p1 != null && p2 != null;
 139                 int fileComp = p1.file().compareTo(p2.file()) ;
 140                 if (fileComp == 0) {
 141                     long diff = (long)p1.line() - (long)p2.line();
 142                     if (diff == 0) {
 143                         diff = Long.signum((long)p1.column() - (long)p2.column());
 144                         if (diff != 0)
 145                             return (int)diff;
 146                         else {
 147                             // declarations may be two
 148                             // compiler-generated members with the
 149                             // same source position
 150                             return compareEqualPosition(d1, d2);
 151                         }
 152                     } else
 153                         return (diff<0)? -1:1;
 154                 } else
 155                     return fileComp;
 156             }
 157         }
 158     }
 159 
 160     final static java.util.Comparator<Declaration> comparator = new SourceOrderComparator();
 161 
 162     SourceOrderDeclScanner(DeclarationVisitor pre, DeclarationVisitor post) {
 163         super(pre, post);
 164     }
 165 
 166     /**
 167      * Visits a type declaration.
 168      *
 169      * @param d the declaration to visit
 170      */
 171     public void visitTypeDeclaration(TypeDeclaration d) {
 172         d.accept(pre);
 173 
 174         SortedSet<Declaration> decls = new
 175             TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ;
 176 
 177         for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
 178             decls.add(tpDecl);
 179         }
 180 
 181         for(FieldDeclaration fieldDecl: d.getFields()) {
 182             decls.add(fieldDecl);
 183         }
 184 
 185         for(MethodDeclaration methodDecl: d.getMethods()) {
 186             decls.add(methodDecl);
 187         }
 188 
 189         for(TypeDeclaration typeDecl: d.getNestedTypes()) {
 190             decls.add(typeDecl);
 191         }
 192 
 193         for(Declaration decl: decls )
 194             decl.accept(this);
 195 
 196         d.accept(post);
 197     }
 198 
 199     /**
 200      * Visits a class declaration.
 201      *
 202      * @param d the declaration to visit
 203      */
 204     public void visitClassDeclaration(ClassDeclaration d) {
 205         d.accept(pre);
 206 
 207         SortedSet<Declaration> decls = new
 208             TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ;
 209 
 210         for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters()) {
 211             decls.add(tpDecl);
 212         }
 213 
 214         for(FieldDeclaration fieldDecl: d.getFields()) {
 215             decls.add(fieldDecl);
 216         }
 217 
 218         for(MethodDeclaration methodDecl: d.getMethods()) {
 219             decls.add(methodDecl);
 220         }
 221 
 222         for(TypeDeclaration typeDecl: d.getNestedTypes()) {
 223             decls.add(typeDecl);
 224         }
 225 
 226         for(ConstructorDeclaration ctorDecl: d.getConstructors()) {
 227             decls.add(ctorDecl);
 228         }
 229 
 230         for(Declaration decl: decls )
 231             decl.accept(this);
 232 
 233         d.accept(post);
 234     }
 235 
 236     public void visitExecutableDeclaration(ExecutableDeclaration d) {
 237         d.accept(pre);
 238 
 239         SortedSet<Declaration> decls = new
 240             TreeSet<Declaration>(SourceOrderDeclScanner.comparator) ;
 241 
 242         for(TypeParameterDeclaration tpDecl: d.getFormalTypeParameters())
 243             decls.add(tpDecl);
 244 
 245         for(ParameterDeclaration pDecl: d.getParameters())
 246             decls.add(pDecl);
 247 
 248         for(Declaration decl: decls )
 249             decl.accept(this);
 250 
 251         d.accept(post);
 252     }
 253 
 254 }