< prev index next >

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

Print this page

 118     protected Check(Context context) {
 119         context.put(checkKey, this);
 120 
 121         names = Names.instance(context);
 122         log = Log.instance(context);
 123         rs = Resolve.instance(context);
 124         syms = Symtab.instance(context);
 125         enter = Enter.instance(context);
 126         deferredAttr = DeferredAttr.instance(context);
 127         infer = Infer.instance(context);
 128         types = Types.instance(context);
 129         typeAnnotations = TypeAnnotations.instance(context);
 130         diags = JCDiagnostic.Factory.instance(context);
 131         Options options = Options.instance(context);
 132         lint = Lint.instance(context);
 133         fileManager = context.get(JavaFileManager.class);
 134 
 135         source = Source.instance(context);
 136         target = Target.instance(context);
 137         warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers");
 138 
 139         Target target = Target.instance(context);
 140         syntheticNameChar = target.syntheticNameChar();
 141 
 142         profile = Profile.instance(context);
 143         preview = Preview.instance(context);
 144 
 145         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 146         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 147         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 148         boolean enforceMandatoryWarnings = true;
 149 
 150         deprecationHandler = new MandatoryWarningHandler(log, null, verboseDeprecated,
 151                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 152         removalHandler = new MandatoryWarningHandler(log, null, verboseRemoval,
 153                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 154         uncheckedHandler = new MandatoryWarningHandler(log, null, verboseUnchecked,
 155                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 156         sunApiHandler = new MandatoryWarningHandler(log, null, false,
 157                 enforceMandatoryWarnings, "sunapi", null);
 158 

 483         uncheckedHandler.clear();
 484         sunApiHandler.clear();
 485     }
 486 
 487     public void putCompiled(ClassSymbol csym) {
 488         compiled.put(Pair.of(csym.packge().modle, csym.flatname), csym);
 489     }
 490 
 491     public ClassSymbol getCompiled(ClassSymbol csym) {
 492         return compiled.get(Pair.of(csym.packge().modle, csym.flatname));
 493     }
 494 
 495     public ClassSymbol getCompiled(ModuleSymbol msym, Name flatname) {
 496         return compiled.get(Pair.of(msym, flatname));
 497     }
 498 
 499     public void removeCompiled(ClassSymbol csym) {
 500         compiled.remove(Pair.of(csym.packge().modle, csym.flatname));
 501     }
 502 
 503 /* *************************************************************************
 504  * Type Checking
 505  **************************************************************************/
 506 
 507     /**
 508      * A check context is an object that can be used to perform compatibility
 509      * checks - depending on the check context, meaning of 'compatibility' might
 510      * vary significantly.
 511      */
 512     public interface CheckContext {
 513         /**
 514          * Is type 'found' compatible with type 'req' in given context
 515          */
 516         boolean compatible(Type found, Type req, Warner warn);
 517         /**
 518          * Report a check error
 519          */
 520         void report(DiagnosticPosition pos, JCDiagnostic details);
 521         /**
 522          * Obtain a warner for this check context
 523          */

 589         public String toString() {
 590             return "CheckContext: basicHandler";
 591         }
 592     };
 593 
 594     /** Check that a given type is assignable to a given proto-type.
 595      *  If it is, return the type, otherwise return errType.
 596      *  @param pos        Position to be used for error reporting.
 597      *  @param found      The type that was found.
 598      *  @param req        The type that was required.
 599      */
 600     public Type checkType(DiagnosticPosition pos, Type found, Type req) {
 601         return checkType(pos, found, req, basicHandler);
 602     }
 603 
 604     Type checkType(final DiagnosticPosition pos, final Type found, final Type req, final CheckContext checkContext) {
 605         final InferenceContext inferenceContext = checkContext.inferenceContext();
 606         if (inferenceContext.free(req) || inferenceContext.free(found)) {
 607             inferenceContext.addFreeTypeListener(List.of(req, found),
 608                     solvedContext -> checkType(pos, solvedContext.asInstType(found), solvedContext.asInstType(req), checkContext));





 609         }
 610         if (req.hasTag(ERROR))
 611             return req;
 612         if (req.hasTag(NONE))
 613             return found;
 614         if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) {
 615             return found;
 616         } else {
 617             if (found.isNumeric() && req.isNumeric()) {
 618                 checkContext.report(pos, diags.fragment(Fragments.PossibleLossOfPrecision(found, req)));
 619                 return types.createErrorType(found);
 620             }
 621             checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)));
 622             return types.createErrorType(found);
 623         }
 624     }
 625 
 626     /** Check that a given type can be cast to a given target type.
 627      *  Return the result of the cast.
 628      *  @param pos        Position to be used for error reporting.

 719     /** Check that type is a class or interface type.
 720      *  @param pos           Position to be used for error reporting.
 721      *  @param t             The type to be checked.
 722      */
 723     Type checkClassType(DiagnosticPosition pos, Type t) {
 724         if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) {
 725             return typeTagError(pos,
 726                                 diags.fragment(Fragments.TypeReqClass),
 727                                 asTypeParam(t));
 728         } else {
 729             return t;
 730         }
 731     }
 732     //where
 733         private Object asTypeParam(Type t) {
 734             return (t.hasTag(TYPEVAR))
 735                                     ? diags.fragment(Fragments.TypeParameter(t))
 736                                     : t;
 737         }
 738 












































 739     /** Check that type is a valid qualifier for a constructor reference expression
 740      */
 741     Type checkConstructorRefType(DiagnosticPosition pos, Type t) {
 742         t = checkClassOrArrayType(pos, t);
 743         if (t.hasTag(CLASS)) {
 744             if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
 745                 log.error(pos, Errors.AbstractCantBeInstantiated(t.tsym));
 746                 t = types.createErrorType(t);
 747             } else if ((t.tsym.flags() & ENUM) != 0) {
 748                 log.error(pos, Errors.EnumCantBeInstantiated);
 749                 t = types.createErrorType(t);
 750             } else {
 751                 t = checkClassType(pos, t, true);












 752             }
 753         } else if (t.hasTag(ARRAY)) {
 754             if (!types.isReifiable(((ArrayType)t).elemtype)) {
 755                 log.error(pos, Errors.GenericArrayCreation);
 756                 t = types.createErrorType(t);
 757             }
 758         }
 759         return t;
 760     }
 761 
 762     /** Check that type is a class or interface type.
 763      *  @param pos           Position to be used for error reporting.
 764      *  @param t             The type to be checked.
 765      *  @param noBounds    True if type bounds are illegal here.
 766      */
 767     Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) {
 768         t = checkClassType(pos, t);
 769         if (noBounds && t.isParameterized()) {
 770             List<Type> args = t.getTypeArguments();
 771             while (args.nonEmpty()) {
 772                 if (args.head.hasTag(WILDCARD))
 773                     return typeTagError(pos,
 774                                         diags.fragment(Fragments.TypeReqExact),
 775                                         args.head);
 776                 args = args.tail;
 777             }
 778         }
 779         return t;
 780     }
 781 
 782     /** Check that type is a reference type, i.e. a class, interface or array type
 783      *  or a type variable.
 784      *  @param pos           Position to be used for error reporting.
 785      *  @param t             The type to be checked.

 786      */
 787     Type checkRefType(DiagnosticPosition pos, Type t) {
 788         if (t.isReference())
 789             return t;
 790         else
 791             return typeTagError(pos,
 792                                 diags.fragment(Fragments.TypeReqRef),
 793                                 t);
 794     }
 795 
































 796     /** Check that each type is a reference type, i.e. a class, interface or array type
 797      *  or a type variable.
 798      *  @param trees         Original trees, used for error reporting.
 799      *  @param types         The types to be checked.
 800      */
 801     List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
 802         List<JCExpression> tl = trees;
 803         for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
 804             l.head = checkRefType(tl.head.pos(), l.head);
 805             tl = tl.tail;
 806         }
 807         return types;
 808     }
 809 
 810     /** Check that type is a null or reference type.
 811      *  @param pos           Position to be used for error reporting.
 812      *  @param t             The type to be checked.
 813      */
 814     Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
 815         if (t.isReference() || t.hasTag(BOT))
 816             return t;
 817         else
 818             return typeTagError(pos,
 819                                 diags.fragment(Fragments.TypeReqRef),
 820                                 t);
 821     }
 822 
 823     /** Check that flag set does not contain elements of two conflicting sets. s
 824      *  Return true if it doesn't.
 825      *  @param pos           Position to be used for error reporting.
 826      *  @param flags         The set of flags to be checked.
 827      *  @param set1          Conflicting flags set #1.
 828      *  @param set2          Conflicting flags set #2.
 829      */
 830     boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) {
 831         if ((flags & set1) != 0 && (flags & set2) != 0) {
 832             log.error(pos,
 833                       Errors.IllegalCombinationOfModifiers(asFlagSet(TreeInfo.firstFlag(flags & set1)),
 834                                                            asFlagSet(TreeInfo.firstFlag(flags & set2))));
 835             return false;
 836         } else
 837             return true;
 838     }
 839 

















































 840     /** Check that usage of diamond operator is correct (i.e. diamond should not
 841      * be used with non-generic classes or in anonymous class creation expressions)
 842      */
 843     Type checkDiamond(JCNewClass tree, Type t) {
 844         if (!TreeInfo.isDiamond(tree) ||
 845                 t.isErroneous()) {
 846             return checkClassType(tree.clazz.pos(), t, true);
 847         } else {
 848             if (tree.def != null && !Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.allowedInSource(source)) {
 849                 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.clazz.pos(),
 850                         Errors.CantApplyDiamond1(t, Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.fragment(source.name)));
 851             }
 852             if (t.tsym.type.getTypeArguments().isEmpty()) {
 853                 log.error(tree.clazz.pos(),
 854                           Errors.CantApplyDiamond1(t,
 855                                                    Fragments.DiamondNonGeneric(t)));
 856                 return types.createErrorType(t);
 857             } else if (tree.typeargs != null &&
 858                     tree.typeargs.nonEmpty()) {
 859                 log.error(tree.clazz.pos(),

 968     }
 969     //where
 970         private boolean isTrustMeAllowedOnMethod(Symbol s) {
 971             return (s.flags() & VARARGS) != 0 &&
 972                 (s.isConstructor() ||
 973                     (s.flags() & (STATIC | FINAL |
 974                                   (Feature.PRIVATE_SAFE_VARARGS.allowedInSource(source) ? PRIVATE : 0) )) != 0);
 975         }
 976 
 977     Type checkLocalVarType(DiagnosticPosition pos, Type t, Name name) {
 978         //check that resulting type is not the null type
 979         if (t.hasTag(BOT)) {
 980             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferNull));
 981             return types.createErrorType(t);
 982         } else if (t.hasTag(VOID)) {
 983             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferVoid));
 984             return types.createErrorType(t);
 985         }
 986 
 987         //upward project the initializer type
 988         return types.upward(t, types.captures(t));





































 989     }
 990 
 991     Type checkMethod(final Type mtype,
 992             final Symbol sym,
 993             final Env<AttrContext> env,
 994             final List<JCExpression> argtrees,
 995             final List<Type> argtypes,
 996             final boolean useVarargs,
 997             InferenceContext inferenceContext) {
 998         // System.out.println("call   : " + env.tree);
 999         // System.out.println("method : " + owntype);
1000         // System.out.println("actuals: " + argtypes);
1001         if (inferenceContext.free(mtype)) {
1002             inferenceContext.addFreeTypeListener(List.of(mtype),
1003                     solvedContext -> checkMethod(solvedContext.asInstType(mtype), sym, env, argtrees, argtypes, useVarargs, solvedContext));
1004             return mtype;
1005         }
1006         Type owntype = mtype;
1007         List<Type> formals = owntype.getParameterTypes();
1008         List<Type> nonInferred = sym.type.getParameterTypes();

1166      *  return modifiers together with any implicit modifiers for that symbol.
1167      *  Warning: we can't use flags() here since this method
1168      *  is called during class enter, when flags() would cause a premature
1169      *  completion.
1170      *  @param pos           Position to be used for error reporting.
1171      *  @param flags         The set of modifiers given in a definition.
1172      *  @param sym           The defined symbol.
1173      */
1174     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1175         long mask;
1176         long implicit = 0;
1177 
1178         switch (sym.kind) {
1179         case VAR:
1180             if (TreeInfo.isReceiverParam(tree))
1181                 mask = ReceiverParamFlags;
1182             else if (sym.owner.kind != TYP)
1183                 mask = LocalVarFlags;
1184             else if ((sym.owner.flags_field & INTERFACE) != 0)
1185                 mask = implicit = InterfaceVarFlags;
1186             else
1187                 mask = VarFlags;




1188             break;
1189         case MTH:
1190             if (sym.name == names.init) {
1191                 if ((sym.owner.flags_field & ENUM) != 0) {
1192                     // enum constructors cannot be declared public or
1193                     // protected and must be implicitly or explicitly
1194                     // private
1195                     implicit = PRIVATE;
1196                     mask = PRIVATE;
1197                 } else
1198                     mask = ConstructorFlags;
1199             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1200                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1201                     mask = AnnotationTypeElementMask;
1202                     implicit = PUBLIC | ABSTRACT;
1203                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1204                     mask = InterfaceMethodMask;
1205                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1206                     if ((flags & DEFAULT) != 0) {
1207                         implicit |= ABSTRACT;
1208                     }
1209                 } else {
1210                     mask = implicit = InterfaceMethodFlags;
1211                 }
1212             } else if ((sym.owner.flags_field & RECORD) != 0) {
1213                 mask = RecordMethodFlags;
1214             } else {
1215                 mask = MethodFlags;


1216             }
1217             if ((flags & STRICTFP) != 0) {
1218                 warnOnExplicitStrictfp(pos);
1219             }
1220             // Imply STRICTFP if owner has STRICTFP set.
1221             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1222                 ((flags) & Flags.DEFAULT) != 0)
1223                 implicit |= sym.owner.flags_field & STRICTFP;
1224             break;
1225         case TYP:
1226             if (sym.owner.kind.matches(KindSelector.VAL_MTH) ||
1227                     (sym.isDirectlyOrIndirectlyLocal() && (flags & ANNOTATION) != 0)) {
1228                 boolean implicitlyStatic = !sym.isAnonymous() &&
1229                         ((flags & RECORD) != 0 || (flags & ENUM) != 0 || (flags & INTERFACE) != 0);
1230                 boolean staticOrImplicitlyStatic = (flags & STATIC) != 0 || implicitlyStatic;
1231                 // local statics are allowed only if records are allowed too
1232                 mask = staticOrImplicitlyStatic && allowRecords && (flags & ANNOTATION) == 0 ? StaticLocalFlags : LocalClassFlags;
1233                 implicit = implicitlyStatic ? STATIC : implicit;
1234             } else if (sym.owner.kind == TYP) {
1235                 // statics in inner classes are allowed only if records are allowed too
1236                 mask = ((flags & STATIC) != 0) && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedMemberStaticClassFlags : ExtendedMemberClassFlags;
1237                 if (sym.owner.owner.kind == PCK ||
1238                     (sym.owner.flags_field & STATIC) != 0) {
1239                     mask |= STATIC;
1240                 } else if (!allowRecords && ((flags & ENUM) != 0 || (flags & RECORD) != 0)) {
1241                     log.error(pos, Errors.StaticDeclarationNotAllowedInInnerClasses);
1242                 }
1243                 // Nested interfaces and enums are always STATIC (Spec ???)
1244                 if ((flags & (INTERFACE | ENUM | RECORD)) != 0 ) implicit = STATIC;
1245             } else {
1246                 mask = ExtendedClassFlags;
1247             }
1248             // Interfaces are always ABSTRACT
1249             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1250 
1251             if ((flags & ENUM) != 0) {
1252                 // enums can't be declared abstract, final, sealed or non-sealed
1253                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED);
1254                 implicit |= implicitEnumFinalFlag(tree);
1255             }
1256             if ((flags & RECORD) != 0) {
1257                 // records can't be declared abstract
1258                 mask &= ~ABSTRACT;
1259                 implicit |= FINAL;
1260             }
1261             if ((flags & STRICTFP) != 0) {
1262                 warnOnExplicitStrictfp(pos);
1263             }
1264             // Imply STRICTFP if owner has STRICTFP set.
1265             implicit |= sym.owner.flags_field & STRICTFP;
1266             break;
1267         default:
1268             throw new AssertionError();
1269         }
1270         long illegal = flags & ExtendedStandardFlags & ~mask;
1271         if (illegal != 0) {
1272             if ((illegal & INTERFACE) != 0) {
1273                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1274                 mask |= INTERFACE;
1275             }
1276             else {
1277                 log.error(pos,
1278                         Errors.ModNotAllowedHere(asFlagSet(illegal)));
1279             }
1280         }
1281         else if ((sym.kind == TYP ||
1282                   // ISSUE: Disallowing abstract&private is no longer appropriate
1283                   // in the presence of inner classes. Should it be deleted here?
1284                   checkDisjoint(pos, flags,
1285                                 ABSTRACT,
1286                                 PRIVATE | STATIC | DEFAULT))
1287                  &&
1288                  checkDisjoint(pos, flags,
1289                                 STATIC | PRIVATE,
1290                                 DEFAULT)
1291                  &&
1292                  checkDisjoint(pos, flags,
1293                                ABSTRACT | INTERFACE,
1294                                FINAL | NATIVE | SYNCHRONIZED)
1295                  &&
1296                  checkDisjoint(pos, flags,
1297                                PUBLIC,
1298                                PRIVATE | PROTECTED)
1299                  &&
1300                  checkDisjoint(pos, flags,
1301                                PRIVATE,
1302                                PUBLIC | PROTECTED)
1303                  &&
1304                  checkDisjoint(pos, flags,
1305                                FINAL,
1306                                VOLATILE)
1307                  &&
1308                  (sym.kind == TYP ||
1309                   checkDisjoint(pos, flags,
1310                                 ABSTRACT | NATIVE,
1311                                 STRICTFP))
1312                  && checkDisjoint(pos, flags,
1313                                 FINAL,
1314                            SEALED | NON_SEALED)
1315                  && checkDisjoint(pos, flags,
1316                                 SEALED,
1317                            FINAL | NON_SEALED)
1318                  && checkDisjoint(pos, flags,
1319                                 SEALED,
1320                                 ANNOTATION)) {
1321             // skip
1322         }
1323         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1324     }

