|
| 1 | +/* |
| 2 | + * Copyright 2024 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.bugpatterns.javadoc; |
| 18 | + |
| 19 | +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 20 | +import static com.google.errorprone.bugpatterns.javadoc.Utils.diagnosticPosition; |
| 21 | + |
| 22 | +import com.google.errorprone.BugPattern; |
| 23 | +import com.google.errorprone.VisitorState; |
| 24 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 25 | +import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher; |
| 26 | +import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; |
| 27 | +import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher; |
| 28 | +import com.google.errorprone.matchers.Description; |
| 29 | +import com.sun.source.doctree.DocTree; |
| 30 | +import com.sun.source.doctree.ErroneousTree; |
| 31 | +import com.sun.source.tree.ClassTree; |
| 32 | +import com.sun.source.tree.MethodTree; |
| 33 | +import com.sun.source.tree.VariableTree; |
| 34 | +import com.sun.source.util.DocTreePath; |
| 35 | +import com.sun.source.util.DocTreePathScanner; |
| 36 | + |
| 37 | +/** A bug pattern; see the summary. */ |
| 38 | +@BugPattern(summary = "This tag is invalid.", severity = WARNING, documentSuppression = false) |
| 39 | +public final class InvalidSnippet extends BugChecker |
| 40 | + implements ClassTreeMatcher, MethodTreeMatcher, VariableTreeMatcher { |
| 41 | + |
| 42 | + private void scanTags(VisitorState state, DocTreePath path) { |
| 43 | + new InvalidTagChecker(state).scan(path, null); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Description matchClass(ClassTree classTree, VisitorState state) { |
| 48 | + DocTreePath path = Utils.getDocTreePath(state); |
| 49 | + if (path != null) { |
| 50 | + scanTags(state, path); |
| 51 | + } |
| 52 | + return Description.NO_MATCH; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Description matchMethod(MethodTree methodTree, VisitorState state) { |
| 57 | + DocTreePath path = Utils.getDocTreePath(state); |
| 58 | + if (path != null) { |
| 59 | + scanTags(state, path); |
| 60 | + } |
| 61 | + return Description.NO_MATCH; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Description matchVariable(VariableTree variableTree, VisitorState state) { |
| 66 | + DocTreePath path = Utils.getDocTreePath(state); |
| 67 | + if (path != null) { |
| 68 | + scanTags(state, path); |
| 69 | + } |
| 70 | + return Description.NO_MATCH; |
| 71 | + } |
| 72 | + |
| 73 | + final class InvalidTagChecker extends DocTreePathScanner<Void, Void> { |
| 74 | + private final VisitorState state; |
| 75 | + |
| 76 | + private InvalidTagChecker(VisitorState state) { |
| 77 | + this.state = state; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Void visitErroneous(ErroneousTree erroneousTree, Void unused) { |
| 82 | + if (erroneousTree.getBody().startsWith("{@snippet")) { |
| 83 | + String message = |
| 84 | + "This @snippet tag looks to be malformed. Did you forget the \":\"? Snippets should" |
| 85 | + + " start with \"{@snippet :\" followed by a newline."; |
| 86 | + state.reportMatch( |
| 87 | + buildDescription(diagnosticPosition(getCurrentPath(), state)) |
| 88 | + .setMessage(message) |
| 89 | + .build()); |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Void scan(DocTree tree, Void unused) { |
| 96 | + return super.scan(tree, null); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments