ReferenceTest.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2012, 2015, 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 */ 23 24/* 25 * @test 26 * @bug 7021614 27 * @summary extend com.sun.source API to support parsing javadoc comments 28 * @summary check references in at-see and {at-link} tags 29 * @modules jdk.compiler 30 * @build ReferenceTest 31 * @compile -processor ReferenceTest -proc:only ReferenceTest.java 32 */ 33 34import com.sun.source.doctree.DocCommentTree; 35import com.sun.source.doctree.DocTree; 36import com.sun.source.doctree.LinkTree; 37import com.sun.source.doctree.ReferenceTree; 38import com.sun.source.doctree.SeeTree; 39import com.sun.source.doctree.TextTree; 40import com.sun.source.util.DocTreePath; 41import com.sun.source.util.DocTreePathScanner; 42import com.sun.source.util.DocTreeScanner; 43import com.sun.source.util.DocTrees; 44import com.sun.source.util.TreePath; 45 46import java.util.List; 47import java.util.Set; 48import javax.annotation.processing.AbstractProcessor; 49import javax.annotation.processing.ProcessingEnvironment; 50import javax.annotation.processing.RoundEnvironment; 51import javax.annotation.processing.SupportedAnnotationTypes; 52import javax.lang.model.SourceVersion; 53import javax.lang.model.element.Element; 54import javax.lang.model.element.TypeElement; 55import javax.tools.Diagnostic.Kind; 56 57/** 58 * {@link java.lang Package} 59 * {@link java.lang.ERROR Bad} 60 * {@link java.lang#ERROR Bad} 61 * 62 * {@link java.lang.String Class} 63 * {@link String Class} 64 * {@link java.lang.String#CASE_INSENSITIVE_ORDER Field} 65 * {@link java.lang.String#String Constructor} 66 * {@link java.lang.String#String(byte[]) Constructor} 67 * {@link java.lang.String#String(byte[] bytes) Constructor} 68 * {@link java.lang.String#String(byte[], String) Constructor} 69 * {@link java.lang.String#String(byte[] bytes, String charSetName) Constructor} 70 * {@link java.lang.String#isEmpty Method} 71 * {@link java.lang.String#isEmpty() Method} 72 * {@link java.lang.String#ERROR Bad} 73 * {@link java.lang.String#equals(Object) Method} 74 * 75 * {@link AbstractProcessor Class} 76 * 77 * {@link List#add(Object) Method} 78 * 79 * {@link #trees Field} 80 * {@link #getSupportedSourceVersion Method} 81 * {@link #init(ProcessingEnvironment Method} 82 * 83 * @see java.lang Package 84 * @see java.lang.ERROR Bad 85 * @see java.lang#ERROR Bad 86 * 87 * @see java.lang.String Class 88 * @see String Class 89 * @see java.lang.String#CASE_INSENSITIVE_ORDER Field 90 * @see java.lang.String#String Constructor 91 * @see java.lang.String#String(byte[]) Constructor 92 * @see java.lang.String#String(byte[] bytes) Constructor 93 * @see java.lang.String#String(byte[],String) Constructor 94 * @see java.lang.String#String(byte[] bytes, String charsetName) Constructor 95 * @see java.lang.String#isEmpty Method 96 * @see java.lang.String#isEmpty() Method 97 * @see java.lang.String#ERROR Bad 98 * @see java.lang.String#equals(Object) Method 99 * 100 * @see AbstractProcessor Class 101 * 102 * @see List#add(Object) Method 103 * 104 * @see #trees Field 105 * @see #getSupportedSourceVersion Method 106 * @see #init(ProcessingEnvironment) Method 107 * 108 * @see java.io.BufferedInputStream#BufferedInputStream(InputStream) Constructor 109 */ 110@SupportedAnnotationTypes("*") 111public class ReferenceTest extends AbstractProcessor { 112 DocTrees trees; 113 114 @Override 115 public SourceVersion getSupportedSourceVersion() { 116 return SourceVersion.latest(); 117 } 118 119 @Override 120 public void init(ProcessingEnvironment pEnv) { 121 super.init(pEnv); 122 trees = DocTrees.instance(pEnv); 123 } 124 125 @Override 126 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 127 for (Element e: roundEnv.getRootElements()) { 128 new DocCommentScanner(trees.getPath(e)).scan(); 129 } 130 return true; 131 } 132 133 class DocCommentScanner extends DocTreePathScanner<Void, Void> { 134 TreePath path; 135 DocCommentTree dc; 136 137 DocCommentScanner(TreePath path) { 138 this.path = path; 139 } 140 141 void scan() { 142 dc = trees.getDocCommentTree(path); 143 scan(new DocTreePath(path, dc), null); 144 } 145 146 @Override 147 public Void visitLink(LinkTree tree, Void ignore) { 148 checkReference(tree.getReference(), tree.getLabel()); 149 return null; 150 } 151 152 @Override 153 public Void visitSee(SeeTree tree, Void ignore) { 154 List<? extends DocTree> refLabel = tree.getReference(); 155 if (refLabel.size() > 1 && (refLabel.get(0) instanceof ReferenceTree)) { 156 ReferenceTree ref = (ReferenceTree) refLabel.get(0); 157 List<? extends DocTree> label = refLabel.subList(1, refLabel.size()); 158 checkReference(ref, label); 159 } 160 return null; 161 } 162 163 void checkReference(ReferenceTree tree, List<? extends DocTree> label) { 164 String sig = tree.getSignature(); 165 166 Element found = trees.getElement(new DocTreePath(getCurrentPath(), tree)); 167 if (found == null) { 168 System.err.println(sig + " NOT FOUND"); 169 } else { 170 System.err.println(sig + " found " + found.getKind() + " " + found); 171 } 172 173 String expect = "UNKNOWN"; 174 if (label.size() > 0 && label.get(0) instanceof TextTree) 175 expect = ((TextTree) label.get(0)).getBody(); 176 177 if (!expect.equalsIgnoreCase(found == null ? "bad" : found.getKind().name())) { 178 error(tree, "Unexpected value found: " + found +", expected: " + expect); 179 } 180 } 181 182 void error(DocTree tree, String msg) { 183 trees.printMessage(Kind.ERROR, msg, tree, dc, path.getCompilationUnit()); 184 } 185 } 186} 187 188/** 189 * @see ReferenceTestExtras Class 190 * @see #ReferenceTestExtras Field 191 * @see #ReferenceTestExtras() Constructor 192 * 193 * @see #X Field 194 * @see #X() Method 195 * 196 * @see #m Method 197 * 198 * @see #varargs(int...) Method 199 * @see #varargs(int... args) Method 200 * @see #varargs(int[]) Method 201 * @see #varargs(int[] args) Method 202 */ 203class ReferenceTestExtras { 204 int ReferenceTestExtras; // field 205 ReferenceTestExtras() { } // constructor 206 void ReferenceTestExtras() { } // method 207 208 int X; 209 void X() { } 210 static class X { } 211 212 void m() { } 213 void m(int i) { } 214 void m(int i, int j) { } 215 216 void varargs(int... args) { } 217} 218 219 220