1 /*
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.processing;
27
28 import javax.annotation.processing.*;
29 import javax.lang.model.*;
30 import javax.lang.model.element.*;
31 import static javax.lang.model.element.ElementKind.*;
32 import static javax.lang.model.element.NestingKind.*;
33 import javax.lang.model.type.*;
34 import javax.lang.model.util.*;
35
36 import java.io.PrintWriter;
37 import java.io.Writer;
38 import java.util.*;
39
40 import com.sun.tools.javac.util.DefinedBy;
41 import com.sun.tools.javac.util.DefinedBy.Api;
42 import com.sun.tools.javac.util.StringUtils;
43
44 /**
45 * A processor which prints out elements. Used to implement the
46 * -Xprint option; the included visitor class is used to implement
47 * Elements.printElements.
48 *
49 * <p><b>This is NOT part of any supported API.
50 * If you write code that depends on this, you do so at your own risk.
51 * This code and its internal interfaces are subject to change or
52 * deletion without notice.</b>
279 writer.println(";");
280 }
281 return this;
282 }
283
284 @Override @DefinedBy(Api.LANGUAGE_MODEL)
285 public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
286 writer.print(e.getSimpleName());
287 return this;
288 }
289
290 // Should we do more here?
291 @Override @DefinedBy(Api.LANGUAGE_MODEL)
292 public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
293 defaultAction(e, false);
294 if (!e.isUnnamed())
295 writer.println("package " + e.getQualifiedName() + ";");
296 else
297 writer.println("// Unnamed package");
298 return this;
299 }
300
301 public void flush() {
302 writer.flush();
303 }
304
305 private void printDocComment(Element e) {
306 String docComment = elementUtils.getDocComment(e);
307
308 if (docComment != null) {
309 // Break comment into lines
310 java.util.StringTokenizer st = new StringTokenizer(docComment,
311 "\n\r");
312 indent();
313 writer.println("/**");
314
315 while(st.hasMoreTokens()) {
316 indent();
317 writer.print(" *");
318 writer.println(st.nextToken());
|
1 /*
2 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javac.processing;
27
28 import javax.annotation.processing.*;
29 import javax.lang.model.*;
30 import javax.lang.model.element.*;
31 import static javax.lang.model.element.ElementKind.*;
32 import static javax.lang.model.element.NestingKind.*;
33 import static javax.lang.model.element.ModuleElement.DirectiveKind.*;
34 import static javax.lang.model.element.ModuleElement.*;
35 import javax.lang.model.type.*;
36 import javax.lang.model.util.*;
37
38 import java.io.PrintWriter;
39 import java.io.Writer;
40 import java.util.*;
41
42 import com.sun.tools.javac.util.DefinedBy;
43 import com.sun.tools.javac.util.DefinedBy.Api;
44 import com.sun.tools.javac.util.StringUtils;
45
46 /**
47 * A processor which prints out elements. Used to implement the
48 * -Xprint option; the included visitor class is used to implement
49 * Elements.printElements.
50 *
51 * <p><b>This is NOT part of any supported API.
52 * If you write code that depends on this, you do so at your own risk.
53 * This code and its internal interfaces are subject to change or
54 * deletion without notice.</b>
281 writer.println(";");
282 }
283 return this;
284 }
285
286 @Override @DefinedBy(Api.LANGUAGE_MODEL)
287 public PrintingElementVisitor visitTypeParameter(TypeParameterElement e, Boolean p) {
288 writer.print(e.getSimpleName());
289 return this;
290 }
291
292 // Should we do more here?
293 @Override @DefinedBy(Api.LANGUAGE_MODEL)
294 public PrintingElementVisitor visitPackage(PackageElement e, Boolean p) {
295 defaultAction(e, false);
296 if (!e.isUnnamed())
297 writer.println("package " + e.getQualifiedName() + ";");
298 else
299 writer.println("// Unnamed package");
300 return this;
301 }
302
303 @Override @DefinedBy(Api.LANGUAGE_MODEL)
304 public PrintingElementVisitor visitModule(ModuleElement e, Boolean p) {
305 defaultAction(e, false);
306
307 if (!e.isUnnamed()) {
308 // TODO: openness of the module not currently exposed
309 // by the language model API, but should be printed
310 // here once available.
311 writer.println("module " + e.getQualifiedName() + " {");
312 indentation++;
313 for (ModuleElement.Directive directive : e.getDirectives()) {
314 printDirective(directive);
315 }
316 indentation--;
317 writer.println("}");
318 } else
319 writer.println("// Unnamed module"); // Should we do more here?
320 return this;
321 }
322
323 void printDirective(ModuleElement.Directive directive) {
324 indent();
325 switch (directive.getKind()) {
326 case EXPORTS: // "exports package-name [to module-name-list]"
327 {
328 ExportsDirective exportsDirective = (ExportsDirective) directive;
329 writer.print("exports ");
330 writer.print(exportsDirective.getPackage().getQualifiedName());
331 printModuleList(exportsDirective.getTargetModules());
332 }
333 break;
334
335 case OPENS: // opens package-name [to module-name-list]
336 {
337 OpensDirective opensDirective = (OpensDirective) directive;
338 writer.print("opens ");
339 writer.print(opensDirective.getPackage().getQualifiedName());
340 printModuleList(opensDirective.getTargetModules());
341 }
342 break;
343
344 case PROVIDES: // provides service-name with implementation-name
345 {
346 ProvidesDirective providesDirective = (ProvidesDirective) directive;
347 writer.print("provides ");
348 writer.print(providesDirective.getService().getQualifiedName());
349 writer.print(" with ");
350 printNameableList(providesDirective.getImplementations());
351 }
352 break;
353
354 case REQUIRES: // requires (static|transitive)* module-name
355 {
356 RequiresDirective requiresDirective = (RequiresDirective) directive;
357 writer.print("requires ");
358 if (requiresDirective.isStatic())
359 writer.print("static ");
360 if (requiresDirective.isTransitive())
361 writer.print("transitive ");
362 writer.print(requiresDirective.getDependency().getQualifiedName());
363 }
364 break;
365
366 case USES: // uses service-name
367 {
368 UsesDirective usesDirective = (UsesDirective) directive;
369 writer.print("uses ");
370 writer.print(usesDirective.getService().getQualifiedName());
371 }
372 break;
373
374 default:
375 throw new UnsupportedOperationException("unknown directive " + directive);
376 }
377 writer.println(";");
378 }
379
380 void printModuleList(List<? extends ModuleElement> modules) {
381 if (modules != null) {
382 writer.print(" to ");
383 printNameableList(modules);
384 }
385 }
386
387 void printNameableList(List<? extends QualifiedNameable> nameables) {
388 boolean first = true;
389 for (QualifiedNameable nameable : nameables) {
390 if (!first)
391 writer.print(", ");
392 writer.print(nameable.getQualifiedName());
393 first = false;
394 }
395 }
396
397 public void flush() {
398 writer.flush();
399 }
400
401 private void printDocComment(Element e) {
402 String docComment = elementUtils.getDocComment(e);
403
404 if (docComment != null) {
405 // Break comment into lines
406 java.util.StringTokenizer st = new StringTokenizer(docComment,
407 "\n\r");
408 indent();
409 writer.println("/**");
410
411 while(st.hasMoreTokens()) {
412 indent();
413 writer.print(" *");
414 writer.println(st.nextToken());
|