< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Print this page




  77  *  deletion without notice.</b>
  78  */
  79 public class Check {
  80     protected static final Context.Key<Check> checkKey = new Context.Key<>();
  81 
  82     private final Names names;
  83     private final Log log;
  84     private final Resolve rs;
  85     private final Symtab syms;
  86     private final Enter enter;
  87     private final DeferredAttr deferredAttr;
  88     private final Infer infer;
  89     private final Types types;
  90     private final TypeAnnotations typeAnnotations;
  91     private final JCDiagnostic.Factory diags;
  92     private final JavaFileManager fileManager;
  93     private final Source source;
  94     private final Target target;
  95     private final Profile profile;
  96     private final boolean warnOnAnyAccessToMembers;


  97 
  98     // The set of lint options currently in effect. It is initialized
  99     // from the context, and then is set/reset as needed by Attr as it
 100     // visits all the various parts of the trees during attribution.
 101     private Lint lint;
 102 
 103     // The method being analyzed in Attr - it is set/reset as needed by
 104     // Attr as it visits new method declarations.
 105     private MethodSymbol method;
 106 
 107     public static Check instance(Context context) {
 108         Check instance = context.get(checkKey);
 109         if (instance == null)
 110             instance = new Check(context);
 111         return instance;
 112     }
 113 
 114     protected Check(Context context) {
 115         context.put(checkKey, this);
 116 
 117         names = Names.instance(context);
 118         dfltTargetMeta = new Name[] { names.PACKAGE, names.TYPE,
 119             names.FIELD, names.METHOD, names.CONSTRUCTOR,
 120             names.ANNOTATION_TYPE, names.LOCAL_VARIABLE, names.PARAMETER};
 121         log = Log.instance(context);
 122         rs = Resolve.instance(context);
 123         syms = Symtab.instance(context);
 124         enter = Enter.instance(context);
 125         deferredAttr = DeferredAttr.instance(context);
 126         infer = Infer.instance(context);
 127         types = Types.instance(context);
 128         typeAnnotations = TypeAnnotations.instance(context);
 129         diags = JCDiagnostic.Factory.instance(context);
 130         Options options = Options.instance(context);
 131         lint = Lint.instance(context);
 132         fileManager = context.get(JavaFileManager.class);
 133 
 134         source = Source.instance(context);
 135         target = Target.instance(context);
 136         warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers");
 137 

 138         Target target = Target.instance(context);
 139         syntheticNameChar = target.syntheticNameChar();
 140 
 141         profile = Profile.instance(context);
 142 
 143         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 144         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 145         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 146         boolean enforceMandatoryWarnings = true;
 147 
 148         deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
 149                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 150         removalHandler = new MandatoryWarningHandler(log, verboseRemoval,
 151                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 152         uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
 153                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 154         sunApiHandler = new MandatoryWarningHandler(log, false,
 155                 enforceMandatoryWarnings, "sunapi", null);
 156 
 157         deferredLintHandler = DeferredLintHandler.instance(context);


 433         compiled.clear();
 434         localClassNameIndexes.clear();
 435     }
 436 
 437     public void putCompiled(ClassSymbol csym) {
 438         compiled.put(Pair.of(csym.packge().modle, csym.flatname), csym);
 439     }
 440 
 441     public ClassSymbol getCompiled(ClassSymbol csym) {
 442         return compiled.get(Pair.of(csym.packge().modle, csym.flatname));
 443     }
 444 
 445     public ClassSymbol getCompiled(ModuleSymbol msym, Name flatname) {
 446         return compiled.get(Pair.of(msym, flatname));
 447     }
 448 
 449     public void removeCompiled(ClassSymbol csym) {
 450         compiled.remove(Pair.of(csym.packge().modle, csym.flatname));
 451     }
 452 
 453 /* *************************************************************************
 454  * Type Checking
 455  **************************************************************************/
 456 
 457     /**
 458      * A check context is an object that can be used to perform compatibility
 459      * checks - depending on the check context, meaning of 'compatibility' might
 460      * vary significantly.
 461      */
 462     public interface CheckContext {
 463         /**
 464          * Is type 'found' compatible with type 'req' in given context
 465          */
 466         boolean compatible(Type found, Type req, Warner warn);
 467         /**
 468          * Report a check error
 469          */
 470         void report(DiagnosticPosition pos, JCDiagnostic details);
 471         /**
 472          * Obtain a warner for this check context
 473          */


 539         public String toString() {
 540             return "CheckContext: basicHandler";
 541         }
 542     };
 543 
 544     /** Check that a given type is assignable to a given proto-type.
 545      *  If it is, return the type, otherwise return errType.
 546      *  @param pos        Position to be used for error reporting.
 547      *  @param found      The type that was found.
 548      *  @param req        The type that was required.
 549      */
 550     public Type checkType(DiagnosticPosition pos, Type found, Type req) {
 551         return checkType(pos, found, req, basicHandler);
 552     }
 553 
 554     Type checkType(final DiagnosticPosition pos, final Type found, final Type req, final CheckContext checkContext) {
 555         final InferenceContext inferenceContext = checkContext.inferenceContext();
 556         if (inferenceContext.free(req) || inferenceContext.free(found)) {
 557             inferenceContext.addFreeTypeListener(List.of(req, found),
 558                     solvedContext -> checkType(pos, solvedContext.asInstType(found), solvedContext.asInstType(req), checkContext));




 559         }
 560         if (req.hasTag(ERROR))
 561             return req;
 562         if (req.hasTag(NONE))
 563             return found;
 564         if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) {



 565             return found;
 566         } else {
 567             if (found.isNumeric() && req.isNumeric()) {
 568                 checkContext.report(pos, diags.fragment(Fragments.PossibleLossOfPrecision(found, req)));
 569                 return types.createErrorType(found);
 570             }
 571             checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)));
 572             return types.createErrorType(found);
 573         }
 574     }
 575 
 576     /** Check that a given type can be cast to a given target type.
 577      *  Return the result of the cast.
 578      *  @param pos        Position to be used for error reporting.
 579      *  @param found      The type that is being cast.
 580      *  @param req        The target type of the cast.
 581      */
 582     Type checkCastable(DiagnosticPosition pos, Type found, Type req) {
 583         return checkCastable(pos, found, req, basicHandler);
 584     }
 585     Type checkCastable(DiagnosticPosition pos, Type found, Type req, CheckContext checkContext) {
 586         if (types.isCastable(found, req, castWarner(pos, found, req))) {







 587             return req;
 588         } else {
 589             checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)));
 590             return types.createErrorType(found);
 591         }
 592     }
 593 
 594     /** Check for redundant casts (i.e. where source type is a subtype of target type)
 595      * The problem should only be reported for non-292 cast
 596      */
 597     public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) {
 598         if (!tree.type.isErroneous()
 599                 && types.isSameType(tree.expr.type, tree.clazz.type)
 600                 && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz))
 601                 && !is292targetTypeCast(tree)) {
 602             deferredLintHandler.report(() -> {
 603                 if (lint.isEnabled(LintCategory.CAST))
 604                     log.warning(LintCategory.CAST,
 605                             tree.pos(), Warnings.RedundantCast(tree.clazz.type));
 606             });


 716      */
 717     Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) {
 718         t = checkClassType(pos, t);
 719         if (noBounds && t.isParameterized()) {
 720             List<Type> args = t.getTypeArguments();
 721             while (args.nonEmpty()) {
 722                 if (args.head.hasTag(WILDCARD))
 723                     return typeTagError(pos,
 724                                         diags.fragment(Fragments.TypeReqExact),
 725                                         args.head);
 726                 args = args.tail;
 727             }
 728         }
 729         return t;
 730     }
 731 
 732     /** Check that type is a reference type, i.e. a class, interface or array type
 733      *  or a type variable.
 734      *  @param pos           Position to be used for error reporting.
 735      *  @param t             The type to be checked.

 736      */
 737     Type checkRefType(DiagnosticPosition pos, Type t) {
 738         if (t.isReference())
 739             return t;
 740         else
 741             return typeTagError(pos,
 742                                 diags.fragment(Fragments.TypeReqRef),
 743                                 t);
 744     }
 745 









 746     /** Check that each type is a reference type, i.e. a class, interface or array type
 747      *  or a type variable.
 748      *  @param trees         Original trees, used for error reporting.
 749      *  @param types         The types to be checked.
 750      */
 751     List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
 752         List<JCExpression> tl = trees;
 753         for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
 754             l.head = checkRefType(tl.head.pos(), l.head);
 755             tl = tl.tail;
 756         }
 757         return types;
 758     }
 759 
 760     /** Check that type is a null or reference type.
 761      *  @param pos           Position to be used for error reporting.
 762      *  @param t             The type to be checked.
 763      */
 764     Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
 765         if (t.isReference() || t.hasTag(BOT))
 766             return t;
 767         else
 768             return typeTagError(pos,
 769                                 diags.fragment(Fragments.TypeReqRef),
 770                                 t);
 771     }
 772 
 773     /** Check that flag set does not contain elements of two conflicting sets. s
 774      *  Return true if it doesn't.
 775      *  @param pos           Position to be used for error reporting.
 776      *  @param flags         The set of flags to be checked.
 777      *  @param set1          Conflicting flags set #1.
 778      *  @param set2          Conflicting flags set #2.
 779      */
 780     boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) {
 781         if ((flags & set1) != 0 && (flags & set2) != 0) {
 782             log.error(pos,
 783                       Errors.IllegalCombinationOfModifiers(asFlagSet(TreeInfo.firstFlag(flags & set1)),
 784                                                            asFlagSet(TreeInfo.firstFlag(flags & set2))));
 785             return false;
 786         } else
 787             return true;
 788     }
 789 


















































 790     /** Check that usage of diamond operator is correct (i.e. diamond should not
 791      * be used with non-generic classes or in anonymous class creation expressions)
 792      */
 793     Type checkDiamond(JCNewClass tree, Type t) {
 794         if (!TreeInfo.isDiamond(tree) ||
 795                 t.isErroneous()) {
 796             return checkClassType(tree.clazz.pos(), t, true);
 797         } else {
 798             if (tree.def != null && !Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.allowedInSource(source)) {
 799                 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.clazz.pos(),
 800                         Errors.CantApplyDiamond1(t, Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.fragment(source.name)));
 801             }
 802             if (t.tsym.type.getTypeArguments().isEmpty()) {
 803                 log.error(tree.clazz.pos(),
 804                           Errors.CantApplyDiamond1(t,
 805                                                    Fragments.DiamondNonGeneric(t)));
 806                 return types.createErrorType(t);
 807             } else if (tree.typeargs != null &&
 808                     tree.typeargs.nonEmpty()) {
 809                 log.error(tree.clazz.pos(),


 918     }
 919     //where
 920         private boolean isTrustMeAllowedOnMethod(Symbol s) {
 921             return (s.flags() & VARARGS) != 0 &&
 922                 (s.isConstructor() ||
 923                     (s.flags() & (STATIC | FINAL |
 924                                   (Feature.PRIVATE_SAFE_VARARGS.allowedInSource(source) ? PRIVATE : 0) )) != 0);
 925         }
 926 
 927     Type checkLocalVarType(DiagnosticPosition pos, Type t, Name name) {
 928         //check that resulting type is not the null type
 929         if (t.hasTag(BOT)) {
 930             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferNull));
 931             return types.createErrorType(t);
 932         } else if (t.hasTag(VOID)) {
 933             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferVoid));
 934             return types.createErrorType(t);
 935         }
 936 
 937         //upward project the initializer type
 938         return types.upward(t, types.captures(t));




 939     }
 940 
 941     Type checkMethod(final Type mtype,
 942             final Symbol sym,
 943             final Env<AttrContext> env,
 944             final List<JCExpression> argtrees,
 945             final List<Type> argtypes,
 946             final boolean useVarargs,
 947             InferenceContext inferenceContext) {
 948         // System.out.println("call   : " + env.tree);
 949         // System.out.println("method : " + owntype);
 950         // System.out.println("actuals: " + argtypes);
 951         if (inferenceContext.free(mtype)) {
 952             inferenceContext.addFreeTypeListener(List.of(mtype),
 953                     solvedContext -> checkMethod(solvedContext.asInstType(mtype), sym, env, argtrees, argtypes, useVarargs, solvedContext));
 954             return mtype;
 955         }
 956         Type owntype = mtype;
 957         List<Type> formals = owntype.getParameterTypes();
 958         List<Type> nonInferred = sym.type.getParameterTypes();


1116      *  return modifiers together with any implicit modifiers for that symbol.
1117      *  Warning: we can't use flags() here since this method
1118      *  is called during class enter, when flags() would cause a premature
1119      *  completion.
1120      *  @param pos           Position to be used for error reporting.
1121      *  @param flags         The set of modifiers given in a definition.
1122      *  @param sym           The defined symbol.
1123      */
1124     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1125         long mask;
1126         long implicit = 0;
1127 
1128         switch (sym.kind) {
1129         case VAR:
1130             if (TreeInfo.isReceiverParam(tree))
1131                 mask = ReceiverParamFlags;
1132             else if (sym.owner.kind != TYP)
1133                 mask = LocalVarFlags;
1134             else if ((sym.owner.flags_field & INTERFACE) != 0)
1135                 mask = implicit = InterfaceVarFlags;
1136             else
1137                 mask = VarFlags;




1138             break;
1139         case MTH:
1140             if (sym.name == names.init) {
1141                 if ((sym.owner.flags_field & ENUM) != 0) {
1142                     // enum constructors cannot be declared public or
1143                     // protected and must be implicitly or explicitly
1144                     // private
1145                     implicit = PRIVATE;
1146                     mask = PRIVATE;
1147                 } else
1148                     mask = ConstructorFlags;
1149             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1150                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1151                     mask = AnnotationTypeElementMask;
1152                     implicit = PUBLIC | ABSTRACT;
1153                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1154                     mask = InterfaceMethodMask;
1155                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1156                     if ((flags & DEFAULT) != 0) {
1157                         implicit |= ABSTRACT;
1158                     }
1159                 } else {
1160                     mask = implicit = InterfaceMethodFlags;
1161                 }
1162             } else {
1163                 mask = MethodFlags;


1164             }
1165             // Imply STRICTFP if owner has STRICTFP set.
1166             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1167                 ((flags) & Flags.DEFAULT) != 0)
1168                 implicit |= sym.owner.flags_field & STRICTFP;
1169             break;
1170         case TYP:
1171             if (sym.isLocal()) {
1172                 mask = LocalClassFlags;
1173                 if ((sym.owner.flags_field & STATIC) == 0 &&
1174                     (flags & ENUM) != 0)
1175                     log.error(pos, Errors.EnumsMustBeStatic);
1176             } else if (sym.owner.kind == TYP) {
1177                 mask = MemberClassFlags;
1178                 if (sym.owner.owner.kind == PCK ||
1179                     (sym.owner.flags_field & STATIC) != 0)
1180                     mask |= STATIC;
1181                 else if ((flags & ENUM) != 0)
1182                     log.error(pos, Errors.EnumsMustBeStatic);
1183                 // Nested interfaces and enums are always STATIC (Spec ???)
1184                 if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC;
1185             } else {
1186                 mask = ClassFlags;
1187             }
1188             // Interfaces are always ABSTRACT
1189             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1190 
1191             if ((flags & ENUM) != 0) {
1192                 // enums can't be declared abstract or final
1193                 mask &= ~(ABSTRACT | FINAL);
1194                 implicit |= implicitEnumFinalFlag(tree);
1195             }
1196             // Imply STRICTFP if owner has STRICTFP set.
1197             implicit |= sym.owner.flags_field & STRICTFP;
1198             break;
1199         default:
1200             throw new AssertionError();
1201         }
1202         long illegal = flags & ExtendedStandardFlags & ~mask;
1203         if (illegal != 0) {
1204             if ((illegal & INTERFACE) != 0) {
1205                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1206                 mask |= INTERFACE;
1207             }
1208             else {
1209                 log.error(pos,
1210                           Errors.ModNotAllowedHere(asFlagSet(illegal)));
1211             }
1212         }
1213         else if ((sym.kind == TYP ||
1214                   // ISSUE: Disallowing abstract&private is no longer appropriate
1215                   // in the presence of inner classes. Should it be deleted here?
1216                   checkDisjoint(pos, flags,
1217                                 ABSTRACT,
1218                                 PRIVATE | STATIC | DEFAULT))
1219                  &&
1220                  checkDisjoint(pos, flags,
1221                                 STATIC | PRIVATE,
1222                                 DEFAULT)
1223                  &&
1224                  checkDisjoint(pos, flags,
1225                                ABSTRACT | INTERFACE,
1226                                FINAL | NATIVE | SYNCHRONIZED)
1227                  &&
1228                  checkDisjoint(pos, flags,
1229                                PUBLIC,
1230                                PRIVATE | PROTECTED)
1231                  &&
1232                  checkDisjoint(pos, flags,
1233                                PRIVATE,
1234                                PUBLIC | PROTECTED)
1235                  &&
1236                  checkDisjoint(pos, flags,
1237                                FINAL,
1238                                VOLATILE)
1239                  &&
1240                  (sym.kind == TYP ||
1241                   checkDisjoint(pos, flags,
1242                                 ABSTRACT | NATIVE,
1243                                 STRICTFP))) {
1244             // skip
1245         }
1246         return flags & (mask | ~ExtendedStandardFlags) | implicit;


