Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javac.processing; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import javax.annotation.processing.*; |
aoqi@0 | 29 | import javax.lang.model.*; |
aoqi@0 | 30 | import javax.lang.model.element.*; |
aoqi@0 | 31 | import static javax.lang.model.element.ElementKind.*; |
aoqi@0 | 32 | import static javax.lang.model.element.NestingKind.*; |
aoqi@0 | 33 | import javax.lang.model.type.*; |
aoqi@0 | 34 | import javax.lang.model.util.*; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import java.io.PrintWriter; |
aoqi@0 | 37 | import java.io.Writer; |
aoqi@0 | 38 | import java.util.*; |
aoqi@0 | 39 | import com.sun.tools.javac.util.StringUtils; |
aoqi@0 | 40 | |
aoqi@0 | 41 | /** |
aoqi@0 | 42 | * A processor which prints out elements. Used to implement the |
aoqi@0 | 43 | * -Xprint option; the included visitor class is used to implement |
aoqi@0 | 44 | * Elements.printElements. |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 47 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 48 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 49 | * deletion without notice.</b> |
aoqi@0 | 50 | */ |
aoqi@0 | 51 | @SupportedAnnotationTypes("*") |
aoqi@0 | 52 | @SupportedSourceVersion(SourceVersion.RELEASE_8) |
aoqi@0 | 53 | public class PrintingProcessor extends AbstractProcessor { |
aoqi@0 | 54 | PrintWriter writer; |
aoqi@0 | 55 | |
aoqi@0 | 56 | public PrintingProcessor() { |
aoqi@0 | 57 | super(); |
aoqi@0 | 58 | writer = new PrintWriter(System.out); |
aoqi@0 | 59 | } |
aoqi@0 | 60 | |
aoqi@0 | 61 | public void setWriter(Writer w) { |
aoqi@0 | 62 | writer = new PrintWriter(w); |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | @Override |
aoqi@0 | 66 | public boolean process(Set<? extends TypeElement> tes, |
aoqi@0 | 67 | RoundEnvironment renv) { |
aoqi@0 | 68 | |
aoqi@0 | 69 | for(Element element : renv.getRootElements()) { |
aoqi@0 | 70 | print(element); |
aoqi@0 | 71 | } |
aoqi@0 | 72 | |
aoqi@0 | 73 | // Just print the elements, nothing more to do. |
aoqi@0 | 74 | return true; |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | void print(Element element) { |
aoqi@0 | 78 | new PrintingElementVisitor(writer, processingEnv.getElementUtils()). |
aoqi@0 | 79 | visit(element).flush(); |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | /** |
aoqi@0 | 83 | * Used for the -Xprint option and called by Elements.printElements |
aoqi@0 | 84 | */ |
aoqi@0 | 85 | public static class PrintingElementVisitor |
aoqi@0 | 86 | extends SimpleElementVisitor8<PrintingElementVisitor, Boolean> { |
aoqi@0 | 87 | int indentation; // Indentation level; |
aoqi@0 | 88 | final PrintWriter writer; |
aoqi@0 | 89 | final Elements elementUtils; |
aoqi@0 | 90 | |
aoqi@0 | 91 | public PrintingElementVisitor(Writer w, Elements elementUtils) { |
aoqi@0 | 92 | super(); |
aoqi@0 | 93 | this.writer = new PrintWriter(w); |
aoqi@0 | 94 | this.elementUtils = elementUtils; |
aoqi@0 | 95 | indentation = 0; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | @Override |
aoqi@0 | 99 | protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) { |
aoqi@0 | 100 | if (newLine != null && newLine) |
aoqi@0 | 101 | writer.println(); |
aoqi@0 | 102 | printDocComment(e); |
aoqi@0 | 103 | printModifiers(e); |
aoqi@0 | 104 | return this; |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | @Override |
aoqi@0 | 108 | public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) { |
aoqi@0 | 109 | ElementKind kind = e.getKind(); |
aoqi@0 | 110 | |
aoqi@0 | 111 | if (kind != STATIC_INIT && |
aoqi@0 | 112 | kind != INSTANCE_INIT) { |
aoqi@0 | 113 | Element enclosing = e.getEnclosingElement(); |
aoqi@0 | 114 | |
aoqi@0 | 115 | // Don't print out the constructor of an anonymous class |
aoqi@0 | 116 | if (kind == CONSTRUCTOR && |
aoqi@0 | 117 | enclosing != null && |
aoqi@0 | 118 | NestingKind.ANONYMOUS == |
aoqi@0 | 119 | // Use an anonymous class to determine anonymity! |
aoqi@0 | 120 | (new SimpleElementVisitor7<NestingKind, Void>() { |
aoqi@0 | 121 | @Override |
aoqi@0 | 122 | public NestingKind visitType(TypeElement e, Void p) { |
aoqi@0 | 123 | return e.getNestingKind(); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | }).visit(enclosing)) |
aoqi@0 | 126 | return this; |
aoqi@0 | 127 | |
aoqi@0 | 128 | defaultAction(e, true); |
aoqi@0 | 129 | printFormalTypeParameters(e, true); |
aoqi@0 | 130 | |
aoqi@0 | 131 | switch(kind) { |
aoqi@0 | 132 | case CONSTRUCTOR: |
aoqi@0 | 133 | // Print out simple name of the class |
aoqi@0 | 134 | writer.print(e.getEnclosingElement().getSimpleName()); |
aoqi@0 | 135 | break; |
aoqi@0 | 136 | |
aoqi@0 | 137 | case METHOD: |
aoqi@0 | 138 | writer.print(e.getReturnType().toString()); |
aoqi@0 | 139 | writer.print(" "); |
aoqi@0 | 140 | writer.print(e.getSimpleName().toString()); |
aoqi@0 | 141 | break; |
aoqi@0 | 142 | } |
aoqi@0 | 143 | |
aoqi@0 | 144 | writer.print("("); |
aoqi@0 | 145 | printParameters(e); |
aoqi@0 | 146 | writer.print(")"); |
aoqi@0 | 147 | AnnotationValue defaultValue = e.getDefaultValue(); |
aoqi@0 | 148 | if (defaultValue != null) |
aoqi@0 | 149 | writer.print(" default " + defaultValue); |
aoqi@0 | 150 | |
aoqi@0 | 151 | printThrows(e); |
aoqi@0 | 152 | writer.println(";"); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | return this; |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | |
aoqi@0 | 158 | @Override |
aoqi@0 | 159 | public PrintingElementVisitor visitType(TypeElement e, Boolean p) { |
aoqi@0 | 160 | ElementKind kind = e.getKind(); |
aoqi@0 | 161 | NestingKind nestingKind = e.getNestingKind(); |
aoqi@0 | 162 | |
aoqi@0 | 163 | if (NestingKind.ANONYMOUS == nestingKind) { |
aoqi@0 | 164 | // Print out an anonymous class in the style of a |
aoqi@0 | 165 | // class instance creation expression rather than a |
aoqi@0 | 166 | // class declaration. |
aoqi@0 | 167 | writer.print("new "); |
aoqi@0 | 168 | |
aoqi@0 | 169 | // If the anonymous class implements an interface |
aoqi@0 | 170 | // print that name, otherwise print the superclass. |
aoqi@0 | 171 | List<? extends TypeMirror> interfaces = e.getInterfaces(); |
aoqi@0 | 172 | if (!interfaces.isEmpty()) |
aoqi@0 | 173 | writer.print(interfaces.get(0)); |
aoqi@0 | 174 | else |
aoqi@0 | 175 | writer.print(e.getSuperclass()); |
aoqi@0 | 176 | |
aoqi@0 | 177 | writer.print("("); |
aoqi@0 | 178 | // Anonymous classes that implement an interface can't |
aoqi@0 | 179 | // have any constructor arguments. |
aoqi@0 | 180 | if (interfaces.isEmpty()) { |
aoqi@0 | 181 | // Print out the parameter list from the sole |
aoqi@0 | 182 | // constructor. For now, don't try to elide any |
aoqi@0 | 183 | // synthetic parameters by determining if the |
aoqi@0 | 184 | // anonymous class is in a static context, etc. |
aoqi@0 | 185 | List<? extends ExecutableElement> constructors = |
aoqi@0 | 186 | ElementFilter.constructorsIn(e.getEnclosedElements()); |
aoqi@0 | 187 | |
aoqi@0 | 188 | if (!constructors.isEmpty()) |
aoqi@0 | 189 | printParameters(constructors.get(0)); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | writer.print(")"); |
aoqi@0 | 192 | } else { |
aoqi@0 | 193 | if (nestingKind == TOP_LEVEL) { |
aoqi@0 | 194 | PackageElement pkg = elementUtils.getPackageOf(e); |
aoqi@0 | 195 | if (!pkg.isUnnamed()) |
aoqi@0 | 196 | writer.print("package " + pkg.getQualifiedName() + ";\n"); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | |
aoqi@0 | 199 | defaultAction(e, true); |
aoqi@0 | 200 | |
aoqi@0 | 201 | switch(kind) { |
aoqi@0 | 202 | case ANNOTATION_TYPE: |
aoqi@0 | 203 | writer.print("@interface"); |
aoqi@0 | 204 | break; |
aoqi@0 | 205 | default: |
aoqi@0 | 206 | writer.print(StringUtils.toLowerCase(kind.toString())); |
aoqi@0 | 207 | } |
aoqi@0 | 208 | writer.print(" "); |
aoqi@0 | 209 | writer.print(e.getSimpleName()); |
aoqi@0 | 210 | |
aoqi@0 | 211 | printFormalTypeParameters(e, false); |
aoqi@0 | 212 | |
aoqi@0 | 213 | // Print superclass information if informative |
aoqi@0 | 214 | if (kind == CLASS) { |
aoqi@0 | 215 | TypeMirror supertype = e.getSuperclass(); |
aoqi@0 | 216 | if (supertype.getKind() != TypeKind.NONE) { |
aoqi@0 | 217 | TypeElement e2 = (TypeElement) |
aoqi@0 | 218 | ((DeclaredType) supertype).asElement(); |
aoqi@0 | 219 | if (e2.getSuperclass().getKind() != TypeKind.NONE) |
aoqi@0 | 220 | writer.print(" extends " + supertype); |
aoqi@0 | 221 | } |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | printInterfaces(e); |
aoqi@0 | 225 | } |
aoqi@0 | 226 | writer.println(" {"); |
aoqi@0 | 227 | indentation++; |
aoqi@0 | 228 | |
aoqi@0 | 229 | if (kind == ENUM) { |
aoqi@0 | 230 | List<Element> enclosedElements = |
aoqi@0 | 231 | new ArrayList<Element>(e.getEnclosedElements()); |
aoqi@0 | 232 | // Handle any enum constants specially before other entities. |
aoqi@0 | 233 | List<Element> enumConstants = new ArrayList<Element>(); |
aoqi@0 | 234 | for(Element element : enclosedElements) { |
aoqi@0 | 235 | if (element.getKind() == ENUM_CONSTANT) |
aoqi@0 | 236 | enumConstants.add(element); |
aoqi@0 | 237 | } |
aoqi@0 | 238 | if (!enumConstants.isEmpty()) { |
aoqi@0 | 239 | int i; |
aoqi@0 | 240 | for(i = 0; i < enumConstants.size()-1; i++) { |
aoqi@0 | 241 | this.visit(enumConstants.get(i), true); |
aoqi@0 | 242 | writer.print(","); |
aoqi@0 | 243 | } |
aoqi@0 | 244 | this.visit(enumConstants.get(i), true); |
aoqi@0 | 245 | writer.println(";\n"); |
aoqi@0 | 246 | |
aoqi@0 | 247 | enclosedElements.removeAll(enumConstants); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | for(Element element : enclosedElements) |
aoqi@0 | 251 | this.visit(element); |
aoqi@0 | 252 | } else { |
aoqi@0 | 253 | for(Element element : e.getEnclosedElements()) |
aoqi@0 | 254 | this.visit(element); |
aoqi@0 | 255 | } |
aoqi@0 | 256 | |
aoqi@0 | 257 | indentation--; |
aoqi@0 | 258 | indent(); |
aoqi@0 | 259 | writer.println("}"); |
aoqi@0 | 260 | return this; |
aoqi@0 | 261 | } |
aoqi@0 | 262 | |
aoqi@0 | 263 | @Override |
aoqi@0 | 264 | public PrintingElementVisitor visitVariable(VariableElement e, Boolean newLine) { |
aoqi@0 | 265 | ElementKind kind = e.getKind(); |
aoqi@0 | 266 | defaultAction(e, newLine); |
aoqi@0 | 267 | |
aoqi@0 | 268 | if (kind == ENUM_CONSTANT) |
aoqi@0 | 269 | writer.print(e.getSimpleName()); |
aoqi@0 | 270 | else { |
aoqi@0 | 271 | writer.print(e.asType().toString() + " " + e.getSimpleName() ); |
aoqi@0 | 272 | Object constantValue = e.getConstantValue(); |
aoqi@0 | 273 | if (constantValue != null) { |
aoqi@0 | 274 | writer.print(" = "); |
aoqi@0 | 275 | writer.print(elementUtils.getConstantExpression(constantValue)); |
aoqi@0 | 276 | } |
aoqi@0 | 277 | writer.println(";"); |
aoqi@0 | 278 | } |
aoqi@0 | 279 | return this; |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | @Override |
aoqi@0 | 283 | public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) { |
aoqi@0 | 284 | writer.print(e.getSimpleName()); |
aoqi@0 | 285 | return this; |
aoqi@0 | 286 | } |
aoqi@0 | 287 | |
aoqi@0 | 288 | // Should we do more here? |
aoqi@0 | 289 | @Override |
aoqi@0 | 290 | public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) { |
aoqi@0 | 291 | defaultAction(e, false); |
aoqi@0 | 292 | if (!e.isUnnamed()) |
aoqi@0 | 293 | writer.println("package " + e.getQualifiedName() + ";"); |
aoqi@0 | 294 | else |
aoqi@0 | 295 | writer.println("// Unnamed package"); |
aoqi@0 | 296 | return this; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | |
aoqi@0 | 299 | public void flush() { |
aoqi@0 | 300 | writer.flush(); |
aoqi@0 | 301 | } |
aoqi@0 | 302 | |
aoqi@0 | 303 | private void printDocComment(Element e) { |
aoqi@0 | 304 | String docComment = elementUtils.getDocComment(e); |
aoqi@0 | 305 | |
aoqi@0 | 306 | if (docComment != null) { |
aoqi@0 | 307 | // Break comment into lines |
aoqi@0 | 308 | java.util.StringTokenizer st = new StringTokenizer(docComment, |
aoqi@0 | 309 | "\n\r"); |
aoqi@0 | 310 | indent(); |
aoqi@0 | 311 | writer.println("/**"); |
aoqi@0 | 312 | |
aoqi@0 | 313 | while(st.hasMoreTokens()) { |
aoqi@0 | 314 | indent(); |
aoqi@0 | 315 | writer.print(" *"); |
aoqi@0 | 316 | writer.println(st.nextToken()); |
aoqi@0 | 317 | } |
aoqi@0 | 318 | |
aoqi@0 | 319 | indent(); |
aoqi@0 | 320 | writer.println(" */"); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | } |
aoqi@0 | 323 | |
aoqi@0 | 324 | private void printModifiers(Element e) { |
aoqi@0 | 325 | ElementKind kind = e.getKind(); |
aoqi@0 | 326 | if (kind == PARAMETER) { |
aoqi@0 | 327 | printAnnotationsInline(e); |
aoqi@0 | 328 | } else { |
aoqi@0 | 329 | printAnnotations(e); |
aoqi@0 | 330 | indent(); |
aoqi@0 | 331 | } |
aoqi@0 | 332 | |
aoqi@0 | 333 | if (kind == ENUM_CONSTANT) |
aoqi@0 | 334 | return; |
aoqi@0 | 335 | |
aoqi@0 | 336 | Set<Modifier> modifiers = new LinkedHashSet<Modifier>(); |
aoqi@0 | 337 | modifiers.addAll(e.getModifiers()); |
aoqi@0 | 338 | |
aoqi@0 | 339 | switch (kind) { |
aoqi@0 | 340 | case ANNOTATION_TYPE: |
aoqi@0 | 341 | case INTERFACE: |
aoqi@0 | 342 | modifiers.remove(Modifier.ABSTRACT); |
aoqi@0 | 343 | break; |
aoqi@0 | 344 | |
aoqi@0 | 345 | case ENUM: |
aoqi@0 | 346 | modifiers.remove(Modifier.FINAL); |
aoqi@0 | 347 | modifiers.remove(Modifier.ABSTRACT); |
aoqi@0 | 348 | break; |
aoqi@0 | 349 | |
aoqi@0 | 350 | case METHOD: |
aoqi@0 | 351 | case FIELD: |
aoqi@0 | 352 | Element enclosingElement = e.getEnclosingElement(); |
aoqi@0 | 353 | if (enclosingElement != null && |
aoqi@0 | 354 | enclosingElement.getKind().isInterface()) { |
aoqi@0 | 355 | modifiers.remove(Modifier.PUBLIC); |
aoqi@0 | 356 | modifiers.remove(Modifier.ABSTRACT); // only for methods |
aoqi@0 | 357 | modifiers.remove(Modifier.STATIC); // only for fields |
aoqi@0 | 358 | modifiers.remove(Modifier.FINAL); // only for fields |
aoqi@0 | 359 | } |
aoqi@0 | 360 | break; |
aoqi@0 | 361 | |
aoqi@0 | 362 | } |
aoqi@0 | 363 | |
aoqi@0 | 364 | for(Modifier m: modifiers) { |
aoqi@0 | 365 | writer.print(m.toString() + " "); |
aoqi@0 | 366 | } |
aoqi@0 | 367 | } |
aoqi@0 | 368 | |
aoqi@0 | 369 | private void printFormalTypeParameters(Parameterizable e, |
aoqi@0 | 370 | boolean pad) { |
aoqi@0 | 371 | List<? extends TypeParameterElement> typeParams = e.getTypeParameters(); |
aoqi@0 | 372 | if (typeParams.size() > 0) { |
aoqi@0 | 373 | writer.print("<"); |
aoqi@0 | 374 | |
aoqi@0 | 375 | boolean first = true; |
aoqi@0 | 376 | for(TypeParameterElement tpe: typeParams) { |
aoqi@0 | 377 | if (!first) |
aoqi@0 | 378 | writer.print(", "); |
aoqi@0 | 379 | printAnnotationsInline(tpe); |
aoqi@0 | 380 | writer.print(tpe.toString()); |
aoqi@0 | 381 | first = false; |
aoqi@0 | 382 | } |
aoqi@0 | 383 | |
aoqi@0 | 384 | writer.print(">"); |
aoqi@0 | 385 | if (pad) |
aoqi@0 | 386 | writer.print(" "); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | } |
aoqi@0 | 389 | |
aoqi@0 | 390 | private void printAnnotationsInline(Element e) { |
aoqi@0 | 391 | List<? extends AnnotationMirror> annots = e.getAnnotationMirrors(); |
aoqi@0 | 392 | for(AnnotationMirror annotationMirror : annots) { |
aoqi@0 | 393 | writer.print(annotationMirror); |
aoqi@0 | 394 | writer.print(" "); |
aoqi@0 | 395 | } |
aoqi@0 | 396 | } |
aoqi@0 | 397 | |
aoqi@0 | 398 | private void printAnnotations(Element e) { |
aoqi@0 | 399 | List<? extends AnnotationMirror> annots = e.getAnnotationMirrors(); |
aoqi@0 | 400 | for(AnnotationMirror annotationMirror : annots) { |
aoqi@0 | 401 | indent(); |
aoqi@0 | 402 | writer.println(annotationMirror); |
aoqi@0 | 403 | } |
aoqi@0 | 404 | } |
aoqi@0 | 405 | |
aoqi@0 | 406 | // TODO: Refactor |
aoqi@0 | 407 | private void printParameters(ExecutableElement e) { |
aoqi@0 | 408 | List<? extends VariableElement> parameters = e.getParameters(); |
aoqi@0 | 409 | int size = parameters.size(); |
aoqi@0 | 410 | |
aoqi@0 | 411 | switch (size) { |
aoqi@0 | 412 | case 0: |
aoqi@0 | 413 | break; |
aoqi@0 | 414 | |
aoqi@0 | 415 | case 1: |
aoqi@0 | 416 | for(VariableElement parameter: parameters) { |
aoqi@0 | 417 | printModifiers(parameter); |
aoqi@0 | 418 | |
aoqi@0 | 419 | if (e.isVarArgs() ) { |
aoqi@0 | 420 | TypeMirror tm = parameter.asType(); |
aoqi@0 | 421 | if (tm.getKind() != TypeKind.ARRAY) |
aoqi@0 | 422 | throw new AssertionError("Var-args parameter is not an array type: " + tm); |
aoqi@0 | 423 | writer.print((ArrayType.class.cast(tm)).getComponentType() ); |
aoqi@0 | 424 | writer.print("..."); |
aoqi@0 | 425 | } else |
aoqi@0 | 426 | writer.print(parameter.asType()); |
aoqi@0 | 427 | writer.print(" " + parameter.getSimpleName()); |
aoqi@0 | 428 | } |
aoqi@0 | 429 | break; |
aoqi@0 | 430 | |
aoqi@0 | 431 | default: |
aoqi@0 | 432 | { |
aoqi@0 | 433 | int i = 1; |
aoqi@0 | 434 | for(VariableElement parameter: parameters) { |
aoqi@0 | 435 | if (i == 2) |
aoqi@0 | 436 | indentation++; |
aoqi@0 | 437 | |
aoqi@0 | 438 | if (i > 1) |
aoqi@0 | 439 | indent(); |
aoqi@0 | 440 | |
aoqi@0 | 441 | printModifiers(parameter); |
aoqi@0 | 442 | |
aoqi@0 | 443 | if (i == size && e.isVarArgs() ) { |
aoqi@0 | 444 | TypeMirror tm = parameter.asType(); |
aoqi@0 | 445 | if (tm.getKind() != TypeKind.ARRAY) |
aoqi@0 | 446 | throw new AssertionError("Var-args parameter is not an array type: " + tm); |
aoqi@0 | 447 | writer.print((ArrayType.class.cast(tm)).getComponentType() ); |
aoqi@0 | 448 | |
aoqi@0 | 449 | writer.print("..."); |
aoqi@0 | 450 | } else |
aoqi@0 | 451 | writer.print(parameter.asType()); |
aoqi@0 | 452 | writer.print(" " + parameter.getSimpleName()); |
aoqi@0 | 453 | |
aoqi@0 | 454 | if (i < size) |
aoqi@0 | 455 | writer.println(","); |
aoqi@0 | 456 | |
aoqi@0 | 457 | i++; |
aoqi@0 | 458 | } |
aoqi@0 | 459 | |
aoqi@0 | 460 | if (parameters.size() >= 2) |
aoqi@0 | 461 | indentation--; |
aoqi@0 | 462 | } |
aoqi@0 | 463 | break; |
aoqi@0 | 464 | } |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | private void printInterfaces(TypeElement e) { |
aoqi@0 | 468 | ElementKind kind = e.getKind(); |
aoqi@0 | 469 | |
aoqi@0 | 470 | if(kind != ANNOTATION_TYPE) { |
aoqi@0 | 471 | List<? extends TypeMirror> interfaces = e.getInterfaces(); |
aoqi@0 | 472 | if (interfaces.size() > 0) { |
aoqi@0 | 473 | writer.print((kind.isClass() ? " implements" : " extends")); |
aoqi@0 | 474 | |
aoqi@0 | 475 | boolean first = true; |
aoqi@0 | 476 | for(TypeMirror interf: interfaces) { |
aoqi@0 | 477 | if (!first) |
aoqi@0 | 478 | writer.print(","); |
aoqi@0 | 479 | writer.print(" "); |
aoqi@0 | 480 | writer.print(interf.toString()); |
aoqi@0 | 481 | first = false; |
aoqi@0 | 482 | } |
aoqi@0 | 483 | } |
aoqi@0 | 484 | } |
aoqi@0 | 485 | } |
aoqi@0 | 486 | |
aoqi@0 | 487 | private void printThrows(ExecutableElement e) { |
aoqi@0 | 488 | List<? extends TypeMirror> thrownTypes = e.getThrownTypes(); |
aoqi@0 | 489 | final int size = thrownTypes.size(); |
aoqi@0 | 490 | if (size != 0) { |
aoqi@0 | 491 | writer.print(" throws"); |
aoqi@0 | 492 | |
aoqi@0 | 493 | int i = 1; |
aoqi@0 | 494 | for(TypeMirror thrownType: thrownTypes) { |
aoqi@0 | 495 | if (i == 1) |
aoqi@0 | 496 | writer.print(" "); |
aoqi@0 | 497 | |
aoqi@0 | 498 | if (i == 2) |
aoqi@0 | 499 | indentation++; |
aoqi@0 | 500 | |
aoqi@0 | 501 | if (i >= 2) |
aoqi@0 | 502 | indent(); |
aoqi@0 | 503 | |
aoqi@0 | 504 | writer.print(thrownType); |
aoqi@0 | 505 | |
aoqi@0 | 506 | if (i != size) |
aoqi@0 | 507 | writer.println(", "); |
aoqi@0 | 508 | |
aoqi@0 | 509 | i++; |
aoqi@0 | 510 | } |
aoqi@0 | 511 | |
aoqi@0 | 512 | if (size >= 2) |
aoqi@0 | 513 | indentation--; |
aoqi@0 | 514 | } |
aoqi@0 | 515 | } |
aoqi@0 | 516 | |
aoqi@0 | 517 | private static final String [] spaces = { |
aoqi@0 | 518 | "", |
aoqi@0 | 519 | " ", |
aoqi@0 | 520 | " ", |
aoqi@0 | 521 | " ", |
aoqi@0 | 522 | " ", |
aoqi@0 | 523 | " ", |
aoqi@0 | 524 | " ", |
aoqi@0 | 525 | " ", |
aoqi@0 | 526 | " ", |
aoqi@0 | 527 | " ", |
aoqi@0 | 528 | " " |
aoqi@0 | 529 | }; |
aoqi@0 | 530 | |
aoqi@0 | 531 | private void indent() { |
aoqi@0 | 532 | int indentation = this.indentation; |
aoqi@0 | 533 | if (indentation < 0) |
aoqi@0 | 534 | return; |
aoqi@0 | 535 | final int maxIndex = spaces.length - 1; |
aoqi@0 | 536 | |
aoqi@0 | 537 | while (indentation > maxIndex) { |
aoqi@0 | 538 | writer.print(spaces[maxIndex]); |
aoqi@0 | 539 | indentation -= maxIndex; |
aoqi@0 | 540 | } |
aoqi@0 | 541 | writer.print(spaces[indentation]); |
aoqi@0 | 542 | } |
aoqi@0 | 543 | |
aoqi@0 | 544 | } |
aoqi@0 | 545 | } |