test/tools/javac/doctree/ReferenceTest.java

Mon, 06 May 2013 16:22:45 +0200

author
jlahoda
date
Mon, 06 May 2013 16:22:45 +0200
changeset 1726
a7ff36d06fa2
parent 1409
33abf479f202
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8009724: Enhance the DocTree API with DocTreePath
Summary: Adding DocTreePath and DocTreePathScanner similar to TreePath and TreePathScanner, respectively
Reviewed-by: jjg
Contributed-by: Ralph Benjamin Ruijs <ralphbenjamin@netbeans.org>, Jan Lahoda <jlahoda@netbeans.org>

jjg@1409 1 /*
jlahoda@1726 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1409 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1409 4 *
jjg@1409 5 * This code is free software; you can redistribute it and/or modify it
jjg@1409 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1409 7 * published by the Free Software Foundation.
jjg@1409 8 *
jjg@1409 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1409 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1409 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1409 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1409 13 * accompanied this code).
jjg@1409 14 *
jjg@1409 15 * You should have received a copy of the GNU General Public License version
jjg@1409 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1409 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1409 18 *
jjg@1409 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1409 20 * or visit www.oracle.com if you need additional information or have any
jjg@1409 21 * questions.
jjg@1409 22 */
jjg@1409 23
jjg@1409 24 /*
jjg@1409 25 * @test
jjg@1409 26 * @bug 7021614
jjg@1409 27 * @summary extend com.sun.source API to support parsing javadoc comments
jjg@1409 28 * @summary check references in at-see and {at-link} tags
jjg@1409 29 * @build ReferenceTest
jjg@1409 30 * @compile -processor ReferenceTest -proc:only ReferenceTest.java
jjg@1409 31 */
jjg@1409 32
jjg@1409 33 import com.sun.source.doctree.DocCommentTree;
jjg@1409 34 import com.sun.source.doctree.DocTree;
jjg@1409 35 import com.sun.source.doctree.LinkTree;
jjg@1409 36 import com.sun.source.doctree.ReferenceTree;
jjg@1409 37 import com.sun.source.doctree.SeeTree;
jjg@1409 38 import com.sun.source.doctree.TextTree;
jlahoda@1726 39 import com.sun.source.util.DocTreePath;
jlahoda@1726 40 import com.sun.source.util.DocTreePathScanner;
jjg@1409 41 import com.sun.source.util.DocTreeScanner;
jjg@1409 42 import com.sun.source.util.DocTrees;
jjg@1409 43 import com.sun.source.util.TreePath;
jjg@1409 44
jjg@1409 45 import java.util.List;
jjg@1409 46 import java.util.Set;
jjg@1409 47 import javax.annotation.processing.AbstractProcessor;
jjg@1409 48 import javax.annotation.processing.ProcessingEnvironment;
jjg@1409 49 import javax.annotation.processing.RoundEnvironment;
jjg@1409 50 import javax.annotation.processing.SupportedAnnotationTypes;
jjg@1409 51 import javax.lang.model.SourceVersion;
jjg@1409 52 import javax.lang.model.element.Element;
jjg@1409 53 import javax.lang.model.element.TypeElement;
jjg@1409 54 import javax.tools.Diagnostic.Kind;
jjg@1409 55
jjg@1409 56 /**
jjg@1409 57 * {@link java.lang Package}
jjg@1409 58 * {@link java.lang.ERROR Bad}
jjg@1409 59 *
jjg@1409 60 * {@link java.lang.String Class}
jjg@1409 61 * {@link String Class}
jjg@1409 62 * {@link java.lang.String#CASE_INSENSITIVE_ORDER Field}
jjg@1409 63 * {@link java.lang.String#String Constructor}
jjg@1409 64 * {@link java.lang.String#String(byte[]) Constructor}
jjg@1409 65 * {@link java.lang.String#String(byte[] bytes) Constructor}
jjg@1409 66 * {@link java.lang.String#String(byte[], String) Constructor}
jjg@1409 67 * {@link java.lang.String#String(byte[] bytes, String charSetName) Constructor}
jjg@1409 68 * {@link java.lang.String#isEmpty Method}
jjg@1409 69 * {@link java.lang.String#isEmpty() Method}
jjg@1409 70 * {@link java.lang.String#ERROR Bad}
jjg@1409 71 * {@link java.lang.String#equals(Object) Method}
jjg@1409 72 *
jjg@1409 73 * {@link AbstractProcessor Class}
jjg@1409 74 *
jjg@1409 75 * {@link List#add(Object) Method}
jjg@1409 76 *
jjg@1409 77 * {@link #trees Field}
jjg@1409 78 * {@link #getSupportedSourceVersion Method}
jjg@1409 79 * {@link #init(ProcessingEnvironment Method}
jjg@1409 80 *
jjg@1409 81 * @see java.lang Package
jjg@1409 82 * @see java.lang.ERROR Bad
jjg@1409 83 *
jjg@1409 84 * @see java.lang.String Class
jjg@1409 85 * @see String Class
jjg@1409 86 * @see java.lang.String#CASE_INSENSITIVE_ORDER Field
jjg@1409 87 * @see java.lang.String#String Constructor
jjg@1409 88 * @see java.lang.String#String(byte[]) Constructor
jjg@1409 89 * @see java.lang.String#String(byte[] bytes) Constructor
jjg@1409 90 * @see java.lang.String#String(byte[],String) Constructor
jjg@1409 91 * @see java.lang.String#String(byte[] bytes, String charsetName) Constructor
jjg@1409 92 * @see java.lang.String#isEmpty Method
jjg@1409 93 * @see java.lang.String#isEmpty() Method
jjg@1409 94 * @see java.lang.String#ERROR Bad
jjg@1409 95 * @see java.lang.String#equals(Object) Method
jjg@1409 96 *
jjg@1409 97 * @see AbstractProcessor Class
jjg@1409 98 *
jjg@1409 99 * @see List#add(Object) Method
jjg@1409 100 *
jjg@1409 101 * @see #trees Field
jjg@1409 102 * @see #getSupportedSourceVersion Method
jjg@1409 103 * @see #init(ProcessingEnvironment) Method
jjg@1409 104 *
jjg@1409 105 * @see java.io.BufferedInputStream#BufferedInputStream(InputStream) Constructor
jjg@1409 106 */
jjg@1409 107 @SupportedAnnotationTypes("*")
jjg@1409 108 public class ReferenceTest extends AbstractProcessor {
jjg@1409 109 DocTrees trees;
jjg@1409 110
jjg@1409 111 @Override
jjg@1409 112 public SourceVersion getSupportedSourceVersion() {
jjg@1409 113 return SourceVersion.latest();
jjg@1409 114 }
jjg@1409 115
jjg@1409 116 @Override
jjg@1409 117 public void init(ProcessingEnvironment pEnv) {
jjg@1409 118 super.init(pEnv);
jjg@1409 119 trees = DocTrees.instance(pEnv);
jjg@1409 120 }
jjg@1409 121
jjg@1409 122 @Override
jjg@1409 123 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@1409 124 for (Element e: roundEnv.getRootElements()) {
jjg@1409 125 new DocCommentScanner(trees.getPath(e)).scan();
jjg@1409 126 }
jjg@1409 127 return true;
jjg@1409 128 }
jjg@1409 129
jlahoda@1726 130 class DocCommentScanner extends DocTreePathScanner<Void, Void> {
jjg@1409 131 TreePath path;
jjg@1409 132 DocCommentTree dc;
jjg@1409 133
jjg@1409 134 DocCommentScanner(TreePath path) {
jjg@1409 135 this.path = path;
jjg@1409 136 }
jjg@1409 137
jjg@1409 138 void scan() {
jjg@1409 139 dc = trees.getDocCommentTree(path);
jlahoda@1726 140 scan(new DocTreePath(path, dc), null);
jjg@1409 141 }
jjg@1409 142
jjg@1409 143 @Override
jjg@1409 144 public Void visitLink(LinkTree tree, Void ignore) {
jjg@1409 145 checkReference(tree.getReference(), tree.getLabel());
jjg@1409 146 return null;
jjg@1409 147 }
jjg@1409 148
jjg@1409 149 @Override
jjg@1409 150 public Void visitSee(SeeTree tree, Void ignore) {
jjg@1409 151 List<? extends DocTree> refLabel = tree.getReference();
jjg@1409 152 if (refLabel.size() > 1 && (refLabel.get(0) instanceof ReferenceTree)) {
jjg@1409 153 ReferenceTree ref = (ReferenceTree) refLabel.get(0);
jjg@1409 154 List<? extends DocTree> label = refLabel.subList(1, refLabel.size());
jjg@1409 155 checkReference(ref, label);
jjg@1409 156 }
jjg@1409 157 return null;
jjg@1409 158 }
jjg@1409 159
jjg@1409 160 void checkReference(ReferenceTree tree, List<? extends DocTree> label) {
jjg@1409 161 String sig = tree.getSignature();
jjg@1409 162
jlahoda@1726 163 Element found = trees.getElement(new DocTreePath(getCurrentPath(), tree));
jjg@1409 164 if (found == null) {
jjg@1409 165 System.err.println(sig + " NOT FOUND");
jjg@1409 166 } else {
jjg@1409 167 System.err.println(sig + " found " + found.getKind() + " " + found);
jjg@1409 168 }
jjg@1409 169
jjg@1409 170 String expect = "UNKNOWN";
jjg@1409 171 if (label.size() > 0 && label.get(0) instanceof TextTree)
jjg@1409 172 expect = ((TextTree) label.get(0)).getBody();
jjg@1409 173
jjg@1409 174 if (!expect.equalsIgnoreCase(found == null ? "bad" : found.getKind().name())) {
jjg@1409 175 error(tree, "Unexpected value found: " + found +", expected: " + expect);
jjg@1409 176 }
jjg@1409 177 }
jjg@1409 178
jjg@1409 179 void error(DocTree tree, String msg) {
jjg@1409 180 trees.printMessage(Kind.ERROR, msg, tree, dc, path.getCompilationUnit());
jjg@1409 181 }
jjg@1409 182 }
jjg@1409 183 }
jjg@1409 184
jjg@1409 185 /**
jjg@1409 186 * @see ReferenceTestExtras Class
jjg@1409 187 * @see #ReferenceTestExtras Field
jjg@1409 188 * @see #ReferenceTestExtras() Constructor
jjg@1409 189 *
jjg@1409 190 * @see #X Field
jjg@1409 191 * @see #X() Method
jjg@1409 192 *
jjg@1409 193 * @see #m Method
jjg@1409 194 *
jjg@1409 195 * @see #varargs(int...) Method
jjg@1409 196 * @see #varargs(int... args) Method
jjg@1409 197 * @see #varargs(int[]) Method
jjg@1409 198 * @see #varargs(int[] args) Method
jjg@1409 199 */
jjg@1409 200 class ReferenceTestExtras {
jjg@1409 201 int ReferenceTestExtras; // field
jjg@1409 202 ReferenceTestExtras() { } // constructor
jjg@1409 203 void ReferenceTestExtras() { } // method
jjg@1409 204
jjg@1409 205 int X;
jjg@1409 206 void X() { }
jjg@1409 207 static class X { }
jjg@1409 208
jjg@1409 209 void m() { }
jjg@1409 210 void m(int i) { }
jjg@1409 211 void m(int i, int j) { }
jjg@1409 212
jjg@1409 213 void varargs(int... args) { }
jjg@1409 214 }
jjg@1409 215
jjg@1409 216

mercurial