2016         if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name))
2017             if (m.overrides(syms.enumFinalFinalize, origin, types, false)) {
2018                 log.error(tree.pos(), Errors.EnumNoFinalize);
2019                 return;
2020             }
2021         for (Type t = origin.type; t.hasTag(CLASS);
2022              t = types.supertype(t)) {
2023             if (t != origin.type) {
2024                 checkOverride(tree, t, origin, m);
2025             }
2026             for (Type t2 : types.interfaces(t)) {
2027                 checkOverride(tree, t2, origin, m);
2028             }
2029         }
2030 
2031         final boolean explicitOverride = m.attribute(syms.overrideType.tsym) != null;
2032         // Check if this method must override a super method due to being annotated with @Override
2033         // or by virtue of being a member of a diamond inferred anonymous class. Latter case is to
2034         // be treated "as if as they were annotated" with @Override.
2035         boolean mustOverride = explicitOverride ||
2036                 (env.info.isAnonymousDiamond && !m.isConstructor() && !m.isPrivate());

2037         if (mustOverride && !isOverrider(m)) {
2038             DiagnosticPosition pos = tree.pos();
2039             for (JCAnnotation a : tree.getModifiers().annotations) {
2040                 if (a.annotationType.type.tsym == syms.overrideType.tsym) {
2041                     pos = a.pos();
2042                     break;
2043                 }
2044             }
2045             log.error(pos,
2046                       explicitOverride ? (m.isStatic() ? Errors.StaticMethodsCannotBeAnnotatedWithOverride : Errors.MethodDoesNotOverrideSuperclass) :
2047                                 Errors.AnonymousDiamondMethodDoesNotOverrideSuperclass(Fragments.DiamondAnonymousMethodsImplicitlyOverride));
2048         }
2049     }
2050 
2051     void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) {
2052         TypeSymbol c = site.tsym;
2053         for (Symbol sym : c.members().getSymbolsByName(m.name)) {
2054             if (m.overrides(sym, origin, types, false)) {
2055                 if ((sym.flags() & ABSTRACT) == 0) {
2056                     checkOverride(tree, m, (MethodSymbol)sym, origin);


2140                 cf.accepts(s2) &&
2141                 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
2142     }
2143 
2144 
2145     /** Check that all abstract members of given class have definitions.
2146      *  @param pos          Position to be used for error reporting.
2147      *  @param c            The class.
2148      */
2149     void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
2150         MethodSymbol undef = types.firstUnimplementedAbstract(c);
2151         if (undef != null) {
2152             MethodSymbol undef1 =
2153                 new MethodSymbol(undef.flags(), undef.name,
2154                                  types.memberType(c.type, undef), undef.owner);
2155             log.error(pos,
2156                       Errors.DoesNotOverrideAbstract(c, undef1, undef1.location()));
2157         }
2158     }
2159 







































2160     void checkNonCyclicDecl(JCClassDecl tree) {
2161         CycleChecker cc = new CycleChecker();
2162         cc.scan(tree);
2163         if (!cc.errorFound && !cc.partialCheck) {
2164             tree.sym.flags_field |= ACYCLIC;
2165         }
2166     }
2167 
2168     class CycleChecker extends TreeScanner {
2169 
2170         List<Symbol> seenClasses = List.nil();
2171         boolean errorFound = false;
2172         boolean partialCheck = false;
2173 
2174         private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
2175             if (sym != null && sym.kind == TYP) {
2176                 Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym);
2177                 if (classEnv != null) {
2178                     DiagnosticSource prevSource = log.currentSource();
2179                     try {


2821     /** Check the type annotations.
2822      */
2823     public void validateTypeAnnotations(List<JCAnnotation> annotations, boolean isTypeParameter) {
2824         for (JCAnnotation a : annotations)
2825             validateTypeAnnotation(a, isTypeParameter);
2826     }
2827 
2828     /** Check an annotation of a symbol.
2829      */
2830     private void validateAnnotation(JCAnnotation a, Symbol s) {
2831         validateAnnotationTree(a);
2832 
2833         if (a.type.tsym.isAnnotationType() && !annotationApplicable(a, s))
2834             log.error(a.pos(), Errors.AnnotationTypeNotApplicable);
2835 
2836         if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) {
2837             if (s.kind != TYP) {
2838                 log.error(a.pos(), Errors.BadFunctionalIntfAnno);
2839             } else if (!s.isInterface() || (s.flags() & ANNOTATION) != 0) {
2840                 log.error(a.pos(), Errors.BadFunctionalIntfAnno1(Fragments.NotAFunctionalIntf(s)));







2841             }
2842         }
2843     }
2844 
2845     public void validateTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
2846         Assert.checkNonNull(a.type);
2847         validateAnnotationTree(a);
2848 
2849         if (a.hasTag(TYPE_ANNOTATION) &&
2850                 !a.annotationType.type.isErroneous() &&
2851                 !isTypeAnnotation(a, isTypeParameter)) {
2852             log.error(a.pos(), Errors.AnnotationTypeNotApplicableToType(a.type));
2853         }
2854     }
2855 
2856     /**
2857      * Validate the proposed container 'repeatable' on the
2858      * annotation type symbol 's'. Report errors at position
2859      * 'pos'.
2860      *




  77  *  deletion without notice.</b>
  78  */
  79 public class Check {
  80     protected static final Context.Key<Check> checkKey = new Context.Key<>();
  81 
  82     private final Names names;
  83     private final Log log;
  84     private final Resolve rs;
  85     private final Symtab syms;
  86     private final Enter enter;
  87     private final DeferredAttr deferredAttr;
  88     private final Infer infer;
  89     private final Types types;
  90     private final TypeAnnotations typeAnnotations;
  91     private final JCDiagnostic.Factory diags;
  92     private final JavaFileManager fileManager;
  93     private final Source source;
  94     private final Target target;
  95     private final Profile profile;
  96     private final boolean warnOnAnyAccessToMembers;
  97     private final boolean allowGenericsOverValues;
  98     private final boolean allowValueBasedClasses;
  99 
 100     // The set of lint options currently in effect. It is initialized
 101     // from the context, and then is set/reset as needed by Attr as it
 102     // visits all the various parts of the trees during attribution.
 103     private Lint lint;
 104 
 105     // The method being analyzed in Attr - it is set/reset as needed by
 106     // Attr as it visits new method declarations.
 107     private MethodSymbol method;
 108 
 109     public static Check instance(Context context) {
 110         Check instance = context.get(checkKey);
 111         if (instance == null)
 112             instance = new Check(context);
 113         return instance;
 114     }
 115 
 116     protected Check(Context context) {
 117         context.put(checkKey, this);
 118 
 119         names = Names.instance(context);
 120         dfltTargetMeta = new Name[] { names.PACKAGE, names.TYPE,
 121             names.FIELD, names.METHOD, names.CONSTRUCTOR,
 122             names.ANNOTATION_TYPE, names.LOCAL_VARIABLE, names.PARAMETER};
 123         log = Log.instance(context);
 124         rs = Resolve.instance(context);
 125         syms = Symtab.instance(context);
 126         enter = Enter.instance(context);
 127         deferredAttr = DeferredAttr.instance(context);
 128         infer = Infer.instance(context);
 129         types = Types.instance(context);
 130         typeAnnotations = TypeAnnotations.instance(context);
 131         diags = JCDiagnostic.Factory.instance(context);
 132         Options options = Options.instance(context);
 133         lint = Lint.instance(context);
 134         fileManager = context.get(JavaFileManager.class);
 135 
 136         source = Source.instance(context);
 137         target = Target.instance(context);
 138         warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers");
 139         allowGenericsOverValues = options.isSet("allowGenericsOverValues");
 140         allowValueBasedClasses = options.isSet("allowValueBasedClasses");
 141         Target target = Target.instance(context);
 142         syntheticNameChar = target.syntheticNameChar();
 143 
 144         profile = Profile.instance(context);
 145 
 146         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 147         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 148         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 149         boolean enforceMandatoryWarnings = true;
 150 
 151         deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated,
 152                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 153         removalHandler = new MandatoryWarningHandler(log, verboseRemoval,
 154                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 155         uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked,
 156                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 157         sunApiHandler = new MandatoryWarningHandler(log, false,
 158                 enforceMandatoryWarnings, "sunapi", null);
 159 
 160         deferredLintHandler = DeferredLintHandler.instance(context);


 436         compiled.clear();
 437         localClassNameIndexes.clear();
 438     }
 439 
 440     public void putCompiled(ClassSymbol csym) {
 441         compiled.put(Pair.of(csym.packge().modle, csym.flatname), csym);
 442     }
 443 
 444     public ClassSymbol getCompiled(ClassSymbol csym) {
 445         return compiled.get(Pair.of(csym.packge().modle, csym.flatname));
 446     }
 447 
 448     public ClassSymbol getCompiled(ModuleSymbol msym, Name flatname) {
 449         return compiled.get(Pair.of(msym, flatname));
 450     }
 451 
 452     public void removeCompiled(ClassSymbol csym) {
 453         compiled.remove(Pair.of(csym.packge().modle, csym.flatname));
 454     }
 455 
 456     /* *************************************************************************
 457  * Type Checking
 458  **************************************************************************/
 459 
 460     /**
 461      * A check context is an object that can be used to perform compatibility
 462      * checks - depending on the check context, meaning of 'compatibility' might
 463      * vary significantly.
 464      */
 465     public interface CheckContext {
 466         /**
 467          * Is type 'found' compatible with type 'req' in given context
 468          */
 469         boolean compatible(Type found, Type req, Warner warn);
 470         /**
 471          * Report a check error
 472          */
 473         void report(DiagnosticPosition pos, JCDiagnostic details);
 474         /**
 475          * Obtain a warner for this check context
 476          */


 542         public String toString() {
 543             return "CheckContext: basicHandler";
 544         }
 545     };
 546 
 547     /** Check that a given type is assignable to a given proto-type.
 548      *  If it is, return the type, otherwise return errType.
 549      *  @param pos        Position to be used for error reporting.
 550      *  @param found      The type that was found.
 551      *  @param req        The type that was required.
 552      */
 553     public Type checkType(DiagnosticPosition pos, Type found, Type req) {
 554         return checkType(pos, found, req, basicHandler);
 555     }
 556 
 557     Type checkType(final DiagnosticPosition pos, final Type found, final Type req, final CheckContext checkContext) {
 558         final InferenceContext inferenceContext = checkContext.inferenceContext();
 559         if (inferenceContext.free(req) || inferenceContext.free(found)) {
 560             inferenceContext.addFreeTypeListener(List.of(req, found),
 561                     solvedContext -> checkType(pos, solvedContext.asInstType(found), solvedContext.asInstType(req), checkContext));
 562         } else {
 563             if (found.hasTag(CLASS)) {
 564                 checkParameterizationWithValues(pos, found);
 565             }
 566         }
 567         if (req.hasTag(ERROR))
 568             return req;
 569         if (req.hasTag(NONE))
 570             return found;
 571         if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) {
 572             if (found.hasTag(BOT) && types.isValueBased(req)) {
 573                 log.warning(pos, Warnings.SuspiciousMixOfNullWithValueBasedClass(req));
 574             }
 575             return found;
 576         } else {
 577             if (found.isNumeric() && req.isNumeric()) {
 578                 checkContext.report(pos, diags.fragment(Fragments.PossibleLossOfPrecision(found, req)));
 579                 return types.createErrorType(found);
 580             }
 581             checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)));
 582             return types.createErrorType(found);
 583         }
 584     }
 585 
 586     /** Check that a given type can be cast to a given target type.
 587      *  Return the result of the cast.
 588      *  @param pos        Position to be used for error reporting.
 589      *  @param found      The type that is being cast.
 590      *  @param req        The target type of the cast.
 591      */
 592     Type checkCastable(DiagnosticPosition pos, Type found, Type req) {
 593         return checkCastable(pos, found, req, basicHandler);
 594     }
 595     Type checkCastable(DiagnosticPosition pos, Type found, Type req, CheckContext checkContext) {
 596         if (types.isCastable(found, req, castWarner(pos, found, req))) {
 597             if (types.isValueBased(req)) {
 598                 if (found.hasTag(BOT)) {
 599                     log.warning(pos, Warnings.SuspiciousMixOfNullWithValueBasedClass(req));
 600                 } else if (!types.isValueBased(found)) {
 601                     log.warning(pos, Warnings.PotentialNullPollution(found));
 602                 }
 603             }
 604             return req;
 605         } else {
 606             checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)));
 607             return types.createErrorType(found);
 608         }
 609     }
 610 
 611     /** Check for redundant casts (i.e. where source type is a subtype of target type)
 612      * The problem should only be reported for non-292 cast
 613      */
 614     public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) {
 615         if (!tree.type.isErroneous()
 616                 && types.isSameType(tree.expr.type, tree.clazz.type)
 617                 && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz))
 618                 && !is292targetTypeCast(tree)) {
 619             deferredLintHandler.report(() -> {
 620                 if (lint.isEnabled(LintCategory.CAST))
 621                     log.warning(LintCategory.CAST,
 622                             tree.pos(), Warnings.RedundantCast(tree.clazz.type));
 623             });


 733      */
 734     Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) {
 735         t = checkClassType(pos, t);
 736         if (noBounds && t.isParameterized()) {
 737             List<Type> args = t.getTypeArguments();
 738             while (args.nonEmpty()) {
 739                 if (args.head.hasTag(WILDCARD))
 740                     return typeTagError(pos,
 741                                         diags.fragment(Fragments.TypeReqExact),
 742                                         args.head);
 743                 args = args.tail;
 744             }
 745         }
 746         return t;
 747     }
 748 
 749     /** Check that type is a reference type, i.e. a class, interface or array type
 750      *  or a type variable.
 751      *  @param pos           Position to be used for error reporting.
 752      *  @param t             The type to be checked.
 753      *  @param valueOK       If false, a value class does not qualify
 754      */
 755     Type checkRefType(DiagnosticPosition pos, Type t, boolean valueOK) {
 756         if (t.isReference() && (valueOK || !types.isValue(t)))
 757             return t;
 758         else
 759             return typeTagError(pos,
 760                                 diags.fragment(Fragments.TypeReqRef),
 761                                 t);
 762     }
 763 
 764     /** Check that type is a reference type, i.e. a class, interface or array type
 765      *  or a type variable.
 766      *  @param pos           Position to be used for error reporting.
 767      *  @param t             The type to be checked.
 768      */
 769     Type checkRefType(DiagnosticPosition pos, Type t) {
 770         return checkRefType(pos, t, true);
 771     }
 772 
 773     /** Check that each type is a reference type, i.e. a class, interface or array type
 774      *  or a type variable.
 775      *  @param trees         Original trees, used for error reporting.
 776      *  @param types         The types to be checked.
 777      */
 778     List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
 779         List<JCExpression> tl = trees;
 780         for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
 781             l.head = checkRefType(tl.head.pos(), l.head, allowGenericsOverValues);
 782             tl = tl.tail;
 783         }
 784         return types;
 785     }
 786 
 787     /** Check that type is a null or reference type.
 788      *  @param pos           Position to be used for error reporting.
 789      *  @param t             The type to be checked.
 790      */
 791     Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
 792         if (t.isReference() || t.hasTag(BOT))
 793             return t;
 794         else
 795             return typeTagError(pos,
 796                                 diags.fragment(Fragments.TypeReqRef),
 797                                 t);
 798     }
 799 
 800     /** Check that flag set does not contain elements of two conflicting sets. s
 801      *  Return true if it doesn't.
 802      *  @param pos           Position to be used for error reporting.
 803      *  @param flags         The set of flags to be checked.
 804      *  @param set1          Conflicting flags set #1.
 805      *  @param set2          Conflicting flags set #2.
 806      */
 807     boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) {
 808         if ((flags & set1) != 0 && (flags & set2) != 0) {
 809             log.error(pos,
 810                       Errors.IllegalCombinationOfModifiers(asFlagSet(TreeInfo.firstFlag(flags & set1)),
 811                                                            asFlagSet(TreeInfo.firstFlag(flags & set2))));
 812             return false;
 813         } else
 814             return true;
 815     }
 816 
 817     void checkParameterizationWithValues(DiagnosticPosition pos, Type t) {
 818         if (!allowGenericsOverValues && t.tsym != syms.classType.tsym) { // tolerate Value.class for now.
 819             valueParameterizationChecker.visit(t, pos);
 820         }
 821     }
 822 
 823     /** valueParameterizationChecker: A type visitor that descends down the given type looking for instances of value types
 824      *  being used as type arguments and issues error against those usages.
 825      */
 826     private final Types.SimpleVisitor<Void, DiagnosticPosition> valueParameterizationChecker = new Types.SimpleVisitor<Void, DiagnosticPosition>() {
 827 
 828         @Override
 829         public Void visitType(Type t, DiagnosticPosition pos) {
 830             return null;
 831         }
 832 
 833         @Override
 834         public Void visitClassType(ClassType t, DiagnosticPosition pos) {
 835             for (Type targ : t.allparams()) {
 836                 if (types.isValue(targ) && !allowGenericsOverValues) {
 837                     log.error(pos, Errors.GenericParameterizationWithValueType(t));
 838                 }
 839                 visit(targ, pos);
 840             }
 841             return null;
 842         }
 843 
 844         @Override
 845         public Void visitTypeVar(TypeVar t, DiagnosticPosition pos) {
 846              return null;
 847         }
 848 
 849         @Override
 850         public Void visitCapturedType(CapturedType t, DiagnosticPosition pos) {
 851             return null;
 852         }
 853 
 854         @Override
 855         public Void visitArrayType(ArrayType t, DiagnosticPosition pos) {
 856             return visit(t.elemtype, pos);
 857         }
 858 
 859         @Override
 860         public Void visitWildcardType(WildcardType t, DiagnosticPosition pos) {
 861             return visit(t.type, pos);
 862         }
 863     };
 864 
 865 
 866 
 867     /** Check that usage of diamond operator is correct (i.e. diamond should not
 868      * be used with non-generic classes or in anonymous class creation expressions)
 869      */
 870     Type checkDiamond(JCNewClass tree, Type t) {
 871         if (!TreeInfo.isDiamond(tree) ||
 872                 t.isErroneous()) {
 873             return checkClassType(tree.clazz.pos(), t, true);
 874         } else {
 875             if (tree.def != null && !Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.allowedInSource(source)) {
 876                 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.clazz.pos(),
 877                         Errors.CantApplyDiamond1(t, Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.fragment(source.name)));
 878             }
 879             if (t.tsym.type.getTypeArguments().isEmpty()) {
 880                 log.error(tree.clazz.pos(),
 881                           Errors.CantApplyDiamond1(t,
 882                                                    Fragments.DiamondNonGeneric(t)));
 883                 return types.createErrorType(t);
 884             } else if (tree.typeargs != null &&
 885                     tree.typeargs.nonEmpty()) {
 886                 log.error(tree.clazz.pos(),


 995     }
 996     //where
 997         private boolean isTrustMeAllowedOnMethod(Symbol s) {
 998             return (s.flags() & VARARGS) != 0 &&
 999                 (s.isConstructor() ||
1000                     (s.flags() & (STATIC | FINAL |
1001                                   (Feature.PRIVATE_SAFE_VARARGS.allowedInSource(source) ? PRIVATE : 0) )) != 0);
1002         }
1003 
1004     Type checkLocalVarType(DiagnosticPosition pos, Type t, Name name) {
1005         //check that resulting type is not the null type
1006         if (t.hasTag(BOT)) {
1007             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferNull));
1008             return types.createErrorType(t);
1009         } else if (t.hasTag(VOID)) {
1010             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferVoid));
1011             return types.createErrorType(t);
1012         }
1013 
1014         //upward project the initializer type
1015         Type varType = types.upward(t, types.captures(t));
1016         if (varType.hasTag(CLASS)) {
1017             checkParameterizationWithValues(pos, varType);
1018         }
1019         return varType;
1020     }
1021 
1022     Type checkMethod(final Type mtype,
1023             final Symbol sym,
1024             final Env<AttrContext> env,
1025             final List<JCExpression> argtrees,
1026             final List<Type> argtypes,
1027             final boolean useVarargs,
1028             InferenceContext inferenceContext) {
1029         // System.out.println("call   : " + env.tree);
1030         // System.out.println("method : " + owntype);
1031         // System.out.println("actuals: " + argtypes);
1032         if (inferenceContext.free(mtype)) {
1033             inferenceContext.addFreeTypeListener(List.of(mtype),
1034                     solvedContext -> checkMethod(solvedContext.asInstType(mtype), sym, env, argtrees, argtypes, useVarargs, solvedContext));
1035             return mtype;
1036         }
1037         Type owntype = mtype;
1038         List<Type> formals = owntype.getParameterTypes();
1039         List<Type> nonInferred = sym.type.getParameterTypes();


1197      *  return modifiers together with any implicit modifiers for that symbol.
1198      *  Warning: we can't use flags() here since this method
1199      *  is called during class enter, when flags() would cause a premature
1200      *  completion.
1201      *  @param pos           Position to be used for error reporting.
1202      *  @param flags         The set of modifiers given in a definition.
1203      *  @param sym           The defined symbol.
1204      */
1205     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1206         long mask;
1207         long implicit = 0;
1208 
1209         switch (sym.kind) {
1210         case VAR:
1211             if (TreeInfo.isReceiverParam(tree))
1212                 mask = ReceiverParamFlags;
1213             else if (sym.owner.kind != TYP)
1214                 mask = LocalVarFlags;
1215             else if ((sym.owner.flags_field & INTERFACE) != 0)
1216                 mask = implicit = InterfaceVarFlags;
1217             else {
1218                 mask = VarFlags;
1219                 if (types.isValue(sym.owner.type) && (flags & STATIC) == 0) {
1220                     implicit |= FINAL;
1221                 }
1222             }
1223             break;
1224         case MTH:
1225             if (sym.name == names.init) {
1226                 if ((sym.owner.flags_field & ENUM) != 0) {
1227                     // enum constructors cannot be declared public or
1228                     // protected and must be implicitly or explicitly
1229                     // private
1230                     implicit = PRIVATE;
1231                     mask = PRIVATE;
1232                 } else
1233                     mask = ConstructorFlags;
1234             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1235                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1236                     mask = AnnotationTypeElementMask;
1237                     implicit = PUBLIC | ABSTRACT;
1238                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1239                     mask = InterfaceMethodMask;
1240                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1241                     if ((flags & DEFAULT) != 0) {
1242                         implicit |= ABSTRACT;
1243                     }
1244                 } else {
1245                     mask = implicit = InterfaceMethodFlags;
1246                 }
1247             } else {
1248                 // instance methods of value types do not have a monitor associated with their `this'
1249                 mask = ((sym.owner.flags_field & VALUE) != 0 && (flags & Flags.STATIC) == 0) ?
1250                         MethodFlags & ~SYNCHRONIZED : MethodFlags;
1251             }
1252             // Imply STRICTFP if owner has STRICTFP set.
1253             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1254                 ((flags) & Flags.DEFAULT) != 0)
1255                 implicit |= sym.owner.flags_field & STRICTFP;
1256             break;
1257         case TYP:
1258             if (sym.isLocal()) {
1259                 mask = LocalClassFlags;
1260                 if ((sym.owner.flags_field & STATIC) == 0 &&
1261                     (flags & ENUM) != 0)
1262                     log.error(pos, Errors.EnumsMustBeStatic);
1263             } else if (sym.owner.kind == TYP) {
1264                 mask = MemberClassFlags;
1265                 if (sym.owner.owner.kind == PCK ||
1266                     (sym.owner.flags_field & STATIC) != 0)
1267                     mask |= STATIC;
1268                 else if ((flags & ENUM) != 0)
1269                     log.error(pos, Errors.EnumsMustBeStatic);
1270                 // Nested interfaces and enums are always STATIC (Spec ???)
1271                 if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC;
1272             } else {
1273                 mask = ClassFlags;
1274             }
1275             // Interfaces are always ABSTRACT
1276             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1277 
1278             if ((flags & ENUM) != 0) {
1279                 // enums can't be declared abstract or final or value type
1280                 mask &= ~(ABSTRACT | FINAL | VALUE);
1281                 implicit |= implicitEnumFinalFlag(tree);
1282             }
1283             // Imply STRICTFP if owner has STRICTFP set.
1284             implicit |= sym.owner.flags_field & STRICTFP;
1285             break;
1286         default:
1287             throw new AssertionError();
1288         }
1289         long illegal = flags & ExtendedStandardFlags & ~mask;
1290         if (illegal != 0) {
1291             if ((illegal & INTERFACE) != 0) {
1292                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1293                 mask |= INTERFACE;
1294             }
1295             else {
1296                 log.error(pos,
1297                           Errors.ModNotAllowedHere(asFlagSet(illegal)));
1298             }
1299         }
1300         else if ((sym.kind == TYP ||
1301                   // ISSUE: Disallowing abstract&private is no longer appropriate
1302                   // in the presence of inner classes. Should it be deleted here?
1303                   checkDisjoint(pos, flags,
1304                                 ABSTRACT,
1305                                 PRIVATE | STATIC | DEFAULT))
1306                  &&
1307                  checkDisjoint(pos, flags,
1308                                 STATIC | PRIVATE,
1309                                 DEFAULT)
1310                  &&
1311                  checkDisjoint(pos, flags,
1312                                ABSTRACT | INTERFACE,
1313                                FINAL | NATIVE | SYNCHRONIZED | VALUE)
1314                  &&
1315                  checkDisjoint(pos, flags,
1316                                PUBLIC,
1317                                PRIVATE | PROTECTED)
1318                  &&
1319                  checkDisjoint(pos, flags,
1320                                PRIVATE,
1321                                PUBLIC | PROTECTED)
1322                  &&
1323                  checkDisjoint(pos, flags,
1324                                FINAL,
1325                                VOLATILE)
1326                  &&
1327                  (sym.kind == TYP ||
1328                   checkDisjoint(pos, flags,
1329                                 ABSTRACT | NATIVE,
1330                                 STRICTFP))) {
1331             // skip
1332         }
1333         return flags & (mask | ~ExtendedStandardFlags) | implicit;


