test/tools/javac/doctree/positions/TestPosition.java

Mon, 16 Sep 2013 14:13:44 +0200

author
jlahoda
date
Mon, 16 Sep 2013 14:13:44 +0200
changeset 2028
4ce8148ffc4f
parent 0
959103a6100f
permissions
-rw-r--r--

8021112: Spurious unchecked warning reported by javac
6480588: No way to suppress deprecation warnings when implementing deprecated interface
Summary: Fixing DeferredLintHandler configuration, so lint warnings are reported with correct @SuppressWarnings settings
Reviewed-by: jjg, vromero

     1 /*
     2  * Copyright (c) 2010, 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.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 8008174
    27  * @summary proper source positions for doc comments
    28  * @build TestPosition
    29  * @compile/ref=TestPosition.out -processor TestPosition -proc:only TestPositionSource.java
    30  */
    32 import com.sun.source.doctree.DocCommentTree;
    33 import com.sun.source.doctree.DocTree;
    34 import com.sun.source.tree.MethodTree;
    35 import com.sun.source.util.DocSourcePositions;
    36 import com.sun.source.util.DocTreeScanner;
    37 import com.sun.source.util.DocTrees;
    38 import com.sun.source.util.TreePath;
    39 import com.sun.source.util.TreePathScanner;
    40 import java.io.IOException;
    41 import java.util.Set;
    42 import javax.annotation.processing.*;
    43 import javax.lang.model.*;
    44 import javax.lang.model.element.*;
    46 @SupportedAnnotationTypes("*")
    47 public class TestPosition extends AbstractProcessor {
    49     @Override
    50     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    51         TypeElement source = processingEnv.getElementUtils().getTypeElement("TestPositionSource");
    53         if (source == null) throw new IllegalStateException();
    55         if (!roundEnv.getRootElements().contains(source)) return false;
    57         final DocTrees trees = DocTrees.instance(processingEnv);
    58         final TreePath testElement = trees.getPath(source);
    60         if (testElement == null) throw new IllegalStateException();
    62         String code;
    64         try {
    65             code = testElement.getCompilationUnit().getSourceFile().getCharContent(false).toString();
    66         } catch ( IOException ex) {
    67             throw new IllegalStateException(ex);
    68         }
    70         new TreePathScanner<Void, Void>() {
    71             @Override public Void visitMethod(MethodTree node, Void p) {
    72                 final DocCommentTree docCommentTree = trees.getDocCommentTree(getCurrentPath());
    74                 if (docCommentTree != null) {
    75                     System.out.println(node.getName() + ":");
    76                     new DocTreeScanner<Void, Void>() {
    77                         @Override public Void scan(DocTree node, Void p) {
    78                             if (node != null) {
    79                                 DocSourcePositions sp = (DocSourcePositions) trees.getSourcePositions(); //XXX: the cast???
    80                                 int start = (int) sp.getStartPosition(testElement.getCompilationUnit(), docCommentTree, node);
    81                                 int end   = (int) sp.getEndPosition(testElement.getCompilationUnit(), docCommentTree, node);
    82                                 String snippet = code.substring(start, end).replace(" \n", "!trailing-whitespace!\n");
    84                                 if (snippet.endsWith(" ")) {
    85                                     snippet = snippet.substring(0, snippet.length() - 1) + "!trailing-whitespace!";
    86                                 }
    87                                 System.out.println(node.getKind().name() + ":" + snippet);
    88                             }
    89                             return super.scan(node, p);
    90                         }
    91                     }.scan(docCommentTree, null);
    92                 }
    94                 return super.visitMethod(node, p);
    95             }
    96         }.scan(testElement, null);
    98         return false;
    99     }
   101     @Override
   102     public SourceVersion getSupportedSourceVersion() {
   103         return SourceVersion.latest();
   104     }
   105 }

mercurial