1473         @Override
1474         public void visitWildcard(JCWildcard tree) {
1475             if (tree.inner != null)
1476                 validateTree(tree.inner, true, isOuter);
1477         }
1478 
1479         @Override
1480         public void visitSelect(JCFieldAccess tree) {
1481             if (tree.type.hasTag(CLASS)) {
1482                 visitSelectInternal(tree);
1483 
1484                 // Check that this type is either fully parameterized, or
1485                 // not parameterized at all.
1486                 if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty())
1487                     log.error(tree.pos(), Errors.ImproperlyFormedTypeParamMissing);
1488             }
1489         }
1490 
1491         public void visitSelectInternal(JCFieldAccess tree) {
1492             if (tree.type.tsym.isStatic() &&
1493                 tree.selected.type.isParameterized()) {

1494                 // The enclosing type is not a class, so we are
1495                 // looking at a static member type.  However, the
1496                 // qualifying expression is parameterized.





1497                 log.error(tree.pos(), Errors.CantSelectStaticClassFromParamType);
1498             } else {
1499                 // otherwise validate the rest of the expression
1500                 tree.selected.accept(this);
1501             }
1502         }
1503 
1504         @Override
1505         public void visitAnnotatedType(JCAnnotatedType tree) {
1506             tree.underlyingType.accept(this);
1507         }
1508 
1509         @Override
1510         public void visitTypeIdent(JCPrimitiveTypeTree that) {
1511             if (that.type.hasTag(TypeTag.VOID)) {
1512                 log.error(that.pos(), Errors.VoidNotAllowedHere);
1513             }
1514             super.visitTypeIdent(that);
1515         }
1516 