2103         if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name))
2104             if (m.overrides(syms.enumFinalFinalize, origin, types, false)) {
2105                 log.error(tree.pos(), Errors.EnumNoFinalize);
2106                 return;
2107             }
2108         for (Type t = origin.type; t.hasTag(CLASS);
2109              t = types.supertype(t)) {
2110             if (t != origin.type) {
2111                 checkOverride(tree, t, origin, m);
2112             }
2113             for (Type t2 : types.interfaces(t)) {
2114                 checkOverride(tree, t2, origin, m);
2115             }
2116         }
2117 
2118         final boolean explicitOverride = m.attribute(syms.overrideType.tsym) != null;
2119         // Check if this method must override a super method due to being annotated with @Override
2120         // or by virtue of being a member of a diamond inferred anonymous class. Latter case is to
2121         // be treated "as if as they were annotated" with @Override.
2122         boolean mustOverride = explicitOverride ||
2123                 (env.info.isAnonymousDiamond && !m.isConstructor() && !m.isPrivate() &&
2124                         (!m.owner.isValue() || (tree.body.flags & SYNTHETIC) == 0));
2125         if (mustOverride && !isOverrider(m)) {
2126             DiagnosticPosition pos = tree.pos();
2127             for (JCAnnotation a : tree.getModifiers().annotations) {
2128                 if (a.annotationType.type.tsym == syms.overrideType.tsym) {
2129                     pos = a.pos();
2130                     break;
2131                 }
2132             }
2133             log.error(pos,
2134                       explicitOverride ? (m.isStatic() ? Errors.StaticMethodsCannotBeAnnotatedWithOverride : Errors.MethodDoesNotOverrideSuperclass) :
2135                                 Errors.AnonymousDiamondMethodDoesNotOverrideSuperclass(Fragments.DiamondAnonymousMethodsImplicitlyOverride));
2136         }
2137     }
2138 
2139     void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) {
2140         TypeSymbol c = site.tsym;
2141         for (Symbol sym : c.members().getSymbolsByName(m.name)) {
2142             if (m.overrides(sym, origin, types, false)) {
2143                 if ((sym.flags() & ABSTRACT) == 0) {
2144                     checkOverride(tree, m, (MethodSymbol)sym, origin);


2228                 cf.accepts(s2) &&
2229                 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
2230     }
2231 
2232 
2233     /** Check that all abstract members of given class have definitions.
2234      *  @param pos          Position to be used for error reporting.
2235      *  @param c            The class.
2236      */
2237     void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
2238         MethodSymbol undef = types.firstUnimplementedAbstract(c);
2239         if (undef != null) {
2240             MethodSymbol undef1 =
2241                 new MethodSymbol(undef.flags(), undef.name,
2242                                  types.memberType(c.type, undef), undef.owner);
2243             log.error(pos,
2244                       Errors.DoesNotOverrideAbstract(c, undef1, undef1.location()));
2245         }
2246     }
2247 
2248     // A value class cannot contain a field of its own type either or indirectly.
2249     void checkNonCyclicMembership(JCClassDecl tree) {
2250         Assert.check((tree.sym.flags_field & LOCKED) == 0);
2251         try {
2252             tree.sym.flags_field |= LOCKED;
2253             for (List<? extends JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
2254                 if (l.head.hasTag(VARDEF)) {
2255                     JCVariableDecl field = (JCVariableDecl) l.head;
2256                     if (cyclePossible(field.sym)) {
2257                         Type fieldType = field.sym.type;
2258                         checkNonCyclicMembership((ClassSymbol) fieldType.tsym, field.pos());
2259                     }
2260                 }
2261             }
2262         } finally {
2263             tree.sym.flags_field &= ~LOCKED;
2264         }
2265 
2266     }
2267     // where
2268     private void checkNonCyclicMembership(ClassSymbol c, DiagnosticPosition pos) {
2269         if ((c.flags_field & LOCKED) != 0) {
2270             log.error(pos, Errors.CyclicValueTypeMembership(c));
2271             return;
2272         }
2273         try {
2274             c.flags_field |= LOCKED;
2275             for (Symbol fld : c.members().getSymbols(s -> s.kind == VAR && cyclePossible((VarSymbol) s), NON_RECURSIVE)) {
2276                 checkNonCyclicMembership((ClassSymbol) fld.type.tsym, pos);
2277             }
2278         } finally {
2279             c.flags_field &= ~LOCKED;
2280         }
2281     }
2282         // where
2283         private boolean cyclePossible(VarSymbol symbol) {
2284             return (symbol.flags() & STATIC) == 0 && types.isValue(symbol.type);
2285         }
2286 
2287     void checkNonCyclicDecl(JCClassDecl tree) {
2288         CycleChecker cc = new CycleChecker();
2289         cc.scan(tree);
2290         if (!cc.errorFound && !cc.partialCheck) {
2291             tree.sym.flags_field |= ACYCLIC;
2292         }
2293     }
2294 
2295     class CycleChecker extends TreeScanner {
2296 
2297         List<Symbol> seenClasses = List.nil();
2298         boolean errorFound = false;
2299         boolean partialCheck = false;
2300 
2301         private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
2302             if (sym != null && sym.kind == TYP) {
2303                 Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym);
2304                 if (classEnv != null) {
2305                     DiagnosticSource prevSource = log.currentSource();
2306                     try {


2948     /** Check the type annotations.
2949      */
2950     public void validateTypeAnnotations(List<JCAnnotation> annotations, boolean isTypeParameter) {
2951         for (JCAnnotation a : annotations)
2952             validateTypeAnnotation(a, isTypeParameter);
2953     }
2954 
2955     /** Check an annotation of a symbol.
2956      */
2957     private void validateAnnotation(JCAnnotation a, Symbol s) {
2958         validateAnnotationTree(a);
2959 
2960         if (a.type.tsym.isAnnotationType() && !annotationApplicable(a, s))
2961             log.error(a.pos(), Errors.AnnotationTypeNotApplicable);
2962 
2963         if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) {
2964             if (s.kind != TYP) {
2965                 log.error(a.pos(), Errors.BadFunctionalIntfAnno);
2966             } else if (!s.isInterface() || (s.flags() & ANNOTATION) != 0) {
2967                 log.error(a.pos(), Errors.BadFunctionalIntfAnno1(Fragments.NotAFunctionalIntf(s)));
2968             }
2969         }
2970         if (a.annotationType.type.tsym == syms.valueBasedType.tsym) {
2971             if (s.isInterface() || s.isEnum()) {
2972                 log.error(a.pos(), Errors.BadValueBasedAnno);
2973             } else if (allowValueBasedClasses) {
2974                 s.flags_field |= VALUEBASED;
2975             }
2976         }
2977     }
2978 
2979     public void validateTypeAnnotation(JCAnnotation a, boolean isTypeParameter) {
2980         Assert.checkNonNull(a.type);
2981         validateAnnotationTree(a);
2982 
2983         if (a.hasTag(TYPE_ANNOTATION) &&
2984                 !a.annotationType.type.isErroneous() &&
2985                 !isTypeAnnotation(a, isTypeParameter)) {
2986             log.error(a.pos(), Errors.AnnotationTypeNotApplicableToType(a.type));
2987         }
2988     }
2989 
2990     /**
2991      * Validate the proposed container 'repeatable' on the
2992      * annotation type symbol 's'. Report errors at position
2993      * 'pos'.
2994      *


< prev index next >