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