langtools/src/share/classes/com/sun/tools/apt/comp/PrintAP.java
author darcy
Sun, 26 Jul 2009 21:27:11 -0700
changeset 3378 22011d9a9398
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6381698: Warn of decommissioning of apt Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.apt.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.mirror.declaration.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import static com.sun.mirror.declaration.Modifier.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.mirror.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.mirror.apt.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.mirror.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * Class used to implement "-print" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 */
3378
22011d9a9398 6381698: Warn of decommissioning of apt
darcy
parents: 10
diff changeset
    39
@SuppressWarnings("deprecation")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
public class PrintAP implements AnnotationProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    static class PrintingVisitors {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        int indentation = 0; // Indentation level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        AnnotationProcessorEnvironment env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        Messager out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        Declaration java_lang_Object;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        Declaration java_lang_annotation_Annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        static Set<Modifier> EMPTY_ELIDES = Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        static Set<Modifier> INTERFACE_ELIDES = EnumSet.of(ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        static Set<Modifier> ENUM_ELIDES = EnumSet.of(FINAL, ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        static Set<Modifier> INTERFACE_MEMBER_ELIDES = EnumSet.of(ABSTRACT, PUBLIC, STATIC, FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        PrintingVisitors(AnnotationProcessorEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            this.out = env.getMessager();
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            this.java_lang_Object = env.getTypeDeclaration("java.lang.Object");
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            this.java_lang_annotation_Annotation = env.getTypeDeclaration("java.lang.annotation.Annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        static String [] spaces = {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            "",
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            "  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            "    ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            "      ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            "        ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            "          ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            "            ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
            "              ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            "                ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            "                  ",
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            "                    "
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        String indent(){
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            else if (indentation <= 10)
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                return spaces[indentation];
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                StringBuilder sb = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                while (indentation > 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                    sb.append(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                    indentation -= 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                sb.append(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        class PrePrinting extends SimpleDeclarationVisitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            Map<EnumDeclaration, Integer> enumCardinality  = new HashMap<EnumDeclaration, Integer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            Map<EnumDeclaration, Integer> enumConstVisited = new HashMap<EnumDeclaration, Integer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            PrePrinting(){}
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            public void visitClassDeclaration(ClassDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                printModifiers(d, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                System.out.print("class " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                // Elide "extends Object"
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                ClassType Super = d.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                if (Super != null && !java_lang_Object.equals(Super.getDeclaration()) )
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
                    System.out.print(" extends " + Super.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            public void visitEnumDeclaration(EnumDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                enumCardinality.put(d, d.getEnumConstants().size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                enumConstVisited.put(d, 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                printModifiers(d, ENUM_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                System.out.print("enum " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            public void visitInterfaceDeclaration(InterfaceDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                printModifiers(d, INTERFACE_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                System.out.print("interface " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                printModifiers(d, INTERFACE_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                System.out.print("@interface " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            public void visitFieldDeclaration(FieldDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                printModifiers(d,
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                               (d.getDeclaringType() instanceof InterfaceDeclaration)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                               INTERFACE_MEMBER_ELIDES : EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                System.out.print(d.getType().toString() + " " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                                   d.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                String constantExpr = d.getConstantExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                if (constantExpr != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                    System.out.print(" = " + constantExpr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                System.out.println(";" );
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                EnumDeclaration ed = d.getDeclaringType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                int enumCard = enumCardinality.get(ed);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                int enumVisit = enumConstVisited.get(ed);
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                System.out.print(d.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                System.out.println((enumVisit < enumCard )? ",":";" );
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                enumConstVisited.put(ed, enumVisit+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            public void visitMethodDeclaration(MethodDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                printModifiers(d,
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                               (d.getDeclaringType() instanceof InterfaceDeclaration)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                               INTERFACE_MEMBER_ELIDES : EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                System.out.print(d.getReturnType().toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                System.out.print(d.getSimpleName() + "(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                printParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                System.out.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                printThrows(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                System.out.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            public void visitConstructorDeclaration(ConstructorDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                printModifiers(d, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                System.out.print(d.getSimpleName() + "(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                printParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                System.out.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                printThrows(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                System.out.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        class PostPrinting extends SimpleDeclarationVisitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            PostPrinting(){}
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            public void visitTypeDeclaration(TypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                PrintingVisitors.this.indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                System.out.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        private void printAnnotations(Collection<AnnotationMirror> annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            for(AnnotationMirror annot: annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                System.out.print(this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                System.out.print(annot.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        private void printAnnotationsInline(Collection<AnnotationMirror> annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            for(AnnotationMirror annot: annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                System.out.print(annot);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        private void printParameters(ExecutableDeclaration ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            Collection<ParameterDeclaration> parameters = ex.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                for(ParameterDeclaration parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                    printModifiers(parameter, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    if (ex.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                        System.out.print(((ArrayType)parameter.getType()).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                        System.out.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                        System.out.print(parameter.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                    System.out.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                    for(ParameterDeclaration parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                            PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                            System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                        printModifiers(parameter, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                        if (i == size && ex.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                            System.out.print(((ArrayType)parameter.getType()).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                            System.out.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                            System.out.print(parameter.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                        System.out.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                            System.out.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                        PrintingVisitors.this.indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        private void printDocComment(Declaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            String docComment = d.getDocComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                System.out.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                    System.out.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                    System.out.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                System.out.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        private void printModifiers(Declaration d, Collection<Modifier> elides) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            printAnnotations(d.getAnnotationMirrors());
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            for(Modifier m: adjustModifiers(d.getModifiers(), elides) ){
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                System.out.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        private void printModifiers(ParameterDeclaration d, Collection<Modifier> elides) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            printAnnotationsInline(d.getAnnotationMirrors());
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
            for(Modifier m: adjustModifiers(d.getModifiers(), elides) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                System.out.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        private Collection<Modifier> adjustModifiers(Collection<Modifier> mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                                                     Collection<Modifier> elides) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            if (elides.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                Collection<Modifier> newMods = new LinkedHashSet<Modifier>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                newMods.addAll(mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                newMods.removeAll(elides);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                return newMods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        private void printFormalTypeParameters(ExecutableDeclaration e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            printFormalTypeParameterSet(e.getFormalTypeParameters(), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        private void printFormalTypeParameters(TypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            printFormalTypeParameterSet(d.getFormalTypeParameters(), false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        private void printFormalTypeParameterSet(Collection<TypeParameterDeclaration> typeParams, boolean pad) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            if (typeParams.size() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                System.out.print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                for(TypeParameterDeclaration tpd: typeParams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                        System.out.print(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                    System.out.print(tpd.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                System.out.print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                    System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        private void printInterfaceSet(Collection<InterfaceType> interfaces,
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                                       boolean classNotInterface) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            if (interfaces.size() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                System.out.print((classNotInterface?" implements" : " extends"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                for(InterfaceType interType: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                        System.out.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                    System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                    System.out.print(interType.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        private void printInterfaces(TypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            printInterfaceSet(d.getSuperinterfaces(), d instanceof ClassDeclaration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        private void printInterfaces(AnnotationTypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            Collection<InterfaceType> interfaces = new HashSet<InterfaceType>(d.getSuperinterfaces());
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            for(InterfaceType interType: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                if (java_lang_annotation_Annotation.equals(interType.getDeclaration()) )
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                    interfaces.remove(interType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            printInterfaceSet(interfaces, d instanceof ClassDeclaration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        private void printThrows(ExecutableDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            Collection<ReferenceType> thrownTypes = d.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                System.out.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                for(ReferenceType thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                    if (i == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                        System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                        PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
                        System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                    System.out.print(thrownType.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                    if (i != size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                        System.out.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                    PrintingVisitors.this.indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
        DeclarationVisitor getPrintingVisitor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            return DeclarationVisitors.getSourceOrderDeclarationScanner(new PrePrinting(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                                                                        new PostPrinting());
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    AnnotationProcessorEnvironment env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    PrintAP(AnnotationProcessorEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    public void process() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        Collection<TypeDeclaration> typedecls = env.getSpecifiedTypeDeclarations();
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        for (TypeDeclaration td: typedecls)
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            td.accept((new PrintingVisitors(env)).getPrintingVisitor());
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
}