jjg@1409: /* jlahoda@1726: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1409: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1409: * jjg@1409: * This code is free software; you can redistribute it and/or modify it jjg@1409: * under the terms of the GNU General Public License version 2 only, as jjg@1409: * published by the Free Software Foundation. jjg@1409: * jjg@1409: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1409: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1409: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1409: * version 2 for more details (a copy is included in the LICENSE file that jjg@1409: * accompanied this code). jjg@1409: * jjg@1409: * You should have received a copy of the GNU General Public License version jjg@1409: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1409: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1409: * jjg@1409: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1409: * or visit www.oracle.com if you need additional information or have any jjg@1409: * questions. jjg@1409: */ jjg@1409: jjg@1409: /* jjg@1409: * @test jjg@1409: * @bug 7021614 jjg@1409: * @summary extend com.sun.source API to support parsing javadoc comments jjg@1409: * @summary check references in at-see and {at-link} tags jjg@1409: * @build ReferenceTest jjg@1409: * @compile -processor ReferenceTest -proc:only ReferenceTest.java jjg@1409: */ jjg@1409: jjg@1409: import com.sun.source.doctree.DocCommentTree; jjg@1409: import com.sun.source.doctree.DocTree; jjg@1409: import com.sun.source.doctree.LinkTree; jjg@1409: import com.sun.source.doctree.ReferenceTree; jjg@1409: import com.sun.source.doctree.SeeTree; jjg@1409: import com.sun.source.doctree.TextTree; jlahoda@1726: import com.sun.source.util.DocTreePath; jlahoda@1726: import com.sun.source.util.DocTreePathScanner; jjg@1409: import com.sun.source.util.DocTreeScanner; jjg@1409: import com.sun.source.util.DocTrees; jjg@1409: import com.sun.source.util.TreePath; jjg@1409: jjg@1409: import java.util.List; jjg@1409: import java.util.Set; jjg@1409: import javax.annotation.processing.AbstractProcessor; jjg@1409: import javax.annotation.processing.ProcessingEnvironment; jjg@1409: import javax.annotation.processing.RoundEnvironment; jjg@1409: import javax.annotation.processing.SupportedAnnotationTypes; jjg@1409: import javax.lang.model.SourceVersion; jjg@1409: import javax.lang.model.element.Element; jjg@1409: import javax.lang.model.element.TypeElement; jjg@1409: import javax.tools.Diagnostic.Kind; jjg@1409: jjg@1409: /** jjg@1409: * {@link java.lang Package} jjg@1409: * {@link java.lang.ERROR Bad} jjg@1409: * jjg@1409: * {@link java.lang.String Class} jjg@1409: * {@link String Class} jjg@1409: * {@link java.lang.String#CASE_INSENSITIVE_ORDER Field} jjg@1409: * {@link java.lang.String#String Constructor} jjg@1409: * {@link java.lang.String#String(byte[]) Constructor} jjg@1409: * {@link java.lang.String#String(byte[] bytes) Constructor} jjg@1409: * {@link java.lang.String#String(byte[], String) Constructor} jjg@1409: * {@link java.lang.String#String(byte[] bytes, String charSetName) Constructor} jjg@1409: * {@link java.lang.String#isEmpty Method} jjg@1409: * {@link java.lang.String#isEmpty() Method} jjg@1409: * {@link java.lang.String#ERROR Bad} jjg@1409: * {@link java.lang.String#equals(Object) Method} jjg@1409: * jjg@1409: * {@link AbstractProcessor Class} jjg@1409: * jjg@1409: * {@link List#add(Object) Method} jjg@1409: * jjg@1409: * {@link #trees Field} jjg@1409: * {@link #getSupportedSourceVersion Method} jjg@1409: * {@link #init(ProcessingEnvironment Method} jjg@1409: * jjg@1409: * @see java.lang Package jjg@1409: * @see java.lang.ERROR Bad jjg@1409: * jjg@1409: * @see java.lang.String Class jjg@1409: * @see String Class jjg@1409: * @see java.lang.String#CASE_INSENSITIVE_ORDER Field jjg@1409: * @see java.lang.String#String Constructor jjg@1409: * @see java.lang.String#String(byte[]) Constructor jjg@1409: * @see java.lang.String#String(byte[] bytes) Constructor jjg@1409: * @see java.lang.String#String(byte[],String) Constructor jjg@1409: * @see java.lang.String#String(byte[] bytes, String charsetName) Constructor jjg@1409: * @see java.lang.String#isEmpty Method jjg@1409: * @see java.lang.String#isEmpty() Method jjg@1409: * @see java.lang.String#ERROR Bad jjg@1409: * @see java.lang.String#equals(Object) Method jjg@1409: * jjg@1409: * @see AbstractProcessor Class jjg@1409: * jjg@1409: * @see List#add(Object) Method jjg@1409: * jjg@1409: * @see #trees Field jjg@1409: * @see #getSupportedSourceVersion Method jjg@1409: * @see #init(ProcessingEnvironment) Method jjg@1409: * jjg@1409: * @see java.io.BufferedInputStream#BufferedInputStream(InputStream) Constructor jjg@1409: */ jjg@1409: @SupportedAnnotationTypes("*") jjg@1409: public class ReferenceTest extends AbstractProcessor { jjg@1409: DocTrees trees; jjg@1409: jjg@1409: @Override jjg@1409: public SourceVersion getSupportedSourceVersion() { jjg@1409: return SourceVersion.latest(); jjg@1409: } jjg@1409: jjg@1409: @Override jjg@1409: public void init(ProcessingEnvironment pEnv) { jjg@1409: super.init(pEnv); jjg@1409: trees = DocTrees.instance(pEnv); jjg@1409: } jjg@1409: jjg@1409: @Override jjg@1409: public boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@1409: for (Element e: roundEnv.getRootElements()) { jjg@1409: new DocCommentScanner(trees.getPath(e)).scan(); jjg@1409: } jjg@1409: return true; jjg@1409: } jjg@1409: jlahoda@1726: class DocCommentScanner extends DocTreePathScanner { jjg@1409: TreePath path; jjg@1409: DocCommentTree dc; jjg@1409: jjg@1409: DocCommentScanner(TreePath path) { jjg@1409: this.path = path; jjg@1409: } jjg@1409: jjg@1409: void scan() { jjg@1409: dc = trees.getDocCommentTree(path); jlahoda@1726: scan(new DocTreePath(path, dc), null); jjg@1409: } jjg@1409: jjg@1409: @Override jjg@1409: public Void visitLink(LinkTree tree, Void ignore) { jjg@1409: checkReference(tree.getReference(), tree.getLabel()); jjg@1409: return null; jjg@1409: } jjg@1409: jjg@1409: @Override jjg@1409: public Void visitSee(SeeTree tree, Void ignore) { jjg@1409: List refLabel = tree.getReference(); jjg@1409: if (refLabel.size() > 1 && (refLabel.get(0) instanceof ReferenceTree)) { jjg@1409: ReferenceTree ref = (ReferenceTree) refLabel.get(0); jjg@1409: List label = refLabel.subList(1, refLabel.size()); jjg@1409: checkReference(ref, label); jjg@1409: } jjg@1409: return null; jjg@1409: } jjg@1409: jjg@1409: void checkReference(ReferenceTree tree, List label) { jjg@1409: String sig = tree.getSignature(); jjg@1409: jlahoda@1726: Element found = trees.getElement(new DocTreePath(getCurrentPath(), tree)); jjg@1409: if (found == null) { jjg@1409: System.err.println(sig + " NOT FOUND"); jjg@1409: } else { jjg@1409: System.err.println(sig + " found " + found.getKind() + " " + found); jjg@1409: } jjg@1409: jjg@1409: String expect = "UNKNOWN"; jjg@1409: if (label.size() > 0 && label.get(0) instanceof TextTree) jjg@1409: expect = ((TextTree) label.get(0)).getBody(); jjg@1409: jjg@1409: if (!expect.equalsIgnoreCase(found == null ? "bad" : found.getKind().name())) { jjg@1409: error(tree, "Unexpected value found: " + found +", expected: " + expect); jjg@1409: } jjg@1409: } jjg@1409: jjg@1409: void error(DocTree tree, String msg) { jjg@1409: trees.printMessage(Kind.ERROR, msg, tree, dc, path.getCompilationUnit()); jjg@1409: } jjg@1409: } jjg@1409: } jjg@1409: jjg@1409: /** jjg@1409: * @see ReferenceTestExtras Class jjg@1409: * @see #ReferenceTestExtras Field jjg@1409: * @see #ReferenceTestExtras() Constructor jjg@1409: * jjg@1409: * @see #X Field jjg@1409: * @see #X() Method jjg@1409: * jjg@1409: * @see #m Method jjg@1409: * jjg@1409: * @see #varargs(int...) Method jjg@1409: * @see #varargs(int... args) Method jjg@1409: * @see #varargs(int[]) Method jjg@1409: * @see #varargs(int[] args) Method jjg@1409: */ jjg@1409: class ReferenceTestExtras { jjg@1409: int ReferenceTestExtras; // field jjg@1409: ReferenceTestExtras() { } // constructor jjg@1409: void ReferenceTestExtras() { } // method jjg@1409: jjg@1409: int X; jjg@1409: void X() { } jjg@1409: static class X { } jjg@1409: jjg@1409: void m() { } jjg@1409: void m(int i) { } jjg@1409: void m(int i, int j) { } jjg@1409: jjg@1409: void varargs(int... args) { } jjg@1409: } jjg@1409: jjg@1409: