< prev index next >

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

Print this page

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

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

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





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

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





















































 747     /** Check that type is a valid qualifier for a constructor reference expression
 748      */
 749     Type checkConstructorRefType(DiagnosticPosition pos, Type t) {
 750         t = checkClassOrArrayType(pos, t);
 751         if (t.hasTag(CLASS)) {
 752             if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
 753                 log.error(pos, Errors.AbstractCantBeInstantiated(t.tsym));
 754                 t = types.createErrorType(t);
 755             } else if ((t.tsym.flags() & ENUM) != 0) {
 756                 log.error(pos, Errors.EnumCantBeInstantiated);
 757                 t = types.createErrorType(t);
 758             } else {
 759                 t = checkClassType(pos, t, true);









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

 794      */
 795     Type checkRefType(DiagnosticPosition pos, Type t) {
 796         if (t.isReference())
 797             return t;
 798         else
 799             return typeTagError(pos,
 800                                 diags.fragment(Fragments.TypeReqRef),
 801                                 t);
 802     }
 803 































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

















































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

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





































 997     }
 998 
 999     Type checkMethod(final Type mtype,
1000             final Symbol sym,
1001             final Env<AttrContext> env,
1002             final List<JCExpression> argtrees,
1003             final List<Type> argtypes,
1004             final boolean useVarargs,
1005             InferenceContext inferenceContext) {
1006         // System.out.println("call   : " + env.tree);
1007         // System.out.println("method : " + owntype);
1008         // System.out.println("actuals: " + argtypes);
1009         if (inferenceContext.free(mtype)) {
1010             inferenceContext.addFreeTypeListener(List.of(mtype),
1011                     solvedContext -> checkMethod(solvedContext.asInstType(mtype), sym, env, argtrees, argtypes, useVarargs, solvedContext));
1012             return mtype;
1013         }
1014         Type owntype = mtype;
1015         List<Type> formals = owntype.getParameterTypes();
1016         List<Type> nonInferred = sym.type.getParameterTypes();

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




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

1222             } else {
1223                 mask = MethodFlags;


1224             }
1225             if ((flags & STRICTFP) != 0) {
1226                 warnOnExplicitStrictfp(pos);
1227             }
1228             // Imply STRICTFP if owner has STRICTFP set.
1229             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1230                 ((flags) & Flags.DEFAULT) != 0)
1231                 implicit |= sym.owner.flags_field & STRICTFP;
1232             break;
1233         case TYP:
1234             if (sym.owner.kind.matches(KindSelector.VAL_MTH) ||
1235                     (sym.isDirectlyOrIndirectlyLocal() && (flags & ANNOTATION) != 0)) {
1236                 boolean implicitlyStatic = !sym.isAnonymous() &&
1237                         ((flags & RECORD) != 0 || (flags & ENUM) != 0 || (flags & INTERFACE) != 0);
1238                 boolean staticOrImplicitlyStatic = (flags & STATIC) != 0 || implicitlyStatic;
1239                 // local statics are allowed only if records are allowed too
1240                 mask = staticOrImplicitlyStatic && allowRecords && (flags & ANNOTATION) == 0 ? StaticLocalFlags : LocalClassFlags;
1241                 implicit = implicitlyStatic ? STATIC : implicit;
1242             } else if (sym.owner.kind == TYP) {
1243                 // statics in inner classes are allowed only if records are allowed too
1244                 mask = ((flags & STATIC) != 0) && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedMemberStaticClassFlags : ExtendedMemberClassFlags;
1245                 if (sym.owner.owner.kind == PCK ||
1246                     (sym.owner.flags_field & STATIC) != 0) {
1247                     mask |= STATIC;
1248                 } else if (!allowRecords && ((flags & ENUM) != 0 || (flags & RECORD) != 0)) {
1249                     log.error(pos, Errors.StaticDeclarationNotAllowedInInnerClasses);
1250                 }
1251                 // Nested interfaces and enums are always STATIC (Spec ???)
1252                 if ((flags & (INTERFACE | ENUM | RECORD)) != 0 ) implicit = STATIC;
1253             } else {
1254                 mask = ExtendedClassFlags;
1255             }
1256             // Interfaces are always ABSTRACT
1257             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1258 
1259             if ((flags & ENUM) != 0) {
1260                 // enums can't be declared abstract, final, sealed or non-sealed
1261                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED);
1262                 implicit |= implicitEnumFinalFlag(tree);
1263             }
1264             if ((flags & RECORD) != 0) {
1265                 // records can't be declared abstract
1266                 mask &= ~ABSTRACT;
1267                 implicit |= FINAL;
1268             }
1269             if ((flags & STRICTFP) != 0) {
1270                 warnOnExplicitStrictfp(pos);
1271             }
1272             // Imply STRICTFP if owner has STRICTFP set.
1273             implicit |= sym.owner.flags_field & STRICTFP;

















1274             break;
1275         default:
1276             throw new AssertionError();
1277         }
1278         long illegal = flags & ExtendedStandardFlags & ~mask;
1279         if (illegal != 0) {
1280             if ((illegal & INTERFACE) != 0) {
1281                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1282                 mask |= INTERFACE;
1283             }
1284             else {
1285                 log.error(pos,
1286                         Errors.ModNotAllowedHere(asFlagSet(illegal)));
1287             }
1288         }
1289         else if ((sym.kind == TYP ||
1290                   // ISSUE: Disallowing abstract&private is no longer appropriate
1291                   // in the presence of inner classes. Should it be deleted here?
1292                   checkDisjoint(pos, flags,
1293                                 ABSTRACT,
1294                                 PRIVATE | STATIC | DEFAULT))
1295                  &&
1296                  checkDisjoint(pos, flags,
1297                                 STATIC | PRIVATE,
1298                                 DEFAULT)
1299                  &&
1300                  checkDisjoint(pos, flags,
1301                                ABSTRACT | INTERFACE,
1302                                FINAL | NATIVE | SYNCHRONIZED)




1303                  &&
1304                  checkDisjoint(pos, flags,
1305                                PUBLIC,
1306                                PRIVATE | PROTECTED)
1307                  &&
1308                  checkDisjoint(pos, flags,
1309                                PRIVATE,
1310                                PUBLIC | PROTECTED)
1311                  &&
1312                  checkDisjoint(pos, flags,
1313                                FINAL,
1314                                VOLATILE)
1315                  &&
1316                  (sym.kind == TYP ||
1317                   checkDisjoint(pos, flags,
1318                                 ABSTRACT | NATIVE,
1319                                 STRICTFP))
1320                  && checkDisjoint(pos, flags,
1321                                 FINAL,
1322                            SEALED | NON_SEALED)
1323                  && checkDisjoint(pos, flags,
1324                                 SEALED,
1325                            FINAL | NON_SEALED)
1326                  && checkDisjoint(pos, flags,
1327                                 SEALED,
1328                                 ANNOTATION)) {






1329             // skip
1330         }
1331         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1332     }
1333 
1334     private void warnOnExplicitStrictfp(DiagnosticPosition pos) {
1335         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(pos);
1336         try {
1337             deferredLintHandler.report(() -> {
1338                                            if (lint.isEnabled(LintCategory.STRICTFP)) {
1339                                                log.warning(LintCategory.STRICTFP,
1340                                                            pos, Warnings.Strictfp); }
1341                                        });
1342         } finally {
1343             deferredLintHandler.setPos(prevLintPos);
1344         }
1345     }
1346 
1347 
1348     /** Determine if this enum should be implicitly final.

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

1502                 // The enclosing type is not a class, so we are
1503                 // looking at a static member type.  However, the
1504                 // qualifying expression is parameterized.


1505                 log.error(tree.pos(), Errors.CantSelectStaticClassFromParamType);
1506             } else {
1507                 // otherwise validate the rest of the expression
1508                 tree.selected.accept(this);
1509             }
1510         }
1511 
1512         @Override
1513         public void visitAnnotatedType(JCAnnotatedType tree) {
1514             tree.underlyingType.accept(this);
1515         }
1516 
1517         @Override
1518         public void visitTypeIdent(JCPrimitiveTypeTree that) {
1519             if (that.type.hasTag(TypeTag.VOID)) {
1520                 log.error(that.pos(), Errors.VoidNotAllowedHere);
1521             }
1522             super.visitTypeIdent(that);
1523         }
1524 

2270                 cf.test(s2) &&
2271                 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
2272     }
2273 
2274 
2275     /** Check that all abstract members of given class have definitions.
2276      *  @param pos          Position to be used for error reporting.
2277      *  @param c            The class.
2278      */
2279     void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
2280         MethodSymbol undef = types.firstUnimplementedAbstract(c);
2281         if (undef != null) {
2282             MethodSymbol undef1 =
2283                 new MethodSymbol(undef.flags(), undef.name,
2284                                  types.memberType(c.type, undef), undef.owner);
2285             log.error(pos,
2286                       Errors.DoesNotOverrideAbstract(c, undef1, undef1.location()));
2287         }
2288     }
2289 






































