8008174: DocTree API should provide start and end positions for tree nodes

Wed, 17 Apr 2013 15:54:24 +0200

author
jlahoda
date
Wed, 17 Apr 2013 15:54:24 +0200
changeset 1704
ed918a442b83
parent 1699
94870c08391c
child 1705
891b88acf47a

8008174: DocTree API should provide start and end positions for tree nodes
Summary: Adding DocSourcePositions to allow access to DocTree starting/ending position
Reviewed-by: jjg, darcy
Contributed-by: Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org>, Jan Lahoda <jlahoda@netbeans.org>

src/share/classes/com/sun/source/util/DocSourcePositions.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/source/util/DocTrees.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/source/util/SourcePositions.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/api/JavacTrees.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/parser/DocCommentParser.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/tree/DCTree.java file | annotate | diff | comparison | revisions
test/tools/javac/doctree/positions/TestPosition.java file | annotate | diff | comparison | revisions
test/tools/javac/doctree/positions/TestPosition.out file | annotate | diff | comparison | revisions
test/tools/javac/doctree/positions/TestPositionSource.java file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/source/util/DocSourcePositions.java	Wed Apr 17 15:54:24 2013 +0200
     1.3 @@ -0,0 +1,98 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.source.util;
    1.30 +
    1.31 +import com.sun.source.doctree.DocCommentTree;
    1.32 +import com.sun.source.doctree.DocTree;
    1.33 +import com.sun.source.tree.CompilationUnitTree;
    1.34 +
    1.35 +/**
    1.36 + * Provides methods to obtain the position of a DocTree within a javadoc comment.
    1.37 + * A position is defined as a simple character offset from the start of a
    1.38 + * CompilationUnit where the first character is at offset 0.
    1.39 + *
    1.40 + * @since 1.8
    1.41 + */
    1.42 +@jdk.Supported
    1.43 +public interface DocSourcePositions extends SourcePositions {
    1.44 +
    1.45 +    /**
    1.46 +     * Gets the starting position of the tree within the comment within the file.  If tree is not found within
    1.47 +     * file, or if the starting position is not available,
    1.48 +     * return {@link javax.tools.Diagnostic#NOPOS}.
    1.49 +     * The given tree should be under the given comment tree, and the given documentation
    1.50 +     * comment tree should be returned from a {@link DocTrees#getDocCommentTree(com.sun.source.util.TreePath) }
    1.51 +     * for a tree under the given file.
    1.52 +     * The returned position must be at the start of the yield of this tree, that
    1.53 +     * is for any sub-tree of this tree, the following must hold:
    1.54 +     *
    1.55 +     * <p>
    1.56 +     * {@code tree.getStartPosition() <= subtree.getStartPosition()} or <br>
    1.57 +     * {@code tree.getStartPosition() == NOPOS} or <br>
    1.58 +     * {@code subtree.getStartPosition() == NOPOS}
    1.59 +     * </p>
    1.60 +     *
    1.61 +     * @param file CompilationUnit in which to find tree.
    1.62 +     * @param comment the comment tree that encloses the tree for which the
    1.63 +     *                position is being sought
    1.64 +     * @param tree tree for which a position is sought.
    1.65 +     * @return the start position of tree.
    1.66 +     */
    1.67 +    long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree);
    1.68 +
    1.69 +    /**
    1.70 +     * Gets the ending position of the tree within the comment within the file.  If tree is not found within
    1.71 +     * file, or if the ending position is not available,
    1.72 +     * return {@link javax.tools.Diagnostic#NOPOS}.
    1.73 +     * The given tree should be under the given comment tree, and the given documentation
    1.74 +     * comment tree should be returned from a {@link DocTrees#getDocCommentTree(com.sun.source.util.TreePath) }
    1.75 +     * for a tree under the given file.
    1.76 +     * The returned position must be at the end of the yield of this tree,
    1.77 +     * that is for any sub-tree of this tree, the following must hold:
    1.78 +     *
    1.79 +     * <p>
    1.80 +     * {@code tree.getEndPosition() >= subtree.getEndPosition()} or <br>
    1.81 +     * {@code tree.getEndPosition() == NOPOS} or <br>
    1.82 +     * {@code subtree.getEndPosition() == NOPOS}
    1.83 +     * </p>
    1.84 +     *
    1.85 +     * In addition, the following must hold:
    1.86 +     *
    1.87 +     * <p>
    1.88 +     * {@code tree.getStartPosition() <= tree.getEndPosition()}  or <br>
    1.89 +     * {@code tree.getStartPosition() == NOPOS} or <br>
    1.90 +     * {@code tree.getEndPosition() == NOPOS}
    1.91 +     * </p>
    1.92 +     *
    1.93 +     * @param file CompilationUnit in which to find tree.
    1.94 +     * @param comment the comment tree that encloses the tree for which the
    1.95 +     *                position is being sought
    1.96 +     * @param tree tree for which a position is sought.
    1.97 +     * @return the start position of tree.
    1.98 +     */
    1.99 +    long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree);
   1.100 +
   1.101 +}
     2.1 --- a/src/share/classes/com/sun/source/util/DocTrees.java	Wed Apr 17 10:31:01 2013 -0700
     2.2 +++ b/src/share/classes/com/sun/source/util/DocTrees.java	Wed Apr 17 15:54:24 2013 +0200
     2.3 @@ -72,6 +72,8 @@
     2.4       */
     2.5      public abstract Element getElement(TreePath path, ReferenceTree reference);
     2.6  
     2.7 +    public abstract DocSourcePositions getSourcePositions();
     2.8 +
     2.9      /**
    2.10       * Prints a message of the specified kind at the location of the
    2.11       * tree within the provided compilation unit
     3.1 --- a/src/share/classes/com/sun/source/util/SourcePositions.java	Wed Apr 17 10:31:01 2013 -0700
     3.2 +++ b/src/share/classes/com/sun/source/util/SourcePositions.java	Wed Apr 17 15:54:24 2013 +0200
     3.3 @@ -59,7 +59,7 @@
     3.4  
     3.5      /**
     3.6       * Gets the ending position of tree within file.  If tree is not found within
     3.7 -     * file, or if the starting position is not available,
     3.8 +     * file, or if the ending position is not available,
     3.9       * return {@link javax.tools.Diagnostic#NOPOS}.
    3.10       * The returned position must be at the end of the yield of this tree,
    3.11       * that is for any sub-tree of this tree, the following must hold:
     4.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Apr 17 10:31:01 2013 -0700
     4.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Apr 17 15:54:24 2013 +0200
     4.3 @@ -1,5 +1,5 @@
     4.4  /*
     4.5 - * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     4.6 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     4.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.8   *
     4.9   * This code is free software; you can redistribute it and/or modify it
    4.10 @@ -43,14 +43,16 @@
    4.11  import javax.tools.JavaFileObject;
    4.12  
    4.13  import com.sun.source.doctree.DocCommentTree;
    4.14 +import com.sun.source.doctree.DocTree;
    4.15  import com.sun.source.doctree.ReferenceTree;
    4.16  import com.sun.source.tree.CatchTree;
    4.17  import com.sun.source.tree.CompilationUnitTree;
    4.18  import com.sun.source.tree.Scope;
    4.19  import com.sun.source.tree.Tree;
    4.20 +import com.sun.source.util.DocSourcePositions;
    4.21 +import com.sun.source.util.DocTreeScanner;
    4.22  import com.sun.source.util.DocTrees;
    4.23  import com.sun.source.util.JavacTask;
    4.24 -import com.sun.source.util.SourcePositions;
    4.25  import com.sun.source.util.TreePath;
    4.26  import com.sun.tools.javac.code.Flags;
    4.27  import com.sun.tools.javac.code.Kinds;
    4.28 @@ -76,8 +78,14 @@
    4.29  import com.sun.tools.javac.model.JavacElements;
    4.30  import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    4.31  import com.sun.tools.javac.tree.DCTree;
    4.32 +import com.sun.tools.javac.tree.DCTree.DCBlockTag;
    4.33  import com.sun.tools.javac.tree.DCTree.DCDocComment;
    4.34 +import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
    4.35 +import com.sun.tools.javac.tree.DCTree.DCErroneous;
    4.36 +import com.sun.tools.javac.tree.DCTree.DCIdentifier;
    4.37 +import com.sun.tools.javac.tree.DCTree.DCParam;
    4.38  import com.sun.tools.javac.tree.DCTree.DCReference;
    4.39 +import com.sun.tools.javac.tree.DCTree.DCText;
    4.40  import com.sun.tools.javac.tree.EndPosTable;
    4.41  import com.sun.tools.javac.tree.JCTree;
    4.42  import com.sun.tools.javac.tree.JCTree.*;
    4.43 @@ -94,6 +102,7 @@
    4.44  import com.sun.tools.javac.util.Name;
    4.45  import com.sun.tools.javac.util.Names;
    4.46  import com.sun.tools.javac.util.Pair;
    4.47 +import com.sun.tools.javac.util.Position;
    4.48  import static com.sun.tools.javac.code.TypeTag.*;
    4.49  
    4.50  /**
    4.51 @@ -166,8 +175,8 @@
    4.52              javacTaskImpl = (JavacTaskImpl) t;
    4.53      }
    4.54  
    4.55 -    public SourcePositions getSourcePositions() {
    4.56 -        return new SourcePositions() {
    4.57 +    public DocSourcePositions getSourcePositions() {
    4.58 +        return new DocSourcePositions() {
    4.59                  public long getStartPosition(CompilationUnitTree file, Tree tree) {
    4.60                      return TreeInfo.getStartPos((JCTree) tree);
    4.61                  }
    4.62 @@ -176,9 +185,80 @@
    4.63                      EndPosTable endPosTable = ((JCCompilationUnit) file).endPositions;
    4.64                      return TreeInfo.getEndPos((JCTree) tree, endPosTable);
    4.65                  }
    4.66 +
    4.67 +                public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
    4.68 +                    return ((DCTree) tree).getSourcePosition((DCDocComment) comment);
    4.69 +                }
    4.70 +                @SuppressWarnings("fallthrough")
    4.71 +                public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
    4.72 +                    DCDocComment dcComment = (DCDocComment) comment;
    4.73 +                    if (tree instanceof DCEndPosTree) {
    4.74 +                        int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
    4.75 +
    4.76 +                        if (endPos != Position.NOPOS) {
    4.77 +                            return endPos;
    4.78 +                        }
    4.79 +                    }
    4.80 +                    int correction = 0;
    4.81 +                    switch (tree.getKind()) {
    4.82 +                        case TEXT:
    4.83 +                            DCText text = (DCText) tree;
    4.84 +
    4.85 +                            return dcComment.comment.getSourcePos(text.pos + text.text.length());
    4.86 +                        case ERRONEOUS:
    4.87 +                            DCErroneous err = (DCErroneous) tree;
    4.88 +
    4.89 +                            return dcComment.comment.getSourcePos(err.pos + err.body.length());
    4.90 +                        case IDENTIFIER:
    4.91 +                            DCIdentifier ident = (DCIdentifier) tree;
    4.92 +
    4.93 +                            return dcComment.comment.getSourcePos(ident.pos + (ident.name != names.error ? ident.name.length() : 0));
    4.94 +                        case PARAM:
    4.95 +                            DCParam param = (DCParam) tree;
    4.96 +
    4.97 +                            if (param.isTypeParameter && param.getDescription().isEmpty()) {
    4.98 +                                correction = 1;
    4.99 +                            }
   4.100 +                        case AUTHOR: case DEPRECATED: case RETURN: case SEE:
   4.101 +                        case SERIAL: case SERIAL_DATA: case SERIAL_FIELD: case SINCE:
   4.102 +                        case THROWS: case UNKNOWN_BLOCK_TAG: case VERSION: {
   4.103 +                            DocTree last = getLastChild(tree);
   4.104 +
   4.105 +                            if (last != null) {
   4.106 +                                return getEndPosition(file, comment, last) + correction;
   4.107 +                            }
   4.108 +
   4.109 +                            DCBlockTag block = (DCBlockTag) tree;
   4.110 +
   4.111 +                            return dcComment.comment.getSourcePos(block.pos + block.getTagName().length() + 1);
   4.112 +                        }
   4.113 +                        default:
   4.114 +                            DocTree last = getLastChild(tree);
   4.115 +
   4.116 +                            if (last != null) {
   4.117 +                                return getEndPosition(file, comment, last);
   4.118 +                            }
   4.119 +                            break;
   4.120 +                    }
   4.121 +
   4.122 +                    return Position.NOPOS;
   4.123 +                }
   4.124              };
   4.125      }
   4.126  
   4.127 +    private DocTree getLastChild(DocTree tree) {
   4.128 +        final DocTree[] last = new DocTree[] {null};
   4.129 +
   4.130 +        tree.accept(new DocTreeScanner<Void, Void>() {
   4.131 +            @Override public Void scan(DocTree node, Void p) {
   4.132 +                if (node != null) last[0] = node;
   4.133 +                return null;
   4.134 +            }
   4.135 +        }, null);
   4.136 +
   4.137 +        return last[0];
   4.138 +    }
   4.139 +
   4.140      public JCClassDecl getTree(TypeElement element) {
   4.141          return (JCClassDecl) getTree((Element) element);
   4.142      }
     5.1 --- a/src/share/classes/com/sun/tools/javac/parser/DocCommentParser.java	Wed Apr 17 10:31:01 2013 -0700
     5.2 +++ b/src/share/classes/com/sun/tools/javac/parser/DocCommentParser.java	Wed Apr 17 15:54:24 2013 +0200
     5.3 @@ -41,6 +41,7 @@
     5.4  import com.sun.tools.javac.tree.DCTree.DCAttribute;
     5.5  import com.sun.tools.javac.tree.DCTree.DCDocComment;
     5.6  import com.sun.tools.javac.tree.DCTree.DCEndElement;
     5.7 +import com.sun.tools.javac.tree.DCTree.DCEndPosTree;
     5.8  import com.sun.tools.javac.tree.DCTree.DCErroneous;
     5.9  import com.sun.tools.javac.tree.DCTree.DCIdentifier;
    5.10  import com.sun.tools.javac.tree.DCTree.DCReference;
    5.11 @@ -336,12 +337,12 @@
    5.12                      DCTree text = inlineText();
    5.13                      if (text != null) {
    5.14                          nextChar();
    5.15 -                        return m.at(p).UnknownInlineTag(name, List.of(text));
    5.16 +                        return m.at(p).UnknownInlineTag(name, List.of(text)).setEndPos(bp);
    5.17                      }
    5.18                  } else if (tp.getKind() == TagParser.Kind.INLINE) {
    5.19 -                    DCTree tree =  tp.parse(p);
    5.20 +                    DCEndPosTree<?> tree = (DCEndPosTree<?>) tp.parse(p);
    5.21                      if (tree != null) {
    5.22 -                        return tree;
    5.23 +                        return tree.setEndPos(bp);
    5.24                      }
    5.25                  } else {
    5.26                      inlineText(); // skip content
    5.27 @@ -509,7 +510,7 @@
    5.28              fac.log.popDiagnosticHandler(deferredDiagnosticHandler);
    5.29          }
    5.30  
    5.31 -        return m.at(pos).Reference(sig, qualExpr, member, paramTypes);
    5.32 +        return m.at(pos).Reference(sig, qualExpr, member, paramTypes).setEndPos(bp);
    5.33      }
    5.34  
    5.35      JCTree parseType(String s) throws ParseException {
    5.36 @@ -741,7 +742,7 @@
    5.37                  }
    5.38                  if (ch == '>') {
    5.39                      nextChar();
    5.40 -                    return m.at(p).StartElement(name, attrs, selfClosing);
    5.41 +                    return m.at(p).StartElement(name, attrs, selfClosing).setEndPos(bp);
    5.42                  }
    5.43              }
    5.44          } else if (ch == '/') {
     6.1 --- a/src/share/classes/com/sun/tools/javac/tree/DCTree.java	Wed Apr 17 10:31:01 2013 -0700
     6.2 +++ b/src/share/classes/com/sun/tools/javac/tree/DCTree.java	Wed Apr 17 15:54:24 2013 +0200
     6.3 @@ -36,6 +36,7 @@
     6.4  import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
     6.5  import com.sun.tools.javac.util.List;
     6.6  import com.sun.tools.javac.util.Name;
     6.7 +import com.sun.tools.javac.util.Position;
     6.8  import java.io.IOException;
     6.9  import java.io.StringWriter;
    6.10  import javax.tools.JavaFileObject;
    6.11 @@ -82,8 +83,24 @@
    6.12          return s.toString();
    6.13      }
    6.14  
    6.15 +    public static abstract class DCEndPosTree<T extends DCEndPosTree<T>> extends DCTree {
    6.16 +
    6.17 +        private int endPos = Position.NOPOS;
    6.18 +
    6.19 +        public int getEndPos(DCDocComment dc) {
    6.20 +            return dc.comment.getSourcePos(endPos);
    6.21 +        }
    6.22 +
    6.23 +        @SuppressWarnings("unchecked")
    6.24 +        public T setEndPos(int endPos) {
    6.25 +            this.endPos = endPos;
    6.26 +            return (T) this;
    6.27 +        }
    6.28 +
    6.29 +    }
    6.30 +
    6.31      public static class DCDocComment extends DCTree implements DocCommentTree {
    6.32 -        final Comment comment; // required for the implicit source pos table
    6.33 +        public final Comment comment; // required for the implicit source pos table
    6.34  
    6.35          public final List<DCTree> firstSentence;
    6.36          public final List<DCTree> body;
    6.37 @@ -125,7 +142,7 @@
    6.38          }
    6.39      }
    6.40  
    6.41 -    public static abstract class DCInlineTag extends DCTree implements InlineTagTree {
    6.42 +    public static abstract class DCInlineTag extends DCEndPosTree<DCInlineTag> implements InlineTagTree {
    6.43          public String getTagName() {
    6.44              return getKind().tagName;
    6.45          }
    6.46 @@ -345,6 +362,7 @@
    6.47          public int getEndPosition(EndPosTable endPosTable) {
    6.48              return pos + body.length();
    6.49          }
    6.50 +
    6.51      }
    6.52  
    6.53      public static class DCIdentifier extends DCTree implements IdentifierTree {
    6.54 @@ -478,7 +496,7 @@
    6.55          }
    6.56      }
    6.57  
    6.58 -    public static class DCReference extends DCTree implements ReferenceTree {
    6.59 +    public static class DCReference extends DCEndPosTree<DCReference> implements ReferenceTree {
    6.60          public final String signature;
    6.61  
    6.62          // The following are not directly exposed through ReferenceTree
    6.63 @@ -663,7 +681,7 @@
    6.64          }
    6.65      }
    6.66  
    6.67 -    public static class DCStartElement extends DCTree implements StartElementTree {
    6.68 +    public static class DCStartElement extends DCEndPosTree<DCStartElement> implements StartElementTree {
    6.69          public final Name name;
    6.70          public final List<DCTree> attrs;
    6.71          public final boolean selfClosing;
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/tools/javac/doctree/positions/TestPosition.java	Wed Apr 17 15:54:24 2013 +0200
     7.3 @@ -0,0 +1,105 @@
     7.4 +/*
     7.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     7.7 + *
     7.8 + * This code is free software; you can redistribute it and/or modify it
     7.9 + * under the terms of the GNU General Public License version 2 only, as
    7.10 + * published by the Free Software Foundation.
    7.11 + *
    7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    7.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    7.15 + * version 2 for more details (a copy is included in the LICENSE file that
    7.16 + * accompanied this code).
    7.17 + *
    7.18 + * You should have received a copy of the GNU General Public License version
    7.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    7.21 + *
    7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    7.23 + * or visit www.oracle.com if you need additional information or have any
    7.24 + * questions.
    7.25 + */
    7.26 +
    7.27 +/*
    7.28 + * @test
    7.29 + * @bug 8008174
    7.30 + * @summary proper source positions for doc comments
    7.31 + * @build TestPosition
    7.32 + * @compile/ref=TestPosition.out -processor TestPosition -proc:only TestPositionSource.java
    7.33 + */
    7.34 +
    7.35 +import com.sun.source.doctree.DocCommentTree;
    7.36 +import com.sun.source.doctree.DocTree;
    7.37 +import com.sun.source.tree.MethodTree;
    7.38 +import com.sun.source.util.DocSourcePositions;
    7.39 +import com.sun.source.util.DocTreeScanner;
    7.40 +import com.sun.source.util.DocTrees;
    7.41 +import com.sun.source.util.TreePath;
    7.42 +import com.sun.source.util.TreePathScanner;
    7.43 +import java.io.IOException;
    7.44 +import java.util.Set;
    7.45 +import javax.annotation.processing.*;
    7.46 +import javax.lang.model.*;
    7.47 +import javax.lang.model.element.*;
    7.48 +
    7.49 +@SupportedAnnotationTypes("*")
    7.50 +public class TestPosition extends AbstractProcessor {
    7.51 +
    7.52 +    @Override
    7.53 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    7.54 +        TypeElement source = processingEnv.getElementUtils().getTypeElement("TestPositionSource");
    7.55 +
    7.56 +        if (source == null) throw new IllegalStateException();
    7.57 +
    7.58 +        if (!roundEnv.getRootElements().contains(source)) return false;
    7.59 +
    7.60 +        final DocTrees trees = DocTrees.instance(processingEnv);
    7.61 +        final TreePath testElement = trees.getPath(source);
    7.62 +
    7.63 +        if (testElement == null) throw new IllegalStateException();
    7.64 +
    7.65 +        String code;
    7.66 +
    7.67 +        try {
    7.68 +            code = testElement.getCompilationUnit().getSourceFile().getCharContent(false).toString();
    7.69 +        } catch ( IOException ex) {
    7.70 +            throw new IllegalStateException(ex);
    7.71 +        }
    7.72 +
    7.73 +        new TreePathScanner<Void, Void>() {
    7.74 +            @Override public Void visitMethod(MethodTree node, Void p) {
    7.75 +                final DocCommentTree docCommentTree = trees.getDocCommentTree(getCurrentPath());
    7.76 +
    7.77 +                if (docCommentTree != null) {
    7.78 +                    System.out.println(node.getName() + ":");
    7.79 +                    new DocTreeScanner<Void, Void>() {
    7.80 +                        @Override public Void scan(DocTree node, Void p) {
    7.81 +                            if (node != null) {
    7.82 +                                DocSourcePositions sp = (DocSourcePositions) trees.getSourcePositions(); //XXX: the cast???
    7.83 +                                int start = (int) sp.getStartPosition(testElement.getCompilationUnit(), docCommentTree, node);
    7.84 +                                int end   = (int) sp.getEndPosition(testElement.getCompilationUnit(), docCommentTree, node);
    7.85 +                                String snippet = code.substring(start, end).replace(" \n", "!trailing-whitespace!\n");
    7.86 +
    7.87 +                                if (snippet.endsWith(" ")) {
    7.88 +                                    snippet = snippet.substring(0, snippet.length() - 1) + "!trailing-whitespace!";
    7.89 +                                }
    7.90 +                                System.out.println(node.getKind().name() + ":" + snippet);
    7.91 +                            }
    7.92 +                            return super.scan(node, p);
    7.93 +                        }
    7.94 +                    }.scan(docCommentTree, null);
    7.95 +                }
    7.96 +
    7.97 +                return super.visitMethod(node, p);
    7.98 +            }
    7.99 +        }.scan(testElement, null);
   7.100 +
   7.101 +        return false;
   7.102 +    }
   7.103 +
   7.104 +    @Override
   7.105 +    public SourceVersion getSupportedSourceVersion() {
   7.106 +        return SourceVersion.latest();
   7.107 +    }
   7.108 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/tools/javac/doctree/positions/TestPosition.out	Wed Apr 17 15:54:24 2013 +0200
     8.3 @@ -0,0 +1,99 @@
     8.4 +valid:
     8.5 +DOC_COMMENT:First sentence.
     8.6 +     *
     8.7 +     * <p>Description with {@link java.io.InputStream link}
     8.8 +     *
     8.9 +     * @param first description
    8.10 +     * @param second description
    8.11 +     * @return whatever
    8.12 +     * @throws IllegalStateException why?
    8.13 +     * @since 1.15
    8.14 +     * @see java.util.List
    8.15 +TEXT:First sentence.
    8.16 +START_ELEMENT:<p>
    8.17 +TEXT:Description with!trailing-whitespace!
    8.18 +LINK:{@link java.io.InputStream link}
    8.19 +REFERENCE:java.io.InputStream
    8.20 +TEXT:link
    8.21 +PARAM:@param first description
    8.22 +IDENTIFIER:first
    8.23 +TEXT:description
    8.24 +PARAM:@param second description
    8.25 +IDENTIFIER:second
    8.26 +TEXT:description
    8.27 +RETURN:@return whatever
    8.28 +TEXT:whatever
    8.29 +THROWS:@throws IllegalStateException why?
    8.30 +REFERENCE:IllegalStateException
    8.31 +TEXT:why?
    8.32 +SINCE:@since 1.15
    8.33 +TEXT:1.15
    8.34 +SEE:@see java.util.List
    8.35 +REFERENCE:java.util.List
    8.36 +erroneous:
    8.37 +DOC_COMMENT:First sentence.
    8.38 +     *
    8.39 +     * <p>Description with {@link}, {@link java.util.List}, {@link
    8.40 +     *
    8.41 +     * @param
    8.42 +     * @param second
    8.43 +     * @return
    8.44 +     * @throws
    8.45 +     * @throws IllegalStateException
    8.46 +     * @since
    8.47 +     * @see
    8.48 +TEXT:First sentence.
    8.49 +START_ELEMENT:<p>
    8.50 +TEXT:Description with!trailing-whitespace!
    8.51 +LINK:{@link}
    8.52 +TEXT:,!trailing-whitespace!
    8.53 +LINK:{@link java.util.List}
    8.54 +REFERENCE:java.util.List
    8.55 +TEXT:,!trailing-whitespace!
    8.56 +ERRONEOUS:{@link
    8.57 +ERRONEOUS:@param
    8.58 +PARAM:@param second
    8.59 +IDENTIFIER:second
    8.60 +RETURN:@return
    8.61 +ERRONEOUS:@throws
    8.62 +THROWS:@throws IllegalStateException
    8.63 +REFERENCE:IllegalStateException
    8.64 +SINCE:@since
    8.65 +ERRONEOUS:@see
    8.66 +withWhiteSpaces:
    8.67 +DOC_COMMENT:First sentence.
    8.68 +     *
    8.69 +     * <p>Description with {@link    }, {@link java.util.List#add(   int   )},
    8.70 +     * {@link java.util.List#add(   int   ) some   text   with   whitespaces}, {@link
    8.71 +     *
    8.72 +     * @param     first
    8.73 +     * @param     second   some   text   with trailing whitespace
    8.74 +     * @return      some   return
    8.75 +     * @throws      java.lang.IllegalStateException
    8.76 +     * @throws   java.lang.IllegalStateException some     text
    8.77 +TEXT:First sentence.
    8.78 +START_ELEMENT:<p>
    8.79 +TEXT:Description with!trailing-whitespace!
    8.80 +LINK:{@link    }
    8.81 +TEXT:,!trailing-whitespace!
    8.82 +LINK:{@link java.util.List#add(   int   )}
    8.83 +REFERENCE:java.util.List#add(   int   )
    8.84 +TEXT:,
    8.85 +     *!trailing-whitespace!
    8.86 +LINK:{@link java.util.List#add(   int   ) some   text   with   whitespaces}
    8.87 +REFERENCE:java.util.List#add(   int   )
    8.88 +TEXT:some   text   with   whitespaces
    8.89 +TEXT:,!trailing-whitespace!
    8.90 +ERRONEOUS:{@link
    8.91 +PARAM:@param     first
    8.92 +IDENTIFIER:first
    8.93 +PARAM:@param     second   some   text   with trailing whitespace
    8.94 +IDENTIFIER:second
    8.95 +TEXT:some   text   with trailing whitespace
    8.96 +RETURN:@return      some   return
    8.97 +TEXT:some   return
    8.98 +THROWS:@throws      java.lang.IllegalStateException
    8.99 +REFERENCE:java.lang.IllegalStateException
   8.100 +THROWS:@throws   java.lang.IllegalStateException some     text
   8.101 +REFERENCE:java.lang.IllegalStateException
   8.102 +TEXT:some     text
   8.103 \ No newline at end of file
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/test/tools/javac/doctree/positions/TestPositionSource.java	Wed Apr 17 15:54:24 2013 +0200
     9.3 @@ -0,0 +1,71 @@
     9.4 +/*
     9.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     9.7 + *
     9.8 + * This code is free software; you can redistribute it and/or modify it
     9.9 + * under the terms of the GNU General Public License version 2 only, as
    9.10 + * published by the Free Software Foundation.
    9.11 + *
    9.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    9.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    9.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    9.15 + * version 2 for more details (a copy is included in the LICENSE file that
    9.16 + * accompanied this code).
    9.17 + *
    9.18 + * You should have received a copy of the GNU General Public License version
    9.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    9.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    9.21 + *
    9.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    9.23 + * or visit www.oracle.com if you need additional information or have any
    9.24 + * questions.
    9.25 + */
    9.26 +public class TestPositionSource {
    9.27 +
    9.28 +    /**First sentence.
    9.29 +     *
    9.30 +     * <p>Description with {@link java.io.InputStream link}
    9.31 +     *
    9.32 +     * @param first description
    9.33 +     * @param second description
    9.34 +     * @return whatever
    9.35 +     * @throws IllegalStateException why?
    9.36 +     * @since 1.15
    9.37 +     * @see java.util.List
    9.38 +     */
    9.39 +    public boolean valid(int first, int second) throws IllegalStateException {
    9.40 +        return true;
    9.41 +    }
    9.42 +
    9.43 +    /**First sentence.
    9.44 +     *
    9.45 +     * <p>Description with {@link}, {@link java.util.List}, {@link
    9.46 +     *
    9.47 +     * @param
    9.48 +     * @param second
    9.49 +     * @return
    9.50 +     * @throws
    9.51 +     * @throws IllegalStateException
    9.52 +     * @since
    9.53 +     * @see
    9.54 +     */
    9.55 +    public boolean erroneous(int first, int second) throws IllegalStateException {
    9.56 +        return true;
    9.57 +    }
    9.58 +
    9.59 +    /**First sentence.
    9.60 +     *
    9.61 +     * <p>Description with {@link    }, {@link java.util.List#add(   int   )},
    9.62 +     * {@link java.util.List#add(   int   ) some   text   with   whitespaces}, {@link
    9.63 +     *
    9.64 +     * @param     first
    9.65 +     * @param     second   some   text   with trailing whitespace
    9.66 +     * @return      some   return
    9.67 +     * @throws      java.lang.IllegalStateException
    9.68 +     * @throws   java.lang.IllegalStateException some     text
    9.69 +     */
    9.70 +    public boolean withWhiteSpaces(int first, int second) throws IllegalStateException {
    9.71 +        return true;
    9.72 +    }
    9.73 +
    9.74 +}

mercurial