8227922: DocTreeScanner does not dive into AttributeTree.getValue() and LiteralTree.getBody()
DocTreeScanner.visitAttribute and visitLiteral have to scan through the subnodes of AttributeTree and LiteralTree, respectivelly. Reviewed-by: vromero
This commit is contained in:
parent
da43cb5e46
commit
6e86f5b47b
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -124,7 +124,7 @@ public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* {@inheritDoc} This implementation returns {@code null}.
|
||||
* {@inheritDoc} This implementation scans the children in left to right order.
|
||||
*
|
||||
* @param node {@inheritDoc}
|
||||
* @param p {@inheritDoc}
|
||||
@ -132,7 +132,7 @@ public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {
|
||||
*/
|
||||
@Override
|
||||
public R visitAttribute(AttributeTree node, P p) {
|
||||
return null;
|
||||
return scan(node.getValue(), p);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -311,7 +311,7 @@ public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc} This implementation returns {@code null}.
|
||||
* {@inheritDoc} This implementation scans the children in left to right order.
|
||||
*
|
||||
* @param node {@inheritDoc}
|
||||
* @param p {@inheritDoc}
|
||||
@ -319,7 +319,7 @@ public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> {
|
||||
*/
|
||||
@Override
|
||||
public R visitLiteral(LiteralTree node, P p) {
|
||||
return null;
|
||||
return scan(node.getBody(), p);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -685,7 +685,7 @@ public class Checker extends DocTreePathScanner<Void, Void> {
|
||||
|
||||
// TODO: basic check on value
|
||||
|
||||
return super.visitAttribute(tree, ignore);
|
||||
return null;
|
||||
}
|
||||
|
||||
private void validateHtml4Attrs(AttributeTree tree, Name name, AttrKind k) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -26,13 +26,17 @@ import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import javax.tools.*;
|
||||
|
||||
import com.sun.source.doctree.DocCommentTree;
|
||||
import com.sun.source.doctree.DocTree;
|
||||
import com.sun.source.tree.CompilationUnitTree;
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.tools.javac.api.JavacTool;
|
||||
import com.sun.tools.javac.tree.DCTree;
|
||||
import com.sun.tools.javac.tree.DCTree.DCDocComment;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Pair;
|
||||
|
||||
public abstract class AbstractTreeScannerTest {
|
||||
|
||||
@ -141,7 +145,7 @@ public abstract class AbstractTreeScannerTest {
|
||||
error("File " + file + " ignored");
|
||||
}
|
||||
|
||||
abstract int test(JCCompilationUnit t);
|
||||
abstract int test(Pair<JavacTask, JCCompilationUnit> taskAndTree);
|
||||
|
||||
// See CR: 6982992 Tests CheckAttributedTree.java, JavacTreeScannerTest.java, and SourceTreeeScannerTest.java timeout
|
||||
StringWriter sw = new StringWriter();
|
||||
@ -157,7 +161,7 @@ public abstract class AbstractTreeScannerTest {
|
||||
* @throws IOException if any IO errors occur
|
||||
* @throws TreePosTest.ParseException if any errors occur while parsing the file
|
||||
*/
|
||||
JCCompilationUnit read(File file) throws IOException, ParseException {
|
||||
Pair<JavacTask, JCCompilationUnit> read(File file) throws IOException, ParseException {
|
||||
JavacTool tool = JavacTool.create();
|
||||
r.errors = 0;
|
||||
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
|
||||
@ -172,7 +176,7 @@ public abstract class AbstractTreeScannerTest {
|
||||
JCCompilationUnit t = (JCCompilationUnit) iter.next();
|
||||
if (iter.hasNext())
|
||||
throw new Error("too many trees found");
|
||||
return t;
|
||||
return Pair.of(task, t);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,15 +207,26 @@ public abstract class AbstractTreeScannerTest {
|
||||
*/
|
||||
void error(JavaFileObject file, Tree tree, String msg) {
|
||||
JCTree t = (JCTree) tree;
|
||||
error(file.getName() + ":" + getLine(file, t) + ": " + msg + " " + trim(t, 64));
|
||||
error(file.getName() + ":" + getLine(file, t.pos) + ": " + msg + " " + trim(t, 64));
|
||||
}
|
||||
|
||||
/**
|
||||
* Report an error for a specific tree node.
|
||||
* @param file the source file for the tree
|
||||
* @param t the tree node
|
||||
* @param label an indication of the error
|
||||
*/
|
||||
void error(JavaFileObject file, DocCommentTree comment, DocTree tree, String msg) {
|
||||
DCDocComment dc = (DCDocComment) comment;
|
||||
DCTree t = (DCTree) tree;
|
||||
error(file.getName() + ":" + getLine(file, t.getSourcePosition(dc)) + ": " + msg + " " + trim(t, 64));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a trimmed string for a tree node, with normalized white space and limited length.
|
||||
*/
|
||||
String trim(Tree tree, int len) {
|
||||
JCTree t = (JCTree) tree;
|
||||
String s = t.toString().replaceAll("\\s+", " ");
|
||||
String trim(Object tree, int len) {
|
||||
String s = tree.toString().replaceAll("\\s+", " ");
|
||||
return (s.length() < len) ? s : s.substring(0, len);
|
||||
}
|
||||
|
||||
@ -276,17 +291,38 @@ public abstract class AbstractTreeScannerTest {
|
||||
// where
|
||||
Map<JCTree.Tag, Set<Field>> map = new HashMap<JCTree.Tag,Set<Field>>();
|
||||
|
||||
/**
|
||||
* Get the set of fields for a tree node that may contain child tree nodes.
|
||||
* These are the fields that are subtypes of DCTree or List.
|
||||
* The results are cached, based on the tree's tag.
|
||||
*/
|
||||
Set<Field> getFields(DCTree tree) {
|
||||
Set<Field> fields = dcMap.get(tree.getKind());
|
||||
if (fields == null) {
|
||||
fields = new HashSet<Field>();
|
||||
for (Field f: tree.getClass().getFields()) {
|
||||
Class<?> fc = f.getType();
|
||||
if (DCTree.class.isAssignableFrom(fc) || List.class.isAssignableFrom(fc))
|
||||
fields.add(f);
|
||||
}
|
||||
dcMap.put(tree.getKind(), fields);
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
// where
|
||||
Map<DCTree.Kind, Set<Field>> dcMap = new HashMap<>();
|
||||
|
||||
/** Get the line number for the primary position for a tree.
|
||||
* The code is intended to be simple, although not necessarily efficient.
|
||||
* However, note that a file manager such as JavacFileManager is likely
|
||||
* to cache the results of file.getCharContent, avoiding the need to read
|
||||
* the bits from disk each time this method is called.
|
||||
*/
|
||||
int getLine(JavaFileObject file, JCTree tree) {
|
||||
int getLine(JavaFileObject file, long pos) {
|
||||
try {
|
||||
CharSequence cs = file.getCharContent(true);
|
||||
int line = 1;
|
||||
for (int i = 0; i < tree.pos; i++) {
|
||||
for (int i = 0; i < pos; i++) {
|
||||
if (cs.charAt(i) == '\n') // jtreg tests always use Unix line endings
|
||||
line++;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -49,10 +49,12 @@ import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import javax.tools.*;
|
||||
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.tree.TreeScanner;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Pair;
|
||||
|
||||
public class JavacTreeScannerTest extends AbstractTreeScannerTest {
|
||||
/**
|
||||
@ -75,8 +77,8 @@ public class JavacTreeScannerTest extends AbstractTreeScannerTest {
|
||||
}
|
||||
}
|
||||
|
||||
int test(JCCompilationUnit tree) {
|
||||
return new ScanTester().test(tree);
|
||||
int test(Pair<JavacTask, JCCompilationUnit> taskAndTree) {
|
||||
return new ScanTester().test(taskAndTree.snd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
190
test/langtools/tools/javac/tree/SourceDocTreeScannerTest.java
Normal file
190
test/langtools/tools/javac/tree/SourceDocTreeScannerTest.java
Normal file
@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utility and test program to check public DocTreeScanner class.
|
||||
* The program can be run standalone, or as a jtreg test. For info on
|
||||
* command line args, run program with no args.
|
||||
*
|
||||
* <p>
|
||||
* jtreg: Note that by using the -r switch in the test description below, this test
|
||||
* will process all java files in the langtools/test directory, thus implicitly
|
||||
* covering any new language features that may be tested in this test suite.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8227922
|
||||
* @summary Verify the behavior of DocTreeScanner
|
||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||
* jdk.compiler/com.sun.tools.javac.tree
|
||||
* jdk.compiler/com.sun.tools.javac.util
|
||||
* @build AbstractTreeScannerTest SourceDocTreeScannerTest
|
||||
* @run main SourceDocTreeScannerTest -q -r .
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.tools.*;
|
||||
|
||||
import com.sun.source.doctree.DocCommentTree;
|
||||
import com.sun.source.doctree.DocTree;
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.DocTreeScanner;
|
||||
import com.sun.source.util.DocTrees;
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.source.util.TreePath;
|
||||
import com.sun.source.util.TreePathScanner;
|
||||
import com.sun.tools.javac.tree.DCTree;
|
||||
import com.sun.tools.javac.tree.DCTree.DCDocComment;
|
||||
import com.sun.tools.javac.tree.DCTree.DCReference;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Pair;
|
||||
|
||||
public class SourceDocTreeScannerTest extends AbstractTreeScannerTest {
|
||||
/**
|
||||
* Main entry point.
|
||||
* If test.src is set, program runs in jtreg mode, and will throw an Error
|
||||
* if any errors arise, otherwise System.exit will be used. In jtreg mode,
|
||||
* the default base directory for file args is the value of ${test.src}.
|
||||
* In jtreg mode, the -r option can be given to change the default base
|
||||
* directory to the root test directory.
|
||||
*/
|
||||
public static void main(String... args) {
|
||||
String testSrc = System.getProperty("test.src");
|
||||
File baseDir = (testSrc == null) ? null : new File(testSrc);
|
||||
boolean ok = new SourceDocTreeScannerTest().run(baseDir, args);
|
||||
if (!ok) {
|
||||
if (testSrc != null) // jtreg mode
|
||||
throw new Error("failed");
|
||||
else
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
int test(Pair<JavacTask, JCCompilationUnit> taskAndTree) {
|
||||
return new ScanTester().test(taskAndTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main class for testing operation of tree scanner.
|
||||
* The set of nodes found by the scanner are compared
|
||||
* against the set of nodes found by reflection.
|
||||
*/
|
||||
private class ScanTester extends DocTreeScanner<Void,Void> {
|
||||
/** Main entry method for the class. */
|
||||
int test(Pair<JavacTask, JCCompilationUnit> taskAndTree) {
|
||||
sourcefile = taskAndTree.snd.sourcefile;
|
||||
int[] count = new int[1];
|
||||
new TreePathScanner<Void, Void>() {
|
||||
@Override
|
||||
public Void scan(Tree tree, Void p) {
|
||||
if (tree != null) {
|
||||
DocTrees trees = DocTrees.instance(taskAndTree.fst);
|
||||
DocCommentTree dcTree = trees.getDocCommentTree(new TreePath(getCurrentPath(), tree));
|
||||
if (dcTree != null) {
|
||||
found = new HashSet<>();
|
||||
ScanTester.this.scan(dcTree, null);
|
||||
expect = new HashSet<>();
|
||||
ScanTester.this.reflectiveScan(dcTree);
|
||||
|
||||
if (found.equals(expect)) {
|
||||
//System.err.println(sourcefile.getName() + ": trees compared OK");
|
||||
count[0] += found.size();
|
||||
} else {
|
||||
error(sourcefile.getName() + ": differences found");
|
||||
|
||||
if (found.size() != expect.size())
|
||||
error("Size mismatch; found: " + found.size() + ", expected: " + expect.size());
|
||||
|
||||
Set<DocTree> missing = new HashSet<>();
|
||||
missing.addAll(expect);
|
||||
missing.removeAll(found);
|
||||
for (DocTree t: missing)
|
||||
error(sourcefile, dcTree, t, "missing");
|
||||
|
||||
Set<DocTree> excess = new HashSet<>();
|
||||
excess.addAll(found);
|
||||
excess.removeAll(expect);
|
||||
for (DocTree t: excess)
|
||||
error(sourcefile, dcTree, t, "unexpected");
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.scan(tree, p);
|
||||
}
|
||||
}.scan(taskAndTree.snd, null);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Record all tree nodes found by scanner. */
|
||||
@Override
|
||||
public Void scan(DocTree tree, Void ignore) {
|
||||
if (tree == null)
|
||||
return null;
|
||||
//System.err.println("FOUND: " + tree.getKind() + " " + trim(tree, 64));
|
||||
found.add(tree);
|
||||
return super.scan(tree, ignore);
|
||||
}
|
||||
|
||||
/** record all tree nodes found by reflection. */
|
||||
public void reflectiveScan(Object o) {
|
||||
if (o == null)
|
||||
return;
|
||||
if (o instanceof DCTree) {
|
||||
DCTree tree = (DCTree) o;
|
||||
//System.err.println("EXPECT: " + tree.getKind() + " " + trim(tree, 64));
|
||||
expect.add(tree);
|
||||
for (Field f: getFields(tree)) {
|
||||
try {
|
||||
if (tree instanceof DCReference && f.getName().equals("paramTypes")) {
|
||||
//ignore - list of JCTrees
|
||||
} else if (tree instanceof DCDocComment &&
|
||||
!f.getName().equals("firstSentence") &&
|
||||
!f.getName().equals("body") &&
|
||||
!f.getName().equals("tags")) {
|
||||
//ignore - covered by other fields
|
||||
} else {
|
||||
reflectiveScan(f.get(tree));
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
error(e.toString());
|
||||
}
|
||||
}
|
||||
} else if (o instanceof List) {
|
||||
List<?> list = (List<?>) o;
|
||||
for (Object item: list)
|
||||
reflectiveScan(item);
|
||||
} else
|
||||
error("unexpected item: " + o);
|
||||
}
|
||||
|
||||
JavaFileObject sourcefile;
|
||||
Set<DocTree> found;
|
||||
Set<DocTree> expect;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -52,6 +52,7 @@ import javax.tools.*;
|
||||
|
||||
import com.sun.source.tree.CaseTree.CaseKind;
|
||||
import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.JavacTask;
|
||||
import com.sun.source.util.TreeScanner;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCase;
|
||||
@ -59,6 +60,7 @@ import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.TypeBoundKind;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Pair;
|
||||
|
||||
public class SourceTreeScannerTest extends AbstractTreeScannerTest {
|
||||
/**
|
||||
@ -81,8 +83,8 @@ public class SourceTreeScannerTest extends AbstractTreeScannerTest {
|
||||
}
|
||||
}
|
||||
|
||||
int test(JCCompilationUnit tree) {
|
||||
return new ScanTester().test(tree);
|
||||
int test(Pair<JavacTask, JCCompilationUnit> taskAndTree) {
|
||||
return new ScanTester().test(taskAndTree.snd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user