1803             return;
1804         }
1805 
1806         if ((m.owner.flags() & ANNOTATION) != 0) {
1807             // handled in validateAnnotationMethod
1808             return;
1809         }
1810 
1811         // Error if overriding method has weaker access (JLS 8.4.6.3).
1812         if (protection(m.flags()) > protection(other.flags())) {
1813             log.error(TreeInfo.diagnosticPositionFor(m, tree),
1814                       (other.flags() & AccessFlags) == 0 ?
1815                               Errors.OverrideWeakerAccess(cannotOverride(m, other),
1816                                                           "package") :
1817                               Errors.OverrideWeakerAccess(cannotOverride(m, other),
1818                                                           asFlagSet(other.flags() & AccessFlags)));
1819             m.flags_field |= BAD_OVERRIDE;
1820             return;
1821         }
1822 









1823         Type mt = types.memberType(origin.type, m);
1824         Type ot = types.memberType(origin.type, other);
1825         // Error if overriding result type is different
1826         // (or, in the case of generics mode, not a subtype) of
1827         // overridden result type. We have to rename any type parameters
1828         // before comparing types.
1829         List<Type> mtvars = mt.getTypeArguments();
1830         List<Type> otvars = ot.getTypeArguments();
1831         Type mtres = mt.getReturnType();
1832         Type otres = types.subst(ot.getReturnType(), otvars, mtvars);
1833 
1834         overrideWarner.clear();
1835         boolean resultTypesOK =
1836             types.returnTypeSubstitutable(mt, ot, otres, overrideWarner);
1837         if (!resultTypesOK) {
1838             if ((m.flags() & STATIC) != 0 && (other.flags() & STATIC) != 0) {
1839                 log.error(TreeInfo.diagnosticPositionFor(m, tree),
1840                           Errors.OverrideIncompatibleRet(Fragments.CantHide(m, m.location(), other,
1841                                         other.location()), mtres, otres));
1842                 m.flags_field |= BAD_OVERRIDE;

2115             if (recordComponent.isPresent()) {
2116                 return;
2117             }
2118         }
2119 
2120         for (Type t = origin.type; t.hasTag(CLASS);
2121              t = types.supertype(t)) {
2122             if (t != origin.type) {
2123                 checkOverride(tree, t, origin, m);
2124             }
2125             for (Type t2 : types.interfaces(t)) {
2126                 checkOverride(tree, t2, origin, m);
2127             }
2128         }
2129 
2130         final boolean explicitOverride = m.attribute(syms.overrideType.tsym) != null;
2131         // Check if this method must override a super method due to being annotated with @Override
2132         // or by virtue of being a member of a diamond inferred anonymous class. Latter case is to
2133         // be treated "as if as they were annotated" with @Override.
2134         boolean mustOverride = explicitOverride ||
2135                 (env.info.isAnonymousDiamond && !m.isConstructor() && !m.isPrivate());

2136         if (mustOverride && !isOverrider(m)) {
2137             DiagnosticPosition pos = tree.pos();
2138             for (JCAnnotation a : tree.getModifiers().annotations) {
2139                 if (a.annotationType.type.tsym == syms.overrideType.tsym) {
2140                     pos = a.pos();
2141                     break;
2142                 }
2143             }
2144             log.error(pos,
2145                       explicitOverride ? (m.isStatic() ? Errors.StaticMethodsCannotBeAnnotatedWithOverride : Errors.MethodDoesNotOverrideSuperclass) :
2146                                 Errors.AnonymousDiamondMethodDoesNotOverrideSuperclass(Fragments.DiamondAnonymousMethodsImplicitlyOverride));
2147         }
2148     }
2149 
2150     void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) {
2151         TypeSymbol c = site.tsym;
2152         for (Symbol sym : c.members().getSymbolsByName(m.name)) {
2153             if (m.overrides(sym, origin, types, false)) {
2154                 if ((sym.flags() & ABSTRACT) == 0) {
2155                     checkOverride(tree, m, (MethodSymbol)sym, origin);

2241                 cf.test(s2) &&
2242                 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
2243     }
2244 
2245 
2246     /** Check that all abstract members of given class have definitions.
2247      *  @param pos          Position to be used for error reporting.
2248      *  @param c            The class.
2249      */
2250     void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
2251         MethodSymbol undef = types.firstUnimplementedAbstract(c);
2252         if (undef != null) {
2253             MethodSymbol undef1 =
2254                 new MethodSymbol(undef.flags(), undef.name,
2255                                  types.memberType(c.type, undef), undef.owner);
2256             log.error(pos,
2257                       Errors.DoesNotOverrideAbstract(c, undef1, undef1.location()));
2258         }
2259     }
2260 






































2261     void checkNonCyclicDecl(JCClassDecl tree) {
2262         CycleChecker cc = new CycleChecker();
2263         cc.scan(tree);
2264         if (!cc.errorFound && !cc.partialCheck) {
2265             tree.sym.flags_field |= ACYCLIC;
2266         }
2267     }
2268 
2269     class CycleChecker extends TreeScanner {
2270 
2271         Set<Symbol> seenClasses = new HashSet<>();
2272         boolean errorFound = false;
2273         boolean partialCheck = false;
2274 
2275         private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
2276             if (sym != null && sym.kind == TYP) {
2277                 Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym);
2278                 if (classEnv != null) {
2279                     DiagnosticSource prevSource = log.currentSource();
2280                     try {

2489     /** Check that all abstract methods implemented by a class are
2490      *  mutually compatible.
2491      *  @param pos          Position to be used for error reporting.
2492      *  @param c            The class whose interfaces are checked.
2493      */
2494     void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
2495         List<Type> supertypes = types.interfaces(c);
2496         Type supertype = types.supertype(c);
2497         if (supertype.hasTag(CLASS) &&
2498             (supertype.tsym.flags() & ABSTRACT) != 0)
2499             supertypes = supertypes.prepend(supertype);
2500         for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
2501             if (!l.head.getTypeArguments().isEmpty() &&
2502                 !checkCompatibleAbstracts(pos, l.head, l.head, c))
2503                 return;
2504             for (List<Type> m = supertypes; m != l; m = m.tail)
2505                 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
2506                     return;
2507         }
2508         checkCompatibleConcretes(pos, c);










2509     }
2510 
2511     /** Check that all non-override equivalent methods accessible from 'site'
2512      *  are mutually compatible (JLS 8.4.8/9.4.1).
2513      *
2514      *  @param pos  Position to be used for error reporting.
2515      *  @param site The class whose methods are checked.
2516      *  @param sym  The method symbol to be checked.
2517      */
2518     void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
2519          ClashFilter cf = new ClashFilter(site);
2520         //for each method m1 that is overridden (directly or indirectly)
2521         //by method 'sym' in 'site'...
2522 
2523         List<MethodSymbol> potentiallyAmbiguousList = List.nil();
2524         boolean overridesAny = false;
2525         ArrayList<Symbol> symbolsByName = new ArrayList<>();
2526         types.membersClosure(site, false).getSymbolsByName(sym.name, cf).forEach(symbolsByName::add);
2527         for (Symbol m1 : symbolsByName) {
2528             if (!sym.overrides(m1, site.tsym, types, false)) {

 118     protected Check(Context context) {
 119         context.put(checkKey, this);
 120 
 121         names = Names.instance(context);
 122         log = Log.instance(context);
 123         rs = Resolve.instance(context);
 124         syms = Symtab.instance(context);
 125         enter = Enter.instance(context);
 126         deferredAttr = DeferredAttr.instance(context);
 127         infer = Infer.instance(context);
 128         types = Types.instance(context);
 129         typeAnnotations = TypeAnnotations.instance(context);
 130         diags = JCDiagnostic.Factory.instance(context);
 131         Options options = Options.instance(context);
 132         lint = Lint.instance(context);
 133         fileManager = context.get(JavaFileManager.class);
 134 
 135         source = Source.instance(context);
 136         target = Target.instance(context);
 137         warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers");

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

 482         uncheckedHandler.clear();
 483         sunApiHandler.clear();
 484     }
 485 
 486     public void putCompiled(ClassSymbol csym) {
 487         compiled.put(Pair.of(csym.packge().modle, csym.flatname), csym);
 488     }
 489 
 490     public ClassSymbol getCompiled(ClassSymbol csym) {
 491         return compiled.get(Pair.of(csym.packge().modle, csym.flatname));
 492     }
 493 
 494     public ClassSymbol getCompiled(ModuleSymbol msym, Name flatname) {
 495         return compiled.get(Pair.of(msym, flatname));
 496     }
 497 
 498     public void removeCompiled(ClassSymbol csym) {
 499         compiled.remove(Pair.of(csym.packge().modle, csym.flatname));
 500     }
 501 
 502     /* *************************************************************************
 503  * Type Checking
 504  **************************************************************************/
 505 
 506     /**
 507      * A check context is an object that can be used to perform compatibility
 508      * checks - depending on the check context, meaning of 'compatibility' might
 509      * vary significantly.
 510      */
 511     public interface CheckContext {
 512         /**
 513          * Is type 'found' compatible with type 'req' in given context
 514          */
 515         boolean compatible(Type found, Type req, Warner warn);
 516         /**
 517          * Report a check error
 518          */
 519         void report(DiagnosticPosition pos, JCDiagnostic details);
 520         /**
 521          * Obtain a warner for this check context
 522          */

 588         public String toString() {
 589             return "CheckContext: basicHandler";
 590         }
 591     };
 592 
 593     /** Check that a given type is assignable to a given proto-type.
 594      *  If it is, return the type, otherwise return errType.
 595      *  @param pos        Position to be used for error reporting.
 596      *  @param found      The type that was found.
 597      *  @param req        The type that was required.
 598      */
 599     public Type checkType(DiagnosticPosition pos, Type found, Type req) {
 600         return checkType(pos, found, req, basicHandler);
 601     }
 602 
 603     Type checkType(final DiagnosticPosition pos, final Type found, final Type req, final CheckContext checkContext) {
 604         final InferenceContext inferenceContext = checkContext.inferenceContext();
 605         if (inferenceContext.free(req) || inferenceContext.free(found)) {
 606             inferenceContext.addFreeTypeListener(List.of(req, found),
 607                     solvedContext -> checkType(pos, solvedContext.asInstType(found), solvedContext.asInstType(req), checkContext));
 608         } else {
 609             if (found.hasTag(CLASS)) {
 610                 if (inferenceContext != infer.emptyContext)
 611                     checkParameterizationByPrimitiveClass(pos, found);
 612             }
 613         }
 614         if (req.hasTag(ERROR))
 615             return req;
 616         if (req.hasTag(NONE))
 617             return found;
 618         if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) {
 619             return found;
 620         } else {
 621             if (found.isNumeric() && req.isNumeric()) {
 622                 checkContext.report(pos, diags.fragment(Fragments.PossibleLossOfPrecision(found, req)));
 623                 return types.createErrorType(found);
 624             }
 625             checkContext.report(pos, diags.fragment(Fragments.InconvertibleTypes(found, req)));
 626             return types.createErrorType(found);
 627         }
 628     }
 629 
 630     /** Check that a given type can be cast to a given target type.
 631      *  Return the result of the cast.
 632      *  @param pos        Position to be used for error reporting.

 723     /** Check that type is a class or interface type.
 724      *  @param pos           Position to be used for error reporting.
 725      *  @param t             The type to be checked.
 726      */
 727     Type checkClassType(DiagnosticPosition pos, Type t) {
 728         if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) {
 729             return typeTagError(pos,
 730                                 diags.fragment(Fragments.TypeReqClass),
 731                                 asTypeParam(t));
 732         } else {
 733             return t;
 734         }
 735     }
 736     //where
 737         private Object asTypeParam(Type t) {
 738             return (t.hasTag(TYPEVAR))
 739                                     ? diags.fragment(Fragments.TypeParameter(t))
 740                                     : t;
 741         }
 742 
 743     void checkSuperConstraintsOfPrimitiveClass(DiagnosticPosition pos, ClassSymbol c) {
 744         for(Type st = types.supertype(c.type); st != Type.noType; st = types.supertype(st)) {
 745             if (st == null || st.tsym == null || st.tsym.kind == ERR)
 746                 return;
 747             if  (st.tsym == syms.objectType.tsym)
 748                 return;
 749             if (!st.tsym.isAbstract()) {
 750                 log.error(pos, Errors.ConcreteSupertypeForPrimitiveClass(c, st));
 751             }
 752             if ((st.tsym.flags() & HASINITBLOCK) != 0) {
 753                 log.error(pos, Errors.SuperClassDeclaresInitBlock(c, st));
 754             }
 755             // No instance fields and no arged constructors both mean inner classes
 756             // cannot be super classes for primitive classes.
 757             Type encl = st.getEnclosingType();
 758             if (encl != null && encl.hasTag(CLASS)) {
 759                 log.error(pos, Errors.SuperClassCannotBeInner(c, st));
 760             }
 761             for (Symbol s : st.tsym.members().getSymbols(NON_RECURSIVE)) {
 762                 switch (s.kind) {
 763                 case VAR:
 764                     if ((s.flags() & STATIC) == 0) {
 765                         log.error(pos, Errors.SuperFieldNotAllowed(s, c, st));
 766                     }
 767                     break;
 768                 case MTH:
 769                     if ((s.flags() & SYNCHRONIZED) != 0) {
 770                         log.error(pos, Errors.SuperMethodCannotBeSynchronized(s, c, st));
 771                     } else if (s.isConstructor()) {
 772                         MethodSymbol m = (MethodSymbol)s;
 773                         if (m.getParameters().size() > 0) {
 774                             log.error(pos, Errors.SuperConstructorCannotTakeArguments(m, c, st));
 775                         } else {
 776                             if ((m.flags() & (GENERATEDCONSTR | EMPTYNOARGCONSTR)) == 0) {
 777                                 log.error(pos, Errors.SuperNoArgConstructorMustBeEmpty(m, c, st));
 778                             }
 779                         }
 780                     }
 781                     break;
 782                 }
 783             }
 784         }
 785     }
 786 
 787     /** Check that type is a valid qualifier for a constructor reference expression
 788      */
 789     Type checkConstructorRefType(JCExpression expr, Type t) {
 790         t = checkClassOrArrayType(expr, t);
 791         if (t.hasTag(CLASS)) {
 792             if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
 793                 log.error(expr, Errors.AbstractCantBeInstantiated(t.tsym));
 794                 t = types.createErrorType(t);
 795             } else if ((t.tsym.flags() & ENUM) != 0) {
 796                 log.error(expr, Errors.EnumCantBeInstantiated);
 797                 t = types.createErrorType(t);
 798             } else {
 799                 // Projection types may not be mentioned in constructor references
 800                 JCExpression instantiation = expr;
 801                 if (instantiation.hasTag(TYPEAPPLY))
 802                     instantiation = ((JCTypeApply) instantiation).clazz;
 803                 if (instantiation.hasTag(SELECT)) {
 804                     JCFieldAccess fieldAccess = (JCFieldAccess) instantiation;
 805                     if (fieldAccess.selected.type.tsym.isPrimitiveClass() &&
 806                             (fieldAccess.name == names.ref || fieldAccess.name == names.val)) {
 807                         log.error(expr, Errors.ProjectionCantBeInstantiated);
 808                         t = types.createErrorType(t);
 809                     }
 810                 }
 811                 t = checkClassType(expr, t, true);
 812             }
 813         } else if (t.hasTag(ARRAY)) {
 814             if (!types.isReifiable(((ArrayType)t).elemtype)) {
 815                 log.error(expr, Errors.GenericArrayCreation);
 816                 t = types.createErrorType(t);
 817             }
 818         }
 819         return t;
 820     }
 821 
 822     /** Check that type is a class or interface type.
 823      *  @param pos           Position to be used for error reporting.
 824      *  @param t             The type to be checked.
 825      *  @param noBounds    True if type bounds are illegal here.
 826      */
 827     Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) {
 828         t = checkClassType(pos, t);
 829         if (noBounds && t.isParameterized()) {
 830             List<Type> args = t.getTypeArguments();
 831             while (args.nonEmpty()) {
 832                 if (args.head.hasTag(WILDCARD))
 833                     return typeTagError(pos,
 834                                         diags.fragment(Fragments.TypeReqExact),
 835                                         args.head);
 836                 args = args.tail;
 837             }
 838         }
 839         return t;
 840     }
 841 
 842     /** Check that type is a reference type, i.e. a class, interface or array type
 843      *  or a type variable.
 844      *  @param pos           Position to be used for error reporting.
 845      *  @param t             The type to be checked.
 846      *  @param primitiveClassOK       If false, a primitive class does not qualify
 847      */
 848     Type checkRefType(DiagnosticPosition pos, Type t, boolean primitiveClassOK) {
 849         if (t.isReference() && (primitiveClassOK || !types.isPrimitiveClass(t)))
 850             return t;
 851         else
 852             return typeTagError(pos,
 853                                 diags.fragment(Fragments.TypeReqRef),
 854                                 t);
 855     }
 856 
 857     /** Check that type is an identity type, i.e. not a primitive type
 858      *  nor its reference projection. When not discernible statically,
 859      *  give it the benefit of doubt and defer to runtime.
 860      *
 861      *  @param pos           Position to be used for error reporting.
 862      *  @param t             The type to be checked.
 863      */
 864     Type checkIdentityType(DiagnosticPosition pos, Type t) {
 865 
 866         if (t.isPrimitive() || t.tsym.isPrimitiveClass())
 867             return typeTagError(pos,
 868                     diags.fragment(Fragments.TypeReqIdentity),
 869                     t);
 870 
 871         /* Not appropriate to check
 872          *     if (types.asSuper(t, syms.identityObjectType.tsym) != null)
 873          * since jlO, interface types and abstract types may fail that check
 874          * at compile time.
 875          */
 876 
 877         return t;
 878     }
 879 
 880     /** Check that type is a reference type, i.e. a class, interface or array type
 881      *  or a type variable.
 882      *  @param pos           Position to be used for error reporting.
 883      *  @param t             The type to be checked.
 884      */
 885     Type checkRefType(DiagnosticPosition pos, Type t) {
 886         return checkRefType(pos, t, true);
 887     }
 888 
 889     /** Check that each type is a reference type, i.e. a class, interface or array type
 890      *  or a type variable.
 891      *  @param trees         Original trees, used for error reporting.
 892      *  @param types         The types to be checked.
 893      */
 894     List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
 895         List<JCExpression> tl = trees;
 896         for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
 897             l.head = checkRefType(tl.head.pos(), l.head, false);
 898             tl = tl.tail;
 899         }
 900         return types;
 901     }
 902 
 903     /** Check that type is a null or reference type.
 904      *  @param pos           Position to be used for error reporting.
 905      *  @param t             The type to be checked.
 906      */
 907     Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
 908         if (t.isReference() || t.hasTag(BOT))
 909             return t;
 910         else
 911             return typeTagError(pos,
 912                                 diags.fragment(Fragments.TypeReqRef),
 913                                 t);
 914     }
 915 
 916     /** Check that flag set does not contain elements of two conflicting sets. s
 917      *  Return true if it doesn't.
 918      *  @param pos           Position to be used for error reporting.
 919      *  @param flags         The set of flags to be checked.
 920      *  @param set1          Conflicting flags set #1.
 921      *  @param set2          Conflicting flags set #2.
 922      */
 923     boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) {
 924         if ((flags & set1) != 0 && (flags & set2) != 0) {
 925             log.error(pos,
 926                       Errors.IllegalCombinationOfModifiers(asFlagSet(TreeInfo.firstFlag(flags & set1)),
 927                                                            asFlagSet(TreeInfo.firstFlag(flags & set2))));
 928             return false;
 929         } else
 930             return true;
 931     }
 932 
 933     void checkParameterizationByPrimitiveClass(DiagnosticPosition pos, Type t) {
 934         parameterizationByPrimitiveClassChecker.visit(t, pos);
 935     }
 936 
 937     /** parameterizationByPrimitiveClassChecker: A type visitor that descends down the given type looking for instances of primitive classes
 938      *  being used as type arguments and issues error against those usages.
 939      */
 940     private final Types.SimpleVisitor<Void, DiagnosticPosition> parameterizationByPrimitiveClassChecker =
 941             new Types.SimpleVisitor<Void, DiagnosticPosition>() {
 942 
 943         @Override
 944         public Void visitType(Type t, DiagnosticPosition pos) {
 945             return null;
 946         }
 947 
 948         @Override
 949         public Void visitClassType(ClassType t, DiagnosticPosition pos) {
 950             for (Type targ : t.allparams()) {
 951                 if (types.isPrimitiveClass(targ)) {
 952                     log.error(pos, Errors.GenericParameterizationWithPrimitiveClass(t));
 953                 }
 954                 visit(targ, pos);
 955             }
 956             return null;
 957         }
 958 
 959         @Override
 960         public Void visitTypeVar(TypeVar t, DiagnosticPosition pos) {
 961              return null;
 962         }
 963 
 964         @Override
 965         public Void visitCapturedType(CapturedType t, DiagnosticPosition pos) {
 966             return null;
 967         }
 968 
 969         @Override
 970         public Void visitArrayType(ArrayType t, DiagnosticPosition pos) {
 971             return visit(t.elemtype, pos);
 972         }
 973 
 974         @Override
 975         public Void visitWildcardType(WildcardType t, DiagnosticPosition pos) {
 976             return visit(t.type, pos);
 977         }
 978     };
 979 
 980 
 981 
 982     /** Check that usage of diamond operator is correct (i.e. diamond should not
 983      * be used with non-generic classes or in anonymous class creation expressions)
 984      */
 985     Type checkDiamond(JCNewClass tree, Type t) {
 986         if (!TreeInfo.isDiamond(tree) ||
 987                 t.isErroneous()) {
 988             return checkClassType(tree.clazz.pos(), t, true);
 989         } else {
 990             if (tree.def != null && !Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.allowedInSource(source)) {
 991                 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.clazz.pos(),
 992                         Errors.CantApplyDiamond1(t, Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.fragment(source.name)));
 993             }
 994             if (t.tsym.type.getTypeArguments().isEmpty()) {
 995                 log.error(tree.clazz.pos(),
 996                           Errors.CantApplyDiamond1(t,
 997                                                    Fragments.DiamondNonGeneric(t)));
 998                 return types.createErrorType(t);
 999             } else if (tree.typeargs != null &&
1000                     tree.typeargs.nonEmpty()) {
1001                 log.error(tree.clazz.pos(),

1110     }
1111     //where
1112         private boolean isTrustMeAllowedOnMethod(Symbol s) {
1113             return (s.flags() & VARARGS) != 0 &&
1114                 (s.isConstructor() ||
1115                     (s.flags() & (STATIC | FINAL |
1116                                   (Feature.PRIVATE_SAFE_VARARGS.allowedInSource(source) ? PRIVATE : 0) )) != 0);
1117         }
1118 
1119     Type checkLocalVarType(DiagnosticPosition pos, Type t, Name name) {
1120         //check that resulting type is not the null type
1121         if (t.hasTag(BOT)) {
1122             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferNull));
1123             return types.createErrorType(t);
1124         } else if (t.hasTag(VOID)) {
1125             log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferVoid));
1126             return types.createErrorType(t);
1127         }
1128 
1129         //upward project the initializer type
1130         Type varType = types.upward(t, types.captures(t));
1131         if (varType.hasTag(CLASS)) {
1132             checkParameterizationByPrimitiveClass(pos, varType);
1133         }
1134         return varType;
1135     }
1136 
1137     public void checkForSuspectClassLiteralComparison(
1138             final JCBinary tree,
1139             final Type leftType,
1140             final Type rightType) {
1141 
1142         if (lint.isEnabled(LintCategory.MIGRATION)) {
1143             if (isInvocationOfGetClass(tree.lhs) && isClassOfSomeInterface(rightType) ||
1144                     isInvocationOfGetClass(tree.rhs) && isClassOfSomeInterface(leftType)) {
1145                 log.warning(LintCategory.MIGRATION, tree.pos(), Warnings.GetClassComparedWithInterface);
1146             }
1147         }
1148     }
1149     //where
1150     private boolean isClassOfSomeInterface(Type someClass) {
1151         if (someClass.tsym.flatName() == names.java_lang_Class) {
1152             List<Type> arguments = someClass.getTypeArguments();
1153             if (arguments.length() == 1) {
1154                 return arguments.head.isInterface();
1155             }
1156         }
1157         return false;
1158     }
1159     //where
1160     private boolean isInvocationOfGetClass(JCExpression tree) {
1161         tree = TreeInfo.skipParens(tree);
1162         if (tree.hasTag(APPLY)) {
1163             JCMethodInvocation apply = (JCMethodInvocation)tree;
1164             MethodSymbol msym = (MethodSymbol)TreeInfo.symbol(apply.meth);
1165             return msym.name == names.getClass && msym.implementedIn(syms.objectType.tsym, types) != null;
1166         }
1167         return false;
1168     }
1169 
1170     Type checkMethod(final Type mtype,
1171             final Symbol sym,
1172             final Env<AttrContext> env,
1173             final List<JCExpression> argtrees,
1174             final List<Type> argtypes,
1175             final boolean useVarargs,
1176             InferenceContext inferenceContext) {
1177         // System.out.println("call   : " + env.tree);
1178         // System.out.println("method : " + owntype);
1179         // System.out.println("actuals: " + argtypes);
1180         if (inferenceContext.free(mtype)) {
1181             inferenceContext.addFreeTypeListener(List.of(mtype),
1182                     solvedContext -> checkMethod(solvedContext.asInstType(mtype), sym, env, argtrees, argtypes, useVarargs, solvedContext));
1183             return mtype;
1184         }
1185         Type owntype = mtype;
1186         List<Type> formals = owntype.getParameterTypes();
1187         List<Type> nonInferred = sym.type.getParameterTypes();

1345      *  return modifiers together with any implicit modifiers for that symbol.
1346      *  Warning: we can't use flags() here since this method
1347      *  is called during class enter, when flags() would cause a premature
1348      *  completion.
1349      *  @param pos           Position to be used for error reporting.
1350      *  @param flags         The set of modifiers given in a definition.
1351      *  @param sym           The defined symbol.
1352      */
1353     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1354         long mask;
1355         long implicit = 0;
1356 
1357         switch (sym.kind) {
1358         case VAR:
1359             if (TreeInfo.isReceiverParam(tree))
1360                 mask = ReceiverParamFlags;
1361             else if (sym.owner.kind != TYP)
1362                 mask = LocalVarFlags;
1363             else if ((sym.owner.flags_field & INTERFACE) != 0)
1364                 mask = implicit = InterfaceVarFlags;
1365             else {
1366                 mask = VarFlags;
1367                 if (sym.owner.isPrimitiveClass() && (flags & STATIC) == 0) {
1368                     implicit |= FINAL;
1369                 }
1370             }
1371             break;
1372         case MTH:
1373             if (sym.name == names.init) {
1374                 if ((sym.owner.flags_field & ENUM) != 0) {
1375                     // enum constructors cannot be declared public or
1376                     // protected and must be implicitly or explicitly
1377                     // private
1378                     implicit = PRIVATE;
1379                     mask = PRIVATE;
1380                 } else
1381                     mask = ConstructorFlags;
1382             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1383                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1384                     mask = AnnotationTypeElementMask;
1385                     implicit = PUBLIC | ABSTRACT;
1386                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1387                     mask = InterfaceMethodMask;
1388                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1389                     if ((flags & DEFAULT) != 0) {
1390                         implicit |= ABSTRACT;
1391                     }
1392                 } else {
1393                     mask = implicit = InterfaceMethodFlags;
1394                 }
1395             } else if ((sym.owner.flags_field & RECORD) != 0) {
1396                 mask = RecordMethodFlags;
1397             } else {
1398                 // instance methods of primitive classes do not have a monitor associated with their `this'
1399                 mask = ((sym.owner.flags_field & PRIMITIVE_CLASS) != 0 && (flags & Flags.STATIC) == 0) ?
1400                         MethodFlags & ~SYNCHRONIZED : MethodFlags;
1401             }
1402             if ((flags & STRICTFP) != 0) {
1403                 warnOnExplicitStrictfp(pos);
1404             }
1405             // Imply STRICTFP if owner has STRICTFP set.
1406             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1407                 ((flags) & Flags.DEFAULT) != 0)
1408                 implicit |= sym.owner.flags_field & STRICTFP;
1409             break;
1410         case TYP:
1411             if (sym.owner.kind.matches(KindSelector.VAL_MTH) ||
1412                     (sym.isDirectlyOrIndirectlyLocal() && (flags & ANNOTATION) != 0)) {
1413                 boolean implicitlyStatic = !sym.isAnonymous() &&
1414                         ((flags & RECORD) != 0 || (flags & ENUM) != 0 || (flags & INTERFACE) != 0);
1415                 boolean staticOrImplicitlyStatic = (flags & STATIC) != 0 || implicitlyStatic;
1416                 // local statics are allowed only if records are allowed too
1417                 mask = staticOrImplicitlyStatic && allowRecords && (flags & ANNOTATION) == 0 ? StaticLocalFlags : LocalClassFlags;
1418                 implicit = implicitlyStatic ? STATIC : implicit;
1419             } else if (sym.owner.kind == TYP) {
1420                 // statics in inner classes are allowed only if records are allowed too
1421                 mask = ((flags & STATIC) != 0) && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedMemberStaticClassFlags : ExtendedMemberClassFlags;
1422                 if (sym.owner.owner.kind == PCK ||
1423                     (sym.owner.flags_field & STATIC) != 0) {
1424                     mask |= STATIC;
1425                 } else if (!allowRecords && ((flags & ENUM) != 0 || (flags & RECORD) != 0)) {
1426                     log.error(pos, Errors.StaticDeclarationNotAllowedInInnerClasses);
1427                 }
1428                 // Nested interfaces and enums are always STATIC (Spec ???)
1429                 if ((flags & (INTERFACE | ENUM | RECORD)) != 0 ) implicit = STATIC;
1430             } else {
1431                 mask = ExtendedClassFlags;
1432             }
1433             // Interfaces are always ABSTRACT
1434             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1435 
1436             if ((flags & ENUM) != 0) {
1437                 // enums can't be declared abstract, final, sealed or non-sealed or primitive
1438                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED | PRIMITIVE_CLASS);
1439                 implicit |= implicitEnumFinalFlag(tree);
1440             }
1441             if ((flags & RECORD) != 0) {
1442                 // records can't be declared abstract
1443                 mask &= ~ABSTRACT;
1444                 implicit |= FINAL;
1445             }
1446             if ((flags & STRICTFP) != 0) {
1447                 warnOnExplicitStrictfp(pos);
1448             }
1449             // Imply STRICTFP if owner has STRICTFP set.
1450             implicit |= sym.owner.flags_field & STRICTFP;
1451             break;
1452         default:
1453             throw new AssertionError();
1454         }
1455         long illegal = flags & ExtendedStandardFlags & ~mask;
1456         if (illegal != 0) {
1457             if ((illegal & INTERFACE) != 0) {
1458                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1459                 mask |= INTERFACE;
1460             }
1461             else {
1462                 log.error(pos,
1463                         Errors.ModNotAllowedHere(asFlagSet(illegal)));
1464             }
1465         }
1466         else if ((sym.kind == TYP ||
1467                   // ISSUE: Disallowing abstract&private is no longer appropriate
1468                   // in the presence of inner classes. Should it be deleted here?
1469                   checkDisjoint(pos, flags,
1470                                 ABSTRACT,
1471                                 PRIVATE | STATIC | DEFAULT))
1472                  &&
1473                  checkDisjoint(pos, flags,
1474                                 STATIC | PRIVATE,
1475                                 DEFAULT)
1476                  &&
1477                  checkDisjoint(pos, flags,
1478                                ABSTRACT | INTERFACE,
1479                                FINAL | NATIVE | SYNCHRONIZED | PRIMITIVE_CLASS)
1480                  &&
1481                  checkDisjoint(pos, flags,
1482                                PUBLIC,
1483                                PRIVATE | PROTECTED)
1484                  &&
1485                  checkDisjoint(pos, flags,
1486                                PRIVATE,
1487                                PUBLIC | PROTECTED)
1488                  &&
1489                  checkDisjoint(pos, (flags | implicit), // complain against volatile & implcitly final entities too.
1490                                FINAL,
1491                                VOLATILE)
1492                  &&
1493                  (sym.kind == TYP ||
1494                   checkDisjoint(pos, flags,
1495                                 ABSTRACT | NATIVE,
1496                                 STRICTFP))
1497                  && checkDisjoint(pos, flags,
1498                                 FINAL,
1499                            SEALED | NON_SEALED)
1500                  && checkDisjoint(pos, flags,
1501                                 SEALED,
1502                            FINAL | NON_SEALED)
1503                  && checkDisjoint(pos, flags,
1504                                 SEALED,
1505                                 ANNOTATION)) {
1506             // skip
1507         }
1508         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1509     }

