test/tools/javac/doctree/ReferenceTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial