src/share/classes/com/sun/tools/apt/comp/PrintAP.java

Thu, 04 Feb 2010 10:14:28 -0800

author
jjg
date
Thu, 04 Feb 2010 10:14:28 -0800
changeset 489
4b4e282a3146
parent 331
d043adadc8b6
child 554
9d9f26857129
permissions
-rw-r--r--

6923080: TreeScanner.visitNewClass should scan tree.typeargs
Reviewed-by: darcy

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

mercurial