1658         @Override
1659         public void visitWildcard(JCWildcard tree) {
1660             if (tree.inner != null)
1661                 validateTree(tree.inner, true, isOuter);
1662         }
1663 
1664         @Override
1665         public void visitSelect(JCFieldAccess tree) {
1666             if (tree.type.hasTag(CLASS)) {
1667                 visitSelectInternal(tree);
1668 
1669                 // Check that this type is either fully parameterized, or
1670                 // not parameterized at all.
1671                 if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty())
1672                     log.error(tree.pos(), Errors.ImproperlyFormedTypeParamMissing);
1673             }
1674         }
1675 
1676         public void visitSelectInternal(JCFieldAccess tree) {
1677             if (tree.type.tsym.isStatic() &&
1678                 tree.selected.type.isParameterized() &&
1679                     (!tree.type.tsym.isPrimitiveClass() || (tree.name != names.ref && tree.name != names.val))) {
1680                 // The enclosing type is not a class, so we are
1681                 // looking at a static member type.  However, the
1682                 // qualifying expression is parameterized.
1683 
1684                 // Tolerate the pseudo-select V.ref/V.val: V<T>.ref/val will be static if V<T> is and
1685                 // should not be confused as selecting a static member of a parameterized type. Both
1686                 // these constructs are illegal anyway & will be more appropriately complained against shortly.
1687                 // Note: the canonicl form is V.ref<T> and V.val<T> not V<T>.ref and V<T>.val
1688                 log.error(tree.pos(), Errors.CantSelectStaticClassFromParamType);
1689             } else {
1690                 // otherwise validate the rest of the expression
1691                 tree.selected.accept(this);
1692             }
1693         }
1694 
1695         @Override
1696         public void visitAnnotatedType(JCAnnotatedType tree) {
1697             tree.underlyingType.accept(this);
1698         }
1699 
1700         @Override
1701         public void visitTypeIdent(JCPrimitiveTypeTree that) {
1702             if (that.type.hasTag(TypeTag.VOID)) {
1703                 log.error(that.pos(), Errors.VoidNotAllowedHere);
1704             }
1705             super.visitTypeIdent(that);
1706         }
1707 

