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