Mon, 14 Oct 2013 12:38:09 -0700
8026368: doclint does not report empty tags when tag closed implicitly
Reviewed-by: darcy
jjg@1455 | 1 | /* |
jjg@1495 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1455 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1455 | 4 | * |
jjg@1455 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1455 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1455 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@1455 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@1455 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@1455 | 10 | * |
jjg@1455 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1455 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1455 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1455 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1455 | 15 | * accompanied this code). |
jjg@1455 | 16 | * |
jjg@1455 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@1455 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1455 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1455 | 20 | * |
jjg@1455 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1455 | 22 | * or visit www.oracle.com if you need additional information or have any |
jjg@1455 | 23 | * questions. |
jjg@1455 | 24 | */ |
jjg@1455 | 25 | |
jjg@1455 | 26 | package com.sun.tools.doclint; |
jjg@1455 | 27 | |
jjg@1650 | 28 | import java.io.IOException; |
jjg@1650 | 29 | import java.io.StringWriter; |
jjg@1455 | 30 | import java.net.URI; |
jjg@1650 | 31 | import java.net.URISyntaxException; |
jjg@1455 | 32 | import java.util.Deque; |
jjg@1455 | 33 | import java.util.EnumSet; |
jjg@1912 | 34 | import java.util.HashMap; |
jjg@1455 | 35 | import java.util.HashSet; |
jjg@1455 | 36 | import java.util.LinkedList; |
jjg@1455 | 37 | import java.util.List; |
jjg@1912 | 38 | import java.util.Map; |
jjg@1455 | 39 | import java.util.Set; |
jjg@1650 | 40 | import java.util.regex.Matcher; |
jjg@1650 | 41 | import java.util.regex.Pattern; |
jjg@1455 | 42 | |
jjg@1455 | 43 | import javax.lang.model.element.Element; |
jjg@1455 | 44 | import javax.lang.model.element.ElementKind; |
jjg@1455 | 45 | import javax.lang.model.element.ExecutableElement; |
jjg@1455 | 46 | import javax.lang.model.element.Name; |
jjg@2053 | 47 | import javax.lang.model.element.VariableElement; |
jjg@1455 | 48 | import javax.lang.model.type.TypeKind; |
jjg@1455 | 49 | import javax.lang.model.type.TypeMirror; |
jjg@1455 | 50 | import javax.tools.Diagnostic.Kind; |
jjg@1912 | 51 | import javax.tools.JavaFileObject; |
jjg@1455 | 52 | |
jjg@1455 | 53 | import com.sun.source.doctree.AttributeTree; |
jjg@1455 | 54 | import com.sun.source.doctree.AuthorTree; |
jjg@1455 | 55 | import com.sun.source.doctree.DocCommentTree; |
jjg@1650 | 56 | import com.sun.source.doctree.DocRootTree; |
jjg@1455 | 57 | import com.sun.source.doctree.DocTree; |
jjg@1455 | 58 | import com.sun.source.doctree.EndElementTree; |
jjg@1455 | 59 | import com.sun.source.doctree.EntityTree; |
jjg@1455 | 60 | import com.sun.source.doctree.ErroneousTree; |
jjg@1455 | 61 | import com.sun.source.doctree.IdentifierTree; |
jjg@1455 | 62 | import com.sun.source.doctree.InheritDocTree; |
jjg@1650 | 63 | import com.sun.source.doctree.LinkTree; |
jjg@1650 | 64 | import com.sun.source.doctree.LiteralTree; |
jjg@1455 | 65 | import com.sun.source.doctree.ParamTree; |
jjg@1455 | 66 | import com.sun.source.doctree.ReferenceTree; |
jjg@1455 | 67 | import com.sun.source.doctree.ReturnTree; |
jjg@1455 | 68 | import com.sun.source.doctree.SerialDataTree; |
jjg@1455 | 69 | import com.sun.source.doctree.SerialFieldTree; |
jjg@1455 | 70 | import com.sun.source.doctree.SinceTree; |
jjg@1455 | 71 | import com.sun.source.doctree.StartElementTree; |
jjg@1455 | 72 | import com.sun.source.doctree.TextTree; |
jjg@1455 | 73 | import com.sun.source.doctree.ThrowsTree; |
jjg@1650 | 74 | import com.sun.source.doctree.ValueTree; |
jjg@1455 | 75 | import com.sun.source.doctree.VersionTree; |
jlahoda@1726 | 76 | import com.sun.source.util.DocTreePath; |
jlahoda@1726 | 77 | import com.sun.source.util.DocTreePathScanner; |
jjg@1455 | 78 | import com.sun.source.util.TreePath; |
jjg@1455 | 79 | import com.sun.tools.doclint.HtmlTag.AttrKind; |
jjg@1650 | 80 | import com.sun.tools.javac.tree.DocPretty; |
jjg@1455 | 81 | import static com.sun.tools.doclint.Messages.Group.*; |
jjg@1455 | 82 | |
jjg@1455 | 83 | |
jjg@1455 | 84 | /** |
jjg@1455 | 85 | * Validate a doc comment. |
jjg@1455 | 86 | * |
jjg@1455 | 87 | * <p><b>This is NOT part of any supported API. |
jjg@1455 | 88 | * If you write code that depends on this, you do so at your own |
jjg@1455 | 89 | * risk. This code and its internal interfaces are subject to change |
jjg@1455 | 90 | * or deletion without notice.</b></p> |
jjg@1455 | 91 | */ |
jlahoda@1726 | 92 | public class Checker extends DocTreePathScanner<Void, Void> { |
jjg@1455 | 93 | final Env env; |
jjg@1455 | 94 | |
jjg@1912 | 95 | Set<Element> foundParams = new HashSet<>(); |
jjg@1912 | 96 | Set<TypeMirror> foundThrows = new HashSet<>(); |
jjg@2052 | 97 | Map<Element, Set<String>> foundAnchors = new HashMap<>(); |
jjg@1455 | 98 | boolean foundInheritDoc = false; |
jjg@1455 | 99 | boolean foundReturn = false; |
jjg@1455 | 100 | |
jjg@1506 | 101 | public enum Flag { |
jjg@1455 | 102 | TABLE_HAS_CAPTION, |
jjg@1455 | 103 | HAS_ELEMENT, |
jjg@1650 | 104 | HAS_INLINE_TAG, |
jjg@1507 | 105 | HAS_TEXT, |
jjg@1507 | 106 | REPORTED_BAD_INLINE |
jjg@1455 | 107 | } |
jjg@1455 | 108 | |
jjg@1455 | 109 | static class TagStackItem { |
jjg@1455 | 110 | final DocTree tree; // typically, but not always, StartElementTree |
jjg@1455 | 111 | final HtmlTag tag; |
jjg@1455 | 112 | final Set<HtmlTag.Attr> attrs; |
jjg@1455 | 113 | final Set<Flag> flags; |
jjg@1455 | 114 | TagStackItem(DocTree tree, HtmlTag tag) { |
jjg@1455 | 115 | this.tree = tree; |
jjg@1455 | 116 | this.tag = tag; |
jjg@1455 | 117 | attrs = EnumSet.noneOf(HtmlTag.Attr.class); |
jjg@1455 | 118 | flags = EnumSet.noneOf(Flag.class); |
jjg@1455 | 119 | } |
jjg@1455 | 120 | @Override |
jjg@1455 | 121 | public String toString() { |
jjg@1455 | 122 | return String.valueOf(tag); |
jjg@1455 | 123 | } |
jjg@1455 | 124 | } |
jjg@1455 | 125 | |
jjg@1455 | 126 | private Deque<TagStackItem> tagStack; // TODO: maybe want to record starting tree as well |
jjg@1455 | 127 | private HtmlTag currHeaderTag; |
jjg@1455 | 128 | |
jjg@1668 | 129 | private final int implicitHeaderLevel; |
jjg@1668 | 130 | |
jjg@1455 | 131 | // <editor-fold defaultstate="collapsed" desc="Top level"> |
jjg@1455 | 132 | |
jjg@1455 | 133 | Checker(Env env) { |
jjg@1455 | 134 | env.getClass(); |
jjg@1455 | 135 | this.env = env; |
jjg@1912 | 136 | tagStack = new LinkedList<>(); |
jjg@1668 | 137 | implicitHeaderLevel = env.implicitHeaderLevel; |
jjg@1455 | 138 | } |
jjg@1455 | 139 | |
jjg@1455 | 140 | public Void scan(DocCommentTree tree, TreePath p) { |
jjg@1455 | 141 | env.setCurrent(p, tree); |
jjg@1455 | 142 | |
jjg@1455 | 143 | boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty(); |
jjg@1455 | 144 | |
jjg@1915 | 145 | if (p.getLeaf() == p.getCompilationUnit()) { |
jjg@1915 | 146 | // If p points to a compilation unit, the implied declaration is the |
jjg@1915 | 147 | // package declaration (if any) for the compilation unit. |
jjg@1915 | 148 | // Handle this case specially, because doc comments are only |
jjg@1915 | 149 | // expected in package-info files. |
jjg@1915 | 150 | JavaFileObject fo = p.getCompilationUnit().getSourceFile(); |
jjg@1915 | 151 | boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE); |
jjg@1915 | 152 | if (tree == null) { |
jjg@1915 | 153 | if (isPkgInfo) |
jjg@1915 | 154 | reportMissing("dc.missing.comment"); |
jjg@1915 | 155 | return null; |
jjg@1915 | 156 | } else { |
jjg@1915 | 157 | if (!isPkgInfo) |
jjg@1915 | 158 | reportReference("dc.unexpected.comment"); |
jjg@1915 | 159 | } |
jjg@1915 | 160 | } else { |
jjg@1915 | 161 | if (tree == null) { |
jjg@1915 | 162 | if (!isSynthetic() && !isOverridingMethod) |
jjg@1915 | 163 | reportMissing("dc.missing.comment"); |
jjg@1915 | 164 | return null; |
jjg@1915 | 165 | } |
jjg@1455 | 166 | } |
jjg@1455 | 167 | |
jjg@1455 | 168 | tagStack.clear(); |
jjg@1455 | 169 | currHeaderTag = null; |
jjg@1455 | 170 | |
jjg@1455 | 171 | foundParams.clear(); |
jjg@1455 | 172 | foundThrows.clear(); |
jjg@1455 | 173 | foundInheritDoc = false; |
jjg@1455 | 174 | foundReturn = false; |
jjg@1455 | 175 | |
jlahoda@1726 | 176 | scan(new DocTreePath(p, tree), null); |
jjg@1455 | 177 | |
jjg@1455 | 178 | if (!isOverridingMethod) { |
jjg@1455 | 179 | switch (env.currElement.getKind()) { |
jjg@1455 | 180 | case METHOD: |
jjg@1455 | 181 | case CONSTRUCTOR: { |
jjg@1455 | 182 | ExecutableElement ee = (ExecutableElement) env.currElement; |
jjg@1455 | 183 | checkParamsDocumented(ee.getTypeParameters()); |
jjg@1455 | 184 | checkParamsDocumented(ee.getParameters()); |
jjg@1455 | 185 | switch (ee.getReturnType().getKind()) { |
jjg@1455 | 186 | case VOID: |
jjg@1455 | 187 | case NONE: |
jjg@1455 | 188 | break; |
jjg@1455 | 189 | default: |
jjg@1455 | 190 | if (!foundReturn |
jjg@1455 | 191 | && !foundInheritDoc |
jjg@1455 | 192 | && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) { |
jjg@1455 | 193 | reportMissing("dc.missing.return"); |
jjg@1455 | 194 | } |
jjg@1455 | 195 | } |
jjg@1455 | 196 | checkThrowsDocumented(ee.getThrownTypes()); |
jjg@1455 | 197 | } |
jjg@1455 | 198 | } |
jjg@1455 | 199 | } |
jjg@1455 | 200 | |
jjg@1455 | 201 | return null; |
jjg@1455 | 202 | } |
jjg@1455 | 203 | |
jjg@1455 | 204 | private void reportMissing(String code, Object... args) { |
jjg@1455 | 205 | env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args); |
jjg@1455 | 206 | } |
jjg@1455 | 207 | |
jjg@1915 | 208 | private void reportReference(String code, Object... args) { |
jjg@1915 | 209 | env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args); |
jjg@1915 | 210 | } |
jjg@1915 | 211 | |
jjg@1455 | 212 | @Override |
jjg@1455 | 213 | public Void visitDocComment(DocCommentTree tree, Void ignore) { |
jjg@1455 | 214 | super.visitDocComment(tree, ignore); |
jjg@1455 | 215 | for (TagStackItem tsi: tagStack) { |
jjg@2110 | 216 | warnIfEmpty(tsi, null); |
jjg@1455 | 217 | if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT |
jjg@1455 | 218 | && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) { |
jjg@1455 | 219 | StartElementTree t = (StartElementTree) tsi.tree; |
jjg@1455 | 220 | env.messages.error(HTML, t, "dc.tag.not.closed", t.getName()); |
jjg@1455 | 221 | } |
jjg@1455 | 222 | } |
jjg@1455 | 223 | return null; |
jjg@1455 | 224 | } |
jjg@1455 | 225 | // </editor-fold> |
jjg@1455 | 226 | |
jjg@1455 | 227 | // <editor-fold defaultstate="collapsed" desc="Text and entities."> |
jjg@1455 | 228 | |
jjg@1455 | 229 | @Override |
jjg@1455 | 230 | public Void visitText(TextTree tree, Void ignore) { |
jjg@1507 | 231 | if (hasNonWhitespace(tree)) { |
jjg@1507 | 232 | checkAllowsText(tree); |
jjg@1455 | 233 | markEnclosingTag(Flag.HAS_TEXT); |
jjg@1455 | 234 | } |
jjg@1455 | 235 | return null; |
jjg@1455 | 236 | } |
jjg@1455 | 237 | |
jjg@1455 | 238 | @Override |
jjg@1455 | 239 | public Void visitEntity(EntityTree tree, Void ignore) { |
jjg@1507 | 240 | checkAllowsText(tree); |
jjg@1455 | 241 | markEnclosingTag(Flag.HAS_TEXT); |
jjg@1455 | 242 | String name = tree.getName().toString(); |
jjg@1455 | 243 | if (name.startsWith("#")) { |
jjg@1455 | 244 | int v = name.toLowerCase().startsWith("#x") |
jjg@1455 | 245 | ? Integer.parseInt(name.substring(2), 16) |
jjg@1455 | 246 | : Integer.parseInt(name.substring(1), 10); |
jjg@1455 | 247 | if (!Entity.isValid(v)) { |
jjg@1455 | 248 | env.messages.error(HTML, tree, "dc.entity.invalid", name); |
jjg@1455 | 249 | } |
jjg@1455 | 250 | } else if (!Entity.isValid(name)) { |
jjg@1455 | 251 | env.messages.error(HTML, tree, "dc.entity.invalid", name); |
jjg@1455 | 252 | } |
jjg@1455 | 253 | return null; |
jjg@1455 | 254 | } |
jjg@1455 | 255 | |
jjg@1507 | 256 | void checkAllowsText(DocTree tree) { |
jjg@1507 | 257 | TagStackItem top = tagStack.peek(); |
jjg@1507 | 258 | if (top != null |
jjg@1507 | 259 | && top.tree.getKind() == DocTree.Kind.START_ELEMENT |
jjg@1507 | 260 | && !top.tag.acceptsText()) { |
jjg@1507 | 261 | if (top.flags.add(Flag.REPORTED_BAD_INLINE)) { |
jjg@1507 | 262 | env.messages.error(HTML, tree, "dc.text.not.allowed", |
jjg@1507 | 263 | ((StartElementTree) top.tree).getName()); |
jjg@1507 | 264 | } |
jjg@1507 | 265 | } |
jjg@1507 | 266 | } |
jjg@1507 | 267 | |
jjg@1455 | 268 | // </editor-fold> |
jjg@1455 | 269 | |
jjg@1455 | 270 | // <editor-fold defaultstate="collapsed" desc="HTML elements"> |
jjg@1455 | 271 | |
jjg@1455 | 272 | @Override |
jjg@1455 | 273 | public Void visitStartElement(StartElementTree tree, Void ignore) { |
jjg@1455 | 274 | final Name treeName = tree.getName(); |
jjg@1455 | 275 | final HtmlTag t = HtmlTag.get(treeName); |
jjg@1455 | 276 | if (t == null) { |
jjg@1455 | 277 | env.messages.error(HTML, tree, "dc.tag.unknown", treeName); |
jjg@1455 | 278 | } else { |
jjg@1552 | 279 | boolean done = false; |
jjg@1507 | 280 | for (TagStackItem tsi: tagStack) { |
jjg@1507 | 281 | if (tsi.tag.accepts(t)) { |
jjg@2110 | 282 | while (tagStack.peek() != tsi) { |
jjg@2110 | 283 | warnIfEmpty(tagStack.peek(), null); |
jjg@2110 | 284 | tagStack.pop(); |
jjg@2110 | 285 | } |
jjg@1552 | 286 | done = true; |
jjg@1507 | 287 | break; |
jjg@1552 | 288 | } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) { |
jjg@1552 | 289 | done = true; |
jjg@1507 | 290 | break; |
jjg@1552 | 291 | } |
jjg@1552 | 292 | } |
jjg@1552 | 293 | if (!done && HtmlTag.BODY.accepts(t)) { |
jjg@2110 | 294 | while (!tagStack.isEmpty()) { |
jjg@2110 | 295 | warnIfEmpty(tagStack.peek(), null); |
jjg@2110 | 296 | tagStack.pop(); |
jjg@2110 | 297 | } |
jjg@1507 | 298 | } |
jjg@1507 | 299 | |
jjg@2110 | 300 | markEnclosingTag(Flag.HAS_ELEMENT); |
jjg@1507 | 301 | checkStructure(tree, t); |
jjg@1507 | 302 | |
jjg@1455 | 303 | // tag specific checks |
jjg@1455 | 304 | switch (t) { |
jjg@1455 | 305 | // check for out of sequence headers, such as <h1>...</h1> <h3>...</h3> |
jjg@1455 | 306 | case H1: case H2: case H3: case H4: case H5: case H6: |
jjg@1455 | 307 | checkHeader(tree, t); |
jjg@1455 | 308 | break; |
jjg@1455 | 309 | } |
jjg@1455 | 310 | |
jjg@1455 | 311 | if (t.flags.contains(HtmlTag.Flag.NO_NEST)) { |
jjg@1455 | 312 | for (TagStackItem i: tagStack) { |
jjg@1455 | 313 | if (t == i.tag) { |
jjg@1455 | 314 | env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName); |
jjg@1455 | 315 | break; |
jjg@1455 | 316 | } |
jjg@1455 | 317 | } |
jjg@1455 | 318 | } |
jjg@1455 | 319 | } |
jjg@1455 | 320 | |
jjg@1455 | 321 | // check for self closing tags, such as <a id="name"/> |
jjg@1455 | 322 | if (tree.isSelfClosing()) { |
jjg@1455 | 323 | env.messages.error(HTML, tree, "dc.tag.self.closing", treeName); |
jjg@1455 | 324 | } |
jjg@1455 | 325 | |
jjg@1455 | 326 | try { |
jjg@1455 | 327 | TagStackItem parent = tagStack.peek(); |
jjg@1455 | 328 | TagStackItem top = new TagStackItem(tree, t); |
jjg@1455 | 329 | tagStack.push(top); |
jjg@1455 | 330 | |
jjg@1455 | 331 | super.visitStartElement(tree, ignore); |
jjg@1455 | 332 | |
jjg@1455 | 333 | // handle attributes that may or may not have been found in start element |
jjg@1455 | 334 | if (t != null) { |
jjg@1455 | 335 | switch (t) { |
jjg@1455 | 336 | case CAPTION: |
jjg@1455 | 337 | if (parent != null && parent.tag == HtmlTag.TABLE) |
jjg@1455 | 338 | parent.flags.add(Flag.TABLE_HAS_CAPTION); |
jjg@1455 | 339 | break; |
jjg@1455 | 340 | |
jjg@1455 | 341 | case IMG: |
jjg@1455 | 342 | if (!top.attrs.contains(HtmlTag.Attr.ALT)) |
jjg@1455 | 343 | env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image"); |
jjg@1455 | 344 | break; |
jjg@1455 | 345 | } |
jjg@1455 | 346 | } |
jjg@1455 | 347 | |
jjg@1455 | 348 | return null; |
jjg@1455 | 349 | } finally { |
jjg@1455 | 350 | |
jjg@1455 | 351 | if (t == null || t.endKind == HtmlTag.EndKind.NONE) |
jjg@1455 | 352 | tagStack.pop(); |
jjg@1455 | 353 | } |
jjg@1455 | 354 | } |
jjg@1455 | 355 | |
jjg@1507 | 356 | private void checkStructure(StartElementTree tree, HtmlTag t) { |
jjg@1507 | 357 | Name treeName = tree.getName(); |
jjg@1507 | 358 | TagStackItem top = tagStack.peek(); |
jjg@1507 | 359 | switch (t.blockType) { |
jjg@1507 | 360 | case BLOCK: |
jjg@1507 | 361 | if (top == null || top.tag.accepts(t)) |
jjg@1507 | 362 | return; |
jjg@1507 | 363 | |
jjg@1507 | 364 | switch (top.tree.getKind()) { |
jjg@1507 | 365 | case START_ELEMENT: { |
jjg@1507 | 366 | if (top.tag.blockType == HtmlTag.BlockType.INLINE) { |
jjg@1507 | 367 | Name name = ((StartElementTree) top.tree).getName(); |
jjg@1507 | 368 | env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element", |
jjg@1507 | 369 | treeName, name); |
jjg@1507 | 370 | return; |
jjg@1507 | 371 | } |
jjg@1507 | 372 | } |
jjg@1507 | 373 | break; |
jjg@1507 | 374 | |
jjg@1507 | 375 | case LINK: |
jjg@1507 | 376 | case LINK_PLAIN: { |
jjg@1507 | 377 | String name = top.tree.getKind().tagName; |
jjg@1507 | 378 | env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag", |
jjg@1507 | 379 | treeName, name); |
jjg@1507 | 380 | return; |
jjg@1507 | 381 | } |
jjg@1507 | 382 | } |
jjg@1507 | 383 | break; |
jjg@1507 | 384 | |
jjg@1507 | 385 | case INLINE: |
jjg@1507 | 386 | if (top == null || top.tag.accepts(t)) |
jjg@1507 | 387 | return; |
jjg@1507 | 388 | break; |
jjg@1507 | 389 | |
jjg@1507 | 390 | case LIST_ITEM: |
jjg@1507 | 391 | case TABLE_ITEM: |
jjg@1507 | 392 | if (top != null) { |
jjg@1507 | 393 | // reset this flag so subsequent bad inline content gets reported |
jjg@1507 | 394 | top.flags.remove(Flag.REPORTED_BAD_INLINE); |
jjg@1507 | 395 | if (top.tag.accepts(t)) |
jjg@1507 | 396 | return; |
jjg@1507 | 397 | } |
jjg@1507 | 398 | break; |
jjg@1507 | 399 | |
jjg@1507 | 400 | case OTHER: |
jjg@1507 | 401 | env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName); |
jjg@1507 | 402 | return; |
jjg@1507 | 403 | } |
jjg@1507 | 404 | |
jjg@1507 | 405 | env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName); |
jjg@1507 | 406 | } |
jjg@1507 | 407 | |
jjg@1455 | 408 | private void checkHeader(StartElementTree tree, HtmlTag tag) { |
jjg@1455 | 409 | // verify the new tag |
jjg@1455 | 410 | if (getHeaderLevel(tag) > getHeaderLevel(currHeaderTag) + 1) { |
jjg@1455 | 411 | if (currHeaderTag == null) { |
jjg@1455 | 412 | env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.1", tag); |
jjg@1455 | 413 | } else { |
jjg@1455 | 414 | env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.2", |
jjg@1455 | 415 | tag, currHeaderTag); |
jjg@1455 | 416 | } |
jjg@1455 | 417 | } |
jjg@1455 | 418 | |
jjg@1455 | 419 | currHeaderTag = tag; |
jjg@1455 | 420 | } |
jjg@1455 | 421 | |
jjg@1455 | 422 | private int getHeaderLevel(HtmlTag tag) { |
jjg@1455 | 423 | if (tag == null) |
jjg@1668 | 424 | return implicitHeaderLevel; |
jjg@1455 | 425 | switch (tag) { |
jjg@1455 | 426 | case H1: return 1; |
jjg@1455 | 427 | case H2: return 2; |
jjg@1455 | 428 | case H3: return 3; |
jjg@1455 | 429 | case H4: return 4; |
jjg@1455 | 430 | case H5: return 5; |
jjg@1455 | 431 | case H6: return 6; |
jjg@1455 | 432 | default: throw new IllegalArgumentException(); |
jjg@1455 | 433 | } |
jjg@1455 | 434 | } |
jjg@1455 | 435 | |
jjg@1455 | 436 | @Override |
jjg@1455 | 437 | public Void visitEndElement(EndElementTree tree, Void ignore) { |
jjg@1455 | 438 | final Name treeName = tree.getName(); |
jjg@1455 | 439 | final HtmlTag t = HtmlTag.get(treeName); |
jjg@1455 | 440 | if (t == null) { |
jjg@1455 | 441 | env.messages.error(HTML, tree, "dc.tag.unknown", treeName); |
jjg@1455 | 442 | } else if (t.endKind == HtmlTag.EndKind.NONE) { |
jjg@1455 | 443 | env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName); |
jjg@1455 | 444 | } else { |
jjg@1499 | 445 | boolean done = false; |
jjg@1455 | 446 | while (!tagStack.isEmpty()) { |
jjg@1455 | 447 | TagStackItem top = tagStack.peek(); |
jjg@1455 | 448 | if (t == top.tag) { |
jjg@1455 | 449 | switch (t) { |
jjg@1455 | 450 | case TABLE: |
jjg@1455 | 451 | if (!top.attrs.contains(HtmlTag.Attr.SUMMARY) |
jjg@1455 | 452 | && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) { |
jjg@1455 | 453 | env.messages.error(ACCESSIBILITY, tree, |
jjg@1455 | 454 | "dc.no.summary.or.caption.for.table"); |
jjg@1455 | 455 | } |
jjg@1455 | 456 | } |
jjg@2110 | 457 | warnIfEmpty(top, tree); |
jjg@1455 | 458 | tagStack.pop(); |
jjg@1499 | 459 | done = true; |
jjg@1455 | 460 | break; |
jjg@1455 | 461 | } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) { |
jjg@1455 | 462 | tagStack.pop(); |
jjg@1455 | 463 | } else { |
jjg@1455 | 464 | boolean found = false; |
jjg@1455 | 465 | for (TagStackItem si: tagStack) { |
jjg@1455 | 466 | if (si.tag == t) { |
jjg@1455 | 467 | found = true; |
jjg@1455 | 468 | break; |
jjg@1455 | 469 | } |
jjg@1455 | 470 | } |
jjg@1455 | 471 | if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) { |
jjg@1455 | 472 | env.messages.error(HTML, top.tree, "dc.tag.start.unmatched", |
jjg@1455 | 473 | ((StartElementTree) top.tree).getName()); |
jjg@1455 | 474 | tagStack.pop(); |
jjg@1455 | 475 | } else { |
jjg@1455 | 476 | env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); |
jjg@1499 | 477 | done = true; |
jjg@1455 | 478 | break; |
jjg@1455 | 479 | } |
jjg@1455 | 480 | } |
jjg@1455 | 481 | } |
jjg@1499 | 482 | |
jjg@1499 | 483 | if (!done && tagStack.isEmpty()) { |
jjg@1499 | 484 | env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); |
jjg@1499 | 485 | } |
jjg@1455 | 486 | } |
jjg@1455 | 487 | |
jjg@1455 | 488 | return super.visitEndElement(tree, ignore); |
jjg@1455 | 489 | } |
jjg@2110 | 490 | |
jjg@2110 | 491 | void warnIfEmpty(TagStackItem tsi, DocTree endTree) { |
jjg@2110 | 492 | if (tsi.tag != null && tsi.tree instanceof StartElementTree) { |
jjg@2110 | 493 | if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT) |
jjg@2110 | 494 | && !tsi.flags.contains(Flag.HAS_TEXT) |
jjg@2110 | 495 | && !tsi.flags.contains(Flag.HAS_ELEMENT) |
jjg@2110 | 496 | && !tsi.flags.contains(Flag.HAS_INLINE_TAG)) { |
jjg@2110 | 497 | DocTree tree = (endTree != null) ? endTree : tsi.tree; |
jjg@2110 | 498 | Name treeName = ((StartElementTree) tsi.tree).getName(); |
jjg@2110 | 499 | env.messages.warning(HTML, tree, "dc.tag.empty", treeName); |
jjg@2110 | 500 | } |
jjg@2110 | 501 | } |
jjg@2110 | 502 | } |
jjg@2110 | 503 | |
jjg@1455 | 504 | // </editor-fold> |
jjg@1455 | 505 | |
jjg@1455 | 506 | // <editor-fold defaultstate="collapsed" desc="HTML attributes"> |
jjg@1455 | 507 | |
jjg@1455 | 508 | @Override @SuppressWarnings("fallthrough") |
jjg@1455 | 509 | public Void visitAttribute(AttributeTree tree, Void ignore) { |
jjg@1455 | 510 | HtmlTag currTag = tagStack.peek().tag; |
jjg@1455 | 511 | if (currTag != null) { |
jjg@1455 | 512 | Name name = tree.getName(); |
jjg@1455 | 513 | HtmlTag.Attr attr = currTag.getAttr(name); |
jjg@1455 | 514 | if (attr != null) { |
jjg@1455 | 515 | boolean first = tagStack.peek().attrs.add(attr); |
jjg@1455 | 516 | if (!first) |
jjg@1455 | 517 | env.messages.error(HTML, tree, "dc.attr.repeated", name); |
jjg@1455 | 518 | } |
jjg@1455 | 519 | AttrKind k = currTag.getAttrKind(name); |
jjg@1455 | 520 | switch (k) { |
jjg@1455 | 521 | case OK: |
jjg@1455 | 522 | break; |
jjg@1455 | 523 | |
jjg@1455 | 524 | case INVALID: |
jjg@1455 | 525 | env.messages.error(HTML, tree, "dc.attr.unknown", name); |
jjg@1455 | 526 | break; |
jjg@1455 | 527 | |
jjg@1455 | 528 | case OBSOLETE: |
jjg@1455 | 529 | env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete", name); |
jjg@1455 | 530 | break; |
jjg@1455 | 531 | |
jjg@1455 | 532 | case USE_CSS: |
jjg@1455 | 533 | env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete.use.css", name); |
jjg@1455 | 534 | break; |
jjg@1455 | 535 | } |
jjg@1455 | 536 | |
jjg@1455 | 537 | if (attr != null) { |
jjg@1455 | 538 | switch (attr) { |
jjg@1455 | 539 | case NAME: |
jjg@1455 | 540 | if (currTag != HtmlTag.A) { |
jjg@1455 | 541 | break; |
jjg@1455 | 542 | } |
jjg@1495 | 543 | // fallthrough |
jjg@1455 | 544 | case ID: |
jjg@1455 | 545 | String value = getAttrValue(tree); |
jjg@1495 | 546 | if (value == null) { |
jjg@1495 | 547 | env.messages.error(HTML, tree, "dc.anchor.value.missing"); |
jjg@1495 | 548 | } else { |
jjg@1495 | 549 | if (!validName.matcher(value).matches()) { |
jjg@1495 | 550 | env.messages.error(HTML, tree, "dc.invalid.anchor", value); |
jjg@1495 | 551 | } |
jjg@1912 | 552 | if (!checkAnchor(value)) { |
jjg@1495 | 553 | env.messages.error(HTML, tree, "dc.anchor.already.defined", value); |
jjg@1495 | 554 | } |
jjg@1455 | 555 | } |
jjg@1455 | 556 | break; |
jjg@1455 | 557 | |
jjg@1455 | 558 | case HREF: |
jjg@1455 | 559 | if (currTag == HtmlTag.A) { |
jjg@1455 | 560 | String v = getAttrValue(tree); |
jjg@1455 | 561 | if (v == null || v.isEmpty()) { |
jjg@1455 | 562 | env.messages.error(HTML, tree, "dc.attr.lacks.value"); |
jjg@1455 | 563 | } else { |
jjg@1455 | 564 | Matcher m = docRoot.matcher(v); |
jjg@1455 | 565 | if (m.matches()) { |
jjg@1455 | 566 | String rest = m.group(2); |
jjg@1455 | 567 | if (!rest.isEmpty()) |
jjg@1455 | 568 | checkURI(tree, rest); |
jjg@1455 | 569 | } else { |
jjg@1455 | 570 | checkURI(tree, v); |
jjg@1455 | 571 | } |
jjg@1455 | 572 | } |
jjg@1455 | 573 | } |
jjg@1455 | 574 | break; |
jjg@1793 | 575 | |
jjg@1793 | 576 | case VALUE: |
jjg@1793 | 577 | if (currTag == HtmlTag.LI) { |
jjg@1793 | 578 | String v = getAttrValue(tree); |
jjg@1793 | 579 | if (v == null || v.isEmpty()) { |
jjg@1793 | 580 | env.messages.error(HTML, tree, "dc.attr.lacks.value"); |
jjg@1793 | 581 | } else if (!validNumber.matcher(v).matches()) { |
jjg@1793 | 582 | env.messages.error(HTML, tree, "dc.attr.not.number"); |
jjg@1793 | 583 | } |
jjg@1793 | 584 | } |
jjg@1793 | 585 | break; |
jjg@1455 | 586 | } |
jjg@1455 | 587 | } |
jjg@1455 | 588 | } |
jjg@1455 | 589 | |
jjg@1455 | 590 | // TODO: basic check on value |
jjg@1455 | 591 | |
jjg@1455 | 592 | return super.visitAttribute(tree, ignore); |
jjg@1455 | 593 | } |
jjg@1455 | 594 | |
jjg@1912 | 595 | private boolean checkAnchor(String name) { |
jjg@2052 | 596 | Element e = getEnclosingPackageOrClass(env.currElement); |
jjg@2052 | 597 | if (e == null) |
jjg@2052 | 598 | return true; |
jjg@2052 | 599 | Set<String> set = foundAnchors.get(e); |
jjg@1912 | 600 | if (set == null) |
jjg@2052 | 601 | foundAnchors.put(e, set = new HashSet<>()); |
jjg@1912 | 602 | return set.add(name); |
jjg@1912 | 603 | } |
jjg@1912 | 604 | |
jjg@2052 | 605 | private Element getEnclosingPackageOrClass(Element e) { |
jjg@2052 | 606 | while (e != null) { |
jjg@2052 | 607 | switch (e.getKind()) { |
jjg@2052 | 608 | case CLASS: |
jjg@2052 | 609 | case ENUM: |
jjg@2052 | 610 | case INTERFACE: |
jjg@2052 | 611 | case PACKAGE: |
jjg@2052 | 612 | return e; |
jjg@2052 | 613 | default: |
jjg@2052 | 614 | e = e.getEnclosingElement(); |
jjg@2052 | 615 | } |
jjg@2052 | 616 | } |
jjg@2052 | 617 | return e; |
jjg@2052 | 618 | } |
jjg@2052 | 619 | |
jjg@1455 | 620 | // http://www.w3.org/TR/html401/types.html#type-name |
jjg@1455 | 621 | private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*"); |
jjg@1455 | 622 | |
jjg@1793 | 623 | private static final Pattern validNumber = Pattern.compile("-?[0-9]+"); |
jjg@1793 | 624 | |
jjg@1455 | 625 | // pattern to remove leading {@docRoot}/? |
jjg@1455 | 626 | private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)"); |
jjg@1455 | 627 | |
jjg@1455 | 628 | private String getAttrValue(AttributeTree tree) { |
jjg@1455 | 629 | if (tree.getValue() == null) |
jjg@1455 | 630 | return null; |
jjg@1455 | 631 | |
jjg@1455 | 632 | StringWriter sw = new StringWriter(); |
jjg@1455 | 633 | try { |
jjg@1455 | 634 | new DocPretty(sw).print(tree.getValue()); |
jjg@1455 | 635 | } catch (IOException e) { |
jjg@1455 | 636 | // cannot happen |
jjg@1455 | 637 | } |
jjg@1455 | 638 | // ignore potential use of entities for now |
jjg@1455 | 639 | return sw.toString(); |
jjg@1455 | 640 | } |
jjg@1455 | 641 | |
jjg@1455 | 642 | private void checkURI(AttributeTree tree, String uri) { |
jjg@1455 | 643 | try { |
jjg@1455 | 644 | URI u = new URI(uri); |
jjg@1455 | 645 | } catch (URISyntaxException e) { |
jjg@1455 | 646 | env.messages.error(HTML, tree, "dc.invalid.uri", uri); |
jjg@1455 | 647 | } |
jjg@1455 | 648 | } |
jjg@1455 | 649 | // </editor-fold> |
jjg@1455 | 650 | |
jjg@1455 | 651 | // <editor-fold defaultstate="collapsed" desc="javadoc tags"> |
jjg@1455 | 652 | |
jjg@1455 | 653 | @Override |
jjg@1455 | 654 | public Void visitAuthor(AuthorTree tree, Void ignore) { |
jjg@1455 | 655 | warnIfEmpty(tree, tree.getName()); |
jjg@1455 | 656 | return super.visitAuthor(tree, ignore); |
jjg@1455 | 657 | } |
jjg@1455 | 658 | |
jjg@1455 | 659 | @Override |
jjg@1650 | 660 | public Void visitDocRoot(DocRootTree tree, Void ignore) { |
jjg@1650 | 661 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
jjg@1650 | 662 | return super.visitDocRoot(tree, ignore); |
jjg@1650 | 663 | } |
jjg@1650 | 664 | |
jjg@1650 | 665 | @Override |
jjg@1455 | 666 | public Void visitInheritDoc(InheritDocTree tree, Void ignore) { |
jjg@1650 | 667 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
jjg@1455 | 668 | // TODO: verify on overridden method |
jjg@1455 | 669 | foundInheritDoc = true; |
jjg@1455 | 670 | return super.visitInheritDoc(tree, ignore); |
jjg@1455 | 671 | } |
jjg@1455 | 672 | |
jjg@1455 | 673 | @Override |
jjg@1455 | 674 | public Void visitLink(LinkTree tree, Void ignore) { |
jjg@1650 | 675 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
jjg@1455 | 676 | // simulate inline context on tag stack |
jjg@1455 | 677 | HtmlTag t = (tree.getKind() == DocTree.Kind.LINK) |
jjg@1455 | 678 | ? HtmlTag.CODE : HtmlTag.SPAN; |
jjg@1455 | 679 | tagStack.push(new TagStackItem(tree, t)); |
jjg@1455 | 680 | try { |
jjg@1455 | 681 | return super.visitLink(tree, ignore); |
jjg@1455 | 682 | } finally { |
jjg@1455 | 683 | tagStack.pop(); |
jjg@1455 | 684 | } |
jjg@1455 | 685 | } |
jjg@1455 | 686 | |
jjg@1455 | 687 | @Override |
jjg@1499 | 688 | public Void visitLiteral(LiteralTree tree, Void ignore) { |
jjg@1650 | 689 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
jjg@1499 | 690 | if (tree.getKind() == DocTree.Kind.CODE) { |
jjg@1499 | 691 | for (TagStackItem tsi: tagStack) { |
jjg@1499 | 692 | if (tsi.tag == HtmlTag.CODE) { |
jjg@1502 | 693 | env.messages.warning(HTML, tree, "dc.tag.code.within.code"); |
jjg@1499 | 694 | break; |
jjg@1499 | 695 | } |
jjg@1499 | 696 | } |
jjg@1499 | 697 | } |
jjg@1499 | 698 | return super.visitLiteral(tree, ignore); |
jjg@1499 | 699 | } |
jjg@1499 | 700 | |
jjg@1499 | 701 | @Override |
jlahoda@1726 | 702 | @SuppressWarnings("fallthrough") |
jjg@1455 | 703 | public Void visitParam(ParamTree tree, Void ignore) { |
jjg@1455 | 704 | boolean typaram = tree.isTypeParameter(); |
jjg@1455 | 705 | IdentifierTree nameTree = tree.getName(); |
jlahoda@1726 | 706 | Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null; |
jlahoda@1726 | 707 | |
jlahoda@1726 | 708 | if (paramElement == null) { |
jlahoda@1726 | 709 | switch (env.currElement.getKind()) { |
jlahoda@1726 | 710 | case CLASS: case INTERFACE: { |
jlahoda@1726 | 711 | if (!typaram) { |
jlahoda@1726 | 712 | env.messages.error(REFERENCE, tree, "dc.invalid.param"); |
jlahoda@1726 | 713 | break; |
jlahoda@1726 | 714 | } |
jlahoda@1726 | 715 | } |
jlahoda@1726 | 716 | case METHOD: case CONSTRUCTOR: { |
jlahoda@1726 | 717 | env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found"); |
jlahoda@1726 | 718 | break; |
jlahoda@1726 | 719 | } |
jlahoda@1726 | 720 | |
jlahoda@1726 | 721 | default: |
jlahoda@1726 | 722 | env.messages.error(REFERENCE, tree, "dc.invalid.param"); |
jlahoda@1726 | 723 | break; |
jjg@1455 | 724 | } |
jlahoda@1726 | 725 | } else { |
jlahoda@1726 | 726 | foundParams.add(paramElement); |
jlahoda@1726 | 727 | } |
jjg@1455 | 728 | |
jjg@1455 | 729 | warnIfEmpty(tree, tree.getDescription()); |
jjg@1455 | 730 | return super.visitParam(tree, ignore); |
jjg@1455 | 731 | } |
jjg@1455 | 732 | |
jjg@1455 | 733 | private void checkParamsDocumented(List<? extends Element> list) { |
jjg@1455 | 734 | if (foundInheritDoc) |
jjg@1455 | 735 | return; |
jjg@1455 | 736 | |
jjg@1455 | 737 | for (Element e: list) { |
jjg@1455 | 738 | if (!foundParams.contains(e)) { |
jjg@1455 | 739 | CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER) |
jjg@1455 | 740 | ? "<" + e.getSimpleName() + ">" |
jjg@1455 | 741 | : e.getSimpleName(); |
jjg@1455 | 742 | reportMissing("dc.missing.param", paramName); |
jjg@1455 | 743 | } |
jjg@1455 | 744 | } |
jjg@1455 | 745 | } |
jjg@1455 | 746 | |
jjg@1455 | 747 | @Override |
jjg@1455 | 748 | public Void visitReference(ReferenceTree tree, Void ignore) { |
jjg@2054 | 749 | String sig = tree.getSignature(); |
jjg@2054 | 750 | if (sig.contains("<") || sig.contains(">")) |
jjg@2054 | 751 | env.messages.error(REFERENCE, tree, "dc.type.arg.not.allowed"); |
jjg@2054 | 752 | |
jlahoda@1726 | 753 | Element e = env.trees.getElement(getCurrentPath()); |
jjg@1455 | 754 | if (e == null) |
jjg@1455 | 755 | env.messages.error(REFERENCE, tree, "dc.ref.not.found"); |
jjg@1455 | 756 | return super.visitReference(tree, ignore); |
jjg@1455 | 757 | } |
jjg@1455 | 758 | |
jjg@1455 | 759 | @Override |
jjg@1455 | 760 | public Void visitReturn(ReturnTree tree, Void ignore) { |
jjg@1455 | 761 | Element e = env.trees.getElement(env.currPath); |
jjg@1455 | 762 | if (e.getKind() != ElementKind.METHOD |
jjg@1455 | 763 | || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID) |
jjg@1455 | 764 | env.messages.error(REFERENCE, tree, "dc.invalid.return"); |
jjg@1455 | 765 | foundReturn = true; |
jjg@1455 | 766 | warnIfEmpty(tree, tree.getDescription()); |
jjg@1455 | 767 | return super.visitReturn(tree, ignore); |
jjg@1455 | 768 | } |
jjg@1455 | 769 | |
jjg@1455 | 770 | @Override |
jjg@1455 | 771 | public Void visitSerialData(SerialDataTree tree, Void ignore) { |
jjg@1455 | 772 | warnIfEmpty(tree, tree.getDescription()); |
jjg@1455 | 773 | return super.visitSerialData(tree, ignore); |
jjg@1455 | 774 | } |
jjg@1455 | 775 | |
jjg@1455 | 776 | @Override |
jjg@1455 | 777 | public Void visitSerialField(SerialFieldTree tree, Void ignore) { |
jjg@1455 | 778 | warnIfEmpty(tree, tree.getDescription()); |
jjg@1455 | 779 | return super.visitSerialField(tree, ignore); |
jjg@1455 | 780 | } |
jjg@1455 | 781 | |
jjg@1455 | 782 | @Override |
jjg@1455 | 783 | public Void visitSince(SinceTree tree, Void ignore) { |
jjg@1455 | 784 | warnIfEmpty(tree, tree.getBody()); |
jjg@1455 | 785 | return super.visitSince(tree, ignore); |
jjg@1455 | 786 | } |
jjg@1455 | 787 | |
jjg@1455 | 788 | @Override |
jjg@1455 | 789 | public Void visitThrows(ThrowsTree tree, Void ignore) { |
jjg@1455 | 790 | ReferenceTree exName = tree.getExceptionName(); |
jlahoda@1726 | 791 | Element ex = env.trees.getElement(new DocTreePath(getCurrentPath(), exName)); |
jjg@1455 | 792 | if (ex == null) { |
jjg@1455 | 793 | env.messages.error(REFERENCE, tree, "dc.ref.not.found"); |
jjg@1917 | 794 | } else if (isThrowable(ex.asType())) { |
jjg@1455 | 795 | switch (env.currElement.getKind()) { |
jjg@1455 | 796 | case CONSTRUCTOR: |
jjg@1455 | 797 | case METHOD: |
jjg@1455 | 798 | if (isCheckedException(ex.asType())) { |
jjg@1455 | 799 | ExecutableElement ee = (ExecutableElement) env.currElement; |
jjg@1455 | 800 | checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes()); |
jjg@1455 | 801 | } |
jjg@1455 | 802 | break; |
jjg@1455 | 803 | default: |
jjg@1455 | 804 | env.messages.error(REFERENCE, tree, "dc.invalid.throws"); |
jjg@1455 | 805 | } |
jjg@1455 | 806 | } else { |
jjg@1455 | 807 | env.messages.error(REFERENCE, tree, "dc.invalid.throws"); |
jjg@1455 | 808 | } |
jjg@1455 | 809 | warnIfEmpty(tree, tree.getDescription()); |
jjg@1455 | 810 | return scan(tree.getDescription(), ignore); |
jjg@1455 | 811 | } |
jjg@1455 | 812 | |
jjg@1917 | 813 | private boolean isThrowable(TypeMirror tm) { |
jjg@1917 | 814 | switch (tm.getKind()) { |
jjg@1917 | 815 | case DECLARED: |
jjg@1917 | 816 | case TYPEVAR: |
jjg@1917 | 817 | return env.types.isAssignable(tm, env.java_lang_Throwable); |
jjg@1917 | 818 | } |
jjg@1917 | 819 | return false; |
jjg@1917 | 820 | } |
jjg@1917 | 821 | |
jjg@1455 | 822 | private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list) { |
jjg@1455 | 823 | boolean found = false; |
jjg@1455 | 824 | for (TypeMirror tl : list) { |
jjg@1455 | 825 | if (env.types.isAssignable(t, tl)) { |
jjg@1455 | 826 | foundThrows.add(tl); |
jjg@1455 | 827 | found = true; |
jjg@1455 | 828 | } |
jjg@1455 | 829 | } |
jjg@1455 | 830 | if (!found) |
jjg@1455 | 831 | env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t); |
jjg@1455 | 832 | } |
jjg@1455 | 833 | |
jjg@1455 | 834 | private void checkThrowsDocumented(List<? extends TypeMirror> list) { |
jjg@1455 | 835 | if (foundInheritDoc) |
jjg@1455 | 836 | return; |
jjg@1455 | 837 | |
jjg@1455 | 838 | for (TypeMirror tl: list) { |
jjg@1455 | 839 | if (isCheckedException(tl) && !foundThrows.contains(tl)) |
jjg@1455 | 840 | reportMissing("dc.missing.throws", tl); |
jjg@1455 | 841 | } |
jjg@1455 | 842 | } |
jjg@1455 | 843 | |
jjg@1455 | 844 | @Override |
jjg@1650 | 845 | public Void visitValue(ValueTree tree, Void ignore) { |
jjg@2053 | 846 | ReferenceTree ref = tree.getReference(); |
jjg@2053 | 847 | if (ref == null || ref.getSignature().isEmpty()) { |
jjg@2053 | 848 | if (!isConstant(env.currElement)) |
jjg@2053 | 849 | env.messages.error(REFERENCE, tree, "dc.value.not.allowed.here"); |
jjg@2053 | 850 | } else { |
jjg@2053 | 851 | Element e = env.trees.getElement(new DocTreePath(getCurrentPath(), ref)); |
jjg@2053 | 852 | if (!isConstant(e)) |
jjg@2053 | 853 | env.messages.error(REFERENCE, tree, "dc.value.not.a.constant"); |
jjg@2053 | 854 | } |
jjg@2053 | 855 | |
jjg@1650 | 856 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
jjg@1650 | 857 | return super.visitValue(tree, ignore); |
jjg@1650 | 858 | } |
jjg@1650 | 859 | |
jjg@2053 | 860 | private boolean isConstant(Element e) { |
jjg@2053 | 861 | if (e == null) |
jjg@2053 | 862 | return false; |
jjg@2053 | 863 | |
jjg@2053 | 864 | switch (e.getKind()) { |
jjg@2053 | 865 | case FIELD: |
jjg@2053 | 866 | Object value = ((VariableElement) e).getConstantValue(); |
jjg@2053 | 867 | return (value != null); // can't distinguish "not a constant" from "constant is null" |
jjg@2053 | 868 | default: |
jjg@2053 | 869 | return false; |
jjg@2053 | 870 | } |
jjg@2053 | 871 | } |
jjg@2053 | 872 | |
jjg@1650 | 873 | @Override |
jjg@1455 | 874 | public Void visitVersion(VersionTree tree, Void ignore) { |
jjg@1455 | 875 | warnIfEmpty(tree, tree.getBody()); |
jjg@1455 | 876 | return super.visitVersion(tree, ignore); |
jjg@1455 | 877 | } |
jjg@1455 | 878 | |
jjg@1455 | 879 | @Override |
jjg@1455 | 880 | public Void visitErroneous(ErroneousTree tree, Void ignore) { |
jjg@1455 | 881 | env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null)); |
jjg@1455 | 882 | return null; |
jjg@1455 | 883 | } |
jjg@1455 | 884 | // </editor-fold> |
jjg@1455 | 885 | |
jjg@1455 | 886 | // <editor-fold defaultstate="collapsed" desc="Utility methods"> |
jjg@1455 | 887 | |
jjg@1455 | 888 | private boolean isCheckedException(TypeMirror t) { |
jjg@1455 | 889 | return !(env.types.isAssignable(t, env.java_lang_Error) |
jjg@1455 | 890 | || env.types.isAssignable(t, env.java_lang_RuntimeException)); |
jjg@1455 | 891 | } |
jjg@1455 | 892 | |
jjg@1455 | 893 | private boolean isSynthetic() { |
jjg@1455 | 894 | switch (env.currElement.getKind()) { |
jjg@1455 | 895 | case CONSTRUCTOR: |
jjg@1455 | 896 | // A synthetic default constructor has the same pos as the |
jjg@1455 | 897 | // enclosing class |
jjg@1455 | 898 | TreePath p = env.currPath; |
jjg@1455 | 899 | return env.getPos(p) == env.getPos(p.getParentPath()); |
jjg@1455 | 900 | } |
jjg@1455 | 901 | return false; |
jjg@1455 | 902 | } |
jjg@1455 | 903 | |
jjg@1455 | 904 | void markEnclosingTag(Flag flag) { |
jjg@1455 | 905 | TagStackItem top = tagStack.peek(); |
jjg@1455 | 906 | if (top != null) |
jjg@1455 | 907 | top.flags.add(flag); |
jjg@1455 | 908 | } |
jjg@1455 | 909 | |
jjg@1455 | 910 | String toString(TreePath p) { |
jjg@1455 | 911 | StringBuilder sb = new StringBuilder("TreePath["); |
jjg@1455 | 912 | toString(p, sb); |
jjg@1455 | 913 | sb.append("]"); |
jjg@1455 | 914 | return sb.toString(); |
jjg@1455 | 915 | } |
jjg@1455 | 916 | |
jjg@1455 | 917 | void toString(TreePath p, StringBuilder sb) { |
jjg@1455 | 918 | TreePath parent = p.getParentPath(); |
jjg@1455 | 919 | if (parent != null) { |
jjg@1455 | 920 | toString(parent, sb); |
jjg@1455 | 921 | sb.append(","); |
jjg@1455 | 922 | } |
jjg@1455 | 923 | sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p)); |
jjg@1455 | 924 | } |
jjg@1455 | 925 | |
jjg@1455 | 926 | void warnIfEmpty(DocTree tree, List<? extends DocTree> list) { |
jjg@1455 | 927 | for (DocTree d: list) { |
jjg@1455 | 928 | switch (d.getKind()) { |
jjg@1455 | 929 | case TEXT: |
jjg@1507 | 930 | if (hasNonWhitespace((TextTree) d)) |
jjg@1455 | 931 | return; |
jjg@1455 | 932 | break; |
jjg@1455 | 933 | default: |
jjg@1455 | 934 | return; |
jjg@1455 | 935 | } |
jjg@1455 | 936 | } |
jjg@1455 | 937 | env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName); |
jjg@1455 | 938 | } |
jjg@1507 | 939 | |
jjg@1507 | 940 | boolean hasNonWhitespace(TextTree tree) { |
jjg@1507 | 941 | String s = tree.getBody(); |
jjg@1507 | 942 | for (int i = 0; i < s.length(); i++) { |
jjg@1507 | 943 | if (!Character.isWhitespace(s.charAt(i))) |
jjg@1507 | 944 | return true; |
jjg@1507 | 945 | } |
jjg@1507 | 946 | return false; |
jjg@1507 | 947 | } |
jjg@1507 | 948 | |
jjg@1455 | 949 | // </editor-fold> |
jjg@1455 | 950 | |
jjg@1455 | 951 | } |