Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 2013, 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 | |
aoqi@0 | 129 | private 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: |
aoqi@0 | 404 | env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName); |
aoqi@0 | 405 | return; |
aoqi@0 | 406 | } |
aoqi@0 | 407 | |
aoqi@0 | 408 | env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName); |
aoqi@0 | 409 | } |
aoqi@0 | 410 | |
aoqi@0 | 411 | private void checkHeader(StartElementTree tree, HtmlTag tag) { |
aoqi@0 | 412 | // verify the new tag |
aoqi@0 | 413 | if (getHeaderLevel(tag) > getHeaderLevel(currHeaderTag) + 1) { |
aoqi@0 | 414 | if (currHeaderTag == null) { |
aoqi@0 | 415 | env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.1", tag); |
aoqi@0 | 416 | } else { |
aoqi@0 | 417 | env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.2", |
aoqi@0 | 418 | tag, currHeaderTag); |
aoqi@0 | 419 | } |
aoqi@0 | 420 | } |
aoqi@0 | 421 | |
aoqi@0 | 422 | currHeaderTag = tag; |
aoqi@0 | 423 | } |
aoqi@0 | 424 | |
aoqi@0 | 425 | private int getHeaderLevel(HtmlTag tag) { |
aoqi@0 | 426 | if (tag == null) |
aoqi@0 | 427 | return implicitHeaderLevel; |
aoqi@0 | 428 | switch (tag) { |
aoqi@0 | 429 | case H1: return 1; |
aoqi@0 | 430 | case H2: return 2; |
aoqi@0 | 431 | case H3: return 3; |
aoqi@0 | 432 | case H4: return 4; |
aoqi@0 | 433 | case H5: return 5; |
aoqi@0 | 434 | case H6: return 6; |
aoqi@0 | 435 | default: throw new IllegalArgumentException(); |
aoqi@0 | 436 | } |
aoqi@0 | 437 | } |
aoqi@0 | 438 | |
aoqi@0 | 439 | @Override |
aoqi@0 | 440 | public Void visitEndElement(EndElementTree tree, Void ignore) { |
aoqi@0 | 441 | final Name treeName = tree.getName(); |
aoqi@0 | 442 | final HtmlTag t = HtmlTag.get(treeName); |
aoqi@0 | 443 | if (t == null) { |
aoqi@0 | 444 | env.messages.error(HTML, tree, "dc.tag.unknown", treeName); |
aoqi@0 | 445 | } else if (t.endKind == HtmlTag.EndKind.NONE) { |
aoqi@0 | 446 | env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName); |
aoqi@0 | 447 | } else { |
aoqi@0 | 448 | boolean done = false; |
aoqi@0 | 449 | while (!tagStack.isEmpty()) { |
aoqi@0 | 450 | TagStackItem top = tagStack.peek(); |
aoqi@0 | 451 | if (t == top.tag) { |
aoqi@0 | 452 | switch (t) { |
aoqi@0 | 453 | case TABLE: |
aoqi@0 | 454 | if (!top.attrs.contains(HtmlTag.Attr.SUMMARY) |
aoqi@0 | 455 | && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) { |
aoqi@0 | 456 | env.messages.error(ACCESSIBILITY, tree, |
aoqi@0 | 457 | "dc.no.summary.or.caption.for.table"); |
aoqi@0 | 458 | } |
aoqi@0 | 459 | } |
aoqi@0 | 460 | warnIfEmpty(top, tree); |
aoqi@0 | 461 | tagStack.pop(); |
aoqi@0 | 462 | done = true; |
aoqi@0 | 463 | break; |
aoqi@0 | 464 | } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) { |
aoqi@0 | 465 | tagStack.pop(); |
aoqi@0 | 466 | } else { |
aoqi@0 | 467 | boolean found = false; |
aoqi@0 | 468 | for (TagStackItem si: tagStack) { |
aoqi@0 | 469 | if (si.tag == t) { |
aoqi@0 | 470 | found = true; |
aoqi@0 | 471 | break; |
aoqi@0 | 472 | } |
aoqi@0 | 473 | } |
aoqi@0 | 474 | if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) { |
aoqi@0 | 475 | env.messages.error(HTML, top.tree, "dc.tag.start.unmatched", |
aoqi@0 | 476 | ((StartElementTree) top.tree).getName()); |
aoqi@0 | 477 | tagStack.pop(); |
aoqi@0 | 478 | } else { |
aoqi@0 | 479 | env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); |
aoqi@0 | 480 | done = true; |
aoqi@0 | 481 | break; |
aoqi@0 | 482 | } |
aoqi@0 | 483 | } |
aoqi@0 | 484 | } |
aoqi@0 | 485 | |
aoqi@0 | 486 | if (!done && tagStack.isEmpty()) { |
aoqi@0 | 487 | env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName); |
aoqi@0 | 488 | } |
aoqi@0 | 489 | } |
aoqi@0 | 490 | |
aoqi@0 | 491 | return super.visitEndElement(tree, ignore); |
aoqi@0 | 492 | } |
aoqi@0 | 493 | |
aoqi@0 | 494 | void warnIfEmpty(TagStackItem tsi, DocTree endTree) { |
aoqi@0 | 495 | if (tsi.tag != null && tsi.tree instanceof StartElementTree) { |
aoqi@0 | 496 | if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT) |
aoqi@0 | 497 | && !tsi.flags.contains(Flag.HAS_TEXT) |
aoqi@0 | 498 | && !tsi.flags.contains(Flag.HAS_ELEMENT) |
aoqi@0 | 499 | && !tsi.flags.contains(Flag.HAS_INLINE_TAG)) { |
aoqi@0 | 500 | DocTree tree = (endTree != null) ? endTree : tsi.tree; |
aoqi@0 | 501 | Name treeName = ((StartElementTree) tsi.tree).getName(); |
aoqi@0 | 502 | env.messages.warning(HTML, tree, "dc.tag.empty", treeName); |
aoqi@0 | 503 | } |
aoqi@0 | 504 | } |
aoqi@0 | 505 | } |
aoqi@0 | 506 | |
aoqi@0 | 507 | // </editor-fold> |
aoqi@0 | 508 | |
aoqi@0 | 509 | // <editor-fold defaultstate="collapsed" desc="HTML attributes"> |
aoqi@0 | 510 | |
aoqi@0 | 511 | @Override @SuppressWarnings("fallthrough") |
aoqi@0 | 512 | public Void visitAttribute(AttributeTree tree, Void ignore) { |
aoqi@0 | 513 | HtmlTag currTag = tagStack.peek().tag; |
aoqi@0 | 514 | if (currTag != null) { |
aoqi@0 | 515 | Name name = tree.getName(); |
aoqi@0 | 516 | HtmlTag.Attr attr = currTag.getAttr(name); |
aoqi@0 | 517 | if (attr != null) { |
aoqi@0 | 518 | boolean first = tagStack.peek().attrs.add(attr); |
aoqi@0 | 519 | if (!first) |
aoqi@0 | 520 | env.messages.error(HTML, tree, "dc.attr.repeated", name); |
aoqi@0 | 521 | } |
aoqi@0 | 522 | AttrKind k = currTag.getAttrKind(name); |
aoqi@0 | 523 | switch (k) { |
aoqi@0 | 524 | case OK: |
aoqi@0 | 525 | break; |
aoqi@0 | 526 | |
aoqi@0 | 527 | case INVALID: |
aoqi@0 | 528 | env.messages.error(HTML, tree, "dc.attr.unknown", name); |
aoqi@0 | 529 | break; |
aoqi@0 | 530 | |
aoqi@0 | 531 | case OBSOLETE: |
aoqi@0 | 532 | env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete", name); |
aoqi@0 | 533 | break; |
aoqi@0 | 534 | |
aoqi@0 | 535 | case USE_CSS: |
aoqi@0 | 536 | env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete.use.css", name); |
aoqi@0 | 537 | break; |
aoqi@0 | 538 | } |
aoqi@0 | 539 | |
aoqi@0 | 540 | if (attr != null) { |
aoqi@0 | 541 | switch (attr) { |
aoqi@0 | 542 | case NAME: |
aoqi@0 | 543 | if (currTag != HtmlTag.A) { |
aoqi@0 | 544 | break; |
aoqi@0 | 545 | } |
aoqi@0 | 546 | // fallthrough |
aoqi@0 | 547 | case ID: |
aoqi@0 | 548 | String value = getAttrValue(tree); |
aoqi@0 | 549 | if (value == null) { |
aoqi@0 | 550 | env.messages.error(HTML, tree, "dc.anchor.value.missing"); |
aoqi@0 | 551 | } else { |
aoqi@0 | 552 | if (!validName.matcher(value).matches()) { |
aoqi@0 | 553 | env.messages.error(HTML, tree, "dc.invalid.anchor", value); |
aoqi@0 | 554 | } |
aoqi@0 | 555 | if (!checkAnchor(value)) { |
aoqi@0 | 556 | env.messages.error(HTML, tree, "dc.anchor.already.defined", value); |
aoqi@0 | 557 | } |
aoqi@0 | 558 | } |
aoqi@0 | 559 | break; |
aoqi@0 | 560 | |
aoqi@0 | 561 | case HREF: |
aoqi@0 | 562 | if (currTag == HtmlTag.A) { |
aoqi@0 | 563 | String v = getAttrValue(tree); |
aoqi@0 | 564 | if (v == null || v.isEmpty()) { |
aoqi@0 | 565 | env.messages.error(HTML, tree, "dc.attr.lacks.value"); |
aoqi@0 | 566 | } else { |
aoqi@0 | 567 | Matcher m = docRoot.matcher(v); |
aoqi@0 | 568 | if (m.matches()) { |
aoqi@0 | 569 | String rest = m.group(2); |
aoqi@0 | 570 | if (!rest.isEmpty()) |
aoqi@0 | 571 | checkURI(tree, rest); |
aoqi@0 | 572 | } else { |
aoqi@0 | 573 | checkURI(tree, v); |
aoqi@0 | 574 | } |
aoqi@0 | 575 | } |
aoqi@0 | 576 | } |
aoqi@0 | 577 | break; |
aoqi@0 | 578 | |
aoqi@0 | 579 | case VALUE: |
aoqi@0 | 580 | if (currTag == HtmlTag.LI) { |
aoqi@0 | 581 | String v = getAttrValue(tree); |
aoqi@0 | 582 | if (v == null || v.isEmpty()) { |
aoqi@0 | 583 | env.messages.error(HTML, tree, "dc.attr.lacks.value"); |
aoqi@0 | 584 | } else if (!validNumber.matcher(v).matches()) { |
aoqi@0 | 585 | env.messages.error(HTML, tree, "dc.attr.not.number"); |
aoqi@0 | 586 | } |
aoqi@0 | 587 | } |
aoqi@0 | 588 | break; |
aoqi@0 | 589 | } |
aoqi@0 | 590 | } |
aoqi@0 | 591 | } |
aoqi@0 | 592 | |
aoqi@0 | 593 | // TODO: basic check on value |
aoqi@0 | 594 | |
aoqi@0 | 595 | return super.visitAttribute(tree, ignore); |
aoqi@0 | 596 | } |
aoqi@0 | 597 | |
aoqi@0 | 598 | private boolean checkAnchor(String name) { |
aoqi@0 | 599 | Element e = getEnclosingPackageOrClass(env.currElement); |
aoqi@0 | 600 | if (e == null) |
aoqi@0 | 601 | return true; |
aoqi@0 | 602 | Set<String> set = foundAnchors.get(e); |
aoqi@0 | 603 | if (set == null) |
aoqi@0 | 604 | foundAnchors.put(e, set = new HashSet<>()); |
aoqi@0 | 605 | return set.add(name); |
aoqi@0 | 606 | } |
aoqi@0 | 607 | |
aoqi@0 | 608 | private Element getEnclosingPackageOrClass(Element e) { |
aoqi@0 | 609 | while (e != null) { |
aoqi@0 | 610 | switch (e.getKind()) { |
aoqi@0 | 611 | case CLASS: |
aoqi@0 | 612 | case ENUM: |
aoqi@0 | 613 | case INTERFACE: |
aoqi@0 | 614 | case PACKAGE: |
aoqi@0 | 615 | return e; |
aoqi@0 | 616 | default: |
aoqi@0 | 617 | e = e.getEnclosingElement(); |
aoqi@0 | 618 | } |
aoqi@0 | 619 | } |
aoqi@0 | 620 | return e; |
aoqi@0 | 621 | } |
aoqi@0 | 622 | |
aoqi@0 | 623 | // http://www.w3.org/TR/html401/types.html#type-name |
aoqi@0 | 624 | private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*"); |
aoqi@0 | 625 | |
aoqi@0 | 626 | private static final Pattern validNumber = Pattern.compile("-?[0-9]+"); |
aoqi@0 | 627 | |
aoqi@0 | 628 | // pattern to remove leading {@docRoot}/? |
aoqi@0 | 629 | private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)"); |
aoqi@0 | 630 | |
aoqi@0 | 631 | private String getAttrValue(AttributeTree tree) { |
aoqi@0 | 632 | if (tree.getValue() == null) |
aoqi@0 | 633 | return null; |
aoqi@0 | 634 | |
aoqi@0 | 635 | StringWriter sw = new StringWriter(); |
aoqi@0 | 636 | try { |
aoqi@0 | 637 | new DocPretty(sw).print(tree.getValue()); |
aoqi@0 | 638 | } catch (IOException e) { |
aoqi@0 | 639 | // cannot happen |
aoqi@0 | 640 | } |
aoqi@0 | 641 | // ignore potential use of entities for now |
aoqi@0 | 642 | return sw.toString(); |
aoqi@0 | 643 | } |
aoqi@0 | 644 | |
aoqi@0 | 645 | private void checkURI(AttributeTree tree, String uri) { |
aoqi@0 | 646 | try { |
aoqi@0 | 647 | URI u = new URI(uri); |
aoqi@0 | 648 | } catch (URISyntaxException e) { |
aoqi@0 | 649 | env.messages.error(HTML, tree, "dc.invalid.uri", uri); |
aoqi@0 | 650 | } |
aoqi@0 | 651 | } |
aoqi@0 | 652 | // </editor-fold> |
aoqi@0 | 653 | |
aoqi@0 | 654 | // <editor-fold defaultstate="collapsed" desc="javadoc tags"> |
aoqi@0 | 655 | |
aoqi@0 | 656 | @Override |
aoqi@0 | 657 | public Void visitAuthor(AuthorTree tree, Void ignore) { |
aoqi@0 | 658 | warnIfEmpty(tree, tree.getName()); |
aoqi@0 | 659 | return super.visitAuthor(tree, ignore); |
aoqi@0 | 660 | } |
aoqi@0 | 661 | |
aoqi@0 | 662 | @Override |
aoqi@0 | 663 | public Void visitDocRoot(DocRootTree tree, Void ignore) { |
aoqi@0 | 664 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
aoqi@0 | 665 | return super.visitDocRoot(tree, ignore); |
aoqi@0 | 666 | } |
aoqi@0 | 667 | |
aoqi@0 | 668 | @Override |
aoqi@0 | 669 | public Void visitInheritDoc(InheritDocTree tree, Void ignore) { |
aoqi@0 | 670 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
aoqi@0 | 671 | // TODO: verify on overridden method |
aoqi@0 | 672 | foundInheritDoc = true; |
aoqi@0 | 673 | return super.visitInheritDoc(tree, ignore); |
aoqi@0 | 674 | } |
aoqi@0 | 675 | |
aoqi@0 | 676 | @Override |
aoqi@0 | 677 | public Void visitLink(LinkTree tree, Void ignore) { |
aoqi@0 | 678 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
aoqi@0 | 679 | // simulate inline context on tag stack |
aoqi@0 | 680 | HtmlTag t = (tree.getKind() == DocTree.Kind.LINK) |
aoqi@0 | 681 | ? HtmlTag.CODE : HtmlTag.SPAN; |
aoqi@0 | 682 | tagStack.push(new TagStackItem(tree, t)); |
aoqi@0 | 683 | try { |
aoqi@0 | 684 | return super.visitLink(tree, ignore); |
aoqi@0 | 685 | } finally { |
aoqi@0 | 686 | tagStack.pop(); |
aoqi@0 | 687 | } |
aoqi@0 | 688 | } |
aoqi@0 | 689 | |
aoqi@0 | 690 | @Override |
aoqi@0 | 691 | public Void visitLiteral(LiteralTree tree, Void ignore) { |
aoqi@0 | 692 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
aoqi@0 | 693 | if (tree.getKind() == DocTree.Kind.CODE) { |
aoqi@0 | 694 | for (TagStackItem tsi: tagStack) { |
aoqi@0 | 695 | if (tsi.tag == HtmlTag.CODE) { |
aoqi@0 | 696 | env.messages.warning(HTML, tree, "dc.tag.code.within.code"); |
aoqi@0 | 697 | break; |
aoqi@0 | 698 | } |
aoqi@0 | 699 | } |
aoqi@0 | 700 | } |
aoqi@0 | 701 | return super.visitLiteral(tree, ignore); |
aoqi@0 | 702 | } |
aoqi@0 | 703 | |
aoqi@0 | 704 | @Override |
aoqi@0 | 705 | @SuppressWarnings("fallthrough") |
aoqi@0 | 706 | public Void visitParam(ParamTree tree, Void ignore) { |
aoqi@0 | 707 | boolean typaram = tree.isTypeParameter(); |
aoqi@0 | 708 | IdentifierTree nameTree = tree.getName(); |
aoqi@0 | 709 | Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null; |
aoqi@0 | 710 | |
aoqi@0 | 711 | if (paramElement == null) { |
aoqi@0 | 712 | switch (env.currElement.getKind()) { |
aoqi@0 | 713 | case CLASS: case INTERFACE: { |
aoqi@0 | 714 | if (!typaram) { |
aoqi@0 | 715 | env.messages.error(REFERENCE, tree, "dc.invalid.param"); |
aoqi@0 | 716 | break; |
aoqi@0 | 717 | } |
aoqi@0 | 718 | } |
aoqi@0 | 719 | case METHOD: case CONSTRUCTOR: { |
aoqi@0 | 720 | env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found"); |
aoqi@0 | 721 | break; |
aoqi@0 | 722 | } |
aoqi@0 | 723 | |
aoqi@0 | 724 | default: |
aoqi@0 | 725 | env.messages.error(REFERENCE, tree, "dc.invalid.param"); |
aoqi@0 | 726 | break; |
aoqi@0 | 727 | } |
aoqi@0 | 728 | } else { |
aoqi@0 | 729 | foundParams.add(paramElement); |
aoqi@0 | 730 | } |
aoqi@0 | 731 | |
aoqi@0 | 732 | warnIfEmpty(tree, tree.getDescription()); |
aoqi@0 | 733 | return super.visitParam(tree, ignore); |
aoqi@0 | 734 | } |
aoqi@0 | 735 | |
aoqi@0 | 736 | private void checkParamsDocumented(List<? extends Element> list) { |
aoqi@0 | 737 | if (foundInheritDoc) |
aoqi@0 | 738 | return; |
aoqi@0 | 739 | |
aoqi@0 | 740 | for (Element e: list) { |
aoqi@0 | 741 | if (!foundParams.contains(e)) { |
aoqi@0 | 742 | CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER) |
aoqi@0 | 743 | ? "<" + e.getSimpleName() + ">" |
aoqi@0 | 744 | : e.getSimpleName(); |
aoqi@0 | 745 | reportMissing("dc.missing.param", paramName); |
aoqi@0 | 746 | } |
aoqi@0 | 747 | } |
aoqi@0 | 748 | } |
aoqi@0 | 749 | |
aoqi@0 | 750 | @Override |
aoqi@0 | 751 | public Void visitReference(ReferenceTree tree, Void ignore) { |
aoqi@0 | 752 | String sig = tree.getSignature(); |
aoqi@0 | 753 | if (sig.contains("<") || sig.contains(">")) |
aoqi@0 | 754 | env.messages.error(REFERENCE, tree, "dc.type.arg.not.allowed"); |
aoqi@0 | 755 | |
aoqi@0 | 756 | Element e = env.trees.getElement(getCurrentPath()); |
aoqi@0 | 757 | if (e == null) |
aoqi@0 | 758 | env.messages.error(REFERENCE, tree, "dc.ref.not.found"); |
aoqi@0 | 759 | return super.visitReference(tree, ignore); |
aoqi@0 | 760 | } |
aoqi@0 | 761 | |
aoqi@0 | 762 | @Override |
aoqi@0 | 763 | public Void visitReturn(ReturnTree tree, Void ignore) { |
aoqi@0 | 764 | Element e = env.trees.getElement(env.currPath); |
aoqi@0 | 765 | if (e.getKind() != ElementKind.METHOD |
aoqi@0 | 766 | || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID) |
aoqi@0 | 767 | env.messages.error(REFERENCE, tree, "dc.invalid.return"); |
aoqi@0 | 768 | foundReturn = true; |
aoqi@0 | 769 | warnIfEmpty(tree, tree.getDescription()); |
aoqi@0 | 770 | return super.visitReturn(tree, ignore); |
aoqi@0 | 771 | } |
aoqi@0 | 772 | |
aoqi@0 | 773 | @Override |
aoqi@0 | 774 | public Void visitSerialData(SerialDataTree tree, Void ignore) { |
aoqi@0 | 775 | warnIfEmpty(tree, tree.getDescription()); |
aoqi@0 | 776 | return super.visitSerialData(tree, ignore); |
aoqi@0 | 777 | } |
aoqi@0 | 778 | |
aoqi@0 | 779 | @Override |
aoqi@0 | 780 | public Void visitSerialField(SerialFieldTree tree, Void ignore) { |
aoqi@0 | 781 | warnIfEmpty(tree, tree.getDescription()); |
aoqi@0 | 782 | return super.visitSerialField(tree, ignore); |
aoqi@0 | 783 | } |
aoqi@0 | 784 | |
aoqi@0 | 785 | @Override |
aoqi@0 | 786 | public Void visitSince(SinceTree tree, Void ignore) { |
aoqi@0 | 787 | warnIfEmpty(tree, tree.getBody()); |
aoqi@0 | 788 | return super.visitSince(tree, ignore); |
aoqi@0 | 789 | } |
aoqi@0 | 790 | |
aoqi@0 | 791 | @Override |
aoqi@0 | 792 | public Void visitThrows(ThrowsTree tree, Void ignore) { |
aoqi@0 | 793 | ReferenceTree exName = tree.getExceptionName(); |
aoqi@0 | 794 | Element ex = env.trees.getElement(new DocTreePath(getCurrentPath(), exName)); |
aoqi@0 | 795 | if (ex == null) { |
aoqi@0 | 796 | env.messages.error(REFERENCE, tree, "dc.ref.not.found"); |
aoqi@0 | 797 | } else if (isThrowable(ex.asType())) { |
aoqi@0 | 798 | switch (env.currElement.getKind()) { |
aoqi@0 | 799 | case CONSTRUCTOR: |
aoqi@0 | 800 | case METHOD: |
aoqi@0 | 801 | if (isCheckedException(ex.asType())) { |
aoqi@0 | 802 | ExecutableElement ee = (ExecutableElement) env.currElement; |
aoqi@0 | 803 | checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes()); |
aoqi@0 | 804 | } |
aoqi@0 | 805 | break; |
aoqi@0 | 806 | default: |
aoqi@0 | 807 | env.messages.error(REFERENCE, tree, "dc.invalid.throws"); |
aoqi@0 | 808 | } |
aoqi@0 | 809 | } else { |
aoqi@0 | 810 | env.messages.error(REFERENCE, tree, "dc.invalid.throws"); |
aoqi@0 | 811 | } |
aoqi@0 | 812 | warnIfEmpty(tree, tree.getDescription()); |
aoqi@0 | 813 | return scan(tree.getDescription(), ignore); |
aoqi@0 | 814 | } |
aoqi@0 | 815 | |
aoqi@0 | 816 | private boolean isThrowable(TypeMirror tm) { |
aoqi@0 | 817 | switch (tm.getKind()) { |
aoqi@0 | 818 | case DECLARED: |
aoqi@0 | 819 | case TYPEVAR: |
aoqi@0 | 820 | return env.types.isAssignable(tm, env.java_lang_Throwable); |
aoqi@0 | 821 | } |
aoqi@0 | 822 | return false; |
aoqi@0 | 823 | } |
aoqi@0 | 824 | |
aoqi@0 | 825 | private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list) { |
aoqi@0 | 826 | boolean found = false; |
aoqi@0 | 827 | for (TypeMirror tl : list) { |
aoqi@0 | 828 | if (env.types.isAssignable(t, tl)) { |
aoqi@0 | 829 | foundThrows.add(tl); |
aoqi@0 | 830 | found = true; |
aoqi@0 | 831 | } |
aoqi@0 | 832 | } |
aoqi@0 | 833 | if (!found) |
aoqi@0 | 834 | env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t); |
aoqi@0 | 835 | } |
aoqi@0 | 836 | |
aoqi@0 | 837 | private void checkThrowsDocumented(List<? extends TypeMirror> list) { |
aoqi@0 | 838 | if (foundInheritDoc) |
aoqi@0 | 839 | return; |
aoqi@0 | 840 | |
aoqi@0 | 841 | for (TypeMirror tl: list) { |
aoqi@0 | 842 | if (isCheckedException(tl) && !foundThrows.contains(tl)) |
aoqi@0 | 843 | reportMissing("dc.missing.throws", tl); |
aoqi@0 | 844 | } |
aoqi@0 | 845 | } |
aoqi@0 | 846 | |
aoqi@0 | 847 | @Override |
aoqi@0 | 848 | public Void visitUnknownBlockTag(UnknownBlockTagTree tree, Void ignore) { |
aoqi@0 | 849 | checkUnknownTag(tree, tree.getTagName()); |
aoqi@0 | 850 | return super.visitUnknownBlockTag(tree, ignore); |
aoqi@0 | 851 | } |
aoqi@0 | 852 | |
aoqi@0 | 853 | @Override |
aoqi@0 | 854 | public Void visitUnknownInlineTag(UnknownInlineTagTree tree, Void ignore) { |
aoqi@0 | 855 | checkUnknownTag(tree, tree.getTagName()); |
aoqi@0 | 856 | return super.visitUnknownInlineTag(tree, ignore); |
aoqi@0 | 857 | } |
aoqi@0 | 858 | |
aoqi@0 | 859 | private void checkUnknownTag(DocTree tree, String tagName) { |
aoqi@0 | 860 | if (env.customTags != null && !env.customTags.contains(tagName)) |
aoqi@0 | 861 | env.messages.error(SYNTAX, tree, "dc.tag.unknown", tagName); |
aoqi@0 | 862 | } |
aoqi@0 | 863 | |
aoqi@0 | 864 | @Override |
aoqi@0 | 865 | public Void visitValue(ValueTree tree, Void ignore) { |
aoqi@0 | 866 | ReferenceTree ref = tree.getReference(); |
aoqi@0 | 867 | if (ref == null || ref.getSignature().isEmpty()) { |
aoqi@0 | 868 | if (!isConstant(env.currElement)) |
aoqi@0 | 869 | env.messages.error(REFERENCE, tree, "dc.value.not.allowed.here"); |
aoqi@0 | 870 | } else { |
aoqi@0 | 871 | Element e = env.trees.getElement(new DocTreePath(getCurrentPath(), ref)); |
aoqi@0 | 872 | if (!isConstant(e)) |
aoqi@0 | 873 | env.messages.error(REFERENCE, tree, "dc.value.not.a.constant"); |
aoqi@0 | 874 | } |
aoqi@0 | 875 | |
aoqi@0 | 876 | markEnclosingTag(Flag.HAS_INLINE_TAG); |
aoqi@0 | 877 | return super.visitValue(tree, ignore); |
aoqi@0 | 878 | } |
aoqi@0 | 879 | |
aoqi@0 | 880 | private boolean isConstant(Element e) { |
aoqi@0 | 881 | if (e == null) |
aoqi@0 | 882 | return false; |
aoqi@0 | 883 | |
aoqi@0 | 884 | switch (e.getKind()) { |
aoqi@0 | 885 | case FIELD: |
aoqi@0 | 886 | Object value = ((VariableElement) e).getConstantValue(); |
aoqi@0 | 887 | return (value != null); // can't distinguish "not a constant" from "constant is null" |
aoqi@0 | 888 | default: |
aoqi@0 | 889 | return false; |
aoqi@0 | 890 | } |
aoqi@0 | 891 | } |
aoqi@0 | 892 | |
aoqi@0 | 893 | @Override |
aoqi@0 | 894 | public Void visitVersion(VersionTree tree, Void ignore) { |
aoqi@0 | 895 | warnIfEmpty(tree, tree.getBody()); |
aoqi@0 | 896 | return super.visitVersion(tree, ignore); |
aoqi@0 | 897 | } |
aoqi@0 | 898 | |
aoqi@0 | 899 | @Override |
aoqi@0 | 900 | public Void visitErroneous(ErroneousTree tree, Void ignore) { |
aoqi@0 | 901 | env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null)); |
aoqi@0 | 902 | return null; |
aoqi@0 | 903 | } |
aoqi@0 | 904 | // </editor-fold> |
aoqi@0 | 905 | |
aoqi@0 | 906 | // <editor-fold defaultstate="collapsed" desc="Utility methods"> |
aoqi@0 | 907 | |
aoqi@0 | 908 | private boolean isCheckedException(TypeMirror t) { |
aoqi@0 | 909 | return !(env.types.isAssignable(t, env.java_lang_Error) |
aoqi@0 | 910 | || env.types.isAssignable(t, env.java_lang_RuntimeException)); |
aoqi@0 | 911 | } |
aoqi@0 | 912 | |
aoqi@0 | 913 | private boolean isSynthetic() { |
aoqi@0 | 914 | switch (env.currElement.getKind()) { |
aoqi@0 | 915 | case CONSTRUCTOR: |
aoqi@0 | 916 | // A synthetic default constructor has the same pos as the |
aoqi@0 | 917 | // enclosing class |
aoqi@0 | 918 | TreePath p = env.currPath; |
aoqi@0 | 919 | return env.getPos(p) == env.getPos(p.getParentPath()); |
aoqi@0 | 920 | } |
aoqi@0 | 921 | return false; |
aoqi@0 | 922 | } |
aoqi@0 | 923 | |
aoqi@0 | 924 | void markEnclosingTag(Flag flag) { |
aoqi@0 | 925 | TagStackItem top = tagStack.peek(); |
aoqi@0 | 926 | if (top != null) |
aoqi@0 | 927 | top.flags.add(flag); |
aoqi@0 | 928 | } |
aoqi@0 | 929 | |
aoqi@0 | 930 | String toString(TreePath p) { |
aoqi@0 | 931 | StringBuilder sb = new StringBuilder("TreePath["); |
aoqi@0 | 932 | toString(p, sb); |
aoqi@0 | 933 | sb.append("]"); |
aoqi@0 | 934 | return sb.toString(); |
aoqi@0 | 935 | } |
aoqi@0 | 936 | |
aoqi@0 | 937 | void toString(TreePath p, StringBuilder sb) { |
aoqi@0 | 938 | TreePath parent = p.getParentPath(); |
aoqi@0 | 939 | if (parent != null) { |
aoqi@0 | 940 | toString(parent, sb); |
aoqi@0 | 941 | sb.append(","); |
aoqi@0 | 942 | } |
aoqi@0 | 943 | sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p)); |
aoqi@0 | 944 | } |
aoqi@0 | 945 | |
aoqi@0 | 946 | void warnIfEmpty(DocTree tree, List<? extends DocTree> list) { |
aoqi@0 | 947 | for (DocTree d: list) { |
aoqi@0 | 948 | switch (d.getKind()) { |
aoqi@0 | 949 | case TEXT: |
aoqi@0 | 950 | if (hasNonWhitespace((TextTree) d)) |
aoqi@0 | 951 | return; |
aoqi@0 | 952 | break; |
aoqi@0 | 953 | default: |
aoqi@0 | 954 | return; |
aoqi@0 | 955 | } |
aoqi@0 | 956 | } |
aoqi@0 | 957 | env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName); |
aoqi@0 | 958 | } |
aoqi@0 | 959 | |
aoqi@0 | 960 | boolean hasNonWhitespace(TextTree tree) { |
aoqi@0 | 961 | String s = tree.getBody(); |
aoqi@0 | 962 | for (int i = 0; i < s.length(); i++) { |
aoqi@0 | 963 | if (!Character.isWhitespace(s.charAt(i))) |
aoqi@0 | 964 | return true; |
aoqi@0 | 965 | } |
aoqi@0 | 966 | return false; |
aoqi@0 | 967 | } |
aoqi@0 | 968 | |
aoqi@0 | 969 | // </editor-fold> |
aoqi@0 | 970 | |
aoqi@0 | 971 | } |