Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8287337: SnippetUtils should throw exceptions if snippets not found
Reviewed-by: hannesw
  • Loading branch information
jonathan-gibbons committed Jun 2, 2022
1 parent cbaeb00 commit deb0653
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Expand Up @@ -67,9 +67,6 @@ public void testEntryPoint(Path base) throws Exception {
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 {
Expand All @@ -94,9 +91,6 @@ public void testDocletExample(Path base) throws Exception {
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
Expand Down
44 changes: 39 additions & 5 deletions test/langtools/tools/lib/snippets/SnippetUtils.java
Expand Up @@ -35,6 +35,7 @@
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;

Expand All @@ -43,6 +44,7 @@
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;
Expand Down Expand Up @@ -97,6 +99,24 @@ public ConfigurationException(String message) {
}
}

/**
* 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;
Expand Down Expand Up @@ -218,20 +238,34 @@ public DocCommentTree getDocCommentTree(Element element) {
*
* @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;
}

/**
* {@return the snippet with a given id in the doc comment tree for an element}
*
* @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);
}

/**
Expand Down

0 comments on commit deb0653

Please sign in to comment.