Mon, 17 Dec 2012 07:47:05 -0800
8004832: Add new doclint package
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
jfranck@1313 | 2 | * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.code; |
duke@1 | 27 | |
duke@1 | 28 | import java.util.EnumSet; |
duke@1 | 29 | import java.util.HashMap; |
duke@1 | 30 | import java.util.Map; |
vromero@1442 | 31 | import java.util.Set; |
vromero@1442 | 32 | import javax.lang.model.element.Modifier; |
duke@1 | 33 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 34 | import com.sun.tools.javac.util.Context; |
duke@1 | 35 | import com.sun.tools.javac.util.List; |
duke@1 | 36 | import com.sun.tools.javac.util.Options; |
duke@1 | 37 | import com.sun.tools.javac.util.Pair; |
vromero@1442 | 38 | |
duke@1 | 39 | import static com.sun.tools.javac.code.Flags.*; |
duke@1 | 40 | |
duke@1 | 41 | |
duke@1 | 42 | /** |
duke@1 | 43 | * A class for handling -Xlint suboptions and @SuppresssWarnings. |
duke@1 | 44 | * |
jjg@581 | 45 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 46 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 47 | * This code and its internal interfaces are subject to change or |
duke@1 | 48 | * deletion without notice.</b> |
duke@1 | 49 | */ |
duke@1 | 50 | public class Lint |
duke@1 | 51 | { |
duke@1 | 52 | /** The context key for the root Lint object. */ |
duke@1 | 53 | protected static final Context.Key<Lint> lintKey = new Context.Key<Lint>(); |
duke@1 | 54 | |
duke@1 | 55 | /** Get the root Lint instance. */ |
duke@1 | 56 | public static Lint instance(Context context) { |
duke@1 | 57 | Lint instance = context.get(lintKey); |
duke@1 | 58 | if (instance == null) |
duke@1 | 59 | instance = new Lint(context); |
duke@1 | 60 | return instance; |
duke@1 | 61 | } |
duke@1 | 62 | |
duke@1 | 63 | /** |
duke@1 | 64 | * Returns the result of combining the values in this object with |
duke@1 | 65 | * the given annotation. |
duke@1 | 66 | */ |
duke@1 | 67 | public Lint augment(Attribute.Compound attr) { |
duke@1 | 68 | return augmentor.augment(this, attr); |
duke@1 | 69 | } |
duke@1 | 70 | |
duke@1 | 71 | |
duke@1 | 72 | /** |
duke@1 | 73 | * Returns the result of combining the values in this object with |
duke@1 | 74 | * the given annotations. |
duke@1 | 75 | */ |
jfranck@1313 | 76 | public Lint augment(Annotations annots) { |
jfranck@1313 | 77 | return augmentor.augment(this, annots.getAttributes()); |
duke@1 | 78 | } |
duke@1 | 79 | |
duke@1 | 80 | /** |
duke@1 | 81 | * Returns the result of combining the values in this object with |
duke@1 | 82 | * the given annotations and flags. |
duke@1 | 83 | */ |
jfranck@1313 | 84 | public Lint augment(Annotations annots, long flags) { |
jfranck@1313 | 85 | Lint l = augmentor.augment(this, annots.getAttributes()); |
duke@1 | 86 | if ((flags & DEPRECATED) != 0) { |
duke@1 | 87 | if (l == this) |
duke@1 | 88 | l = new Lint(this); |
duke@1 | 89 | l.values.remove(LintCategory.DEPRECATION); |
duke@1 | 90 | l.suppressedValues.add(LintCategory.DEPRECATION); |
duke@1 | 91 | } |
duke@1 | 92 | return l; |
duke@1 | 93 | } |
duke@1 | 94 | |
duke@1 | 95 | |
duke@1 | 96 | private final AugmentVisitor augmentor; |
duke@1 | 97 | |
duke@1 | 98 | private final EnumSet<LintCategory> values; |
duke@1 | 99 | private final EnumSet<LintCategory> suppressedValues; |
duke@1 | 100 | |
vromero@1442 | 101 | private static final Map<String, LintCategory> map = |
vromero@1442 | 102 | new java.util.concurrent.ConcurrentHashMap<String, LintCategory>(20); |
duke@1 | 103 | |
duke@1 | 104 | |
duke@1 | 105 | protected Lint(Context context) { |
duke@1 | 106 | // initialize values according to the lint options |
duke@1 | 107 | Options options = Options.instance(context); |
duke@1 | 108 | values = EnumSet.noneOf(LintCategory.class); |
duke@1 | 109 | for (Map.Entry<String, LintCategory> e: map.entrySet()) { |
duke@1 | 110 | if (options.lint(e.getKey())) |
duke@1 | 111 | values.add(e.getValue()); |
duke@1 | 112 | } |
duke@1 | 113 | |
duke@1 | 114 | suppressedValues = EnumSet.noneOf(LintCategory.class); |
duke@1 | 115 | |
duke@1 | 116 | context.put(lintKey, this); |
duke@1 | 117 | augmentor = new AugmentVisitor(context); |
duke@1 | 118 | } |
duke@1 | 119 | |
duke@1 | 120 | protected Lint(Lint other) { |
duke@1 | 121 | this.augmentor = other.augmentor; |
duke@1 | 122 | this.values = other.values.clone(); |
duke@1 | 123 | this.suppressedValues = other.suppressedValues.clone(); |
duke@1 | 124 | } |
duke@1 | 125 | |
jjg@612 | 126 | @Override |
duke@1 | 127 | public String toString() { |
duke@1 | 128 | return "Lint:[values" + values + " suppressedValues" + suppressedValues + "]"; |
duke@1 | 129 | } |
duke@1 | 130 | |
duke@1 | 131 | /** |
duke@1 | 132 | * Categories of warnings that can be generated by the compiler. |
duke@1 | 133 | */ |
duke@1 | 134 | public enum LintCategory { |
duke@1 | 135 | /** |
ohrstrom@1384 | 136 | * Warn when code refers to a auxiliary class that is hidden in a source file (ie source file name is |
ohrstrom@1384 | 137 | * different from the class name, and the type is not properly nested) and the referring code |
ohrstrom@1384 | 138 | * is not located in the same source file. |
ohrstrom@1384 | 139 | */ |
ohrstrom@1384 | 140 | AUXILIARYCLASS("auxiliaryclass"), |
ohrstrom@1384 | 141 | |
ohrstrom@1384 | 142 | /** |
duke@1 | 143 | * Warn about use of unnecessary casts. |
duke@1 | 144 | */ |
duke@1 | 145 | CAST("cast"), |
duke@1 | 146 | |
duke@1 | 147 | /** |
jjg@776 | 148 | * Warn about issues related to classfile contents |
jjg@776 | 149 | */ |
jjg@776 | 150 | CLASSFILE("classfile"), |
jjg@776 | 151 | |
jjg@776 | 152 | /** |
duke@1 | 153 | * Warn about use of deprecated items. |
duke@1 | 154 | */ |
duke@1 | 155 | DEPRECATION("deprecation"), |
duke@1 | 156 | |
duke@1 | 157 | /** |
duke@1 | 158 | * Warn about items which are documented with an {@code @deprecated} JavaDoc |
duke@1 | 159 | * comment, but which do not have {@code @Deprecated} annotation. |
duke@1 | 160 | */ |
duke@1 | 161 | DEP_ANN("dep-ann"), |
duke@1 | 162 | |
duke@1 | 163 | /** |
duke@1 | 164 | * Warn about division by constant integer 0. |
duke@1 | 165 | */ |
duke@1 | 166 | DIVZERO("divzero"), |
duke@1 | 167 | |
duke@1 | 168 | /** |
duke@1 | 169 | * Warn about empty statement after if. |
duke@1 | 170 | */ |
duke@1 | 171 | EMPTY("empty"), |
duke@1 | 172 | |
duke@1 | 173 | /** |
duke@1 | 174 | * Warn about falling through from one case of a switch statement to the next. |
duke@1 | 175 | */ |
duke@1 | 176 | FALLTHROUGH("fallthrough"), |
duke@1 | 177 | |
duke@1 | 178 | /** |
duke@1 | 179 | * Warn about finally clauses that do not terminate normally. |
duke@1 | 180 | */ |
duke@1 | 181 | FINALLY("finally"), |
duke@1 | 182 | |
duke@1 | 183 | /** |
jjg@757 | 184 | * Warn about issues relating to use of command line options |
jjg@757 | 185 | */ |
jjg@757 | 186 | OPTIONS("options"), |
jjg@757 | 187 | |
jjg@757 | 188 | /** |
duke@1 | 189 | * Warn about issues regarding method overrides. |
duke@1 | 190 | */ |
duke@1 | 191 | OVERRIDES("overrides"), |
duke@1 | 192 | |
duke@1 | 193 | /** |
duke@1 | 194 | * Warn about invalid path elements on the command line. |
duke@1 | 195 | * Such warnings cannot be suppressed with the SuppressWarnings |
duke@1 | 196 | * annotation. |
duke@1 | 197 | */ |
duke@1 | 198 | PATH("path"), |
duke@1 | 199 | |
duke@1 | 200 | /** |
martin@124 | 201 | * Warn about issues regarding annotation processing. |
martin@124 | 202 | */ |
martin@124 | 203 | PROCESSING("processing"), |
martin@124 | 204 | |
martin@124 | 205 | /** |
jjg@757 | 206 | * Warn about unchecked operations on raw types. |
jjg@757 | 207 | */ |
jjg@757 | 208 | RAW("rawtypes"), |
jjg@757 | 209 | |
jjg@757 | 210 | /** |
duke@1 | 211 | * Warn about Serializable classes that do not provide a serial version ID. |
duke@1 | 212 | */ |
duke@1 | 213 | SERIAL("serial"), |
duke@1 | 214 | |
duke@1 | 215 | /** |
jjg@757 | 216 | * Warn about issues relating to use of statics |
jjg@757 | 217 | */ |
jjg@757 | 218 | STATIC("static"), |
jjg@757 | 219 | |
jjg@757 | 220 | /** |
jjg@757 | 221 | * Warn about proprietary API that may be removed in a future release. |
jjg@757 | 222 | */ |
jjg@757 | 223 | SUNAPI("sunapi", true), |
jjg@757 | 224 | |
jjg@757 | 225 | /** |
jjg@757 | 226 | * Warn about issues relating to use of try blocks (i.e. try-with-resources) |
jjg@757 | 227 | */ |
jjg@757 | 228 | TRY("try"), |
jjg@757 | 229 | |
jjg@757 | 230 | /** |
duke@1 | 231 | * Warn about unchecked operations on raw types. |
duke@1 | 232 | */ |
mcimadamore@122 | 233 | UNCHECKED("unchecked"), |
mcimadamore@122 | 234 | |
mcimadamore@122 | 235 | /** |
mcimadamore@580 | 236 | * Warn about potentially unsafe vararg methods |
mcimadamore@580 | 237 | */ |
jjg@757 | 238 | VARARGS("varargs"); |
duke@1 | 239 | |
duke@1 | 240 | LintCategory(String option) { |
jjg@377 | 241 | this(option, false); |
jjg@377 | 242 | } |
jjg@377 | 243 | |
jjg@377 | 244 | LintCategory(String option, boolean hidden) { |
duke@1 | 245 | this.option = option; |
jjg@377 | 246 | this.hidden = hidden; |
duke@1 | 247 | map.put(option, this); |
duke@1 | 248 | } |
duke@1 | 249 | |
duke@1 | 250 | static LintCategory get(String option) { |
duke@1 | 251 | return map.get(option); |
duke@1 | 252 | } |
duke@1 | 253 | |
jjg@11 | 254 | public final String option; |
jjg@377 | 255 | public final boolean hidden; |
duke@1 | 256 | }; |
duke@1 | 257 | |
duke@1 | 258 | /** |
duke@1 | 259 | * Checks if a warning category is enabled. A warning category may be enabled |
duke@1 | 260 | * on the command line, or by default, and can be temporarily disabled with |
duke@1 | 261 | * the SuppressWarnings annotation. |
duke@1 | 262 | */ |
duke@1 | 263 | public boolean isEnabled(LintCategory lc) { |
duke@1 | 264 | return values.contains(lc); |
duke@1 | 265 | } |
duke@1 | 266 | |
duke@1 | 267 | /** |
duke@1 | 268 | * Checks is a warning category has been specifically suppressed, by means |
duke@1 | 269 | * of the SuppressWarnings annotation, or, in the case of the deprecated |
duke@1 | 270 | * category, whether it has been implicitly suppressed by virtue of the |
duke@1 | 271 | * current entity being itself deprecated. |
duke@1 | 272 | */ |
duke@1 | 273 | public boolean isSuppressed(LintCategory lc) { |
duke@1 | 274 | return suppressedValues.contains(lc); |
duke@1 | 275 | } |
duke@1 | 276 | |
duke@1 | 277 | protected static class AugmentVisitor implements Attribute.Visitor { |
duke@1 | 278 | private final Context context; |
duke@1 | 279 | private Symtab syms; |
duke@1 | 280 | private Lint parent; |
duke@1 | 281 | private Lint lint; |
duke@1 | 282 | |
duke@1 | 283 | AugmentVisitor(Context context) { |
duke@1 | 284 | // to break an ugly sequence of initialization dependencies, |
duke@1 | 285 | // we defer the initialization of syms until it is needed |
duke@1 | 286 | this.context = context; |
duke@1 | 287 | } |
duke@1 | 288 | |
duke@1 | 289 | Lint augment(Lint parent, Attribute.Compound attr) { |
duke@1 | 290 | initSyms(); |
duke@1 | 291 | this.parent = parent; |
duke@1 | 292 | lint = null; |
duke@1 | 293 | attr.accept(this); |
duke@1 | 294 | return (lint == null ? parent : lint); |
duke@1 | 295 | } |
duke@1 | 296 | |
duke@1 | 297 | Lint augment(Lint parent, List<Attribute.Compound> attrs) { |
duke@1 | 298 | initSyms(); |
duke@1 | 299 | this.parent = parent; |
duke@1 | 300 | lint = null; |
duke@1 | 301 | for (Attribute.Compound a: attrs) { |
duke@1 | 302 | a.accept(this); |
duke@1 | 303 | } |
duke@1 | 304 | return (lint == null ? parent : lint); |
duke@1 | 305 | } |
duke@1 | 306 | |
duke@1 | 307 | private void initSyms() { |
duke@1 | 308 | if (syms == null) |
duke@1 | 309 | syms = Symtab.instance(context); |
duke@1 | 310 | } |
duke@1 | 311 | |
duke@1 | 312 | private void suppress(LintCategory lc) { |
duke@1 | 313 | if (lint == null) |
duke@1 | 314 | lint = new Lint(parent); |
duke@1 | 315 | lint.suppressedValues.add(lc); |
duke@1 | 316 | lint.values.remove(lc); |
duke@1 | 317 | } |
duke@1 | 318 | |
duke@1 | 319 | public void visitConstant(Attribute.Constant value) { |
duke@1 | 320 | if (value.type.tsym == syms.stringType.tsym) { |
duke@1 | 321 | LintCategory lc = LintCategory.get((String) (value.value)); |
duke@1 | 322 | if (lc != null) |
duke@1 | 323 | suppress(lc); |
duke@1 | 324 | } |
duke@1 | 325 | } |
duke@1 | 326 | |
duke@1 | 327 | public void visitClass(Attribute.Class clazz) { |
duke@1 | 328 | } |
duke@1 | 329 | |
duke@1 | 330 | // If we find a @SuppressWarnings annotation, then we continue |
duke@1 | 331 | // walking the tree, in order to suppress the individual warnings |
duke@1 | 332 | // specified in the @SuppressWarnings annotation. |
duke@1 | 333 | public void visitCompound(Attribute.Compound compound) { |
duke@1 | 334 | if (compound.type.tsym == syms.suppressWarningsType.tsym) { |
duke@1 | 335 | for (List<Pair<MethodSymbol,Attribute>> v = compound.values; |
duke@1 | 336 | v.nonEmpty(); v = v.tail) { |
duke@1 | 337 | Pair<MethodSymbol,Attribute> value = v.head; |
duke@1 | 338 | if (value.fst.name.toString().equals("value")) |
duke@1 | 339 | value.snd.accept(this); |
duke@1 | 340 | } |
duke@1 | 341 | |
duke@1 | 342 | } |
duke@1 | 343 | } |
duke@1 | 344 | |
duke@1 | 345 | public void visitArray(Attribute.Array array) { |
duke@1 | 346 | for (Attribute value : array.values) |
duke@1 | 347 | value.accept(this); |
duke@1 | 348 | } |
duke@1 | 349 | |
duke@1 | 350 | public void visitEnum(Attribute.Enum e) { |
duke@1 | 351 | } |
duke@1 | 352 | |
duke@1 | 353 | public void visitError(Attribute.Error e) { |
duke@1 | 354 | } |
duke@1 | 355 | }; |
duke@1 | 356 | } |