8287337: SnippetUtils should throw exceptions if snippets not found
Reviewed-by: hannesw
This commit is contained in:
parent
cbaeb00fce
commit
deb06539b0
@ -67,9 +67,6 @@ public class TestDocletExample extends TestRunner {
|
||||
var docletPkg = snippets.getElements().getPackageElement("jdk.javadoc.doclet");
|
||||
var dc = snippets.getDocTrees().getDocCommentTree(docletPkg);
|
||||
var entryPointSnippet = snippets.getSnippetById(dc, "entry-point");
|
||||
if (entryPointSnippet == null) {
|
||||
throw new Error("Cannot find snippet \"entry-point\"");
|
||||
}
|
||||
var entryPointCode = entryPointSnippet.getBody().getBody();
|
||||
var code = """
|
||||
class C {
|
||||
@ -94,9 +91,6 @@ public class TestDocletExample extends TestRunner {
|
||||
var docletPkg = snippets.getElements().getPackageElement("jdk.javadoc.doclet");
|
||||
var dc = snippets.getDocTrees().getDocCommentTree(docletPkg);
|
||||
var exampleSnippet = snippets.getSnippetById(dc, "Example.java");
|
||||
if (exampleSnippet == null) {
|
||||
throw new Error("Cannot find snippet \"Example.java\"");
|
||||
}
|
||||
var exampleCode = exampleSnippet.getBody().getBody();
|
||||
|
||||
// compile it
|
||||
|
@ -35,6 +35,7 @@ import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -43,6 +44,7 @@ import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.element.ModuleElement;
|
||||
import javax.lang.model.element.PackageElement;
|
||||
import javax.lang.model.element.QualifiedNameable;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.util.Elements;
|
||||
@ -97,6 +99,24 @@ public class SnippetUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception used to report that a snippet could not be found.
|
||||
*/
|
||||
public static class SnippetNotFoundException extends Exception {
|
||||
public SnippetNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception used to report that a doc comment could not be found.
|
||||
*/
|
||||
public static class DocCommentNotFoundException extends Exception {
|
||||
public DocCommentNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
private static final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
private final StandardJavaFileManager fileManager;
|
||||
@ -218,9 +238,15 @@ public class SnippetUtils {
|
||||
*
|
||||
* @param tree the doc comment tree
|
||||
* @param id the id
|
||||
*
|
||||
* @throws SnippetNotFoundException if the snippet cannot be found
|
||||
*/
|
||||
public SnippetTree getSnippetById(DocCommentTree tree, String id) {
|
||||
return new SnippetFinder().scan(tree, id);
|
||||
public SnippetTree getSnippetById(DocCommentTree tree, String id) throws SnippetNotFoundException {
|
||||
SnippetTree result = new SnippetFinder().scan(tree, id);
|
||||
if (result == null) {
|
||||
throw new SnippetNotFoundException(id);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -228,10 +254,18 @@ public class SnippetUtils {
|
||||
*
|
||||
* @param element the element
|
||||
* @param id the id
|
||||
*
|
||||
* @throws DocCommentNotFoundException if the doc comment for the element cannot be found
|
||||
* @throws SnippetNotFoundException if the snippet cannot be found
|
||||
*/
|
||||
public SnippetTree getSnippetById(Element element, String id) {
|
||||
DocCommentTree tree = getDocCommentTree(element);
|
||||
return new SnippetFinder().scan(tree, id);
|
||||
public SnippetTree getSnippetById(Element element, String id)
|
||||
throws DocCommentNotFoundException, SnippetNotFoundException {
|
||||
DocCommentTree docCommentTree = getDocCommentTree(element);
|
||||
if (docCommentTree == null) {
|
||||
var name = (element instanceof QualifiedNameable q) ? q.getQualifiedName() : element.getSimpleName();
|
||||
throw new DocCommentNotFoundException(element.getKind() + " " + name);
|
||||
}
|
||||
return getSnippetById(docCommentTree, id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user