langtools/src/share/classes/com/sun/tools/apt/comp/PrintAP.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 3378 22011d9a9398
permissions -rw-r--r--
Initial load
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
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
public class PrintAP implements AnnotationProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    static class PrintingVisitors {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
        int indentation = 0; // Indentation level;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        AnnotationProcessorEnvironment env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        Messager out;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        Declaration java_lang_Object;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
        Declaration java_lang_annotation_Annotation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        static Set<Modifier> EMPTY_ELIDES = Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        static Set<Modifier> INTERFACE_ELIDES = EnumSet.of(ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        static Set<Modifier> ENUM_ELIDES = EnumSet.of(FINAL, ABSTRACT);
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        static Set<Modifier> INTERFACE_MEMBER_ELIDES = EnumSet.of(ABSTRACT, PUBLIC, STATIC, FINAL);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        PrintingVisitors(AnnotationProcessorEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            this.out = env.getMessager();
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            this.java_lang_Object = env.getTypeDeclaration("java.lang.Object");
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            this.java_lang_annotation_Annotation = env.getTypeDeclaration("java.lang.annotation.Annotation");
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        static String [] spaces = {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
            "",
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
        String indent(){
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            int indentation = this.indentation;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            if (indentation < 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            else if (indentation <= 10)
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                return spaces[indentation];
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                StringBuilder sb = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                while (indentation > 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                    sb.append(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                    indentation -= 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                sb.append(spaces[indentation]);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            return sb.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            }
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
        class PrePrinting extends SimpleDeclarationVisitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            Map<EnumDeclaration, Integer> enumCardinality  = new HashMap<EnumDeclaration, Integer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            Map<EnumDeclaration, Integer> enumConstVisited = new HashMap<EnumDeclaration, Integer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            PrePrinting(){}
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            public void visitClassDeclaration(ClassDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                printModifiers(d, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                System.out.print("class " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                // Elide "extends Object"
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                ClassType Super = d.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                if (Super != null && !java_lang_Object.equals(Super.getDeclaration()) )
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                    System.out.print(" extends " + Super.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            public void visitEnumDeclaration(EnumDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                enumCardinality.put(d, d.getEnumConstants().size());
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                enumConstVisited.put(d, 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                printModifiers(d, ENUM_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                System.out.print("enum " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            public void visitInterfaceDeclaration(InterfaceDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                printModifiers(d, INTERFACE_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                System.out.print("interface " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            public void visitAnnotationTypeDeclaration(AnnotationTypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                printModifiers(d, INTERFACE_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                System.out.print("@interface " + d.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                printInterfaces(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                System.out.println(" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            public void visitFieldDeclaration(FieldDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                printModifiers(d,
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                               (d.getDeclaringType() instanceof InterfaceDeclaration)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                               INTERFACE_MEMBER_ELIDES : EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                System.out.print(d.getType().toString() + " " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                                   d.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                String constantExpr = d.getConstantExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                if (constantExpr != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                    System.out.print(" = " + constantExpr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                System.out.println(";" );
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            public void visitEnumConstantDeclaration(EnumConstantDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                EnumDeclaration ed = d.getDeclaringType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                int enumCard = enumCardinality.get(ed);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                int enumVisit = enumConstVisited.get(ed);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                System.out.print(d.getSimpleName() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                System.out.println((enumVisit < enumCard )? ",":";" );
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                enumConstVisited.put(ed, enumVisit+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            public void visitMethodDeclaration(MethodDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                printModifiers(d,
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                               (d.getDeclaringType() instanceof InterfaceDeclaration)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                               INTERFACE_MEMBER_ELIDES : EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                System.out.print(d.getReturnType().toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                System.out.print(d.getSimpleName() + "(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                printParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                System.out.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                printThrows(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                System.out.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
            public void visitConstructorDeclaration(ConstructorDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                printDocComment(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                printModifiers(d, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                printFormalTypeParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                System.out.print(d.getSimpleName() + "(");
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                printParameters(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                System.out.print(")");
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                printThrows(d);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                System.out.println(";");
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            }
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
        class PostPrinting extends SimpleDeclarationVisitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            PostPrinting(){}
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            public void visitTypeDeclaration(TypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                PrintingVisitors.this.indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                System.out.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        private void printAnnotations(Collection<AnnotationMirror> annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            for(AnnotationMirror annot: annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                System.out.print(this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                System.out.print(annot.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                System.out.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        private void printAnnotationsInline(Collection<AnnotationMirror> annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            for(AnnotationMirror annot: annots) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
                System.out.print(annot);
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            }
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
        private void printParameters(ExecutableDeclaration ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            Collection<ParameterDeclaration> parameters = ex.getParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            int size = parameters.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            switch (size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            case 0:
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            case 1:
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                for(ParameterDeclaration parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                    printModifiers(parameter, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                    if (ex.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                        System.out.print(((ArrayType)parameter.getType()).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                        System.out.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                    } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                        System.out.print(parameter.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                    System.out.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                    int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                    for(ParameterDeclaration parameter: parameters) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                        if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                            PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                        if (i > 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                            System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                        printModifiers(parameter, EMPTY_ELIDES);
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                        if (i == size && ex.isVarArgs() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                            System.out.print(((ArrayType)parameter.getType()).getComponentType() );
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                            System.out.print("...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                            System.out.print(parameter.getType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                        System.out.print(" " + parameter.getSimpleName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                        if (i < size)
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                            System.out.println(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                        i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    if (parameters.size() >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                        PrintingVisitors.this.indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        private void printDocComment(Declaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
            String docComment = d.getDocComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            if (docComment != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                // Break comment into lines
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                java.util.StringTokenizer st = new StringTokenizer(docComment,
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                                                                  "\n\r");
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                System.out.println("/**");
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                while(st.hasMoreTokens()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                    System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    System.out.print(" *");
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                    System.out.println(st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                System.out.println(" */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        private void printModifiers(Declaration d, Collection<Modifier> elides) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            printAnnotations(d.getAnnotationMirrors());
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            for(Modifier m: adjustModifiers(d.getModifiers(), elides) ){
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                System.out.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
        private void printModifiers(ParameterDeclaration d, Collection<Modifier> elides) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
            printAnnotationsInline(d.getAnnotationMirrors());
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            for(Modifier m: adjustModifiers(d.getModifiers(), elides) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                System.out.print(m.toString() + " ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        private Collection<Modifier> adjustModifiers(Collection<Modifier> mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                                                     Collection<Modifier> elides) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            if (elides.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                Collection<Modifier> newMods = new LinkedHashSet<Modifier>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                newMods.addAll(mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                newMods.removeAll(elides);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                return newMods;
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        private void printFormalTypeParameters(ExecutableDeclaration e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            printFormalTypeParameterSet(e.getFormalTypeParameters(), true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        private void printFormalTypeParameters(TypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
            printFormalTypeParameterSet(d.getFormalTypeParameters(), false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
        private void printFormalTypeParameterSet(Collection<TypeParameterDeclaration> typeParams, boolean pad) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            if (typeParams.size() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                System.out.print("<");
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                for(TypeParameterDeclaration tpd: typeParams) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                        System.out.print(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                    System.out.print(tpd.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                System.out.print(">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                if (pad)
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                    System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
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
        private void printInterfaceSet(Collection<InterfaceType> interfaces,
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                                       boolean classNotInterface) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            if (interfaces.size() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                System.out.print((classNotInterface?" implements" : " extends"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                for(InterfaceType interType: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                    if (!first)
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                        System.out.print(",");
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                    System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                    System.out.print(interType.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                    first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                }
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
        private void printInterfaces(TypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            printInterfaceSet(d.getSuperinterfaces(), d instanceof ClassDeclaration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        private void printInterfaces(AnnotationTypeDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            Collection<InterfaceType> interfaces = new HashSet<InterfaceType>(d.getSuperinterfaces());
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
            for(InterfaceType interType: interfaces) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                if (java_lang_annotation_Annotation.equals(interType.getDeclaration()) )
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                    interfaces.remove(interType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            printInterfaceSet(interfaces, d instanceof ClassDeclaration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        private void printThrows(ExecutableDeclaration d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            Collection<ReferenceType> thrownTypes = d.getThrownTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            final int size = thrownTypes.size();
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            if (size != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                System.out.print(" throws");
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                int i = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                for(ReferenceType thrownType: thrownTypes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                    if (i == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                        System.out.print(" ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                    if (i == 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                        PrintingVisitors.this.indentation++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                    if (i >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                        System.out.print(PrintingVisitors.this.indent());
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                    System.out.print(thrownType.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
                    if (i != size) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                        System.out.println(", ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                if (size >= 2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                    PrintingVisitors.this.indentation--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
        DeclarationVisitor getPrintingVisitor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            return DeclarationVisitors.getSourceOrderDeclarationScanner(new PrePrinting(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                                                                        new PostPrinting());
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    AnnotationProcessorEnvironment env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    PrintAP(AnnotationProcessorEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    public void process() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
        Collection<TypeDeclaration> typedecls = env.getSpecifiedTypeDeclarations();
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        for (TypeDeclaration td: typedecls)
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            td.accept((new PrintingVisitors(env)).getPrintingVisitor());
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
}