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