< prev index next >

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

Print this page

 742     /** Check that type is a class or interface type.
 743      *  @param pos           Position to be used for error reporting.
 744      *  @param t             The type to be checked.
 745      */
 746     Type checkClassType(DiagnosticPosition pos, Type t) {
 747         if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) {
 748             return typeTagError(pos,
 749                                 diags.fragment(Fragments.TypeReqClass),
 750                                 asTypeParam(t));
 751         } else {
 752             return t;
 753         }
 754     }
 755     //where
 756         private Object asTypeParam(Type t) {
 757             return (t.hasTag(TYPEVAR))
 758                                     ? diags.fragment(Fragments.TypeParameter(t))
 759                                     : t;
 760         }
 761 

























 762     /** Check that type is a valid qualifier for a constructor reference expression
 763      */
 764     Type checkConstructorRefType(DiagnosticPosition pos, Type t) {
 765         t = checkClassOrArrayType(pos, t);
 766         if (t.hasTag(CLASS)) {
 767             if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
 768                 log.error(pos, Errors.AbstractCantBeInstantiated(t.tsym));
 769                 t = types.createErrorType(t);
 770             } else if ((t.tsym.flags() & ENUM) != 0) {
 771                 log.error(pos, Errors.EnumCantBeInstantiated);
 772                 t = types.createErrorType(t);
 773             } else {
 774                 t = checkClassType(pos, t, true);
 775             }
 776         } else if (t.hasTag(ARRAY)) {
 777             if (!types.isReifiable(((ArrayType)t).elemtype)) {
 778                 log.error(pos, Errors.GenericArrayCreation);
 779                 t = types.createErrorType(t);
 780             }
 781         }

 799                 args = args.tail;
 800             }
 801         }
 802         return t;
 803     }
 804 
 805     /** Check that type is a reference type, i.e. a class, interface or array type
 806      *  or a type variable.
 807      *  @param pos           Position to be used for error reporting.
 808      *  @param t             The type to be checked.
 809      */
 810     Type checkRefType(DiagnosticPosition pos, Type t) {
 811         if (t.isReference())
 812             return t;
 813         else
 814             return typeTagError(pos,
 815                                 diags.fragment(Fragments.TypeReqRef),
 816                                 t);
 817     }
 818 






















 819     /** Check that each type is a reference type, i.e. a class, interface or array type
 820      *  or a type variable.
 821      *  @param trees         Original trees, used for error reporting.
 822      *  @param types         The types to be checked.
 823      */
 824     List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
 825         List<JCExpression> tl = trees;
 826         for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
 827             l.head = checkRefType(tl.head.pos(), l.head);
 828             tl = tl.tail;
 829         }
 830         return types;
 831     }
 832 
 833     /** Check that type is a null or reference type.
 834      *  @param pos           Position to be used for error reporting.
 835      *  @param t             The type to be checked.
 836      */
 837     Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
 838         if (t.isReference() || t.hasTag(BOT))

1189      *  return modifiers together with any implicit modifiers for that symbol.
1190      *  Warning: we can't use flags() here since this method
1191      *  is called during class enter, when flags() would cause a premature
1192      *  completion.
1193      *  @param pos           Position to be used for error reporting.
1194      *  @param flags         The set of modifiers given in a definition.
1195      *  @param sym           The defined symbol.
1196      */
1197     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1198         long mask;
1199         long implicit = 0;
1200 
1201         switch (sym.kind) {
1202         case VAR:
1203             if (TreeInfo.isReceiverParam(tree))
1204                 mask = ReceiverParamFlags;
1205             else if (sym.owner.kind != TYP)
1206                 mask = LocalVarFlags;
1207             else if ((sym.owner.flags_field & INTERFACE) != 0)
1208                 mask = implicit = InterfaceVarFlags;
1209             else
1210                 mask = VarFlags;





1211             break;
1212         case MTH:
1213             if (sym.name == names.init) {
1214                 if ((sym.owner.flags_field & ENUM) != 0) {
1215                     // enum constructors cannot be declared public or
1216                     // protected and must be implicitly or explicitly
1217                     // private
1218                     implicit = PRIVATE;
1219                     mask = PRIVATE;
1220                 } else
1221                     mask = ConstructorFlags;
1222             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1223                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1224                     mask = AnnotationTypeElementMask;
1225                     implicit = PUBLIC | ABSTRACT;
1226                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1227                     mask = InterfaceMethodMask;
1228                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1229                     if ((flags & DEFAULT) != 0) {
1230                         implicit |= ABSTRACT;
1231                     }
1232                 } else {
1233                     mask = implicit = InterfaceMethodFlags;
1234                 }
1235             } else if ((sym.owner.flags_field & RECORD) != 0) {
1236                 mask = RecordMethodFlags;

1237             } else {
1238                 mask = MethodFlags;


1239             }
1240             if ((flags & STRICTFP) != 0) {
1241                 warnOnExplicitStrictfp(pos);
1242             }
1243             // Imply STRICTFP if owner has STRICTFP set.
1244             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1245                 ((flags) & Flags.DEFAULT) != 0)
1246                 implicit |= sym.owner.flags_field & STRICTFP;
1247             break;
1248         case TYP:
1249             if (sym.owner.kind.matches(KindSelector.VAL_MTH) ||
1250                     (sym.isDirectlyOrIndirectlyLocal() && (flags & ANNOTATION) != 0)) {
1251                 boolean implicitlyStatic = !sym.isAnonymous() &&
1252                         ((flags & RECORD) != 0 || (flags & ENUM) != 0 || (flags & INTERFACE) != 0);
1253                 boolean staticOrImplicitlyStatic = (flags & STATIC) != 0 || implicitlyStatic;
1254                 // local statics are allowed only if records are allowed too
1255                 mask = staticOrImplicitlyStatic && allowRecords && (flags & ANNOTATION) == 0 ? StaticLocalFlags : LocalClassFlags;
1256                 implicit = implicitlyStatic ? STATIC : implicit;
1257             } else if (sym.owner.kind == TYP) {
1258                 // statics in inner classes are allowed only if records are allowed too
1259                 mask = ((flags & STATIC) != 0) && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedMemberStaticClassFlags : ExtendedMemberClassFlags;
1260                 if (sym.owner.owner.kind == PCK ||
1261                     (sym.owner.flags_field & STATIC) != 0) {
1262                     mask |= STATIC;
1263                 } else if (!allowRecords && ((flags & ENUM) != 0 || (flags & RECORD) != 0)) {
1264                     log.error(pos, Errors.StaticDeclarationNotAllowedInInnerClasses);
1265                 }
1266                 // Nested interfaces and enums are always STATIC (Spec ???)
1267                 if ((flags & (INTERFACE | ENUM | RECORD)) != 0 ) implicit = STATIC;
1268             } else {
1269                 mask = ExtendedClassFlags;
1270             }
1271             // Interfaces are always ABSTRACT
1272             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1273 




1274             if ((flags & ENUM) != 0) {
1275                 // enums can't be declared abstract, final, sealed or non-sealed
1276                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED);
1277                 implicit |= implicitEnumFinalFlag(tree);
1278             }
1279             if ((flags & RECORD) != 0) {
1280                 // records can't be declared abstract
1281                 mask &= ~ABSTRACT;
1282                 implicit |= FINAL;
1283             }
1284             if ((flags & STRICTFP) != 0) {
1285                 warnOnExplicitStrictfp(pos);
1286             }
1287             // Imply STRICTFP if owner has STRICTFP set.
1288             implicit |= sym.owner.flags_field & STRICTFP;








