Jan Lahoda | 77b43ba | 2013-04-17 15:54:24 +0200 | [diff] [blame] | 1 | /* |
Alexander Kulyakhtin | b626244 | 2015-05-21 11:41:04 -0700 | [diff] [blame] | 2 | * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
Jan Lahoda | 77b43ba | 2013-04-17 15:54:24 +0200 | [diff] [blame] | 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 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 8008174 |
| 27 | * @summary proper source positions for doc comments |
Alexander Kulyakhtin | b626244 | 2015-05-21 11:41:04 -0700 | [diff] [blame] | 28 | * @modules jdk.compiler |
Jan Lahoda | 77b43ba | 2013-04-17 15:54:24 +0200 | [diff] [blame] | 29 | * @build TestPosition |
| 30 | * @compile/ref=TestPosition.out -processor TestPosition -proc:only TestPositionSource.java |
| 31 | */ |
| 32 | |
| 33 | import com.sun.source.doctree.DocCommentTree; |
| 34 | import com.sun.source.doctree.DocTree; |
| 35 | import com.sun.source.tree.MethodTree; |
| 36 | import com.sun.source.util.DocSourcePositions; |
| 37 | import com.sun.source.util.DocTreeScanner; |
| 38 | import com.sun.source.util.DocTrees; |
| 39 | import com.sun.source.util.TreePath; |
| 40 | import com.sun.source.util.TreePathScanner; |
| 41 | import java.io.IOException; |
| 42 | import java.util.Set; |
| 43 | import javax.annotation.processing.*; |
| 44 | import javax.lang.model.*; |
| 45 | import javax.lang.model.element.*; |
| 46 | |
| 47 | @SupportedAnnotationTypes("*") |
| 48 | public class TestPosition extends AbstractProcessor { |
| 49 | |
| 50 | @Override |
| 51 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 52 | TypeElement source = processingEnv.getElementUtils().getTypeElement("TestPositionSource"); |
| 53 | |
| 54 | if (source == null) throw new IllegalStateException(); |
| 55 | |
| 56 | if (!roundEnv.getRootElements().contains(source)) return false; |
| 57 | |
| 58 | final DocTrees trees = DocTrees.instance(processingEnv); |
| 59 | final TreePath testElement = trees.getPath(source); |
| 60 | |
| 61 | if (testElement == null) throw new IllegalStateException(); |
| 62 | |
| 63 | String code; |
| 64 | |
| 65 | try { |
| 66 | code = testElement.getCompilationUnit().getSourceFile().getCharContent(false).toString(); |
| 67 | } catch ( IOException ex) { |
| 68 | throw new IllegalStateException(ex); |
| 69 | } |
| 70 | |
| 71 | new TreePathScanner<Void, Void>() { |
| 72 | @Override public Void visitMethod(MethodTree node, Void p) { |
| 73 | final DocCommentTree docCommentTree = trees.getDocCommentTree(getCurrentPath()); |
| 74 | |
| 75 | if (docCommentTree != null) { |
| 76 | System.out.println(node.getName() + ":"); |
| 77 | new DocTreeScanner<Void, Void>() { |
| 78 | @Override public Void scan(DocTree node, Void p) { |
| 79 | if (node != null) { |
| 80 | DocSourcePositions sp = (DocSourcePositions) trees.getSourcePositions(); //XXX: the cast??? |
| 81 | int start = (int) sp.getStartPosition(testElement.getCompilationUnit(), docCommentTree, node); |
| 82 | int end = (int) sp.getEndPosition(testElement.getCompilationUnit(), docCommentTree, node); |
| 83 | String snippet = code.substring(start, end).replace(" \n", "!trailing-whitespace!\n"); |
| 84 | |
| 85 | if (snippet.endsWith(" ")) { |
| 86 | snippet = snippet.substring(0, snippet.length() - 1) + "!trailing-whitespace!"; |
| 87 | } |
| 88 | System.out.println(node.getKind().name() + ":" + snippet); |
| 89 | } |
| 90 | return super.scan(node, p); |
| 91 | } |
| 92 | }.scan(docCommentTree, null); |
| 93 | } |
| 94 | |
| 95 | return super.visitMethod(node, p); |
| 96 | } |
| 97 | }.scan(testElement, null); |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public SourceVersion getSupportedSourceVersion() { |
| 104 | return SourceVersion.latest(); |
| 105 | } |
| 106 | } |