2290     void checkNonCyclicDecl(JCClassDecl tree) {
2291         CycleChecker cc = new CycleChecker();
2292         cc.scan(tree);
2293         if (!cc.errorFound && !cc.partialCheck) {
2294             tree.sym.flags_field |= ACYCLIC;
2295         }
2296     }
2297 
2298     class CycleChecker extends TreeScanner {
2299 
2300         Set<Symbol> seenClasses = new HashSet<>();
2301         boolean errorFound = false;
2302         boolean partialCheck = false;
2303 
2304         private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
2305             if (sym != null && sym.kind == TYP) {
2306                 Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym);
2307                 if (classEnv != null) {
2308                     DiagnosticSource prevSource = log.currentSource();
2309                     try {

2518     /** Check that all abstract methods implemented by a class are
2519      *  mutually compatible.
2520      *  @param pos          Position to be used for error reporting.
2521      *  @param c            The class whose interfaces are checked.
2522      */
2523     void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
2524         List<Type> supertypes = types.interfaces(c);
2525         Type supertype = types.supertype(c);
2526         if (supertype.hasTag(CLASS) &&
2527             (supertype.tsym.flags() & ABSTRACT) != 0)
2528             supertypes = supertypes.prepend(supertype);
2529         for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
2530             if (!l.head.getTypeArguments().isEmpty() &&
2531                 !checkCompatibleAbstracts(pos, l.head, l.head, c))
2532                 return;
2533             for (List<Type> m = supertypes; m != l; m = m.tail)
2534                 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
2535                     return;
2536         }
2537         checkCompatibleConcretes(pos, c);






















2538     }
2539 
2540     /** Check that all non-override equivalent methods accessible from 'site'
2541      *  are mutually compatible (JLS 8.4.8/9.4.1).
2542      *
2543      *  @param pos  Position to be used for error reporting.
2544      *  @param site The class whose methods are checked.
2545      *  @param sym  The method symbol to be checked.
2546      */
2547     void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
2548          ClashFilter cf = new ClashFilter(site);
2549         //for each method m1 that is overridden (directly or indirectly)
2550         //by method 'sym' in 'site'...
2551 
2552         List<MethodSymbol> potentiallyAmbiguousList = List.nil();
2553         boolean overridesAny = false;
2554         ArrayList<Symbol> symbolsByName = new ArrayList<>();
2555         types.membersClosure(site, false).getSymbolsByName(sym.name, cf).forEach(symbolsByName::add);
2556         for (Symbol m1 : symbolsByName) {
2557             if (!sym.overrides(m1, site.tsym, types, false)) {

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

 146         Target target = Target.instance(context);
 147         syntheticNameChar = target.syntheticNameChar();
 148 
 149         profile = Profile.instance(context);
 150         preview = Preview.instance(context);
 151 
 152         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 153         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 154         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 155         boolean enforceMandatoryWarnings = true;
 156 
 157         deprecationHandler = new MandatoryWarningHandler(log, null, verboseDeprecated,
 158                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 159         removalHandler = new MandatoryWarningHandler(log, null, verboseRemoval,
 160                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 161         uncheckedHandler = new MandatoryWarningHandler(log, null, verboseUnchecked,
 162                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 163         sunApiHandler = new MandatoryWarningHandler(log, null, false,
 164                 enforceMandatoryWarnings, "sunapi", null);
 165 

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

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

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

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

1358      *  return modifiers together with any implicit modifiers for that symbol.
1359      *  Warning: we can't use flags() here since this method
1360      *  is called during class enter, when flags() would cause a premature
1361      *  completion.
1362      *  @param pos           Position to be used for error reporting.
1363      *  @param flags         The set of modifiers given in a definition.
1364      *  @param sym           The defined symbol.
1365      */
1366     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1367         long mask;
1368         long implicit = 0;
1369 
1370         switch (sym.kind) {
1371         case VAR:
1372             if (TreeInfo.isReceiverParam(tree))
1373                 mask = ReceiverParamFlags;
1374             else if (sym.owner.kind != TYP)
1375                 mask = LocalVarFlags;
1376             else if ((sym.owner.flags_field & INTERFACE) != 0)
1377                 mask = implicit = InterfaceVarFlags;
1378             else {
1379                 mask = VarFlags;
1380                 if (sym.owner.type.isValueClass() && (flags & STATIC) == 0) {
1381                     implicit |= FINAL;
1382                 }
1383             }
1384             break;
1385         case MTH:
1386             if (sym.name == names.init) {
1387                 if ((sym.owner.flags_field & ENUM) != 0) {
1388                     // enum constructors cannot be declared public or
1389                     // protected and must be implicitly or explicitly
1390                     // private
1391                     implicit = PRIVATE;
1392                     mask = PRIVATE;
1393                 } else
1394                     mask = ConstructorFlags;
1395             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1396                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1397                     mask = AnnotationTypeElementMask;
1398                     implicit = PUBLIC | ABSTRACT;
1399                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1400                     mask = InterfaceMethodMask;
1401                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1402                     if ((flags & DEFAULT) != 0) {
1403                         implicit |= ABSTRACT;
1404                     }
1405                 } else {
1406                     mask = implicit = InterfaceMethodFlags;
1407                 }
1408             } else if ((sym.owner.flags_field & RECORD) != 0) {
1409                 mask = ((sym.owner.flags_field & VALUE_CLASS) != 0 && (flags & Flags.STATIC) == 0) ?
1410                         RecordMethodFlags & ~SYNCHRONIZED : RecordMethodFlags;
1411             } else {
1412                 // value objects do not have an associated monitor/lock
1413                 mask = ((sym.owner.flags_field & VALUE_CLASS) != 0 && (flags & Flags.STATIC) == 0) ?
1414                         MethodFlags & ~SYNCHRONIZED : MethodFlags;
1415             }
1416             if ((flags & STRICTFP) != 0) {
1417                 warnOnExplicitStrictfp(pos);
1418             }
1419             // Imply STRICTFP if owner has STRICTFP set.
1420             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1421                 ((flags) & Flags.DEFAULT) != 0)
1422                 implicit |= sym.owner.flags_field & STRICTFP;
1423             break;
1424         case TYP:
1425             if (sym.owner.kind.matches(KindSelector.VAL_MTH) ||
1426                     (sym.isDirectlyOrIndirectlyLocal() && (flags & ANNOTATION) != 0)) {
1427                 boolean implicitlyStatic = !sym.isAnonymous() &&
1428                         ((flags & RECORD) != 0 || (flags & ENUM) != 0 || (flags & INTERFACE) != 0);
1429                 boolean staticOrImplicitlyStatic = (flags & STATIC) != 0 || implicitlyStatic;
1430                 // local statics are allowed only if records are allowed too
1431                 mask = staticOrImplicitlyStatic && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedStaticLocalClassFlags : ExtendedLocalClassFlags;
1432                 implicit = implicitlyStatic ? STATIC : implicit;
1433             } else if (sym.owner.kind == TYP) {
1434                 // statics in inner classes are allowed only if records are allowed too
1435                 mask = ((flags & STATIC) != 0) && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedMemberStaticClassFlags : ExtendedMemberClassFlags;
1436                 if (sym.owner.owner.kind == PCK ||
1437                     (sym.owner.flags_field & STATIC) != 0) {
1438                     mask |= STATIC;
1439                 } else if (!allowRecords && ((flags & ENUM) != 0 || (flags & RECORD) != 0)) {
1440                     log.error(pos, Errors.StaticDeclarationNotAllowedInInnerClasses);
1441                 }
1442                 // Nested interfaces and enums are always STATIC (Spec ???)
1443                 if ((flags & (INTERFACE | ENUM | RECORD)) != 0 ) implicit = STATIC;
1444             } else {
1445                 mask = ExtendedClassFlags;
1446             }
1447             // Interfaces are always ABSTRACT
1448             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1449 
1450             if ((flags & ENUM) != 0) {
1451                 // enums can't be declared abstract, final, sealed or non-sealed or primitive/value
1452                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED | PRIMITIVE_CLASS | VALUE_CLASS);
1453                 implicit |= implicitEnumFinalFlag(tree);
1454             }
1455             if ((flags & RECORD) != 0) {
1456                 // records can't be declared abstract
1457                 mask &= ~ABSTRACT;
1458                 implicit |= FINAL;
1459             }
1460             if ((flags & STRICTFP) != 0) {
1461                 warnOnExplicitStrictfp(pos);
1462             }
1463             // Imply STRICTFP if owner has STRICTFP set.
1464             implicit |= sym.owner.flags_field & STRICTFP;
1465 
1466             // primitive classes are implicitly final value classes.
1467             if ((flags & PRIMITIVE_CLASS) != 0)
1468                 implicit |= VALUE_CLASS | FINAL;
1469 
1470             // concrete value classes are implicitly final
1471             if ((flags & (ABSTRACT | INTERFACE | VALUE_CLASS)) == VALUE_CLASS) {
1472                 implicit |= FINAL;
1473                 if ((flags & NON_SEALED) != 0) {
1474                     // cant declare a final value class non-sealed
1475                     log.error(pos,
1476                             Errors.ModNotAllowedHere(asFlagSet(NON_SEALED)));
1477                 }
1478             }
1479 
1480             // TYPs can't be declared synchronized
1481             mask &= ~SYNCHRONIZED;
1482             break;
1483         default:
1484             throw new AssertionError();
1485         }
1486         long illegal = flags & ExtendedStandardFlags & ~mask;
1487         if (illegal != 0) {
1488             if ((illegal & INTERFACE) != 0) {
1489                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1490                 mask |= INTERFACE;
1491             }
1492             else {
1493                 log.error(pos,
1494                         Errors.ModNotAllowedHere(asFlagSet(illegal)));
1495             }
1496         }
1497         else if ((sym.kind == TYP ||
1498                   // ISSUE: Disallowing abstract&private is no longer appropriate
1499                   // in the presence of inner classes. Should it be deleted here?
1500                   checkDisjoint(pos, flags,
1501                                 ABSTRACT,
1502                                 PRIVATE | STATIC | DEFAULT))
1503                  &&
1504                  checkDisjoint(pos, flags,
1505                                 STATIC | PRIVATE,
1506                                 DEFAULT)
1507                  &&
1508                  checkDisjoint(pos, flags,
1509                                ABSTRACT | INTERFACE,
1510                                FINAL | NATIVE | SYNCHRONIZED | PRIMITIVE_CLASS)
1511                  &&
1512                  checkDisjoint(pos, flags,
1513                         IDENTITY_TYPE,
1514                         PRIMITIVE_CLASS | VALUE_CLASS)
1515                  &&
1516                  checkDisjoint(pos, flags,
1517                                PUBLIC,
1518                                PRIVATE | PROTECTED)
1519                  &&
1520                  checkDisjoint(pos, flags,
1521                                PRIVATE,
1522                                PUBLIC | PROTECTED)
1523                  &&
1524                  checkDisjoint(pos, (flags | implicit), // complain against volatile & implcitly final entities too.
1525                                FINAL,
1526                                VOLATILE)
1527                  &&
1528                  (sym.kind == TYP ||
1529                   checkDisjoint(pos, flags,
1530                                 ABSTRACT | NATIVE,
1531                                 STRICTFP))
1532                  && checkDisjoint(pos, flags,
1533                                 FINAL,
1534                            SEALED | NON_SEALED)
1535                  && checkDisjoint(pos, flags,
1536                                 SEALED,
1537                            FINAL | NON_SEALED)
1538                  && checkDisjoint(pos, flags,
1539                                 SEALED,
1540                                 ANNOTATION)
1541                  && checkDisjoint(pos, flags,
1542                                 IDENTITY_TYPE,
1543                                 ANNOTATION)
1544                 && checkDisjoint(pos, flags,
1545                                 VALUE_CLASS,
1546                                 ANNOTATION) ) {
1547             // skip
1548         }
1549         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1550     }
1551 
1552     private void warnOnExplicitStrictfp(DiagnosticPosition pos) {
1553         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(pos);
1554         try {
1555             deferredLintHandler.report(() -> {
1556                                            if (lint.isEnabled(LintCategory.STRICTFP)) {
1557                                                log.warning(LintCategory.STRICTFP,
1558                                                            pos, Warnings.Strictfp); }
1559                                        });
1560         } finally {
1561             deferredLintHandler.setPos(prevLintPos);
1562         }
1563     }
1564 
1565 
1566     /** Determine if this enum should be implicitly final.

1699         @Override
1700         public void visitWildcard(JCWildcard tree) {
1701             if (tree.inner != null)
1702                 validateTree(tree.inner, true, isOuter);
1703         }
1704 
1705         @Override
1706         public void visitSelect(JCFieldAccess tree) {
1707             if (tree.type.hasTag(CLASS)) {
1708                 visitSelectInternal(tree);
1709 
1710                 // Check that this type is either fully parameterized, or
1711                 // not parameterized at all.
1712                 if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty())
1713                     log.error(tree.pos(), Errors.ImproperlyFormedTypeParamMissing);
1714             }
1715         }
1716 
1717         public void visitSelectInternal(JCFieldAccess tree) {
1718             if (tree.type.tsym.isStatic() &&
1719                 tree.selected.type.isParameterized() &&
1720                     (tree.name != names.ref || !tree.type.isReferenceProjection())) {
1721                 // The enclosing type is not a class, so we are
1722                 // looking at a static member type.  However, the
1723                 // qualifying expression is parameterized.
1724                 // Tolerate the pseudo-select V.ref: V<T>.ref will be static if V<T> is and
1725                 // should not be confused as selecting a static member of a parameterized type.
1726                 log.error(tree.pos(), Errors.CantSelectStaticClassFromParamType);
1727             } else {
1728                 // otherwise validate the rest of the expression
1729                 tree.selected.accept(this);
1730             }
1731         }
1732 
1733         @Override
1734         public void visitAnnotatedType(JCAnnotatedType tree) {
1735             tree.underlyingType.accept(this);
1736         }
1737 
1738         @Override
1739         public void visitTypeIdent(JCPrimitiveTypeTree that) {
1740             if (that.type.hasTag(TypeTag.VOID)) {
1741                 log.error(that.pos(), Errors.VoidNotAllowedHere);
1742             }
1743             super.visitTypeIdent(that);
1744         }
1745 

2491                 cf.test(s2) &&
2492                 types.hasSameArgs(s1.erasure(types), s2.erasure(types)));
2493     }
2494 
2495 
2496     /** Check that all abstract members of given class have definitions.
2497      *  @param pos          Position to be used for error reporting.
2498      *  @param c            The class.
2499      */
2500     void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) {
2501         MethodSymbol undef = types.firstUnimplementedAbstract(c);
2502         if (undef != null) {
2503             MethodSymbol undef1 =
2504                 new MethodSymbol(undef.flags(), undef.name,
2505                                  types.memberType(c.type, undef), undef.owner);
2506             log.error(pos,
2507                       Errors.DoesNotOverrideAbstract(c, undef1, undef1.location()));
2508         }
2509     }
2510 
2511     // A primitive class cannot contain a field of its own type either or indirectly.
2512     void checkNonCyclicMembership(JCClassDecl tree) {
2513         Assert.check((tree.sym.flags_field & LOCKED) == 0);
2514         try {
2515             tree.sym.flags_field |= LOCKED;
2516             for (List<? extends JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
2517                 if (l.head.hasTag(VARDEF)) {
2518                     JCVariableDecl field = (JCVariableDecl) l.head;
2519                     if (cyclePossible(field.sym)) {
2520                         checkNonCyclicMembership((ClassSymbol) field.type.tsym, field.pos());
2521                     }
2522                 }
2523             }
2524         } finally {
2525             tree.sym.flags_field &= ~LOCKED;
2526         }
2527 
2528     }
2529     // where
2530     private void checkNonCyclicMembership(ClassSymbol c, DiagnosticPosition pos) {
2531         if ((c.flags_field & LOCKED) != 0) {
2532             log.error(pos, Errors.CyclicPrimitiveClassMembership(c));
2533             return;
2534         }
2535         try {
2536             c.flags_field |= LOCKED;
2537             for (Symbol fld : c.members().getSymbols(s -> s.kind == VAR && cyclePossible((VarSymbol) s), NON_RECURSIVE)) {
2538                 checkNonCyclicMembership((ClassSymbol) fld.type.tsym, pos);
2539             }
2540         } finally {
2541             c.flags_field &= ~LOCKED;
2542         }
2543     }
2544         // where
2545         private boolean cyclePossible(VarSymbol symbol) {
2546             return (symbol.flags() & STATIC) == 0 && symbol.type.isPrimitiveClass();
2547         }
2548 
2549     void checkNonCyclicDecl(JCClassDecl tree) {
2550         CycleChecker cc = new CycleChecker();
2551         cc.scan(tree);
2552         if (!cc.errorFound && !cc.partialCheck) {
2553             tree.sym.flags_field |= ACYCLIC;
2554         }
2555     }
2556 
2557     class CycleChecker extends TreeScanner {
2558 
2559         Set<Symbol> seenClasses = new HashSet<>();
2560         boolean errorFound = false;
2561         boolean partialCheck = false;
2562 
2563         private void checkSymbol(DiagnosticPosition pos, Symbol sym) {
2564             if (sym != null && sym.kind == TYP) {
2565                 Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym);
2566                 if (classEnv != null) {
2567                     DiagnosticSource prevSource = log.currentSource();
2568                     try {

2777     /** Check that all abstract methods implemented by a class are
2778      *  mutually compatible.
2779      *  @param pos          Position to be used for error reporting.
2780      *  @param c            The class whose interfaces are checked.
2781      */
2782     void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
2783         List<Type> supertypes = types.interfaces(c);
2784         Type supertype = types.supertype(c);
2785         if (supertype.hasTag(CLASS) &&
2786             (supertype.tsym.flags() & ABSTRACT) != 0)
2787             supertypes = supertypes.prepend(supertype);
2788         for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
2789             if (!l.head.getTypeArguments().isEmpty() &&
2790                 !checkCompatibleAbstracts(pos, l.head, l.head, c))
2791                 return;
2792             for (List<Type> m = supertypes; m != l; m = m.tail)
2793                 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
2794                     return;
2795         }
2796         checkCompatibleConcretes(pos, c);
2797 
2798         boolean cIsValue = (c.tsym.flags() & VALUE_CLASS) != 0;
2799         boolean cHasIdentity = (c.tsym.flags() & IDENTITY_TYPE) != 0;
2800         Type identitySuper = null, valueSuper = null;
2801         for (Type t : types.closure(c)) {
2802             if (t != c) {
2803                 if ((t.tsym.flags() & IDENTITY_TYPE) != 0)
2804                     identitySuper = t;
2805                 else if ((t.tsym.flags() & VALUE_CLASS) != 0)
2806                     valueSuper = t;
2807                 if (cIsValue &&  identitySuper != null) {
2808                     log.error(pos, Errors.ValueTypeHasIdentitySuperType(c, identitySuper));
2809                     break;
2810                 } else if (cHasIdentity &&  valueSuper != null) {
2811                     log.error(pos, Errors.IdentityTypeHasValueSuperType(c, valueSuper));
2812                     break;
2813                 } else if (identitySuper != null && valueSuper != null) {
2814                     log.error(pos, Errors.MutuallyIncompatibleSupers(c, identitySuper, valueSuper));
2815                     break;
2816                 }
2817             }
2818         }
2819     }
2820 
2821     /** Check that all non-override equivalent methods accessible from 'site'
2822      *  are mutually compatible (JLS 8.4.8/9.4.1).
2823      *
2824      *  @param pos  Position to be used for error reporting.
2825      *  @param site The class whose methods are checked.
2826      *  @param sym  The method symbol to be checked.
2827      */
2828     void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
2829          ClashFilter cf = new ClashFilter(site);
2830         //for each method m1 that is overridden (directly or indirectly)
2831         //by method 'sym' in 'site'...
2832 
2833         List<MethodSymbol> potentiallyAmbiguousList = List.nil();
2834         boolean overridesAny = false;
2835         ArrayList<Symbol> symbolsByName = new ArrayList<>();
2836         types.membersClosure(site, false).getSymbolsByName(sym.name, cf).forEach(symbolsByName::add);
2837         for (Symbol m1 : symbolsByName) {
2838             if (!sym.overrides(m1, site.tsym, types, false)) {
< prev index next >