blob: 143444c5b7eb950e637b5dd815eba33b80fd8f62 [file] [log] [blame]
Jan Lahoda77b43ba2013-04-17 15:54:24 +02001/*
Alexander Kulyakhtinb6262442015-05-21 11:41:04 -07002 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
Jan Lahoda77b43ba2013-04-17 15:54:24 +02003 * 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 Kulyakhtinb6262442015-05-21 11:41:04 -070028 * @modules jdk.compiler
Jan Lahoda77b43ba2013-04-17 15:54:24 +020029 * @build TestPosition
30 * @compile/ref=TestPosition.out -processor TestPosition -proc:only TestPositionSource.java
31 */
32
33import com.sun.source.doctree.DocCommentTree;
34import com.sun.source.doctree.DocTree;
35import com.sun.source.tree.MethodTree;
36import com.sun.source.util.DocSourcePositions;
37import com.sun.source.util.DocTreeScanner;
38import com.sun.source.util.DocTrees;
39import com.sun.source.util.TreePath;
40import com.sun.source.util.TreePathScanner;
41import java.io.IOException;
42import java.util.Set;
43import javax.annotation.processing.*;
44import javax.lang.model.*;
45import javax.lang.model.element.*;
46
47@SupportedAnnotationTypes("*")
48public 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}