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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     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  */
    26 package com.sun.tools.javac.tree;
    29 import javax.tools.Diagnostic;
    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;
    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 {
    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;
    63     public long getSourcePosition(DCDocComment dc) {
    64         return dc.comment.getSourcePos(pos);
    65     }
    67     public JCDiagnostic.DiagnosticPosition pos(DCDocComment dc) {
    68         return new SimpleDiagnosticPosition(dc.comment.getSourcePos(pos));
    69     }
    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     }
    86     public static abstract class DCEndPosTree<T extends DCEndPosTree<T>> extends DCTree {
    88         private int endPos = Position.NOPOS;
    90         public int getEndPos(DCDocComment dc) {
    91             return dc.comment.getSourcePos(endPos);
    92         }
    94         @SuppressWarnings("unchecked")
    95         public T setEndPos(int endPos) {
    96             this.endPos = endPos;
    97             return (T) this;
    98         }
   100     }
   102     public static class DCDocComment extends DCTree implements DocCommentTree {
   103         public final Comment comment; // required for the implicit source pos table
   105         public final List<DCTree> firstSentence;
   106         public final List<DCTree> body;
   107         public final List<DCTree> tags;
   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         }
   117         public Kind getKind() {
   118             return Kind.DOC_COMMENT;
   119         }
   121         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   122             return v.visitDocComment(this, d);
   123         }
   125         public List<? extends DocTree> getFirstSentence() {
   126             return firstSentence;
   127         }
   129         public List<? extends DocTree> getBody() {
   130             return body;
   131         }
   133         public List<? extends DocTree> getBlockTags() {
   134             return tags;
   135         }
   137     }
   139     public static abstract class DCBlockTag extends DCTree implements BlockTagTree {
   140         public String getTagName() {
   141             return getKind().tagName;
   142         }
   143     }
   145     public static abstract class DCInlineTag extends DCEndPosTree<DCInlineTag> implements InlineTagTree {
   146         public String getTagName() {
   147             return getKind().tagName;
   148         }
   149     }
   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;
   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         }
   163         @Override
   164         public Kind getKind() {
   165             return Kind.ATTRIBUTE;
   166         }
   168         @Override
   169         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   170             return v.visitAttribute(this, d);
   171         }
   173         @Override
   174         public Name getName() {
   175             return name;
   176         }
   178         @Override
   179         public ValueKind getValueKind() {
   180             return vkind;
   181         }
   183         @Override
   184         public List<DCTree> getValue() {
   185             return value;
   186         }
   187     }
   189     public static class DCAuthor extends DCBlockTag implements AuthorTree {
   190         public final List<DCTree> name;
   192         DCAuthor(List<DCTree> name) {
   193             this.name = name;
   194         }
   196         @Override
   197         public Kind getKind() {
   198             return Kind.AUTHOR;
   199         }
   201         @Override
   202         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   203             return v.visitAuthor(this, d);
   204         }
   206         @Override
   207         public List<? extends DocTree> getName() {
   208             return name;
   209         }
   210     }
   212     public static class DCComment extends DCTree implements CommentTree {
   213         public final String body;
   215         DCComment(String body) {
   216             this.body = body;
   217         }
   219         @Override
   220         public Kind getKind() {
   221             return Kind.COMMENT;
   222         }
   224         @Override
   225         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   226             return v.visitComment(this, d);
   227         }
   229         @Override
   230         public String getBody() {
   231             return body;
   232         }
   233     }
   235     public static class DCDeprecated extends DCBlockTag implements DeprecatedTree {
   236         public final List<DCTree> body;
   238         DCDeprecated(List<DCTree> body) {
   239             this.body = body;
   240         }
   242         @Override
   243         public Kind getKind() {
   244             return Kind.DEPRECATED;
   245         }
   247         @Override
   248         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   249             return v.visitDeprecated(this, d);
   250         }
   252         @Override
   253         public List<? extends DocTree> getBody() {
   254             return body;
   255         }
   256     }
   258     public static class DCDocRoot extends DCInlineTag implements DocRootTree {
   260         @Override
   261         public Kind getKind() {
   262             return Kind.DOC_ROOT;
   263         }
   265         @Override
   266         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   267             return v.visitDocRoot(this, d);
   268         }
   269     }
   271     public static class DCEndElement extends DCTree implements EndElementTree {
   272         public final Name name;
   274         DCEndElement(Name name) {
   275             this.name = name;
   276         }
   278         @Override
   279         public Kind getKind() {
   280             return Kind.END_ELEMENT;
   281         }
   283         @Override
   284         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   285             return v.visitEndElement(this, d);
   286         }
   288         @Override
   289         public Name getName() {
   290             return name;
   291         }
   292     }
   294     public static class DCEntity extends DCTree implements EntityTree {
   295         public final Name name;
   297         DCEntity(Name name) {
   298             this.name = name;
   299         }
   301         @Override
   302         public Kind getKind() {
   303             return Kind.ENTITY;
   304         }
   306         @Override
   307         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   308             return v.visitEntity(this, d);
   309         }
   311         @Override
   312         public Name getName() {
   313             return name;
   314         }
   315     }
   317     public static class DCErroneous extends DCTree implements ErroneousTree, JCDiagnostic.DiagnosticPosition {
   318         public final String body;
   319         public final JCDiagnostic diag;
   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         }
   326         @Override
   327         public Kind getKind() {
   328             return Kind.ERRONEOUS;
   329         }
   331         @Override
   332         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   333             return v.visitErroneous(this, d);
   334         }
   336         @Override
   337         public String getBody() {
   338             return body;
   339         }
   341         @Override
   342         public Diagnostic<JavaFileObject> getDiagnostic() {
   343             return diag;
   344         }
   346         @Override
   347         public JCTree getTree() {
   348             return null;
   349         }
   351         @Override
   352         public int getStartPosition() {
   353             return pos;
   354         }
   356         @Override
   357         public int getPreferredPosition() {
   358             return pos + body.length() - 1;
   359         }
   361         @Override
   362         public int getEndPosition(EndPosTable endPosTable) {
   363             return pos + body.length();
   364         }
   366     }
   368     public static class DCIdentifier extends DCTree implements IdentifierTree {
   369         public final Name name;
   371         DCIdentifier(Name name) {
   372             this.name = name;
   373         }
   375         @Override
   376         public Kind getKind() {
   377             return Kind.IDENTIFIER;
   378         }
   380         @Override
   381         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   382             return v.visitIdentifier(this, d);
   383         }
   385         @Override
   386         public Name getName() {
   387             return name;
   388         }
   389     }
   391     public static class DCInheritDoc extends DCInlineTag implements InheritDocTree {
   392         @Override
   393         public Kind getKind() {
   394             return Kind.INHERIT_DOC;
   395         }
   397         @Override
   398         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   399             return v.visitInheritDoc(this, d);
   400         }
   401     }
   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;
   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         }
   415         @Override
   416         public Kind getKind() {
   417             return kind;
   418         }
   420         @Override
   421         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   422             return v.visitLink(this, d);
   423         }
   425         @Override
   426         public ReferenceTree getReference() {
   427             return ref;
   428         }
   430         @Override
   431         public List<? extends DocTree> getLabel() {
   432             return label;
   433         }
   434     }
   436     public static class DCLiteral extends DCInlineTag implements LiteralTree {
   437         public final Kind kind;
   438         public final DCText body;
   440         DCLiteral(Kind kind, DCText body) {
   441             Assert.check(kind == Kind.CODE || kind == Kind.LITERAL);
   442             this.kind = kind;
   443             this.body = body;
   444         }
   446         @Override
   447         public Kind getKind() {
   448             return kind;
   449         }
   451         @Override
   452         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   453             return v.visitLiteral(this, d);
   454         }
   456         @Override
   457         public DCText getBody() {
   458             return body;
   459         }
   460     }
   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;
   467         DCParam(boolean isTypeParameter, DCIdentifier name, List<DCTree> description) {
   468             this.isTypeParameter = isTypeParameter;
   469             this.name = name;
   470             this.description = description;
   471         }
   473         @Override
   474         public Kind getKind() {
   475             return Kind.PARAM;
   476         }
   478         @Override
   479         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   480             return v.visitParam(this, d);
   481         }
   483         @Override
   484         public boolean isTypeParameter() {
   485             return isTypeParameter;
   486         }
   488         @Override
   489         public IdentifierTree getName() {
   490             return name;
   491         }
   493         @Override
   494         public List<? extends DocTree> getDescription() {
   495             return description;
   496         }
   497     }
   499     public static class DCReference extends DCEndPosTree<DCReference> implements ReferenceTree {
   500         public final String signature;
   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;
   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         }
   516         @Override
   517         public Kind getKind() {
   518             return Kind.REFERENCE;
   519         }
   521         @Override
   522         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   523             return v.visitReference(this, d);
   524         }
   526         @Override
   527         public String getSignature() {
   528             return signature;
   529         }
   530     }
   532     public static class DCReturn extends DCBlockTag implements ReturnTree {
   533         public final List<DCTree> description;
   535         DCReturn(List<DCTree> description) {
   536             this.description = description;
   537         }
   539         @Override
   540         public Kind getKind() {
   541             return Kind.RETURN;
   542         }
   544         @Override
   545         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   546             return v.visitReturn(this, d);
   547         }
   549         @Override
   550         public List<? extends DocTree> getDescription() {
   551             return description;
   552         }
   553     }
   555     public static class DCSee extends DCBlockTag implements SeeTree {
   556         public final List<DCTree> reference;
   558         DCSee(List<DCTree> reference) {
   559             this.reference = reference;
   560         }
   562         @Override
   563         public Kind getKind() {
   564             return Kind.SEE;
   565         }
   567         @Override
   568         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   569             return v.visitSee(this, d);
   570         }
   572         @Override
   573         public List<? extends DocTree> getReference() {
   574             return reference;
   575         }
   576     }
   578     public static class DCSerial extends DCBlockTag implements SerialTree {
   579         public final List<DCTree> description;
   581         DCSerial(List<DCTree> description) {
   582             this.description = description;
   583         }
   585         @Override
   586         public Kind getKind() {
   587             return Kind.SERIAL;
   588         }
   590         @Override
   591         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   592             return v.visitSerial(this, d);
   593         }
   595         @Override
   596         public List<? extends DocTree> getDescription() {
   597             return description;
   598         }
   599     }
   601     public static class DCSerialData extends DCBlockTag implements SerialDataTree {
   602         public final List<DCTree> description;
   604         DCSerialData(List<DCTree> description) {
   605             this.description = description;
   606         }
   608         @Override
   609         public Kind getKind() {
   610             return Kind.SERIAL_DATA;
   611         }
   613         @Override
   614         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   615             return v.visitSerialData(this, d);
   616         }
   618         @Override
   619         public List<? extends DocTree> getDescription() {
   620             return description;
   621         }
   622     }
   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;
   629         DCSerialField(DCIdentifier name, DCReference type, List<DCTree> description) {
   630             this.description = description;
   631             this.name = name;
   632             this.type = type;
   633         }
   635         @Override
   636         public Kind getKind() {
   637             return Kind.SERIAL_FIELD;
   638         }
   640         @Override
   641         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   642             return v.visitSerialField(this, d);
   643         }
   645         @Override
   646         public List<? extends DocTree> getDescription() {
   647             return description;
   648         }
   650         @Override
   651         public IdentifierTree getName() {
   652             return name;
   653         }
   655         @Override
   656         public ReferenceTree getType() {
   657             return type;
   658         }
   659     }
   661     public static class DCSince extends DCBlockTag implements SinceTree {
   662         public final List<DCTree> body;
   664         DCSince(List<DCTree> body) {
   665             this.body = body;
   666         }
   668         @Override
   669         public Kind getKind() {
   670             return Kind.SINCE;
   671         }
   673         @Override
   674         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   675             return v.visitSince(this, d);
   676         }
   678         @Override
   679         public List<? extends DocTree> getBody() {
   680             return body;
   681         }
   682     }
   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;
   689         DCStartElement(Name name, List<DCTree> attrs, boolean selfClosing) {
   690             this.name = name;
   691             this.attrs = attrs;
   692             this.selfClosing = selfClosing;
   693         }
   695         @Override
   696         public Kind getKind() {
   697             return Kind.START_ELEMENT;
   698         }
   700         @Override
   701         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   702             return v.visitStartElement(this, d);
   703         }
   705         @Override
   706         public Name getName() {
   707             return name;
   708         }
   710         @Override
   711         public List<? extends DocTree> getAttributes() {
   712             return attrs;
   713         }
   715         @Override
   716         public boolean isSelfClosing() {
   717             return selfClosing;
   718         }
   719     }
   721     public static class DCText extends DCTree implements TextTree {
   722         public final String text;
   724         DCText(String text) {
   725             this.text = text;
   726         }
   728         @Override
   729         public Kind getKind() {
   730             return Kind.TEXT;
   731         }
   733         @Override
   734         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   735             return v.visitText(this, d);
   736         }
   738         @Override
   739         public String getBody() {
   740             return text;
   741         }
   742     }
   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;
   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         }
   756         @Override
   757         public Kind getKind() {
   758             return kind;
   759         }
   761         @Override
   762         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   763             return v.visitThrows(this, d);
   764         }
   766         @Override
   767         public ReferenceTree getExceptionName() {
   768             return name;
   769         }
   771         @Override
   772         public List<? extends DocTree> getDescription() {
   773             return description;
   774         }
   775     }
   777     public static class DCUnknownBlockTag extends DCBlockTag implements UnknownBlockTagTree {
   778         public final Name name;
   779         public final List<DCTree> content;
   781         DCUnknownBlockTag(Name name, List<DCTree> content) {
   782             this.name = name;
   783             this.content = content;
   784         }
   786         @Override
   787         public Kind getKind() {
   788             return Kind.UNKNOWN_BLOCK_TAG;
   789         }
   791         @Override
   792         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   793             return v.visitUnknownBlockTag(this, d);
   794         }
   796         @Override
   797         public String getTagName() {
   798             return name.toString();
   799         }
   801         @Override
   802         public List<? extends DocTree> getContent() {
   803             return content;
   804         }
   805     }
   807     public static class DCUnknownInlineTag extends DCInlineTag implements UnknownInlineTagTree {
   808         public final Name name;
   809         public final List<DCTree> content;
   811         DCUnknownInlineTag(Name name, List<DCTree> content) {
   812             this.name = name;
   813             this.content = content;
   814         }
   816         @Override
   817         public Kind getKind() {
   818             return Kind.UNKNOWN_INLINE_TAG;
   819         }
   821         @Override
   822         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   823             return v.visitUnknownInlineTag(this, d);
   824         }
   826         @Override
   827         public String getTagName() {
   828             return name.toString();
   829         }
   831         @Override
   832         public List<? extends DocTree> getContent() {
   833             return content;
   834         }
   835     }
   837     public static class DCValue extends DCInlineTag implements ValueTree {
   838         public final DCReference ref;
   840         DCValue(DCReference ref) {
   841             this.ref = ref;
   842         }
   844         @Override
   845         public Kind getKind() {
   846             return Kind.VALUE;
   847         }
   849         @Override
   850         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   851             return v.visitValue(this, d);
   852         }
   854         @Override
   855         public ReferenceTree getReference() {
   856             return ref;
   857         }
   858     }
   860     public static class DCVersion extends DCBlockTag implements VersionTree {
   861         public final List<DCTree> body;
   863         DCVersion(List<DCTree> body) {
   864             this.body = body;
   865         }
   867         @Override
   868         public Kind getKind() {
   869             return Kind.VERSION;
   870         }
   872         @Override
   873         public <R, D> R accept(DocTreeVisitor<R, D> v, D d) {
   874             return v.visitVersion(this, d);
   875         }
   877         @Override
   878         public List<? extends DocTree> getBody() {
   879             return body;
   880         }
   881     }
   883 }

mercurial