1994             return;
1995         }
1996 
1997         if ((m.owner.flags() & ANNOTATION) != 0) {
1998             // handled in validateAnnotationMethod
1999             return;
2000         }
2001 
2002         // Error if overriding method has weaker access (JLS 8.4.6.3).
2003         if (protection(m.flags()) > protection(other.flags())) {
2004             log.error(TreeInfo.diagnosticPositionFor(m, tree),
2005                       (other.flags() & AccessFlags) == 0 ?
2006                               Errors.OverrideWeakerAccess(cannotOverride(m, other),
2007                                                           "package") :
2008                               Errors.OverrideWeakerAccess(cannotOverride(m, other),
2009                                                           asFlagSet(other.flags() & AccessFlags)));
2010             m.flags_field |= BAD_OVERRIDE;
2011             return;
2012         }
2013 
2014         if (origin.isPrimitiveClass() && other.owner == syms.objectType.tsym && m.type.getParameterTypes().size() == 0) {
2015             if (m.name == names.clone || m.name == names.finalize) {
2016                 log.error(TreeInfo.diagnosticPositionFor(m, tree),
2017                         Errors.PrimitiveClassMayNotOverride(m.name));
2018                 m.flags_field |= BAD_OVERRIDE;
2019                 return;
2020             }
2021         }
2022 
2023         Type mt = types.memberType(origin.type, m);
2024         Type ot = types.memberType(origin.type, other);
2025         // Error if overriding result type is different
2026         // (or, in the case of generics mode, not a subtype) of
2027         // overridden result type. We have to rename any type parameters
2028         // before comparing types.
2029         List<Type> mtvars = mt.getTypeArguments();
2030         List<Type> otvars = ot.getTypeArguments();
2031         Type mtres = mt.getReturnType();
2032         Type otres = types.subst(ot.getReturnType(), otvars, mtvars);
2033 
2034         overrideWarner.clear();
2035         boolean resultTypesOK =
2036             types.returnTypeSubstitutable(mt, ot, otres, overrideWarner);
2037         if (!resultTypesOK) {
2038             if ((m.flags() & STATIC) != 0 && (other.flags() & STATIC) != 0) {
2039                 log.error(TreeInfo.diagnosticPositionFor(m, tree),
2040                           Errors.OverrideIncompatibleRet(Fragments.CantHide(m, m.location(), other,
2041                                         other.location()), mtres, otres));
2042                 m.flags_field |= BAD_OVERRIDE;

2315             if (recordComponent.isPresent()) {
2316                 return;
2317             }
2318         }
2319 
2320         for (Type t = origin.type; t.hasTag(CLASS);
2321              t = types.supertype(t)) {
2322             if (t != origin.type) {
2323                 checkOverride(tree, t, origin, m);
2324             }
2325             for (Type t2 : types.interfaces(t)) {
2326                 checkOverride(tree, t2, origin, m);
2327             }
2328         }
2329 
2330         final boolean explicitOverride = m.attribute(syms.overrideType.tsym) != null;
2331         // Check if this method must override a super method due to being annotated with @Override
2332         // or by virtue of being a member of a diamond inferred anonymous class. Latter case is to
2333         // be treated "as if as they were annotated" with @Override.
2334         boolean mustOverride = explicitOverride ||
2335                 (env.info.isAnonymousDiamond && !m.isConstructor() && !m.isPrivate() &&
2336                         (!m.owner.isPrimitiveClass() || (tree.body.flags & SYNTHETIC) == 0));
2337         if (mustOverride && !isOverrider(m)) {
2338             DiagnosticPosition pos = tree.pos();
2339             for (JCAnnotation a : tree.getModifiers().annotations) {
2340                 if (a.annotationType.type.tsym == syms.overrideType.tsym) {
2341                     pos = a.pos();
2342                     break;
2343                 }
2344             }
2345             log.error(pos,
2346                       explicitOverride ? (m.isStatic() ? Errors.StaticMethodsCannotBeAnnotatedWithOverride : Errors.MethodDoesNotOverrideSuperclass) :
2347                                 Errors.AnonymousDiamondMethodDoesNotOverrideSuperclass(Fragments.DiamondAnonymousMethodsImplicitlyOverride));
2348         }
2349     }
2350 
2351     void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) {
2352         TypeSymbol c = site.tsym;
2353         for (Symbol sym : c.members().getSymbolsByName(m.name)) {
2354             if (m.overrides(sym, origin, types, false)) {
2355                 if ((sym.flags() & ABSTRACT) == 0) {
2356                     checkOverride(tree, m, (MethodSymbol)sym, origin);

2442                 cf.test(s2) &&
2443                 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
2444     }
2445 
2446 
2447     /** Check that all abstract members of given class have definitions.
2448      *  @param pos          Position to be used for error reporting.
2449      *  @param c            The class.
2450      */
2451     void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
2452         MethodSymbol undef = types.firstUnimplementedAbstract(c);
2453         if (undef != null) {
2454             MethodSymbol undef1 =
2455                 new MethodSymbol(undef.flags(), undef.name,
2456                                  types.memberType(c.type, undef), undef.owner);
2457             log.error(pos,
2458                       Errors.DoesNotOverrideAbstract(c, undef1, undef1.location()));
2459         }
2460     }
2461 
2462     // A primitive class cannot contain a field of its own type either or indirectly.
2463     void checkNonCyclicMembership(JCClassDecl tree) {
2464         Assert.check((tree.sym.flags_field & LOCKED) == 0);
2465         try {
2466             tree.sym.flags_field |= LOCKED;
2467             for (List<? extends JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
2468                 if (l.head.hasTag(VARDEF)) {
2469                     JCVariableDecl field = (JCVariableDecl) l.head;
2470                     if (cyclePossible(field.sym)) {
2471                         checkNonCyclicMembership((ClassSymbol) field.type.tsym, field.pos());
2472                     }
2473                 }
2474             }
2475         } finally {
2476             tree.sym.flags_field &= ~LOCKED;
2477         }
2478 
2479     }
2480     // where
2481     private void checkNonCyclicMembership(ClassSymbol c, DiagnosticPosition pos) {
2482         if ((c.flags_field & LOCKED) != 0) {
2483             log.error(pos, Errors.CyclicPrimitiveClassMembership(c));
2484             return;
2485         }
2486         try {
2487             c.flags_field |= LOCKED;
2488             for (Symbol fld : c.members().getSymbols(s -> s.kind == VAR && cyclePossible((VarSymbol) s), NON_RECURSIVE)) {
2489                 checkNonCyclicMembership((ClassSymbol) fld.type.tsym, pos);
2490             }
2491         } finally {
2492             c.flags_field &= ~LOCKED;
2493         }
2494     }
2495         // where
2496         private boolean cyclePossible(VarSymbol symbol) {
2497             return (symbol.flags() & STATIC) == 0 && types.isPrimitiveClass(symbol.type);
2498         }
2499 
2500     void checkNonCyclicDecl(JCClassDecl tree) {
2501         CycleChecker cc = new CycleChecker();
2502         cc.scan(tree);
2503         if (!cc.errorFound && !cc.partialCheck) {
2504             tree.sym.flags_field |= ACYCLIC;
2505         }
2506     }
2507 
2508     class CycleChecker extends TreeScanner {
2509 
2510         Set<Symbol> seenClasses = new HashSet<>();
2511         boolean errorFound = false;
2512         boolean partialCheck = false;
2513 
2514         private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
2515             if (sym != null && sym.kind == TYP) {
2516                 Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym);
2517                 if (classEnv != null) {
2518                     DiagnosticSource prevSource = log.currentSource();
2519                     try {

2728     /** Check that all abstract methods implemented by a class are
2729      *  mutually compatible.
2730      *  @param pos          Position to be used for error reporting.
2731      *  @param c            The class whose interfaces are checked.
2732      */
2733     void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
2734         List<Type> supertypes = types.interfaces(c);
2735         Type supertype = types.supertype(c);
2736         if (supertype.hasTag(CLASS) &&
2737             (supertype.tsym.flags() & ABSTRACT) != 0)
2738             supertypes = supertypes.prepend(supertype);
2739         for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
2740             if (!l.head.getTypeArguments().isEmpty() &&
2741                 !checkCompatibleAbstracts(pos, l.head, l.head, c))
2742                 return;
2743             for (List<Type> m = supertypes; m != l; m = m.tail)
2744                 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
2745                     return;
2746         }
2747         checkCompatibleConcretes(pos, c);
2748 
2749         boolean implementsIdentityObject = types.asSuper(c.referenceProjectionOrSelf(), syms.identityObjectType.tsym) != null;
2750         boolean implementsPrimitiveObject = types.asSuper(c.referenceProjectionOrSelf(), syms.primitiveObjectType.tsym) != null;
2751         if (c.tsym.isPrimitiveClass() && implementsIdentityObject) {
2752             log.error(pos, Errors.PrimitiveClassMustNotImplementIdentityObject(c));
2753         } else if (implementsPrimitiveObject && !c.tsym.isPrimitiveClass() && !c.isReferenceProjection() && !c.tsym.isInterface() && !c.tsym.isAbstract()) {
2754             log.error(pos, Errors.IdentityClassMustNotImplementPrimitiveObject(c));
2755         } else if (implementsPrimitiveObject && implementsIdentityObject) {
2756             log.error(pos, Errors.MutuallyIncompatibleSuperInterfaces(c));
2757         }
2758     }
2759 
2760     /** Check that all non-override equivalent methods accessible from 'site'
2761      *  are mutually compatible (JLS 8.4.8/9.4.1).
2762      *
2763      *  @param pos  Position to be used for error reporting.
2764      *  @param site The class whose methods are checked.
2765      *  @param sym  The method symbol to be checked.
2766      */
2767     void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
2768          ClashFilter cf = new ClashFilter(site);
2769         //for each method m1 that is overridden (directly or indirectly)
2770         //by method 'sym' in 'site'...
2771 
2772         List<MethodSymbol> potentiallyAmbiguousList = List.nil();
2773         boolean overridesAny = false;
2774         ArrayList<Symbol> symbolsByName = new ArrayList<>();
2775         types.membersClosure(site, false).getSymbolsByName(sym.name, cf).forEach(symbolsByName::add);
2776         for (Symbol m1 : symbolsByName) {
2777             if (!sym.overrides(m1, site.tsym, types, false)) {
< prev index next >