1289             break;
1290         default:
1291             throw new AssertionError();
1292         }
1293         long illegal = flags & ExtendedStandardFlags & ~mask;
1294         if (illegal != 0) {
1295             if ((illegal & INTERFACE) != 0) {
1296                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1297                 mask |= INTERFACE;
1298             }
1299             else {
1300                 log.error(pos,
1301                         Errors.ModNotAllowedHere(asFlagSet(illegal)));
1302             }
1303         }
1304         else if ((sym.kind == TYP ||
1305                   // ISSUE: Disallowing abstract&private is no longer appropriate
1306                   // in the presence of inner classes. Should it be deleted here?
1307                   checkDisjoint(pos, flags,
1308                                 ABSTRACT,
1309                                 PRIVATE | STATIC | DEFAULT))
1310                  &&
1311                  checkDisjoint(pos, flags,
1312                                 STATIC | PRIVATE,
1313                                 DEFAULT)
1314                  &&
1315                  checkDisjoint(pos, flags,
1316                                ABSTRACT | INTERFACE,
1317                                FINAL | NATIVE | SYNCHRONIZED)
1318                  &&




1319                  checkDisjoint(pos, flags,
1320                                PUBLIC,
1321                                PRIVATE | PROTECTED)
1322                  &&
1323                  checkDisjoint(pos, flags,
1324                                PRIVATE,
1325                                PUBLIC | PROTECTED)
1326                  &&
1327                  checkDisjoint(pos, flags,
1328                                FINAL,
1329                                VOLATILE)
1330                  &&
1331                  (sym.kind == TYP ||
1332                   checkDisjoint(pos, flags,
1333                                 ABSTRACT | NATIVE,
1334                                 STRICTFP))
1335                  && checkDisjoint(pos, flags,
1336                                 FINAL,
1337                            SEALED | NON_SEALED)
1338                  && checkDisjoint(pos, flags,
1339                                 SEALED,
1340                            FINAL | NON_SEALED)
1341                  && checkDisjoint(pos, flags,
1342                                 SEALED,
1343                                 ANNOTATION)) {









1344             // skip
1345         }
1346         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1347     }
1348 
1349     private void warnOnExplicitStrictfp(DiagnosticPosition pos) {
1350         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(pos);
1351         try {
1352             deferredLintHandler.report(() -> {
1353                                            if (lint.isEnabled(LintCategory.STRICTFP)) {
1354                                                log.warning(LintCategory.STRICTFP,
1355                                                            pos, Warnings.Strictfp); }
1356                                        });
1357         } finally {
1358             deferredLintHandler.setPos(prevLintPos);
1359         }
1360     }
1361 
1362 
1363     /** Determine if this enum should be implicitly final.

2568     /** Check that all abstract methods implemented by a class are
2569      *  mutually compatible.
2570      *  @param pos          Position to be used for error reporting.
2571      *  @param c            The class whose interfaces are checked.
2572      */
2573     void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
2574         List<Type> supertypes = types.interfaces(c);
2575         Type supertype = types.supertype(c);
2576         if (supertype.hasTag(CLASS) &&
2577             (supertype.tsym.flags() & ABSTRACT) != 0)
2578             supertypes = supertypes.prepend(supertype);
2579         for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
2580             if (!l.head.getTypeArguments().isEmpty() &&
2581                 !checkCompatibleAbstracts(pos, l.head, l.head, c))
2582                 return;
2583             for (List<Type> m = supertypes; m != l; m = m.tail)
2584                 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
2585                     return;
2586         }
2587         checkCompatibleConcretes(pos, c);















