src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
changeset 48721 ef3557eb4306
parent 48459 a5f815d1060b
child 49294 3556298e481d
equal deleted inserted replaced
48720:290b480df13e 48721:ef3557eb4306
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    41 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    41 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
    42 import com.sun.tools.javac.tree.*;
    42 import com.sun.tools.javac.tree.*;
    43 import com.sun.tools.javac.util.*;
    43 import com.sun.tools.javac.util.*;
    44 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    44 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
    45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
    45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
       
    46 import com.sun.tools.javac.util.JCDiagnostic.Error;
    46 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
    47 import com.sun.tools.javac.util.JCDiagnostic.Fragment;
       
    48 import com.sun.tools.javac.util.JCDiagnostic.Warning;
    47 import com.sun.tools.javac.util.List;
    49 import com.sun.tools.javac.util.List;
    48 
    50 
    49 import com.sun.tools.javac.code.Lint;
    51 import com.sun.tools.javac.code.Lint;
    50 import com.sun.tools.javac.code.Lint.LintCategory;
    52 import com.sun.tools.javac.code.Lint.LintCategory;
    51 import com.sun.tools.javac.code.Scope.WriteableScope;
    53 import com.sun.tools.javac.code.Scope.WriteableScope;
   203      */
   205      */
   204     void warnDeprecated(DiagnosticPosition pos, Symbol sym) {
   206     void warnDeprecated(DiagnosticPosition pos, Symbol sym) {
   205         if (sym.isDeprecatedForRemoval()) {
   207         if (sym.isDeprecatedForRemoval()) {
   206             if (!lint.isSuppressed(LintCategory.REMOVAL)) {
   208             if (!lint.isSuppressed(LintCategory.REMOVAL)) {
   207                 if (sym.kind == MDL) {
   209                 if (sym.kind == MDL) {
   208                     removalHandler.report(pos, "has.been.deprecated.for.removal.module", sym);
   210                     removalHandler.report(pos, Warnings.HasBeenDeprecatedForRemovalModule(sym));
   209                 } else {
   211                 } else {
   210                     removalHandler.report(pos, "has.been.deprecated.for.removal", sym, sym.location());
   212                     removalHandler.report(pos, Warnings.HasBeenDeprecatedForRemoval(sym, sym.location()));
   211                 }
   213                 }
   212             }
   214             }
   213         } else if (!lint.isSuppressed(LintCategory.DEPRECATION)) {
   215         } else if (!lint.isSuppressed(LintCategory.DEPRECATION)) {
   214             if (sym.kind == MDL) {
   216             if (sym.kind == MDL) {
   215                 deprecationHandler.report(pos, "has.been.deprecated.module", sym);
   217                 deprecationHandler.report(pos, Warnings.HasBeenDeprecatedModule(sym));
   216             } else {
   218             } else {
   217                 deprecationHandler.report(pos, "has.been.deprecated", sym, sym.location());
   219                 deprecationHandler.report(pos, Warnings.HasBeenDeprecated(sym, sym.location()));
   218             }
   220             }
   219         }
   221         }
   220     }
   222     }
   221 
   223 
   222     /** Warn about unchecked operation.
   224     /** Warn about unchecked operation.
   223      *  @param pos        Position to be used for error reporting.
   225      *  @param pos        Position to be used for error reporting.
   224      *  @param msg        A string describing the problem.
   226      *  @param msg        A string describing the problem.
   225      */
   227      */
   226     public void warnUnchecked(DiagnosticPosition pos, String msg, Object... args) {
   228     public void warnUnchecked(DiagnosticPosition pos, Warning warnKey) {
   227         if (!lint.isSuppressed(LintCategory.UNCHECKED))
   229         if (!lint.isSuppressed(LintCategory.UNCHECKED))
   228             uncheckedHandler.report(pos, msg, args);
   230             uncheckedHandler.report(pos, warnKey);
   229     }
   231     }
   230 
   232 
   231     /** Warn about unsafe vararg method decl.
   233     /** Warn about unsafe vararg method decl.
   232      *  @param pos        Position to be used for error reporting.
   234      *  @param pos        Position to be used for error reporting.
   233      */
   235      */
   234     void warnUnsafeVararg(DiagnosticPosition pos, String key, Object... args) {
   236     void warnUnsafeVararg(DiagnosticPosition pos, Warning warnKey) {
   235         if (lint.isEnabled(LintCategory.VARARGS) && Feature.SIMPLIFIED_VARARGS.allowedInSource(source))
   237         if (lint.isEnabled(LintCategory.VARARGS) && Feature.SIMPLIFIED_VARARGS.allowedInSource(source))
   236             log.warning(LintCategory.VARARGS, pos, key, args);
   238             log.warning(LintCategory.VARARGS, pos, warnKey);
   237     }
   239     }
   238 
   240 
   239     public void warnStatic(DiagnosticPosition pos, String msg, Object... args) {
   241     public void warnStatic(DiagnosticPosition pos, Warning warnKey) {
   240         if (lint.isEnabled(LintCategory.STATIC))
   242         if (lint.isEnabled(LintCategory.STATIC))
   241             log.warning(LintCategory.STATIC, pos, msg, args);
   243             log.warning(LintCategory.STATIC, pos, warnKey);
   242     }
   244     }
   243 
   245 
   244     /** Warn about division by integer constant zero.
   246     /** Warn about division by integer constant zero.
   245      *  @param pos        Position to be used for error reporting.
   247      *  @param pos        Position to be used for error reporting.
   246      */
   248      */
   901                           Errors.VarargsInvalidTrustmeAnno(syms.trustMeType.tsym,
   903                           Errors.VarargsInvalidTrustmeAnno(syms.trustMeType.tsym,
   902                                                            Fragments.VarargsTrustmeOnNonVarargsMeth(m)));
   904                                                            Fragments.VarargsTrustmeOnNonVarargsMeth(m)));
   903             }
   905             }
   904         } else if (hasTrustMeAnno && varargElemType != null &&
   906         } else if (hasTrustMeAnno && varargElemType != null &&
   905                             types.isReifiable(varargElemType)) {
   907                             types.isReifiable(varargElemType)) {
   906             warnUnsafeVararg(tree,
   908             warnUnsafeVararg(tree, Warnings.VarargsRedundantTrustmeAnno(
   907                             "varargs.redundant.trustme.anno",
   909                                 syms.trustMeType.tsym,
   908                             syms.trustMeType.tsym,
   910                                 diags.fragment(Fragments.VarargsTrustmeOnReifiableVarargs(varargElemType))));
   909                             diags.fragment(Fragments.VarargsTrustmeOnReifiableVarargs(varargElemType)));
       
   910         }
   911         }
   911         else if (!hasTrustMeAnno && varargElemType != null &&
   912         else if (!hasTrustMeAnno && varargElemType != null &&
   912                 !types.isReifiable(varargElemType)) {
   913                 !types.isReifiable(varargElemType)) {
   913             warnUnchecked(tree.params.head.pos(), "unchecked.varargs.non.reifiable.type", varargElemType);
   914             warnUnchecked(tree.params.head.pos(), Warnings.UncheckedVarargsNonReifiableType(varargElemType));
   914         }
   915         }
   915     }
   916     }
   916     //where
   917     //where
   917         private boolean isTrustMeAllowedOnMethod(Symbol s) {
   918         private boolean isTrustMeAllowedOnMethod(Symbol s) {
   918             return (s.flags() & VARARGS) != 0 &&
   919             return (s.flags() & VARARGS) != 0 &&
   996             Type argtype = owntype.getParameterTypes().last();
   997             Type argtype = owntype.getParameterTypes().last();
   997             if (!types.isReifiable(argtype) &&
   998             if (!types.isReifiable(argtype) &&
   998                 (!Feature.SIMPLIFIED_VARARGS.allowedInSource(source) ||
   999                 (!Feature.SIMPLIFIED_VARARGS.allowedInSource(source) ||
   999                  sym.baseSymbol().attribute(syms.trustMeType.tsym) == null ||
  1000                  sym.baseSymbol().attribute(syms.trustMeType.tsym) == null ||
  1000                  !isTrustMeAllowedOnMethod(sym))) {
  1001                  !isTrustMeAllowedOnMethod(sym))) {
  1001                 warnUnchecked(env.tree.pos(),
  1002                 warnUnchecked(env.tree.pos(), Warnings.UncheckedGenericArrayCreation(argtype));
  1002                                   "unchecked.generic.array.creation",
       
  1003                                   argtype);
       
  1004             }
  1003             }
  1005             if ((sym.baseSymbol().flags() & SIGNATURE_POLYMORPHIC) == 0) {
  1004             if ((sym.baseSymbol().flags() & SIGNATURE_POLYMORPHIC) == 0) {
  1006                 TreeInfo.setVarargsElement(env.tree, types.elemtype(argtype));
  1005                 TreeInfo.setVarargsElement(env.tree, types.elemtype(argtype));
  1007             }
  1006             }
  1008          }
  1007          }
  1759                 m.flags_field |= BAD_OVERRIDE;
  1758                 m.flags_field |= BAD_OVERRIDE;
  1760             }
  1759             }
  1761             return;
  1760             return;
  1762         } else if (overrideWarner.hasNonSilentLint(LintCategory.UNCHECKED)) {
  1761         } else if (overrideWarner.hasNonSilentLint(LintCategory.UNCHECKED)) {
  1763             warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
  1762             warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
  1764                     "override.unchecked.ret",
  1763                     Warnings.OverrideUncheckedRet(uncheckedOverrides(m, other), mtres, otres));
  1765                     uncheckedOverrides(m, other),
       
  1766                     mtres, otres);
       
  1767         }
  1764         }
  1768 
  1765 
  1769         // Error if overriding method throws an exception not reported
  1766         // Error if overriding method throws an exception not reported
  1770         // by overridden method.
  1767         // by overridden method.
  1771         List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars);
  1768         List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars);
  1777             m.flags_field |= BAD_OVERRIDE;
  1774             m.flags_field |= BAD_OVERRIDE;
  1778             return;
  1775             return;
  1779         }
  1776         }
  1780         else if (unhandledUnerased.nonEmpty()) {
  1777         else if (unhandledUnerased.nonEmpty()) {
  1781             warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
  1778             warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree),
  1782                           "override.unchecked.thrown",
  1779                           Warnings.OverrideUncheckedThrown(cannotOverride(m, other), unhandledUnerased.head));
  1783                          cannotOverride(m, other),
       
  1784                          unhandledUnerased.head);
       
  1785             return;
  1780             return;
  1786         }
  1781         }
  1787 
  1782 
  1788         // Optional warning if varargs don't agree
  1783         // Optional warning if varargs don't agree
  1789         if ((((m.flags() ^ other.flags()) & Flags.VARARGS) != 0)
  1784         if ((((m.flags() ^ other.flags()) & Flags.VARARGS) != 0)
  3235                 missingDefaults = missingDefaults.append(m.name);
  3230                 missingDefaults = missingDefaults.append(m.name);
  3236         }
  3231         }
  3237         missingDefaults = missingDefaults.reverse();
  3232         missingDefaults = missingDefaults.reverse();
  3238         if (missingDefaults.nonEmpty()) {
  3233         if (missingDefaults.nonEmpty()) {
  3239             isValid = false;
  3234             isValid = false;
  3240             String key = (missingDefaults.size() > 1)
  3235             Error errorKey = (missingDefaults.size() > 1)
  3241                     ? "annotation.missing.default.value.1"
  3236                     ? Errors.AnnotationMissingDefaultValue1(a.type, missingDefaults)
  3242                     : "annotation.missing.default.value";
  3237                     : Errors.AnnotationMissingDefaultValue(a.type, missingDefaults);
  3243             log.error(a.pos(), key, a.type, missingDefaults);
  3238             log.error(a.pos(), errorKey);
  3244         }
  3239         }
  3245 
  3240 
  3246         return isValid && validateTargetAnnotationValue(a);
  3241         return isValid && validateTargetAnnotationValue(a);
  3247     }
  3242     }
  3248 
  3243 
  3603             boolean warned = this.warned;
  3598             boolean warned = this.warned;
  3604             super.warn(lint);
  3599             super.warn(lint);
  3605             if (warned) return; // suppress redundant diagnostics
  3600             if (warned) return; // suppress redundant diagnostics
  3606             switch (lint) {
  3601             switch (lint) {
  3607                 case UNCHECKED:
  3602                 case UNCHECKED:
  3608                     Check.this.warnUnchecked(pos(), "prob.found.req", diags.fragment(uncheckedKey), found, expected);
  3603                     Check.this.warnUnchecked(pos(), Warnings.ProbFoundReq(diags.fragment(uncheckedKey), found, expected));
  3609                     break;
  3604                     break;
  3610                 case VARARGS:
  3605                 case VARARGS:
  3611                     if (method != null &&
  3606                     if (method != null &&
  3612                             method.attribute(syms.trustMeType.tsym) != null &&
  3607                             method.attribute(syms.trustMeType.tsym) != null &&
  3613                             isTrustMeAllowedOnMethod(method) &&
  3608                             isTrustMeAllowedOnMethod(method) &&
  3614                             !types.isReifiable(method.type.getParameterTypes().last())) {
  3609                             !types.isReifiable(method.type.getParameterTypes().last())) {
  3615                         Check.this.warnUnsafeVararg(pos(), "varargs.unsafe.use.varargs.param", method.params.last());
  3610                         Check.this.warnUnsafeVararg(pos(), Warnings.VarargsUnsafeUseVarargsParam(method.params.last()));
  3616                     }
  3611                     }
  3617                     break;
  3612                     break;
  3618                 default:
  3613                 default:
  3619                     throw new AssertionError("Unexpected lint: " + lint);
  3614                     throw new AssertionError("Unexpected lint: " + lint);
  3620             }
  3615             }