src/share/classes/com/sun/tools/javac/tree/DCTree.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2011, 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.tree;
27
28
29 import javax.tools.Diagnostic;
30
31 import com.sun.source.doctree.*;
32 import com.sun.tools.javac.parser.Tokens.Comment;
33 import com.sun.tools.javac.util.Assert;
34 import com.sun.tools.javac.util.DiagnosticSource;
35 import com.sun.tools.javac.util.JCDiagnostic;
36 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
37 import com.sun.tools.javac.util.List;
38 import com.sun.tools.javac.util.Name;
39 import com.sun.tools.javac.util.Position;
40 import java.io.IOException;
41 import java.io.StringWriter;
42 import javax.tools.JavaFileObject;
43
44 /**
45 * <p><b>This is NOT part of any supported API.
46 * If you write code that depends on this, you do so at your own risk.
47 * This code and its internal interfaces are subject to change or
48 * deletion without notice.</b>
49 */
50 public abstract class DCTree implements DocTree {
51
52 /**
53 * The position in the comment string.
54 * Use {@link #getSourcePosition getSourcePosition} to convert
55 * it to a position in the source file.
56 *
57 * TODO: why not simply translate all these values into
58 * source file positions? Is it useful to have string-offset
59 * positions as well?
60 */
61 public int pos;
62
63 public long getSourcePosition(DCDocComment dc) {
64 return dc.comment.getSourcePos(pos);
65 }
66
67 public JCDiagnostic.DiagnosticPosition pos(DCDocComment dc) {
68 return new SimpleDiagnosticPosition(dc.comment.getSourcePos(pos));
69 }
70
71 /** Convert a tree to a pretty-printed string. */
72 @Override
73 public String toString() {
74 StringWriter s = new StringWriter();
75 try {
76 new DocPretty(s).print(this);
77 }
78 catch (IOException e) {
79 // should never happen, because StringWriter is defined
80 // never to throw any IOExceptions
81 throw new AssertionError(e);
82 }
83 return s.toString();
84 }
85
86 public static abstract class DCEndPosTree<T extends DCEndPosTree<T>> extends DCTree {
87
88 private int endPos = Position.NOPOS;
89
90 public int getEndPos(DCDocComment dc) {
91 return dc.comment.getSourcePos(endPos);
92 }
93
94 @SuppressWarnings("unchecked")
95 public T setEndPos(int endPos) {
96 this.endPos = endPos;
97 return (T) this;
98 }
99
100 }
101
102 public static class DCDocComment extends DCTree implements DocCommentTree {
103 public final Comment comment; // required for the implicit source pos table
104
105 public final List<DCTree> firstSentence;
106 public final List<DCTree> body;
107 public final List<DCTree> tags;
108
109 public DCDocComment(Comment comment,
110 List<DCTree> firstSentence, List<DCTree> body, List<DCTree> tags) {
111 this.comment = comment;
112 this.firstSentence = firstSentence;
113 this.body = body;
114 this.tags = tags;
115 }
116
117 public Kind getKind() {
118 return Kind.DOC_COMMENT;
119 }
120
121 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
122 return v.visitDocComment(this, d);
123 }
124
125 public List<? extends DocTree> getFirstSentence() {
126 return firstSentence;
127 }
128
129 public List<? extends DocTree> getBody() {
130 return body;
131 }
132
133 public List<? extends DocTree> getBlockTags() {
134 return tags;
135 }
136
137 }
138
139 public static abstract class DCBlockTag extends DCTree implements BlockTagTree {
140 public String getTagName() {
141 return getKind().tagName;
142 }
143 }
144
145 public static abstract class DCInlineTag extends DCEndPosTree<DCInlineTag> implements InlineTagTree {
146 public String getTagName() {
147 return getKind().tagName;
148 }
149 }
150
151 public static class DCAttribute extends DCTree implements AttributeTree {
152 public final Name name;
153 public final ValueKind vkind;
154 public final List<DCTree> value;
155
156 DCAttribute(Name name, ValueKind vkind, List<DCTree> value) {
157 Assert.check((vkind == ValueKind.EMPTY) ? (value == null) : (value != null));
158 this.name = name;
159 this.vkind = vkind;
160 this.value = value;
161 }
162
163 @Override
164 public Kind getKind() {
165 return Kind.ATTRIBUTE;
166 }
167
168 @Override
169 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
170 return v.visitAttribute(this, d);
171 }
172
173 @Override
174 public Name getName() {
175 return name;
176 }
177
178 @Override
179 public ValueKind getValueKind() {
180 return vkind;
181 }
182
183 @Override
184 public List<DCTree> getValue() {
185 return value;
186 }
187 }
188
189 public static class DCAuthor extends DCBlockTag implements AuthorTree {
190 public final List<DCTree> name;
191
192 DCAuthor(List<DCTree> name) {
193 this.name = name;
194 }
195
196 @Override
197 public Kind getKind() {
198 return Kind.AUTHOR;
199 }
200
201 @Override
202 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
203 return v.visitAuthor(this, d);
204 }
205
206 @Override
207 public List<? extends DocTree> getName() {
208 return name;
209 }
210 }
211
212 public static class DCComment extends DCTree implements CommentTree {
213 public final String body;
214
215 DCComment(String body) {
216 this.body = body;
217 }
218
219 @Override
220 public Kind getKind() {
221 return Kind.COMMENT;
222 }
223
224 @Override
225 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
226 return v.visitComment(this, d);
227 }
228
229 @Override
230 public String getBody() {
231 return body;
232 }
233 }
234
235 public static class DCDeprecated extends DCBlockTag implements DeprecatedTree {
236 public final List<DCTree> body;
237
238 DCDeprecated(List<DCTree> body) {
239 this.body = body;
240 }
241
242 @Override
243 public Kind getKind() {
244 return Kind.DEPRECATED;
245 }
246
247 @Override
248 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
249 return v.visitDeprecated(this, d);
250 }
251
252 @Override
253 public List<? extends DocTree> getBody() {
254 return body;
255 }
256 }
257
258 public static class DCDocRoot extends DCInlineTag implements DocRootTree {
259
260 @Override
261 public Kind getKind() {
262 return Kind.DOC_ROOT;
263 }
264
265 @Override
266 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
267 return v.visitDocRoot(this, d);
268 }
269 }
270
271 public static class DCEndElement extends DCTree implements EndElementTree {
272 public final Name name;
273
274 DCEndElement(Name name) {
275 this.name = name;
276 }
277
278 @Override
279 public Kind getKind() {
280 return Kind.END_ELEMENT;
281 }
282
283 @Override
284 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
285 return v.visitEndElement(this, d);
286 }
287
288 @Override
289 public Name getName() {
290 return name;
291 }
292 }
293
294 public static class DCEntity extends DCTree implements EntityTree {
295 public final Name name;
296
297 DCEntity(Name name) {
298 this.name = name;
299 }
300
301 @Override
302 public Kind getKind() {
303 return Kind.ENTITY;
304 }
305
306 @Override
307 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
308 return v.visitEntity(this, d);
309 }
310
311 @Override
312 public Name getName() {
313 return name;
314 }
315 }
316
317 public static class DCErroneous extends DCTree implements ErroneousTree, JCDiagnostic.DiagnosticPosition {
318 public final String body;
319 public final JCDiagnostic diag;
320
321 DCErroneous(String body, JCDiagnostic.Factory diags, DiagnosticSource diagSource, String code, Object... args) {
322 this.body = body;
323 this.diag = diags.error(diagSource, this, code, args);
324 }
325
326 @Override
327 public Kind getKind() {
328 return Kind.ERRONEOUS;
329 }
330
331 @Override
332 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
333 return v.visitErroneous(this, d);
334 }
335
336 @Override
337 public String getBody() {
338 return body;
339 }
340
341 @Override
342 public Diagnostic<JavaFileObject> getDiagnostic() {
343 return diag;
344 }
345
346 @Override
347 public JCTree getTree() {
348 return null;
349 }
350
351 @Override
352 public int getStartPosition() {
353 return pos;
354 }
355
356 @Override
357 public int getPreferredPosition() {
358 return pos + body.length() - 1;
359 }
360
361 @Override
362 public int getEndPosition(EndPosTable endPosTable) {
363 return pos + body.length();
364 }
365
366 }
367
368 public static class DCIdentifier extends DCTree implements IdentifierTree {
369 public final Name name;
370
371 DCIdentifier(Name name) {
372 this.name = name;
373 }
374
375 @Override
376 public Kind getKind() {
377 return Kind.IDENTIFIER;
378 }
379
380 @Override
381 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
382 return v.visitIdentifier(this, d);
383 }
384
385 @Override
386 public Name getName() {
387 return name;
388 }
389 }
390
391 public static class DCInheritDoc extends DCInlineTag implements InheritDocTree {
392 @Override
393 public Kind getKind() {
394 return Kind.INHERIT_DOC;
395 }
396
397 @Override
398 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
399 return v.visitInheritDoc(this, d);
400 }
401 }
402
403 public static class DCLink extends DCInlineTag implements LinkTree {
404 public final Kind kind;
405 public final DCReference ref;
406 public final List<DCTree> label;
407
408 DCLink(Kind kind, DCReference ref, List<DCTree> label) {
409 Assert.check(kind == Kind.LINK || kind == Kind.LINK_PLAIN);
410 this.kind = kind;
411 this.ref = ref;
412 this.label = label;
413 }
414
415 @Override
416 public Kind getKind() {
417 return kind;
418 }
419
420 @Override
421 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
422 return v.visitLink(this, d);
423 }
424
425 @Override
426 public ReferenceTree getReference() {
427 return ref;
428 }
429
430 @Override
431 public List<? extends DocTree> getLabel() {
432 return label;
433 }
434 }
435
436 public static class DCLiteral extends DCInlineTag implements LiteralTree {
437 public final Kind kind;
438 public final DCText body;
439
440 DCLiteral(Kind kind, DCText body) {
441 Assert.check(kind == Kind.CODE || kind == Kind.LITERAL);
442 this.kind = kind;
443 this.body = body;
444 }
445
446 @Override
447 public Kind getKind() {
448 return kind;
449 }
450
451 @Override
452 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
453 return v.visitLiteral(this, d);
454 }
455
456 @Override
457 public DCText getBody() {
458 return body;
459 }
460 }
461
462 public static class DCParam extends DCBlockTag implements ParamTree {
463 public final boolean isTypeParameter;
464 public final DCIdentifier name;
465 public final List<DCTree> description;
466
467 DCParam(boolean isTypeParameter, DCIdentifier name, List<DCTree> description) {
468 this.isTypeParameter = isTypeParameter;
469 this.name = name;
470 this.description = description;
471 }
472
473 @Override
474 public Kind getKind() {
475 return Kind.PARAM;
476 }
477
478 @Override
479 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
480 return v.visitParam(this, d);
481 }
482
483 @Override
484 public boolean isTypeParameter() {
485 return isTypeParameter;
486 }
487
488 @Override
489 public IdentifierTree getName() {
490 return name;
491 }
492
493 @Override
494 public List<? extends DocTree> getDescription() {
495 return description;
496 }
497 }
498
499 public static class DCReference extends DCEndPosTree<DCReference> implements ReferenceTree {
500 public final String signature;
501
502 // The following are not directly exposed through ReferenceTree
503 // use DocTrees.getElement(TreePath,ReferenceTree)
504 public final JCTree qualifierExpression;
505 public final Name memberName;
506 public final List<JCTree> paramTypes;
507
508
509 DCReference(String signature, JCTree qualExpr, Name member, List<JCTree> paramTypes) {
510 this.signature = signature;
511 qualifierExpression = qualExpr;
512 memberName = member;
513 this.paramTypes = paramTypes;
514 }
515
516 @Override
517 public Kind getKind() {
518 return Kind.REFERENCE;
519 }
520
521 @Override
522 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
523 return v.visitReference(this, d);
524 }
525
526 @Override
527 public String getSignature() {
528 return signature;
529 }
530 }
531
532 public static class DCReturn extends DCBlockTag implements ReturnTree {
533 public final List<DCTree> description;
534
535 DCReturn(List<DCTree> description) {
536 this.description = description;
537 }
538
539 @Override
540 public Kind getKind() {
541 return Kind.RETURN;
542 }
543
544 @Override
545 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
546 return v.visitReturn(this, d);
547 }
548
549 @Override
550 public List<? extends DocTree> getDescription() {
551 return description;
552 }
553 }
554
555 public static class DCSee extends DCBlockTag implements SeeTree {
556 public final List<DCTree> reference;
557
558 DCSee(List<DCTree> reference) {
559 this.reference = reference;
560 }
561
562 @Override
563 public Kind getKind() {
564 return Kind.SEE;
565 }
566
567 @Override
568 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
569 return v.visitSee(this, d);
570 }
571
572 @Override
573 public List<? extends DocTree> getReference() {
574 return reference;
575 }
576 }
577
578 public static class DCSerial extends DCBlockTag implements SerialTree {
579 public final List<DCTree> description;
580
581 DCSerial(List<DCTree> description) {
582 this.description = description;
583 }
584
585 @Override
586 public Kind getKind() {
587 return Kind.SERIAL;
588 }
589
590 @Override
591 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
592 return v.visitSerial(this, d);
593 }
594
595 @Override
596 public List<? extends DocTree> getDescription() {
597 return description;
598 }
599 }
600
601 public static class DCSerialData extends DCBlockTag implements SerialDataTree {
602 public final List<DCTree> description;
603
604 DCSerialData(List<DCTree> description) {
605 this.description = description;
606 }
607
608 @Override
609 public Kind getKind() {
610 return Kind.SERIAL_DATA;
611 }
612
613 @Override
614 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
615 return v.visitSerialData(this, d);
616 }
617
618 @Override
619 public List<? extends DocTree> getDescription() {
620 return description;
621 }
622 }
623
624 public static class DCSerialField extends DCBlockTag implements SerialFieldTree {
625 public final DCIdentifier name;
626 public final DCReference type;
627 public final List<DCTree> description;
628
629 DCSerialField(DCIdentifier name, DCReference type, List<DCTree> description) {
630 this.description = description;
631 this.name = name;
632 this.type = type;
633 }
634
635 @Override
636 public Kind getKind() {
637 return Kind.SERIAL_FIELD;
638 }
639
640 @Override
641 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
642 return v.visitSerialField(this, d);
643 }
644
645 @Override
646 public List<? extends DocTree> getDescription() {
647 return description;
648 }
649
650 @Override
651 public IdentifierTree getName() {
652 return name;
653 }
654
655 @Override
656 public ReferenceTree getType() {
657 return type;
658 }
659 }
660
661 public static class DCSince extends DCBlockTag implements SinceTree {
662 public final List<DCTree> body;
663
664 DCSince(List<DCTree> body) {
665 this.body = body;
666 }
667
668 @Override
669 public Kind getKind() {
670 return Kind.SINCE;
671 }
672
673 @Override
674 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
675 return v.visitSince(this, d);
676 }
677
678 @Override
679 public List<? extends DocTree> getBody() {
680 return body;
681 }
682 }
683
684 public static class DCStartElement extends DCEndPosTree<DCStartElement> implements StartElementTree {
685 public final Name name;
686 public final List<DCTree> attrs;
687 public final boolean selfClosing;
688
689 DCStartElement(Name name, List<DCTree> attrs, boolean selfClosing) {
690 this.name = name;
691 this.attrs = attrs;
692 this.selfClosing = selfClosing;
693 }
694
695 @Override
696 public Kind getKind() {
697 return Kind.START_ELEMENT;
698 }
699
700 @Override
701 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
702 return v.visitStartElement(this, d);
703 }
704
705 @Override
706 public Name getName() {
707 return name;
708 }
709
710 @Override
711 public List<? extends DocTree> getAttributes() {
712 return attrs;
713 }
714
715 @Override
716 public boolean isSelfClosing() {
717 return selfClosing;
718 }
719 }
720
721 public static class DCText extends DCTree implements TextTree {
722 public final String text;
723
724 DCText(String text) {
725 this.text = text;
726 }
727
728 @Override
729 public Kind getKind() {
730 return Kind.TEXT;
731 }
732
733 @Override
734 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
735 return v.visitText(this, d);
736 }
737
738 @Override
739 public String getBody() {
740 return text;
741 }
742 }
743
744 public static class DCThrows extends DCBlockTag implements ThrowsTree {
745 public final Kind kind;
746 public final DCReference name;
747 public final List<DCTree> description;
748
749 DCThrows(Kind kind, DCReference name, List<DCTree> description) {
750 Assert.check(kind == Kind.EXCEPTION || kind == Kind.THROWS);
751 this.kind = kind;
752 this.name = name;
753 this.description = description;
754 }
755
756 @Override
757 public Kind getKind() {
758 return kind;
759 }
760
761 @Override
762 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
763 return v.visitThrows(this, d);
764 }
765
766 @Override
767 public ReferenceTree getExceptionName() {
768 return name;
769 }
770
771 @Override
772 public List<? extends DocTree> getDescription() {
773 return description;
774 }
775 }
776
777 public static class DCUnknownBlockTag extends DCBlockTag implements UnknownBlockTagTree {
778 public final Name name;
779 public final List<DCTree> content;
780
781 DCUnknownBlockTag(Name name, List<DCTree> content) {
782 this.name = name;
783 this.content = content;
784 }
785
786 @Override
787 public Kind getKind() {
788 return Kind.UNKNOWN_BLOCK_TAG;
789 }
790
791 @Override
792 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
793 return v.visitUnknownBlockTag(this, d);
794 }
795
796 @Override
797 public String getTagName() {
798 return name.toString();
799 }
800
801 @Override
802 public List<? extends DocTree> getContent() {
803 return content;
804 }
805 }
806
807 public static class DCUnknownInlineTag extends DCInlineTag implements UnknownInlineTagTree {
808 public final Name name;
809 public final List<DCTree> content;
810
811 DCUnknownInlineTag(Name name, List<DCTree> content) {
812 this.name = name;
813 this.content = content;
814 }
815
816 @Override
817 public Kind getKind() {
818 return Kind.UNKNOWN_INLINE_TAG;
819 }
820
821 @Override
822 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
823 return v.visitUnknownInlineTag(this, d);
824 }
825
826 @Override
827 public String getTagName() {
828 return name.toString();
829 }
830
831 @Override
832 public List<? extends DocTree> getContent() {
833 return content;
834 }
835 }
836
837 public static class DCValue extends DCInlineTag implements ValueTree {
838 public final DCReference ref;
839
840 DCValue(DCReference ref) {
841 this.ref = ref;
842 }
843
844 @Override
845 public Kind getKind() {
846 return Kind.VALUE;
847 }
848
849 @Override
850 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
851 return v.visitValue(this, d);
852 }
853
854 @Override
855 public ReferenceTree getReference() {
856 return ref;
857 }
858 }
859
860 public static class DCVersion extends DCBlockTag implements VersionTree {
861 public final List<DCTree> body;
862
863 DCVersion(List<DCTree> body) {
864 this.body = body;
865 }
866
867 @Override
868 public Kind getKind() {
869 return Kind.VERSION;
870 }
871
872 @Override
873 public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
874 return v.visitVersion(this, d);
875 }
876
877 @Override
878 public List<? extends DocTree> getBody() {
879 return body;
880 }
881 }
882
883 }

mercurial