2588     }
2589 
2590     /** Check that all non-override equivalent methods accessible from 'site'
2591      *  are mutually compatible (JLS 8.4.8/9.4.1).
2592      *
2593      *  @param pos  Position to be used for error reporting.
2594      *  @param site The class whose methods are checked.
2595      *  @param sym  The method symbol to be checked.
2596      */
2597     void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
2598          ClashFilter cf = new ClashFilter(site);
2599         //for each method m1 that is overridden (directly or indirectly)
2600         //by method 'sym' in 'site'...
2601 
2602         ArrayList<Symbol> symbolsByName = new ArrayList<>();
2603         types.membersClosure(site, false).getSymbolsByName(sym.name, cf).forEach(symbolsByName::add);
2604         for (Symbol m1 : symbolsByName) {
2605             if (!sym.overrides(m1, site.tsym, types, false)) {
2606                 continue;
2607             }

4042                     break;
4043 
4044                 // super()/this() calls must only appear in a constructor
4045                 if (!constructor) {
4046                     log.error(apply.pos(), Errors.CallMustOnlyAppearInCtor);
4047                     break;
4048                 }
4049 
4050                 // super()/this() calls must be a top level statement
4051                 if (scanDepth != MATCH_SCAN_DEPTH) {
4052                     log.error(apply.pos(), Errors.CtorCallsNotAllowedHere);
4053                     break;
4054                 }
4055 
4056                 // super()/this() calls must not appear more than once
4057                 if (initCall != null) {
4058                     log.error(apply.pos(), Errors.RedundantSuperclassInit);
4059                     break;
4060                 }
4061 


4062                 // If super()/this() isn't first, require "statements before super()" feature
4063                 if (!firstStatement)
4064                     preview.checkSourceLevel(apply.pos(), Feature.SUPER_INIT);

4065 
4066                 // We found a legitimate super()/this() call; remember it
4067                 initCall = methodName;
4068             } while (false);
4069 
4070             // Proceed
4071             super.visitApply(apply);
4072         }
4073 
4074         @Override
4075         public void visitReturn(JCReturn tree) {
4076             if (constructor && initCall == null && earlyReturn == null)
4077                 earlyReturn = tree;             // we have seen a return but not (yet) a super()/this()
4078             super.visitReturn(tree);
4079         }
4080 
4081         @Override
4082         public void visitClassDef(JCClassDecl tree) {
4083             // don't descend any further
4084         }

 742     /** Check that type is a class or interface type.
 743      *  @param pos           Position to be used for error reporting.
 744      *  @param t             The type to be checked.
 745      */
 746     Type checkClassType(DiagnosticPosition pos, Type t) {
 747         if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) {
 748             return typeTagError(pos,
 749                                 diags.fragment(Fragments.TypeReqClass),
 750                                 asTypeParam(t));
 751         } else {
 752             return t;
 753         }
 754     }
 755     //where
 756         private Object asTypeParam(Type t) {
 757             return (t.hasTag(TYPEVAR))
 758                                     ? diags.fragment(Fragments.TypeParameter(t))
 759                                     : t;
 760         }
 761 
 762     void checkConstraintsOfValueClass(JCClassDecl tree, ClassSymbol c) {
 763         DiagnosticPosition pos = tree.pos();
 764         for (Type st : types.closure(c.type)) {
 765             if (st == null || st.tsym == null || st.tsym.kind == ERR)
 766                 continue;
 767             if  (st.tsym == syms.objectType.tsym || st.tsym == syms.recordType.tsym || st.isInterface())
 768                 continue;
 769             if (!st.tsym.isAbstract()) {
 770                 if (c != st.tsym) {
 771                     log.error(pos, Errors.ConcreteSupertypeForValueClass(c, st));
 772                 }
 773                 continue;
 774             }
 775             // dealing with an abstract value or value super class below.
 776             for (Symbol s : st.tsym.members().getSymbols(NON_RECURSIVE)) {
 777                 if (s.kind == MTH) {
 778                     if ((s.flags() & (SYNCHRONIZED | STATIC)) == SYNCHRONIZED) {
 779                         log.error(pos, Errors.SuperClassMethodCannotBeSynchronized(s, c, st));
 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(DiagnosticPosition pos, Type t) {
 790         t = checkClassOrArrayType(pos, t);
 791         if (t.hasTag(CLASS)) {
 792             if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
 793                 log.error(pos, Errors.AbstractCantBeInstantiated(t.tsym));
 794                 t = types.createErrorType(t);
 795             } else if ((t.tsym.flags() & ENUM) != 0) {
 796                 log.error(pos, Errors.EnumCantBeInstantiated);
 797                 t = types.createErrorType(t);
 798             } else {
 799                 t = checkClassType(pos, t, true);
 800             }
 801         } else if (t.hasTag(ARRAY)) {
 802             if (!types.isReifiable(((ArrayType)t).elemtype)) {
 803                 log.error(pos, Errors.GenericArrayCreation);
 804                 t = types.createErrorType(t);
 805             }
 806         }

 824                 args = args.tail;
 825             }
 826         }
 827         return t;
 828     }
 829 
 830     /** Check that type is a reference type, i.e. a class, interface or array type
 831      *  or a type variable.
 832      *  @param pos           Position to be used for error reporting.
 833      *  @param t             The type to be checked.
 834      */
 835     Type checkRefType(DiagnosticPosition pos, Type t) {
 836         if (t.isReference())
 837             return t;
 838         else
 839             return typeTagError(pos,
 840                                 diags.fragment(Fragments.TypeReqRef),
 841                                 t);
 842     }
 843 
 844     /** Check that type is an identity type, i.e. not a value type.
 845      *  When not discernible statically, give it the benefit of doubt
 846      *  and defer to runtime.
 847      *
 848      *  @param pos           Position to be used for error reporting.
 849      *  @param t             The type to be checked.
 850      */
 851     void checkIdentityType(DiagnosticPosition pos, Type t) {
 852         if (t.hasTag(TYPEVAR)) {
 853             t = types.skipTypeVars(t, false);
 854         }
 855         if (t.isIntersection()) {
 856             IntersectionClassType ict = (IntersectionClassType)t;
 857             for (Type component : ict.getExplicitComponents()) {
 858                 checkIdentityType(pos, component);
 859             }
 860             return;
 861         }
 862         if (t.isPrimitive() || (t.isValueClass() && !t.tsym.isAbstract()))
 863             typeTagError(pos, diags.fragment(Fragments.TypeReqIdentity), t);
 864     }
 865 
 866     /** Check that each type is a reference type, i.e. a class, interface or array type
 867      *  or a type variable.
 868      *  @param trees         Original trees, used for error reporting.
 869      *  @param types         The types to be checked.
 870      */
 871     List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) {
 872         List<JCExpression> tl = trees;
 873         for (List<Type> l = types; l.nonEmpty(); l = l.tail) {
 874             l.head = checkRefType(tl.head.pos(), l.head);
 875             tl = tl.tail;
 876         }
 877         return types;
 878     }
 879 
 880     /** Check that type is a null or reference type.
 881      *  @param pos           Position to be used for error reporting.
 882      *  @param t             The type to be checked.
 883      */
 884     Type checkNullOrRefType(DiagnosticPosition pos, Type t) {
 885         if (t.isReference() || t.hasTag(BOT))

1236      *  return modifiers together with any implicit modifiers for that symbol.
1237      *  Warning: we can't use flags() here since this method
1238      *  is called during class enter, when flags() would cause a premature
1239      *  completion.
1240      *  @param pos           Position to be used for error reporting.
1241      *  @param flags         The set of modifiers given in a definition.
1242      *  @param sym           The defined symbol.
1243      */
1244     long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) {
1245         long mask;
1246         long implicit = 0;
1247 
1248         switch (sym.kind) {
1249         case VAR:
1250             if (TreeInfo.isReceiverParam(tree))
1251                 mask = ReceiverParamFlags;
1252             else if (sym.owner.kind != TYP)
1253                 mask = LocalVarFlags;
1254             else if ((sym.owner.flags_field & INTERFACE) != 0)
1255                 mask = implicit = InterfaceVarFlags;
1256             else {
1257                 boolean isInstanceFieldOfValueClass = sym.owner.type.isValueClass() && (flags & STATIC) == 0;
1258                 mask = !isInstanceFieldOfValueClass ? VarFlags : ExtendedVarFlags;
1259                 if (isInstanceFieldOfValueClass) {
1260                     implicit |= FINAL | STRICT;
1261                 }
1262             }
1263             break;
1264         case MTH:
1265             if (sym.name == names.init) {
1266                 if ((sym.owner.flags_field & ENUM) != 0) {
1267                     // enum constructors cannot be declared public or
1268                     // protected and must be implicitly or explicitly
1269                     // private
1270                     implicit = PRIVATE;
1271                     mask = PRIVATE;
1272                 } else
1273                     mask = ConstructorFlags;
1274             }  else if ((sym.owner.flags_field & INTERFACE) != 0) {
1275                 if ((sym.owner.flags_field & ANNOTATION) != 0) {
1276                     mask = AnnotationTypeElementMask;
1277                     implicit = PUBLIC | ABSTRACT;
1278                 } else if ((flags & (DEFAULT | STATIC | PRIVATE)) != 0) {
1279                     mask = InterfaceMethodMask;
1280                     implicit = (flags & PRIVATE) != 0 ? 0 : PUBLIC;
1281                     if ((flags & DEFAULT) != 0) {
1282                         implicit |= ABSTRACT;
1283                     }
1284                 } else {
1285                     mask = implicit = InterfaceMethodFlags;
1286                 }
1287             } else if ((sym.owner.flags_field & RECORD) != 0) {
1288                 mask = ((sym.owner.flags_field & VALUE_CLASS) != 0 && (flags & Flags.STATIC) == 0) ?
1289                         RecordMethodFlags & ~SYNCHRONIZED : RecordMethodFlags;
1290             } else {
1291                 // value objects do not have an associated monitor/lock
1292                 mask = ((sym.owner.flags_field & VALUE_CLASS) != 0 && (flags & Flags.STATIC) == 0) ?
1293                         MethodFlags & ~SYNCHRONIZED : MethodFlags;
1294             }
1295             if ((flags & STRICTFP) != 0) {
1296                 warnOnExplicitStrictfp(pos);
1297             }
1298             // Imply STRICTFP if owner has STRICTFP set.
1299             if (((flags|implicit) & Flags.ABSTRACT) == 0 ||
1300                 ((flags) & Flags.DEFAULT) != 0)
1301                 implicit |= sym.owner.flags_field & STRICTFP;
1302             break;
1303         case TYP:
1304             if (sym.owner.kind.matches(KindSelector.VAL_MTH) ||
1305                     (sym.isDirectlyOrIndirectlyLocal() && (flags & ANNOTATION) != 0)) {
1306                 boolean implicitlyStatic = !sym.isAnonymous() &&
1307                         ((flags & RECORD) != 0 || (flags & ENUM) != 0 || (flags & INTERFACE) != 0);
1308                 boolean staticOrImplicitlyStatic = (flags & STATIC) != 0 || implicitlyStatic;
1309                 // local statics are allowed only if records are allowed too
1310                 mask = staticOrImplicitlyStatic && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedStaticLocalClassFlags : ExtendedLocalClassFlags;
1311                 implicit = implicitlyStatic ? STATIC : implicit;
1312             } else if (sym.owner.kind == TYP) {
1313                 // statics in inner classes are allowed only if records are allowed too
1314                 mask = ((flags & STATIC) != 0) && allowRecords && (flags & ANNOTATION) == 0 ? ExtendedMemberStaticClassFlags : ExtendedMemberClassFlags;
1315                 if (sym.owner.owner.kind == PCK ||
1316                     (sym.owner.flags_field & STATIC) != 0) {
1317                     mask |= STATIC;
1318                 } else if (!allowRecords && ((flags & ENUM) != 0 || (flags & RECORD) != 0)) {
1319                     log.error(pos, Errors.StaticDeclarationNotAllowedInInnerClasses);
1320                 }
1321                 // Nested interfaces and enums are always STATIC (Spec ???)
1322                 if ((flags & (INTERFACE | ENUM | RECORD)) != 0 ) implicit = STATIC;
1323             } else {
1324                 mask = ExtendedClassFlags;
1325             }
1326             // Interfaces are always ABSTRACT
1327             if ((flags & INTERFACE) != 0) implicit |= ABSTRACT;
1328 
1329             if ((flags & (INTERFACE | VALUE_CLASS)) == 0) {
1330                 implicit |= IDENTITY_TYPE;
1331             }
1332 
1333             if ((flags & ENUM) != 0) {
1334                 // enums can't be declared abstract, final, sealed or non-sealed or value
1335                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED | VALUE_CLASS);
1336                 implicit |= implicitEnumFinalFlag(tree);
1337             }
1338             if ((flags & RECORD) != 0) {
1339                 // records can't be declared abstract
1340                 mask &= ~ABSTRACT;
1341                 implicit |= FINAL;
1342             }
1343             if ((flags & STRICTFP) != 0) {
1344                 warnOnExplicitStrictfp(pos);
1345             }
1346             // Imply STRICTFP if owner has STRICTFP set.
1347             implicit |= sym.owner.flags_field & STRICTFP;
1348 
1349             // concrete value classes are implicitly final
1350             if ((flags & (ABSTRACT | INTERFACE | VALUE_CLASS)) == VALUE_CLASS) {
1351                 implicit |= FINAL;
1352             }
1353 
1354             // TYPs can't be declared synchronized
1355             mask &= ~SYNCHRONIZED;
1356             break;
1357         default:
1358             throw new AssertionError();
1359         }
1360         long illegal = flags & ExtendedStandardFlags & ~mask;
1361         if (illegal != 0) {
1362             if ((illegal & INTERFACE) != 0) {
1363                 log.error(pos, ((flags & ANNOTATION) != 0) ? Errors.AnnotationDeclNotAllowedHere : Errors.IntfNotAllowedHere);
1364                 mask |= INTERFACE;
1365             }
1366             else {
1367                 log.error(pos,
1368                         Errors.ModNotAllowedHere(asFlagSet(illegal)));
1369             }
1370         }
1371         else if ((sym.kind == TYP ||
1372                   // ISSUE: Disallowing abstract&private is no longer appropriate
1373                   // in the presence of inner classes. Should it be deleted here?
1374                   checkDisjoint(pos, flags,
1375                                 ABSTRACT,
1376                                 PRIVATE | STATIC | DEFAULT))
1377                  &&
1378                  checkDisjoint(pos, flags,
1379                                 STATIC | PRIVATE,
1380                                 DEFAULT)
1381                  &&
1382                  checkDisjoint(pos, flags,
1383                                ABSTRACT | INTERFACE,
1384                                FINAL | NATIVE | SYNCHRONIZED)
1385                  &&
1386                  checkDisjoint(pos, flags,
1387                         INTERFACE,
1388                         VALUE_CLASS)
1389                  &&
1390                  checkDisjoint(pos, flags,
1391                                PUBLIC,
1392                                PRIVATE | PROTECTED)
1393                  &&
1394                  checkDisjoint(pos, flags,
1395                                PRIVATE,
1396                                PUBLIC | PROTECTED)
1397                  &&
1398                  checkDisjoint(pos, (flags | implicit), // complain against volatile & implcitly final entities too.
1399                                FINAL,
1400                                VOLATILE)
1401                  &&
1402                  (sym.kind == TYP ||
1403                   checkDisjoint(pos, flags,
1404                                 ABSTRACT | NATIVE,
1405                                 STRICTFP))
1406                  && checkDisjoint(pos, flags,
1407                                 FINAL,
1408                            SEALED | NON_SEALED)
1409                  && checkDisjoint(pos, flags,
1410                                 SEALED,
1411                            FINAL | NON_SEALED)
1412                  && checkDisjoint(pos, flags,
1413                                 SEALED,
1414                                 ANNOTATION)
1415                 && checkDisjoint(pos, flags,
1416                                 VALUE_CLASS,
1417                                 ANNOTATION)
1418                 && checkDisjoint(pos, flags,
1419                                 VALUE_CLASS,
1420                                 NON_SEALED)
1421                 && checkDisjoint(pos, flags,
1422                                 VALUE_CLASS,
1423                                 INTERFACE) ) {
1424             // skip
1425         }
1426         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1427     }
1428 
1429     private void warnOnExplicitStrictfp(DiagnosticPosition pos) {
1430         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(pos);
1431         try {
1432             deferredLintHandler.report(() -> {
1433                                            if (lint.isEnabled(LintCategory.STRICTFP)) {
1434                                                log.warning(LintCategory.STRICTFP,
1435                                                            pos, Warnings.Strictfp); }
1436                                        });
1437         } finally {
1438             deferredLintHandler.setPos(prevLintPos);
1439         }
1440     }
1441 
1442 
1443     /** Determine if this enum should be implicitly final.

2648     /** Check that all abstract methods implemented by a class are
2649      *  mutually compatible.
2650      *  @param pos          Position to be used for error reporting.
2651      *  @param c            The class whose interfaces are checked.
2652      */
2653     void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {
2654         List<Type> supertypes = types.interfaces(c);
2655         Type supertype = types.supertype(c);
2656         if (supertype.hasTag(CLASS) &&
2657             (supertype.tsym.flags() & ABSTRACT) != 0)
2658             supertypes = supertypes.prepend(supertype);
2659         for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) {
2660             if (!l.head.getTypeArguments().isEmpty() &&
2661                 !checkCompatibleAbstracts(pos, l.head, l.head, c))
2662                 return;
2663             for (List<Type> m = supertypes; m != l; m = m.tail)
2664                 if (!checkCompatibleAbstracts(pos, l.head, m.head, c))
2665                     return;
2666         }
2667         checkCompatibleConcretes(pos, c);
2668 
2669         boolean cIsValue = (c.tsym.flags() & VALUE_CLASS) != 0;
2670         Type identitySuper = null, valueSuper = null;
2671         for (Type t : types.closure(c)) {
2672             if (t != c) {
2673                 if ((t.tsym.flags() & IDENTITY_TYPE) != 0 && (t.tsym.flags() & VALUE_BASED) == 0)
2674                     identitySuper = t;
2675                 else if ((t.tsym.flags() & VALUE_CLASS) != 0)
2676                     valueSuper = t;
2677                 if (cIsValue && identitySuper != null && identitySuper.tsym != syms.objectType.tsym) { // Object is special
2678                     log.error(pos, Errors.ValueTypeHasIdentitySuperType(c, identitySuper));
2679                     break;
2680                 }
2681             }
2682         }
2683     }
2684 
2685     /** Check that all non-override equivalent methods accessible from 'site'
2686      *  are mutually compatible (JLS 8.4.8/9.4.1).
2687      *
2688      *  @param pos  Position to be used for error reporting.
2689      *  @param site The class whose methods are checked.
2690      *  @param sym  The method symbol to be checked.
2691      */
2692     void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
2693          ClashFilter cf = new ClashFilter(site);
2694         //for each method m1 that is overridden (directly or indirectly)
2695         //by method 'sym' in 'site'...
2696 
2697         ArrayList<Symbol> symbolsByName = new ArrayList<>();
2698         types.membersClosure(site, false).getSymbolsByName(sym.name, cf).forEach(symbolsByName::add);
2699         for (Symbol m1 : symbolsByName) {
2700             if (!sym.overrides(m1, site.tsym, types, false)) {
2701                 continue;
2702             }

4137                     break;
4138 
4139                 // super()/this() calls must only appear in a constructor
4140                 if (!constructor) {
4141                     log.error(apply.pos(), Errors.CallMustOnlyAppearInCtor);
4142                     break;
4143                 }
4144 
4145                 // super()/this() calls must be a top level statement
4146                 if (scanDepth != MATCH_SCAN_DEPTH) {
4147                     log.error(apply.pos(), Errors.CtorCallsNotAllowedHere);
4148                     break;
4149                 }
4150 
4151                 // super()/this() calls must not appear more than once
4152                 if (initCall != null) {
4153                     log.error(apply.pos(), Errors.RedundantSuperclassInit);
4154                     break;
4155                 }
4156 
4157                 // valhalla is using this feature so commenting this code for now so that the
4158                 // build doesn't depend on preview code
4159                 // If super()/this() isn't first, require "statements before super()" feature
4160                 /*if (!firstStatement) {
4161                     preview.checkSourceLevel(apply.pos(), Feature.SUPER_INIT);
4162                 }*/
4163 
4164                 // We found a legitimate super()/this() call; remember it
4165                 initCall = methodName;
4166             } while (false);
4167 
4168             // Proceed
4169             super.visitApply(apply);
4170         }
4171 
4172         @Override
4173         public void visitReturn(JCReturn tree) {
4174             if (constructor && initCall == null && earlyReturn == null)
4175                 earlyReturn = tree;             // we have seen a return but not (yet) a super()/this()
4176             super.visitReturn(tree);
4177         }
4178 
4179         @Override
4180         public void visitClassDef(JCClassDecl tree) {
4181             // don't descend any further
4182         }
< prev index next >