8236142: DocTrees should provide getCharacters(EntityTree)

Reviewed-by: prappo
This commit is contained in:
Jonathan Gibbons 2020-09-11 14:47:15 +00:00
parent e7a1b9bf81
commit 7f27d0b013
7 changed files with 2242 additions and 2234 deletions

View File

@ -39,6 +39,8 @@ import javax.tools.JavaCompiler.CompilationTask;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.EntityTree;
import com.sun.source.tree.CompilationUnitTree;
/**
* Provides access to syntax trees for doc comments.
@ -202,19 +204,17 @@ public abstract class DocTrees extends Trees {
* @param root the compilation unit that contains tree
*/
public abstract void printMessage(Diagnostic.Kind kind, CharSequence msg,
com.sun.source.doctree.DocTree t,
com.sun.source.doctree.DocCommentTree c,
com.sun.source.tree.CompilationUnitTree root);
DocTree t, DocCommentTree c, CompilationUnitTree root);
/**
* Sets the break iterator to compute the first sentence of
* documentation comments.
* @param breakiterator a break iterator or {@code null} to specify the default
* @param breakIterator a break iterator or {@code null} to specify the default
* sentence breaker
*
* @since 9
*/
public abstract void setBreakIterator(BreakIterator breakiterator);
public abstract void setBreakIterator(BreakIterator breakIterator);
/**
* Returns a utility object for creating {@code DocTree} objects.
@ -223,4 +223,16 @@ public abstract class DocTrees extends Trees {
* @since 9
*/
public abstract DocTreeFactory getDocTreeFactory();
/**
* Returns a string containing the characters for the entity in a given entity tree,
* or {@code null} if the tree does not represent a valid series of characters.
*
* <p>The interpretation of entities is based on section
* <a href="https://www.w3.org/TR/html52/syntax.html#character-references">8.1.4. Character references</a>
* in the HTML 5.2 specification.</p>
*
* @return a string containing the characters
*/
public abstract String getCharacters(EntityTree tree);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, 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
@ -295,18 +295,12 @@ public class Checker extends DocTreePathScanner<Void, Void> {
public Void visitEntity(EntityTree tree, Void ignore) {
checkAllowsText(tree);
markEnclosingTag(Flag.HAS_TEXT);
String name = tree.getName().toString();
if (name.startsWith("#")) {
int v = StringUtils.toLowerCase(name).startsWith("#x")
? Integer.parseInt(name.substring(2), 16)
: Integer.parseInt(name.substring(1), 10);
if (!Entity.isValid(v)) {
env.messages.error(HTML, tree, "dc.entity.invalid", name);
}
} else if (!Entity.isValid(name)) {
env.messages.error(HTML, tree, "dc.entity.invalid", name);
String s = env.trees.getCharacters(tree);
if (s == null) {
env.messages.error(HTML, tree, "dc.entity.invalid", tree.getName());
}
return null;
}
void checkAllowsText(DocTree tree) {

File diff suppressed because it is too large Load Diff

View File

@ -59,6 +59,7 @@ import javax.tools.StandardLocation;
import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.DocTree;
import com.sun.source.doctree.EntityTree;
import com.sun.source.tree.CatchTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
@ -102,7 +103,6 @@ import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.parser.DocCommentParser;
import com.sun.tools.javac.parser.ParserFactory;
import com.sun.tools.javac.parser.Tokens.Comment;
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.resources.CompilerProperties.Errors;
import com.sun.tools.javac.resources.CompilerProperties.Notes;
@ -1162,6 +1162,11 @@ public class JavacTrees extends DocTrees {
this.breakIterator = breakiterator;
}
@Override @DefinedBy(Api.COMPILER_TREE)
public String getCharacters(EntityTree tree) {
return Entity.getCharacters(tree);
}
/**
* Makes a copy of a tree, noting the value resulting from copying a particular leaf.
**/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020, 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
@ -58,7 +58,6 @@ import com.sun.source.doctree.ThrowsTree;
import com.sun.source.util.DocTreeScanner;
import com.sun.source.util.DocTrees;
import com.sun.source.util.JavacTask;
import com.sun.tools.doclint.Entity;
import com.sun.tools.doclint.HtmlTag;
import com.sun.tools.javac.util.DefinedBy;
import com.sun.tools.javac.util.DefinedBy.Api;
@ -128,6 +127,7 @@ public class JavadocFormatter {
private class FormatJavadocScanner extends DocTreeScanner<Object, Object> {
private final StringBuilder result;
private final JavacTask task;
private final DocTrees trees;
private int reflownTo;
private int indent;
private int limit = Math.min(lineLimit, MAX_LINE_LENGTH);
@ -137,6 +137,7 @@ public class JavadocFormatter {
public FormatJavadocScanner(StringBuilder result, JavacTask task) {
this.result = result;
this.task = task;
this.trees = DocTrees.instance(task);
}
@Override @DefinedBy(Api.COMPILER_TREE)
@ -511,31 +512,10 @@ public class JavadocFormatter {
@Override @DefinedBy(Api.COMPILER_TREE)
public Object visitEntity(EntityTree node, Object p) {
String name = node.getName().toString();
int code = -1;
String value = null;
if (name.startsWith("#")) {
try {
int v = StringUtils.toLowerCase(name).startsWith("#x")
? Integer.parseInt(name.substring(2), 16)
: Integer.parseInt(name.substring(1), 10);
if (Entity.isValid(v)) {
code = v;
}
} catch (NumberFormatException ex) {
//ignore
}
} else {
value = Entity.getValue(name);
}
if (code != (-1)) {
result.appendCodePoint(code);
} else if (value != null) {
result.append(value);
} else {
result.append(node.toString());
}
String value = trees.getCharacters(node);
result.append(value == null ? node.toString() : value);
return super.visitEntity(node, p);
}
private DocTree lastNode;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020, 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
@ -21,7 +21,7 @@
* questions.
*/
/**
/*
* @test
* @bug 8006263
* @summary Supplementary test cases needed for doclint
@ -29,7 +29,6 @@
*/
import com.sun.tools.doclint.Checker;
import com.sun.tools.doclint.Entity;
import com.sun.tools.doclint.HtmlTag;
import com.sun.tools.doclint.Messages;
import java.util.Objects;