src/share/classes/com/sun/tools/doclint/Checker.java

Sun, 16 Dec 2012 11:09:36 +0100

author
jfranck
date
Sun, 16 Dec 2012 11:09:36 +0100
changeset 1464
f72c9c5aeaef
parent 1455
75ab654b5cd5
child 1495
bc1023e0e533
permissions
-rw-r--r--

8005098: Provide isSynthesized() information on Attribute.Compound
Reviewed-by: jjg

jjg@1455 1 /*
jjg@1455 2 * Copyright (c) 2012, 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@1455 28 import java.util.regex.Matcher;
jjg@1455 29 import com.sun.source.doctree.LinkTree;
jjg@1455 30 import java.net.URI;
jjg@1455 31 import java.util.regex.Pattern;
jjg@1455 32 import java.io.IOException;
jjg@1455 33 import com.sun.tools.javac.tree.DocPretty;
jjg@1455 34 import java.io.StringWriter;
jjg@1455 35 import java.util.Deque;
jjg@1455 36 import java.util.EnumSet;
jjg@1455 37 import java.util.HashSet;
jjg@1455 38 import java.util.LinkedList;
jjg@1455 39 import java.util.List;
jjg@1455 40 import java.util.Set;
jjg@1455 41
jjg@1455 42 import javax.lang.model.element.Element;
jjg@1455 43 import javax.lang.model.element.ElementKind;
jjg@1455 44 import javax.lang.model.element.ExecutableElement;
jjg@1455 45 import javax.lang.model.element.Name;
jjg@1455 46 import javax.lang.model.element.TypeElement;
jjg@1455 47 import javax.lang.model.type.TypeKind;
jjg@1455 48 import javax.lang.model.type.TypeMirror;
jjg@1455 49 import javax.tools.Diagnostic.Kind;
jjg@1455 50
jjg@1455 51 import com.sun.source.doctree.AttributeTree;
jjg@1455 52 import com.sun.source.doctree.AuthorTree;
jjg@1455 53 import com.sun.source.doctree.DocCommentTree;
jjg@1455 54 import com.sun.source.doctree.DocTree;
jjg@1455 55 import com.sun.source.doctree.EndElementTree;
jjg@1455 56 import com.sun.source.doctree.EntityTree;
jjg@1455 57 import com.sun.source.doctree.ErroneousTree;
jjg@1455 58 import com.sun.source.doctree.IdentifierTree;
jjg@1455 59 import com.sun.source.doctree.InheritDocTree;
jjg@1455 60 import com.sun.source.doctree.ParamTree;
jjg@1455 61 import com.sun.source.doctree.ReferenceTree;
jjg@1455 62 import com.sun.source.doctree.ReturnTree;
jjg@1455 63 import com.sun.source.doctree.SerialDataTree;
jjg@1455 64 import com.sun.source.doctree.SerialFieldTree;
jjg@1455 65 import com.sun.source.doctree.SinceTree;
jjg@1455 66 import com.sun.source.doctree.StartElementTree;
jjg@1455 67 import com.sun.source.doctree.TextTree;
jjg@1455 68 import com.sun.source.doctree.ThrowsTree;
jjg@1455 69 import com.sun.source.doctree.VersionTree;
jjg@1455 70 import com.sun.source.util.DocTreeScanner;
jjg@1455 71 import com.sun.source.util.TreePath;
jjg@1455 72 import com.sun.tools.doclint.HtmlTag.AttrKind;
jjg@1455 73 import java.net.URISyntaxException;
jjg@1455 74 import static com.sun.tools.doclint.Messages.Group.*;
jjg@1455 75
jjg@1455 76
jjg@1455 77 /**
jjg@1455 78 * Validate a doc comment.
jjg@1455 79 *
jjg@1455 80 * <p><b>This is NOT part of any supported API.
jjg@1455 81 * If you write code that depends on this, you do so at your own
jjg@1455 82 * risk. This code and its internal interfaces are subject to change
jjg@1455 83 * or deletion without notice.</b></p>
jjg@1455 84 */
jjg@1455 85 public class Checker extends DocTreeScanner<Void, Void> {
jjg@1455 86 final Env env;
jjg@1455 87
jjg@1455 88 Set<Element> foundParams = new HashSet<Element>();
jjg@1455 89 Set<TypeMirror> foundThrows = new HashSet<TypeMirror>();
jjg@1455 90 Set<String> foundAnchors = new HashSet<String>();
jjg@1455 91 boolean foundInheritDoc = false;
jjg@1455 92 boolean foundReturn = false;
jjg@1455 93
jjg@1455 94 enum Flag {
jjg@1455 95 TABLE_HAS_CAPTION,
jjg@1455 96 HAS_ELEMENT,
jjg@1455 97 HAS_TEXT
jjg@1455 98 }
jjg@1455 99
jjg@1455 100 static class TagStackItem {
jjg@1455 101 final DocTree tree; // typically, but not always, StartElementTree
jjg@1455 102 final HtmlTag tag;
jjg@1455 103 final Set<HtmlTag.Attr> attrs;
jjg@1455 104 final Set<Flag> flags;
jjg@1455 105 TagStackItem(DocTree tree, HtmlTag tag) {
jjg@1455 106 this.tree = tree;
jjg@1455 107 this.tag = tag;
jjg@1455 108 attrs = EnumSet.noneOf(HtmlTag.Attr.class);
jjg@1455 109 flags = EnumSet.noneOf(Flag.class);
jjg@1455 110 }
jjg@1455 111 @Override
jjg@1455 112 public String toString() {
jjg@1455 113 return String.valueOf(tag);
jjg@1455 114 }
jjg@1455 115 }
jjg@1455 116
jjg@1455 117 private Deque<TagStackItem> tagStack; // TODO: maybe want to record starting tree as well
jjg@1455 118 private HtmlTag currHeaderTag;
jjg@1455 119
jjg@1455 120 // <editor-fold defaultstate="collapsed" desc="Top level">
jjg@1455 121
jjg@1455 122 Checker(Env env) {
jjg@1455 123 env.getClass();
jjg@1455 124 this.env = env;
jjg@1455 125 tagStack = new LinkedList<TagStackItem>();
jjg@1455 126 }
jjg@1455 127
jjg@1455 128 public Void scan(DocCommentTree tree, TreePath p) {
jjg@1455 129 env.setCurrent(p, tree);
jjg@1455 130
jjg@1455 131 boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();
jjg@1455 132
jjg@1455 133 if (tree == null) {
jjg@1455 134 if (!isSynthetic() && !isOverridingMethod)
jjg@1455 135 reportMissing("dc.missing.comment");
jjg@1455 136 return null;
jjg@1455 137 }
jjg@1455 138
jjg@1455 139 tagStack.clear();
jjg@1455 140 currHeaderTag = null;
jjg@1455 141
jjg@1455 142 foundParams.clear();
jjg@1455 143 foundThrows.clear();
jjg@1455 144 foundInheritDoc = false;
jjg@1455 145 foundReturn = false;
jjg@1455 146
jjg@1455 147 scan(tree, (Void) null);
jjg@1455 148
jjg@1455 149 if (!isOverridingMethod) {
jjg@1455 150 switch (env.currElement.getKind()) {
jjg@1455 151 case METHOD:
jjg@1455 152 case CONSTRUCTOR: {
jjg@1455 153 ExecutableElement ee = (ExecutableElement) env.currElement;
jjg@1455 154 checkParamsDocumented(ee.getTypeParameters());
jjg@1455 155 checkParamsDocumented(ee.getParameters());
jjg@1455 156 switch (ee.getReturnType().getKind()) {
jjg@1455 157 case VOID:
jjg@1455 158 case NONE:
jjg@1455 159 break;
jjg@1455 160 default:
jjg@1455 161 if (!foundReturn
jjg@1455 162 && !foundInheritDoc
jjg@1455 163 && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) {
jjg@1455 164 reportMissing("dc.missing.return");
jjg@1455 165 }
jjg@1455 166 }
jjg@1455 167 checkThrowsDocumented(ee.getThrownTypes());
jjg@1455 168 }
jjg@1455 169 }
jjg@1455 170 }
jjg@1455 171
jjg@1455 172 return null;
jjg@1455 173 }
jjg@1455 174
jjg@1455 175 private void reportMissing(String code, Object... args) {
jjg@1455 176 env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args);
jjg@1455 177 }
jjg@1455 178
jjg@1455 179 @Override
jjg@1455 180 public Void visitDocComment(DocCommentTree tree, Void ignore) {
jjg@1455 181 super.visitDocComment(tree, ignore);
jjg@1455 182 for (TagStackItem tsi: tagStack) {
jjg@1455 183 if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT
jjg@1455 184 && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) {
jjg@1455 185 StartElementTree t = (StartElementTree) tsi.tree;
jjg@1455 186 env.messages.error(HTML, t, "dc.tag.not.closed", t.getName());
jjg@1455 187 }
jjg@1455 188 }
jjg@1455 189 return null;
jjg@1455 190 }
jjg@1455 191 // </editor-fold>
jjg@1455 192
jjg@1455 193 // <editor-fold defaultstate="collapsed" desc="Text and entities.">
jjg@1455 194
jjg@1455 195 @Override
jjg@1455 196 public Void visitText(TextTree tree, Void ignore) {
jjg@1455 197 if (!tree.getBody().trim().isEmpty()) {
jjg@1455 198 markEnclosingTag(Flag.HAS_TEXT);
jjg@1455 199 }
jjg@1455 200 return null;
jjg@1455 201 }
jjg@1455 202
jjg@1455 203 @Override
jjg@1455 204 public Void visitEntity(EntityTree tree, Void ignore) {
jjg@1455 205 markEnclosingTag(Flag.HAS_TEXT);
jjg@1455 206 String name = tree.getName().toString();
jjg@1455 207 if (name.startsWith("#")) {
jjg@1455 208 int v = name.toLowerCase().startsWith("#x")
jjg@1455 209 ? Integer.parseInt(name.substring(2), 16)
jjg@1455 210 : Integer.parseInt(name.substring(1), 10);
jjg@1455 211 if (!Entity.isValid(v)) {
jjg@1455 212 env.messages.error(HTML, tree, "dc.entity.invalid", name);
jjg@1455 213 }
jjg@1455 214 } else if (!Entity.isValid(name)) {
jjg@1455 215 env.messages.error(HTML, tree, "dc.entity.invalid", name);
jjg@1455 216 }
jjg@1455 217 return null;
jjg@1455 218 }
jjg@1455 219
jjg@1455 220 // </editor-fold>
jjg@1455 221
jjg@1455 222 // <editor-fold defaultstate="collapsed" desc="HTML elements">
jjg@1455 223
jjg@1455 224 @Override
jjg@1455 225 public Void visitStartElement(StartElementTree tree, Void ignore) {
jjg@1455 226 markEnclosingTag(Flag.HAS_ELEMENT);
jjg@1455 227 final Name treeName = tree.getName();
jjg@1455 228 final HtmlTag t = HtmlTag.get(treeName);
jjg@1455 229 if (t == null) {
jjg@1455 230 env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
jjg@1455 231 } else {
jjg@1455 232 // tag specific checks
jjg@1455 233 switch (t) {
jjg@1455 234 // check for out of sequence headers, such as <h1>...</h1> <h3>...</h3>
jjg@1455 235 case H1: case H2: case H3: case H4: case H5: case H6:
jjg@1455 236 checkHeader(tree, t);
jjg@1455 237 break;
jjg@1455 238 // <p> inside <pre>
jjg@1455 239 case P:
jjg@1455 240 TagStackItem top = tagStack.peek();
jjg@1455 241 if (top != null && top.tag == HtmlTag.PRE)
jjg@1455 242 env.messages.warning(HTML, tree, "dc.tag.p.in.pre");
jjg@1455 243 break;
jjg@1455 244 }
jjg@1455 245
jjg@1455 246 // check that only block tags and inline tags are used,
jjg@1455 247 // and that blocks tags are not used within inline tags
jjg@1455 248 switch (t.blockType) {
jjg@1455 249 case INLINE:
jjg@1455 250 break;
jjg@1455 251 case BLOCK:
jjg@1455 252 TagStackItem top = tagStack.peek();
jjg@1455 253 if (top != null && top.tag != null && top.tag.blockType == HtmlTag.BlockType.INLINE) {
jjg@1455 254 switch (top.tree.getKind()) {
jjg@1455 255 case START_ELEMENT: {
jjg@1455 256 Name name = ((StartElementTree) top.tree).getName();
jjg@1455 257 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element",
jjg@1455 258 treeName, name);
jjg@1455 259 break;
jjg@1455 260 }
jjg@1455 261 case LINK:
jjg@1455 262 case LINK_PLAIN: {
jjg@1455 263 String name = top.tree.getKind().tagName;
jjg@1455 264 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag",
jjg@1455 265 treeName, name);
jjg@1455 266 break;
jjg@1455 267 }
jjg@1455 268 default:
jjg@1455 269 env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.other",
jjg@1455 270 treeName);
jjg@1455 271 }
jjg@1455 272 }
jjg@1455 273 break;
jjg@1455 274 case OTHER:
jjg@1455 275 env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName);
jjg@1455 276 break;
jjg@1455 277 default:
jjg@1455 278 throw new AssertionError();
jjg@1455 279 }
jjg@1455 280
jjg@1455 281 if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
jjg@1455 282 for (TagStackItem i: tagStack) {
jjg@1455 283 if (t == i.tag) {
jjg@1455 284 env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
jjg@1455 285 break;
jjg@1455 286 }
jjg@1455 287 }
jjg@1455 288 }
jjg@1455 289 }
jjg@1455 290
jjg@1455 291 // check for self closing tags, such as <a id="name"/>
jjg@1455 292 if (tree.isSelfClosing()) {
jjg@1455 293 env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
jjg@1455 294 }
jjg@1455 295
jjg@1455 296 try {
jjg@1455 297 TagStackItem parent = tagStack.peek();
jjg@1455 298 TagStackItem top = new TagStackItem(tree, t);
jjg@1455 299 tagStack.push(top);
jjg@1455 300
jjg@1455 301 super.visitStartElement(tree, ignore);
jjg@1455 302
jjg@1455 303 // handle attributes that may or may not have been found in start element
jjg@1455 304 if (t != null) {
jjg@1455 305 switch (t) {
jjg@1455 306 case CAPTION:
jjg@1455 307 if (parent != null && parent.tag == HtmlTag.TABLE)
jjg@1455 308 parent.flags.add(Flag.TABLE_HAS_CAPTION);
jjg@1455 309 break;
jjg@1455 310
jjg@1455 311 case IMG:
jjg@1455 312 if (!top.attrs.contains(HtmlTag.Attr.ALT))
jjg@1455 313 env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
jjg@1455 314 break;
jjg@1455 315 }
jjg@1455 316 }
jjg@1455 317
jjg@1455 318 return null;
jjg@1455 319 } finally {
jjg@1455 320
jjg@1455 321 if (t == null || t.endKind == HtmlTag.EndKind.NONE)
jjg@1455 322 tagStack.pop();
jjg@1455 323 }
jjg@1455 324 }
jjg@1455 325
jjg@1455 326 private void checkHeader(StartElementTree tree, HtmlTag tag) {
jjg@1455 327 // verify the new tag
jjg@1455 328 if (getHeaderLevel(tag) > getHeaderLevel(currHeaderTag) + 1) {
jjg@1455 329 if (currHeaderTag == null) {
jjg@1455 330 env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.1", tag);
jjg@1455 331 } else {
jjg@1455 332 env.messages.error(ACCESSIBILITY, tree, "dc.tag.header.sequence.2",
jjg@1455 333 tag, currHeaderTag);
jjg@1455 334 }
jjg@1455 335 }
jjg@1455 336
jjg@1455 337 currHeaderTag = tag;
jjg@1455 338 }
jjg@1455 339
jjg@1455 340 private int getHeaderLevel(HtmlTag tag) {
jjg@1455 341 if (tag == null)
jjg@1455 342 return 0;
jjg@1455 343 switch (tag) {
jjg@1455 344 case H1: return 1;
jjg@1455 345 case H2: return 2;
jjg@1455 346 case H3: return 3;
jjg@1455 347 case H4: return 4;
jjg@1455 348 case H5: return 5;
jjg@1455 349 case H6: return 6;
jjg@1455 350 default: throw new IllegalArgumentException();
jjg@1455 351 }
jjg@1455 352 }
jjg@1455 353
jjg@1455 354 @Override
jjg@1455 355 public Void visitEndElement(EndElementTree tree, Void ignore) {
jjg@1455 356 final Name treeName = tree.getName();
jjg@1455 357 final HtmlTag t = HtmlTag.get(treeName);
jjg@1455 358 if (t == null) {
jjg@1455 359 env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
jjg@1455 360 } else if (t.endKind == HtmlTag.EndKind.NONE) {
jjg@1455 361 env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName);
jjg@1455 362 } else if (tagStack.isEmpty()) {
jjg@1455 363 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
jjg@1455 364 } else {
jjg@1455 365 while (!tagStack.isEmpty()) {
jjg@1455 366 TagStackItem top = tagStack.peek();
jjg@1455 367 if (t == top.tag) {
jjg@1455 368 switch (t) {
jjg@1455 369 case TABLE:
jjg@1455 370 if (!top.attrs.contains(HtmlTag.Attr.SUMMARY)
jjg@1455 371 && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) {
jjg@1455 372 env.messages.error(ACCESSIBILITY, tree,
jjg@1455 373 "dc.no.summary.or.caption.for.table");
jjg@1455 374 }
jjg@1455 375 }
jjg@1455 376 if (t.flags.contains(HtmlTag.Flag.EXPECT_CONTENT)
jjg@1455 377 && !top.flags.contains(Flag.HAS_TEXT)
jjg@1455 378 && !top.flags.contains(Flag.HAS_ELEMENT)) {
jjg@1455 379 env.messages.warning(HTML, tree, "dc.tag.empty", treeName);
jjg@1455 380 }
jjg@1455 381 if (t.flags.contains(HtmlTag.Flag.NO_TEXT)
jjg@1455 382 && top.flags.contains(Flag.HAS_TEXT)) {
jjg@1455 383 env.messages.error(HTML, tree, "dc.text.not.allowed", treeName);
jjg@1455 384 }
jjg@1455 385 tagStack.pop();
jjg@1455 386 break;
jjg@1455 387 } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) {
jjg@1455 388 tagStack.pop();
jjg@1455 389 } else {
jjg@1455 390 boolean found = false;
jjg@1455 391 for (TagStackItem si: tagStack) {
jjg@1455 392 if (si.tag == t) {
jjg@1455 393 found = true;
jjg@1455 394 break;
jjg@1455 395 }
jjg@1455 396 }
jjg@1455 397 if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) {
jjg@1455 398 env.messages.error(HTML, top.tree, "dc.tag.start.unmatched",
jjg@1455 399 ((StartElementTree) top.tree).getName());
jjg@1455 400 tagStack.pop();
jjg@1455 401 } else {
jjg@1455 402 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
jjg@1455 403 break;
jjg@1455 404 }
jjg@1455 405 }
jjg@1455 406 }
jjg@1455 407 }
jjg@1455 408
jjg@1455 409 return super.visitEndElement(tree, ignore);
jjg@1455 410 }
jjg@1455 411 // </editor-fold>
jjg@1455 412
jjg@1455 413 // <editor-fold defaultstate="collapsed" desc="HTML attributes">
jjg@1455 414
jjg@1455 415 @Override @SuppressWarnings("fallthrough")
jjg@1455 416 public Void visitAttribute(AttributeTree tree, Void ignore) {
jjg@1455 417 HtmlTag currTag = tagStack.peek().tag;
jjg@1455 418 if (currTag != null) {
jjg@1455 419 Name name = tree.getName();
jjg@1455 420 HtmlTag.Attr attr = currTag.getAttr(name);
jjg@1455 421 if (attr != null) {
jjg@1455 422 boolean first = tagStack.peek().attrs.add(attr);
jjg@1455 423 if (!first)
jjg@1455 424 env.messages.error(HTML, tree, "dc.attr.repeated", name);
jjg@1455 425 }
jjg@1455 426 AttrKind k = currTag.getAttrKind(name);
jjg@1455 427 switch (k) {
jjg@1455 428 case OK:
jjg@1455 429 break;
jjg@1455 430
jjg@1455 431 case INVALID:
jjg@1455 432 env.messages.error(HTML, tree, "dc.attr.unknown", name);
jjg@1455 433 break;
jjg@1455 434
jjg@1455 435 case OBSOLETE:
jjg@1455 436 env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete", name);
jjg@1455 437 break;
jjg@1455 438
jjg@1455 439 case USE_CSS:
jjg@1455 440 env.messages.warning(ACCESSIBILITY, tree, "dc.attr.obsolete.use.css", name);
jjg@1455 441 break;
jjg@1455 442 }
jjg@1455 443
jjg@1455 444 if (attr != null) {
jjg@1455 445 switch (attr) {
jjg@1455 446 case NAME:
jjg@1455 447 if (currTag != HtmlTag.A) {
jjg@1455 448 break;
jjg@1455 449 }
jjg@1455 450 // fallthrough
jjg@1455 451 case ID:
jjg@1455 452 String value = getAttrValue(tree);
jjg@1455 453 if (!validName.matcher(value).matches()) {
jjg@1455 454 env.messages.error(HTML, tree, "dc.invalid.anchor", value);
jjg@1455 455 }
jjg@1455 456 if (!foundAnchors.add(value)) {
jjg@1455 457 env.messages.error(HTML, tree, "dc.anchor.already.defined", value);
jjg@1455 458 }
jjg@1455 459 break;
jjg@1455 460
jjg@1455 461 case HREF:
jjg@1455 462 if (currTag == HtmlTag.A) {
jjg@1455 463 String v = getAttrValue(tree);
jjg@1455 464 if (v == null || v.isEmpty()) {
jjg@1455 465 env.messages.error(HTML, tree, "dc.attr.lacks.value");
jjg@1455 466 } else {
jjg@1455 467 Matcher m = docRoot.matcher(v);
jjg@1455 468 if (m.matches()) {
jjg@1455 469 String rest = m.group(2);
jjg@1455 470 if (!rest.isEmpty())
jjg@1455 471 checkURI(tree, rest);
jjg@1455 472 } else {
jjg@1455 473 checkURI(tree, v);
jjg@1455 474 }
jjg@1455 475 }
jjg@1455 476 }
jjg@1455 477 break;
jjg@1455 478 }
jjg@1455 479 }
jjg@1455 480 }
jjg@1455 481
jjg@1455 482 // TODO: basic check on value
jjg@1455 483
jjg@1455 484 return super.visitAttribute(tree, ignore);
jjg@1455 485 }
jjg@1455 486
jjg@1455 487 // http://www.w3.org/TR/html401/types.html#type-name
jjg@1455 488 private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*");
jjg@1455 489
jjg@1455 490 // pattern to remove leading {@docRoot}/?
jjg@1455 491 private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)");
jjg@1455 492
jjg@1455 493 private String getAttrValue(AttributeTree tree) {
jjg@1455 494 if (tree.getValue() == null)
jjg@1455 495 return null;
jjg@1455 496
jjg@1455 497 StringWriter sw = new StringWriter();
jjg@1455 498 try {
jjg@1455 499 new DocPretty(sw).print(tree.getValue());
jjg@1455 500 } catch (IOException e) {
jjg@1455 501 // cannot happen
jjg@1455 502 }
jjg@1455 503 // ignore potential use of entities for now
jjg@1455 504 return sw.toString();
jjg@1455 505 }
jjg@1455 506
jjg@1455 507 private void checkURI(AttributeTree tree, String uri) {
jjg@1455 508 try {
jjg@1455 509 URI u = new URI(uri);
jjg@1455 510 } catch (URISyntaxException e) {
jjg@1455 511 env.messages.error(HTML, tree, "dc.invalid.uri", uri);
jjg@1455 512 }
jjg@1455 513 }
jjg@1455 514 // </editor-fold>
jjg@1455 515
jjg@1455 516 // <editor-fold defaultstate="collapsed" desc="javadoc tags">
jjg@1455 517
jjg@1455 518 @Override
jjg@1455 519 public Void visitAuthor(AuthorTree tree, Void ignore) {
jjg@1455 520 warnIfEmpty(tree, tree.getName());
jjg@1455 521 return super.visitAuthor(tree, ignore);
jjg@1455 522 }
jjg@1455 523
jjg@1455 524 @Override
jjg@1455 525 public Void visitInheritDoc(InheritDocTree tree, Void ignore) {
jjg@1455 526 // TODO: verify on overridden method
jjg@1455 527 foundInheritDoc = true;
jjg@1455 528 return super.visitInheritDoc(tree, ignore);
jjg@1455 529 }
jjg@1455 530
jjg@1455 531 @Override
jjg@1455 532 public Void visitLink(LinkTree tree, Void ignore) {
jjg@1455 533 // simulate inline context on tag stack
jjg@1455 534 HtmlTag t = (tree.getKind() == DocTree.Kind.LINK)
jjg@1455 535 ? HtmlTag.CODE : HtmlTag.SPAN;
jjg@1455 536 tagStack.push(new TagStackItem(tree, t));
jjg@1455 537 try {
jjg@1455 538 return super.visitLink(tree, ignore);
jjg@1455 539 } finally {
jjg@1455 540 tagStack.pop();
jjg@1455 541 }
jjg@1455 542 }
jjg@1455 543
jjg@1455 544 @Override
jjg@1455 545 public Void visitParam(ParamTree tree, Void ignore) {
jjg@1455 546 boolean typaram = tree.isTypeParameter();
jjg@1455 547 IdentifierTree nameTree = tree.getName();
jjg@1455 548 Element e = env.currElement;
jjg@1455 549 switch (e.getKind()) {
jjg@1455 550 case METHOD: case CONSTRUCTOR: {
jjg@1455 551 ExecutableElement ee = (ExecutableElement) e;
jjg@1455 552 checkParamDeclared(nameTree, typaram ? ee.getTypeParameters() : ee.getParameters());
jjg@1455 553 break;
jjg@1455 554 }
jjg@1455 555
jjg@1455 556 case CLASS: case INTERFACE: {
jjg@1455 557 TypeElement te = (TypeElement) e;
jjg@1455 558 if (typaram) {
jjg@1455 559 checkParamDeclared(nameTree, te.getTypeParameters());
jjg@1455 560 } else {
jjg@1455 561 env.messages.error(REFERENCE, tree, "dc.invalid.param");
jjg@1455 562 }
jjg@1455 563 break;
jjg@1455 564 }
jjg@1455 565
jjg@1455 566 default:
jjg@1455 567 env.messages.error(REFERENCE, tree, "dc.invalid.param");
jjg@1455 568 break;
jjg@1455 569 }
jjg@1455 570 warnIfEmpty(tree, tree.getDescription());
jjg@1455 571 return super.visitParam(tree, ignore);
jjg@1455 572 }
jjg@1455 573 // where
jjg@1455 574 private void checkParamDeclared(IdentifierTree nameTree, List<? extends Element> list) {
jjg@1455 575 Name name = nameTree.getName();
jjg@1455 576 boolean found = false;
jjg@1455 577 for (Element e: list) {
jjg@1455 578 if (name.equals(e.getSimpleName())) {
jjg@1455 579 foundParams.add(e);
jjg@1455 580 found = true;
jjg@1455 581 }
jjg@1455 582 }
jjg@1455 583 if (!found)
jjg@1455 584 env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
jjg@1455 585 }
jjg@1455 586
jjg@1455 587 private void checkParamsDocumented(List<? extends Element> list) {
jjg@1455 588 if (foundInheritDoc)
jjg@1455 589 return;
jjg@1455 590
jjg@1455 591 for (Element e: list) {
jjg@1455 592 if (!foundParams.contains(e)) {
jjg@1455 593 CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER)
jjg@1455 594 ? "<" + e.getSimpleName() + ">"
jjg@1455 595 : e.getSimpleName();
jjg@1455 596 reportMissing("dc.missing.param", paramName);
jjg@1455 597 }
jjg@1455 598 }
jjg@1455 599 }
jjg@1455 600
jjg@1455 601 @Override
jjg@1455 602 public Void visitReference(ReferenceTree tree, Void ignore) {
jjg@1455 603 Element e = env.trees.getElement(env.currPath, tree);
jjg@1455 604 if (e == null)
jjg@1455 605 env.messages.error(REFERENCE, tree, "dc.ref.not.found");
jjg@1455 606 return super.visitReference(tree, ignore);
jjg@1455 607 }
jjg@1455 608
jjg@1455 609 @Override
jjg@1455 610 public Void visitReturn(ReturnTree tree, Void ignore) {
jjg@1455 611 Element e = env.trees.getElement(env.currPath);
jjg@1455 612 if (e.getKind() != ElementKind.METHOD
jjg@1455 613 || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID)
jjg@1455 614 env.messages.error(REFERENCE, tree, "dc.invalid.return");
jjg@1455 615 foundReturn = true;
jjg@1455 616 warnIfEmpty(tree, tree.getDescription());
jjg@1455 617 return super.visitReturn(tree, ignore);
jjg@1455 618 }
jjg@1455 619
jjg@1455 620 @Override
jjg@1455 621 public Void visitSerialData(SerialDataTree tree, Void ignore) {
jjg@1455 622 warnIfEmpty(tree, tree.getDescription());
jjg@1455 623 return super.visitSerialData(tree, ignore);
jjg@1455 624 }
jjg@1455 625
jjg@1455 626 @Override
jjg@1455 627 public Void visitSerialField(SerialFieldTree tree, Void ignore) {
jjg@1455 628 warnIfEmpty(tree, tree.getDescription());
jjg@1455 629 return super.visitSerialField(tree, ignore);
jjg@1455 630 }
jjg@1455 631
jjg@1455 632 @Override
jjg@1455 633 public Void visitSince(SinceTree tree, Void ignore) {
jjg@1455 634 warnIfEmpty(tree, tree.getBody());
jjg@1455 635 return super.visitSince(tree, ignore);
jjg@1455 636 }
jjg@1455 637
jjg@1455 638 @Override
jjg@1455 639 public Void visitThrows(ThrowsTree tree, Void ignore) {
jjg@1455 640 ReferenceTree exName = tree.getExceptionName();
jjg@1455 641 Element ex = env.trees.getElement(env.currPath, exName);
jjg@1455 642 if (ex == null) {
jjg@1455 643 env.messages.error(REFERENCE, tree, "dc.ref.not.found");
jjg@1455 644 } else if (ex.asType().getKind() == TypeKind.DECLARED
jjg@1455 645 && env.types.isAssignable(ex.asType(), env.java_lang_Throwable)) {
jjg@1455 646 switch (env.currElement.getKind()) {
jjg@1455 647 case CONSTRUCTOR:
jjg@1455 648 case METHOD:
jjg@1455 649 if (isCheckedException(ex.asType())) {
jjg@1455 650 ExecutableElement ee = (ExecutableElement) env.currElement;
jjg@1455 651 checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes());
jjg@1455 652 }
jjg@1455 653 break;
jjg@1455 654 default:
jjg@1455 655 env.messages.error(REFERENCE, tree, "dc.invalid.throws");
jjg@1455 656 }
jjg@1455 657 } else {
jjg@1455 658 env.messages.error(REFERENCE, tree, "dc.invalid.throws");
jjg@1455 659 }
jjg@1455 660 warnIfEmpty(tree, tree.getDescription());
jjg@1455 661 return scan(tree.getDescription(), ignore);
jjg@1455 662 }
jjg@1455 663
jjg@1455 664 private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list) {
jjg@1455 665 boolean found = false;
jjg@1455 666 for (TypeMirror tl : list) {
jjg@1455 667 if (env.types.isAssignable(t, tl)) {
jjg@1455 668 foundThrows.add(tl);
jjg@1455 669 found = true;
jjg@1455 670 }
jjg@1455 671 }
jjg@1455 672 if (!found)
jjg@1455 673 env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t);
jjg@1455 674 }
jjg@1455 675
jjg@1455 676 private void checkThrowsDocumented(List<? extends TypeMirror> list) {
jjg@1455 677 if (foundInheritDoc)
jjg@1455 678 return;
jjg@1455 679
jjg@1455 680 for (TypeMirror tl: list) {
jjg@1455 681 if (isCheckedException(tl) && !foundThrows.contains(tl))
jjg@1455 682 reportMissing("dc.missing.throws", tl);
jjg@1455 683 }
jjg@1455 684 }
jjg@1455 685
jjg@1455 686 @Override
jjg@1455 687 public Void visitVersion(VersionTree tree, Void ignore) {
jjg@1455 688 warnIfEmpty(tree, tree.getBody());
jjg@1455 689 return super.visitVersion(tree, ignore);
jjg@1455 690 }
jjg@1455 691
jjg@1455 692 @Override
jjg@1455 693 public Void visitErroneous(ErroneousTree tree, Void ignore) {
jjg@1455 694 env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null));
jjg@1455 695 return null;
jjg@1455 696 }
jjg@1455 697 // </editor-fold>
jjg@1455 698
jjg@1455 699 // <editor-fold defaultstate="collapsed" desc="Utility methods">
jjg@1455 700
jjg@1455 701 private boolean isCheckedException(TypeMirror t) {
jjg@1455 702 return !(env.types.isAssignable(t, env.java_lang_Error)
jjg@1455 703 || env.types.isAssignable(t, env.java_lang_RuntimeException));
jjg@1455 704 }
jjg@1455 705
jjg@1455 706 private boolean isSynthetic() {
jjg@1455 707 switch (env.currElement.getKind()) {
jjg@1455 708 case CONSTRUCTOR:
jjg@1455 709 // A synthetic default constructor has the same pos as the
jjg@1455 710 // enclosing class
jjg@1455 711 TreePath p = env.currPath;
jjg@1455 712 return env.getPos(p) == env.getPos(p.getParentPath());
jjg@1455 713 }
jjg@1455 714 return false;
jjg@1455 715 }
jjg@1455 716
jjg@1455 717 void markEnclosingTag(Flag flag) {
jjg@1455 718 TagStackItem top = tagStack.peek();
jjg@1455 719 if (top != null)
jjg@1455 720 top.flags.add(flag);
jjg@1455 721 }
jjg@1455 722
jjg@1455 723 String toString(TreePath p) {
jjg@1455 724 StringBuilder sb = new StringBuilder("TreePath[");
jjg@1455 725 toString(p, sb);
jjg@1455 726 sb.append("]");
jjg@1455 727 return sb.toString();
jjg@1455 728 }
jjg@1455 729
jjg@1455 730 void toString(TreePath p, StringBuilder sb) {
jjg@1455 731 TreePath parent = p.getParentPath();
jjg@1455 732 if (parent != null) {
jjg@1455 733 toString(parent, sb);
jjg@1455 734 sb.append(",");
jjg@1455 735 }
jjg@1455 736 sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p));
jjg@1455 737 }
jjg@1455 738
jjg@1455 739 void warnIfEmpty(DocTree tree, List<? extends DocTree> list) {
jjg@1455 740 for (DocTree d: list) {
jjg@1455 741 switch (d.getKind()) {
jjg@1455 742 case TEXT:
jjg@1455 743 if (!((TextTree) d).getBody().trim().isEmpty())
jjg@1455 744 return;
jjg@1455 745 break;
jjg@1455 746 default:
jjg@1455 747 return;
jjg@1455 748 }
jjg@1455 749 }
jjg@1455 750 env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName);
jjg@1455 751 }
jjg@1455 752 // </editor-fold>
jjg@1455 753
jjg@1455 754 }

mercurial