< prev index next >

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

Print this page

 167 
 168         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 169         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 170         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 171         boolean enforceMandatoryWarnings = true;
 172 
 173         deprecationHandler = new MandatoryWarningHandler(log, null, verboseDeprecated,
 174                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 175         removalHandler = new MandatoryWarningHandler(log, null, verboseRemoval,
 176                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 177         uncheckedHandler = new MandatoryWarningHandler(log, null, verboseUnchecked,
 178                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 179         sunApiHandler = new MandatoryWarningHandler(log, null, false,
 180                 enforceMandatoryWarnings, "sunapi", null);
 181 
 182         deferredLintHandler = DeferredLintHandler.instance(context);
 183 
 184         allowModules = Feature.MODULES.allowedInSource(source);
 185         allowRecords = Feature.RECORDS.allowedInSource(source);
 186         allowSealed = Feature.SEALED_CLASSES.allowedInSource(source);

 187     }
 188 
 189     /** Character for synthetic names
 190      */
 191     char syntheticNameChar;
 192 
 193     /** A table mapping flat names of all compiled classes for each module in this run
 194      *  to their symbols; maintained from outside.
 195      */
 196     private Map<Pair<ModuleSymbol, Name>,ClassSymbol> compiled = new HashMap<>();
 197 
 198     /** A handler for messages about deprecated usage.
 199      */
 200     private MandatoryWarningHandler deprecationHandler;
 201 
 202     /** A handler for messages about deprecated-for-removal usage.
 203      */
 204     private MandatoryWarningHandler removalHandler;
 205 
 206     /** A handler for messages about unchecked or unsafe usage.

 210     /** A handler for messages about using proprietary API.
 211      */
 212     private MandatoryWarningHandler sunApiHandler;
 213 
 214     /** A handler for deferred lint warnings.
 215      */
 216     private DeferredLintHandler deferredLintHandler;
 217 
 218     /** Are modules allowed
 219      */
 220     private final boolean allowModules;
 221 
 222     /** Are records allowed
 223      */
 224     private final boolean allowRecords;
 225 
 226     /** Are sealed classes allowed
 227      */
 228     private final boolean allowSealed;
 229 




 230 /* *************************************************************************
 231  * Errors and Warnings
 232  **************************************************************************/
 233 
 234     Lint setLint(Lint newLint) {
 235         Lint prev = lint;
 236         lint = newLint;
 237         return prev;
 238     }
 239 
 240     MethodSymbol setMethod(MethodSymbol newMethod) {
 241         MethodSymbol prev = method;
 242         method = newMethod;
 243         return prev;
 244     }
 245 
 246     /** Warn about deprecated symbol.
 247      *  @param pos        Position to be used for error reporting.
 248      *  @param sym        The deprecated symbol.
 249      */

 342         return syms.errType;
 343     }
 344 
 345     /** Report an error that wrong type tag was found.
 346      *  @param pos        Position to be used for error reporting.
 347      *  @param required   An internationalized string describing the type tag
 348      *                    required.
 349      *  @param found      The type that was found.
 350      */
 351     Type typeTagError(DiagnosticPosition pos, JCDiagnostic required, Object found) {
 352         // this error used to be raised by the parser,
 353         // but has been delayed to this point:
 354         if (found instanceof Type type && type.hasTag(VOID)) {
 355             log.error(pos, Errors.IllegalStartOfType);
 356             return syms.errType;
 357         }
 358         log.error(pos, Errors.TypeFoundReq(found, required));
 359         return types.createErrorType(found instanceof Type type ? type : syms.errType);
 360     }
 361 
 362     /** Report an error that symbol cannot be referenced before super
 363      *  has been called.
 364      *  @param pos        Position to be used for error reporting.
 365      *  @param sym        The referenced symbol.
 366      */
 367     void earlyRefError(DiagnosticPosition pos, Symbol sym) {
 368         log.error(pos, Errors.CantRefBeforeCtorCalled(sym));
 369     }
 370 
 371     /** Report duplicate declaration error.
 372      */
 373     void duplicateError(DiagnosticPosition pos, Symbol sym) {
 374         if (!sym.type.isErroneous()) {
 375             Symbol location = sym.location();
 376             if (location.kind == MTH &&
 377                     ((MethodSymbol)location).isStaticOrInstanceInit()) {
 378                 log.error(pos,
 379                           Errors.AlreadyDefinedInClinit(kindName(sym),
 380                                                         sym,
 381                                                         kindName(sym.location()),
 382                                                         kindName(sym.location().enclClass()),
 383                                                         sym.location().enclClass()));
 384             } else {
 385                 /* dont error if this is a duplicated parameter of a generated canonical constructor
 386                  * as we should have issued an error for the duplicated fields
 387                  */
 388                 if (location.kind != MTH ||
 389                         ((sym.owner.flags_field & GENERATEDCONSTR) == 0) ||
 390                         ((sym.owner.flags_field & RECORD) == 0)) {

 751     /** Check that type is a class or interface type.
 752      *  @param pos           Position to be used for error reporting.
 753      *  @param t             The type to be checked.
 754      */
 755     Type checkClassType(DiagnosticPosition pos, Type t) {
 756         if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) {
 757             return typeTagError(pos,
 758                                 diags.fragment(Fragments.TypeReqClass),
 759                                 asTypeParam(t));
 760         } else {
 761             return t;
 762         }
 763     }
 764     //where
 765         private Object asTypeParam(Type t) {
 766             return (t.hasTag(TYPEVAR))
 767                                     ? diags.fragment(Fragments.TypeParameter(t))
 768                                     : t;
 769         }
 770 

























 771     /** Check that type is a valid qualifier for a constructor reference expression
 772      */
 773     Type checkConstructorRefType(DiagnosticPosition pos, Type t) {
 774         t = checkClassOrArrayType(pos, t);
 775         if (t.hasTag(CLASS)) {
 776             if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) {
 777                 log.error(pos, Errors.AbstractCantBeInstantiated(t.tsym));
 778                 t = types.createErrorType(t);
 779             } else if ((t.tsym.flags() & ENUM) != 0) {
 780                 log.error(pos, Errors.EnumCantBeInstantiated);
 781                 t = types.createErrorType(t);
 782             } else {
 783                 t = checkClassType(pos, t, true);
 784             }
 785         } else if (t.hasTag(ARRAY)) {
 786             if (!types.isReifiable(((ArrayType)t).elemtype)) {
 787                 log.error(pos, Errors.GenericArrayCreation);
 788                 t = types.createErrorType(t);
 789             }
 790         }

 808                 args = args.tail;
 809             }
 810         }
 811         return t;
 812     }
 813 
 814     /** Check that type is a reference type, i.e. a class, interface or array type
 815      *  or a type variable.
 816      *  @param pos           Position to be used for error reporting.
 817      *  @param t             The type to be checked.
 818      */
 819     Type checkRefType(DiagnosticPosition pos, Type t) {
 820         if (t.isReference())
 821             return t;
 822         else
 823             return typeTagError(pos,
 824                                 diags.fragment(Fragments.TypeReqRef),
 825                                 t);
 826     }
 827 






















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

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





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

1246             } else {
1247                 mask = MethodFlags;


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




1283             if ((flags & ENUM) != 0) {
1284                 // enums can't be declared abstract, final, sealed or non-sealed
1285                 mask &= ~(ABSTRACT | FINAL | SEALED | NON_SEALED);
1286                 implicit |= implicitEnumFinalFlag(tree);
1287             }
1288             if ((flags & RECORD) != 0) {
1289                 // records can't be declared abstract
1290                 mask &= ~ABSTRACT;
1291                 implicit |= FINAL;
1292             }
1293             if ((flags & STRICTFP) != 0) {
1294                 warnOnExplicitStrictfp(pos);
1295             }
1296             // Imply STRICTFP if owner has STRICTFP set.
1297             implicit |= sym.owner.flags_field & STRICTFP;








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




1328                  checkDisjoint(pos, flags,
1329                                PUBLIC,
1330                                PRIVATE | PROTECTED)
1331                  &&
1332                  checkDisjoint(pos, flags,
1333                                PRIVATE,
1334                                PUBLIC | PROTECTED)
1335                  &&
1336                  checkDisjoint(pos, flags,
1337                                FINAL,
1338                                VOLATILE)
1339                  &&
1340                  (sym.kind == TYP ||
1341                   checkDisjoint(pos, flags,
1342                                 ABSTRACT | NATIVE,
1343                                 STRICTFP))
1344                  && checkDisjoint(pos, flags,
1345                                 FINAL,
1346                            SEALED | NON_SEALED)
1347                  && checkDisjoint(pos, flags,
1348                                 SEALED,
1349                            FINAL | NON_SEALED)
1350                  && checkDisjoint(pos, flags,
1351                                 SEALED,
1352                                 ANNOTATION)) {









1353             // skip
1354         }
1355         return flags & (mask | ~ExtendedStandardFlags) | implicit;
1356     }
1357 
1358     private void warnOnExplicitStrictfp(DiagnosticPosition pos) {
1359         DiagnosticPosition prevLintPos = deferredLintHandler.setPos(pos);
1360         try {
1361             deferredLintHandler.report(() -> {
1362                                            if (lint.isEnabled(LintCategory.STRICTFP)) {
1363                                                log.warning(LintCategory.STRICTFP,
1364                                                            pos, Warnings.Strictfp); }
1365                                        });
1366         } finally {
1367             deferredLintHandler.setPos(prevLintPos);
1368         }
1369     }
1370 
1371 
1372     /** Determine if this enum should be implicitly final.

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















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

3917             checkAnnotationResType(pos, types.elemtype(type));
3918             break;
3919         default:
3920             break; // int etc
3921         }
3922     }
3923 
3924 /* *************************************************************************
3925  * Check for cycles in the constructor call graph.
3926  **************************************************************************/
3927 
3928     /** Check for cycles in the graph of constructors calling other
3929      *  constructors.
3930      */
3931     void checkCyclicConstructors(JCClassDecl tree) {
3932         // use LinkedHashMap so we generate errors deterministically
3933         Map<Symbol,Symbol> callMap = new LinkedHashMap<>();
3934 
3935         // enter each constructor this-call into the map
3936         for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
3937             JCMethodInvocation app = TreeInfo.firstConstructorCall(l.head);
3938             if (app == null) continue;
3939             JCMethodDecl meth = (JCMethodDecl) l.head;
3940             if (TreeInfo.name(app.meth) == names._this) {

3941                 callMap.put(meth.sym, TreeInfo.symbol(app.meth));
3942             } else {
3943                 meth.sym.flags_field |= ACYCLIC;
3944             }
3945         }
3946 
3947         // Check for cycles in the map
3948         Symbol[] ctors = new Symbol[0];
3949         ctors = callMap.keySet().toArray(ctors);
3950         for (Symbol caller : ctors) {
3951             checkCyclicConstructor(tree, caller, callMap);
3952         }
3953     }
3954 
3955     /** Look in the map to see if the given constructor is part of a
3956      *  call cycle.
3957      */
3958     private void checkCyclicConstructor(JCClassDecl tree, Symbol ctor,
3959                                         Map<Symbol,Symbol> callMap) {
3960         if (ctor != null && (ctor.flags_field & ACYCLIC) == 0) {
3961             if ((ctor.flags_field & LOCKED) != 0) {
3962                 log.error(TreeInfo.diagnosticPositionFor(ctor, tree, false, t -> t.hasTag(IDENT)),
3963                           Errors.RecursiveCtorInvocation);
3964             } else {
3965                 ctor.flags_field |= LOCKED;
3966                 checkCyclicConstructor(tree, callMap.remove(ctor), callMap);
3967                 ctor.flags_field &= ~LOCKED;
3968             }
3969             ctor.flags_field |= ACYCLIC;
3970         }
3971     }
3972 





























































































































3973 /* *************************************************************************
3974  * Miscellaneous
3975  **************************************************************************/
3976 
3977     /**
3978      *  Check for division by integer constant zero
3979      *  @param pos           Position for error reporting.
3980      *  @param operator      The operator for the expression
3981      *  @param operand       The right hand operand for the expression
3982      */
3983     void checkDivZero(final DiagnosticPosition pos, Symbol operator, Type operand) {
3984         if (operand.constValue() != null
3985             && operand.getTag().isSubRangeOf(LONG)
3986             && ((Number) (operand.constValue())).longValue() == 0) {
3987             int opc = ((OperatorSymbol)operator).opcode;
3988             if (opc == ByteCodes.idiv || opc == ByteCodes.imod
3989                 || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) {
3990                 deferredLintHandler.report(() -> warnDivZero(pos));
3991             }
3992         }

 167 
 168         boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION);
 169         boolean verboseRemoval = lint.isEnabled(LintCategory.REMOVAL);
 170         boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED);
 171         boolean enforceMandatoryWarnings = true;
 172 
 173         deprecationHandler = new MandatoryWarningHandler(log, null, verboseDeprecated,
 174                 enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION);
 175         removalHandler = new MandatoryWarningHandler(log, null, verboseRemoval,
 176                 enforceMandatoryWarnings, "removal", LintCategory.REMOVAL);
 177         uncheckedHandler = new MandatoryWarningHandler(log, null, verboseUnchecked,
 178                 enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED);
 179         sunApiHandler = new MandatoryWarningHandler(log, null, false,
 180                 enforceMandatoryWarnings, "sunapi", null);
 181 
 182         deferredLintHandler = DeferredLintHandler.instance(context);
 183 
 184         allowModules = Feature.MODULES.allowedInSource(source);
 185         allowRecords = Feature.RECORDS.allowedInSource(source);
 186         allowSealed = Feature.SEALED_CLASSES.allowedInSource(source);
 187         allowValueClasses = Feature.VALUE_CLASSES.allowedInSource(source);
 188     }
 189 
 190     /** Character for synthetic names
 191      */
 192     char syntheticNameChar;
 193 
 194     /** A table mapping flat names of all compiled classes for each module in this run
 195      *  to their symbols; maintained from outside.
 196      */
 197     private Map<Pair<ModuleSymbol, Name>,ClassSymbol> compiled = new HashMap<>();
 198 
 199     /** A handler for messages about deprecated usage.
 200      */
 201     private MandatoryWarningHandler deprecationHandler;
 202 
 203     /** A handler for messages about deprecated-for-removal usage.
 204      */
 205     private MandatoryWarningHandler removalHandler;
 206 
 207     /** A handler for messages about unchecked or unsafe usage.

 211     /** A handler for messages about using proprietary API.
 212      */
 213     private MandatoryWarningHandler sunApiHandler;
 214 
 215     /** A handler for deferred lint warnings.
 216      */
 217     private DeferredLintHandler deferredLintHandler;
 218 
 219     /** Are modules allowed
 220      */
 221     private final boolean allowModules;
 222 
 223     /** Are records allowed
 224      */
 225     private final boolean allowRecords;
 226 
 227     /** Are sealed classes allowed
 228      */
 229     private final boolean allowSealed;
 230 
 231     /** Are value classes allowed
 232      */
 233     private final boolean allowValueClasses;
 234 
 235 /* *************************************************************************
 236  * Errors and Warnings
 237  **************************************************************************/
 238 
 239     Lint setLint(Lint newLint) {
 240         Lint prev = lint;
 241         lint = newLint;
 242         return prev;
 243     }
 244 
 245     MethodSymbol setMethod(MethodSymbol newMethod) {
 246         MethodSymbol prev = method;
 247         method = newMethod;
 248         return prev;
 249     }
 250 
 251     /** Warn about deprecated symbol.
 252      *  @param pos        Position to be used for error reporting.
 253      *  @param sym        The deprecated symbol.
 254      */

 347         return syms.errType;
 348     }
 349 
 350     /** Report an error that wrong type tag was found.
 351      *  @param pos        Position to be used for error reporting.
 352      *  @param required   An internationalized string describing the type tag
 353      *                    required.
 354      *  @param found      The type that was found.
 355      */
 356     Type typeTagError(DiagnosticPosition pos, JCDiagnostic required, Object found) {
 357         // this error used to be raised by the parser,
 358         // but has been delayed to this point:
 359         if (found instanceof Type type && type.hasTag(VOID)) {
 360             log.error(pos, Errors.IllegalStartOfType);
 361             return syms.errType;
 362         }
 363         log.error(pos, Errors.TypeFoundReq(found, required));
 364         return types.createErrorType(found instanceof Type type ? type : syms.errType);
 365     }
 366 









 367     /** Report duplicate declaration error.
 368      */
 369     void duplicateError(DiagnosticPosition pos, Symbol sym) {
 370         if (!sym.type.isErroneous()) {
 371             Symbol location = sym.location();
 372             if (location.kind == MTH &&
 373                     ((MethodSymbol)location).isStaticOrInstanceInit()) {
 374                 log.error(pos,
 375                           Errors.AlreadyDefinedInClinit(kindName(sym),
 376                                                         sym,
 377                                                         kindName(sym.location()),
 378                                                         kindName(sym.location().enclClass()),
 379                                                         sym.location().enclClass()));
 380             } else {
 381                 /* dont error if this is a duplicated parameter of a generated canonical constructor
 382                  * as we should have issued an error for the duplicated fields
 383                  */
 384                 if (location.kind != MTH ||
 385                         ((sym.owner.flags_field & GENERATEDCONSTR) == 0) ||
 386                         ((sym.owner.flags_field & RECORD) == 0)) {

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

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

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

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

4008             checkAnnotationResType(pos, types.elemtype(type));
4009             break;
4010         default:
4011             break; // int etc
4012         }
4013     }
4014 
4015 /* *************************************************************************
4016  * Check for cycles in the constructor call graph.
4017  **************************************************************************/
4018 
4019     /** Check for cycles in the graph of constructors calling other
4020      *  constructors.
4021      */
4022     void checkCyclicConstructors(JCClassDecl tree) {
4023         // use LinkedHashMap so we generate errors deterministically
4024         Map<Symbol,Symbol> callMap = new LinkedHashMap<>();
4025 
4026         // enter each constructor this-call into the map
4027         for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
4028             if (!TreeInfo.isConstructor(l.head))
4029                 continue;
4030             JCMethodDecl meth = (JCMethodDecl)l.head;
4031             JCMethodInvocation app = TreeInfo.findConstructorCall(meth);
4032             if (app != null && TreeInfo.name(app.meth) == names._this) {
4033                 callMap.put(meth.sym, TreeInfo.symbol(app.meth));
4034             } else {
4035                 meth.sym.flags_field |= ACYCLIC;
4036             }
4037         }
4038 
4039         // Check for cycles in the map
4040         Symbol[] ctors = new Symbol[0];
4041         ctors = callMap.keySet().toArray(ctors);
4042         for (Symbol caller : ctors) {
4043             checkCyclicConstructor(tree, caller, callMap);
4044         }
4045     }
4046 
4047     /** Look in the map to see if the given constructor is part of a
4048      *  call cycle.
4049      */
4050     private void checkCyclicConstructor(JCClassDecl tree, Symbol ctor,
4051                                         Map<Symbol,Symbol> callMap) {
4052         if (ctor != null && (ctor.flags_field & ACYCLIC) == 0) {
4053             if ((ctor.flags_field & LOCKED) != 0) {
4054                 log.error(TreeInfo.diagnosticPositionFor(ctor, tree, false, t -> t.hasTag(IDENT)),
4055                           Errors.RecursiveCtorInvocation);
4056             } else {
4057                 ctor.flags_field |= LOCKED;
4058                 checkCyclicConstructor(tree, callMap.remove(ctor), callMap);
4059                 ctor.flags_field &= ~LOCKED;
4060             }
4061             ctor.flags_field |= ACYCLIC;
4062         }
4063     }
4064 
4065 /* *************************************************************************
4066  * Verify the proper placement of super()/this() calls.
4067  *
4068  *    - super()/this() may only appear in constructors
4069  *    - There must be at most one super()/this() call per constructor
4070  *    - The super()/this() call, if any, must be a top-level statement in the
4071  *      constructor, i.e., not nested inside any other statement or block
4072  *    - There must be no return statements prior to the super()/this() call
4073  **************************************************************************/
4074 
4075     void checkSuperInitCalls(JCClassDecl tree) {
4076         new SuperThisChecker().check(tree);
4077     }
4078 
4079     private class SuperThisChecker extends TreeScanner {
4080 
4081         // Match this scan stack: 1=JCMethodDecl, 2=JCExpressionStatement, 3=JCMethodInvocation
4082         private static final int MATCH_SCAN_DEPTH = 3;
4083 
4084         private boolean constructor;        // is this method a constructor?
4085         private boolean firstStatement;     // at the first statement in method?
4086         private JCReturn earlyReturn;       // first return prior to the super()/init(), if any
4087         private Name initCall;              // whichever of "super" or "init" we've seen already
4088         private int scanDepth;              // current scan recursion depth in method body
4089 
4090         public void check(JCClassDecl classDef) {
4091             scan(classDef.defs);
4092         }
4093 
4094         @Override
4095         public void visitMethodDef(JCMethodDecl tree) {
4096             Assert.check(!constructor);
4097             Assert.check(earlyReturn == null);
4098             Assert.check(initCall == null);
4099             Assert.check(scanDepth == 1);
4100 
4101             // Initialize state for this method
4102             constructor = TreeInfo.isConstructor(tree);
4103             try {
4104 
4105                 // Scan method body
4106                 if (tree.body != null) {
4107                     firstStatement = true;
4108                     for (List<JCStatement> l = tree.body.stats; l.nonEmpty(); l = l.tail) {
4109                         scan(l.head);
4110                         firstStatement = false;
4111                     }
4112                 }
4113 
4114                 // Verify no 'return' seen prior to an explicit super()/this() call
4115                 if (constructor && earlyReturn != null && initCall != null)
4116                     log.error(earlyReturn.pos(), Errors.ReturnBeforeSuperclassInitialized);
4117             } finally {
4118                 firstStatement = false;
4119                 constructor = false;
4120                 earlyReturn = null;
4121                 initCall = null;
4122             }
4123         }
4124 
4125         @Override
4126         public void scan(JCTree tree) {
4127             scanDepth++;
4128             try {
4129                 super.scan(tree);
4130             } finally {
4131                 scanDepth--;
4132             }
4133         }
4134 
4135         @Override
4136         public void visitApply(JCMethodInvocation apply) {
4137             do {
4138 
4139                 // Is this a super() or this() call?
4140                 Name methodName = TreeInfo.name(apply.meth);
4141                 if (methodName != names._super && methodName != names._this)
4142                     break;
4143 
4144                 // super()/this() calls must only appear in a constructor
4145                 if (!constructor) {
4146                     log.error(apply.pos(), Errors.CallMustOnlyAppearInCtor);
4147                     break;
4148                 }
4149 
4150                 // super()/this() calls must be a top level statement
4151                 if (scanDepth != MATCH_SCAN_DEPTH) {
4152                     log.error(apply.pos(), Errors.CtorCallsNotAllowedHere);
4153                     break;
4154                 }
4155 
4156                 // super()/this() calls must not appear more than once
4157                 if (initCall != null) {
4158                     log.error(apply.pos(), Errors.RedundantSuperclassInit);
4159                     break;
4160                 }
4161 
4162                 // valhalla is using this feature so commenting this code for now so that the
4163                 // build doesn't depend on preview code
4164                 // If super()/this() isn't first, require "statements before super()" feature
4165                 /*if (!firstStatement) {
4166                     preview.checkSourceLevel(apply.pos(), Feature.SUPER_INIT);
4167                 }*/
4168 
4169                 // We found a legitimate super()/this() call; remember it
4170                 initCall = methodName;
4171             } while (false);
4172 
4173             // Proceed
4174             super.visitApply(apply);
4175         }
4176 
4177         @Override
4178         public void visitReturn(JCReturn tree) {
4179             if (constructor && initCall == null && earlyReturn == null)
4180                 earlyReturn = tree;             // we have seen a return but not (yet) a super()/this()
4181             super.visitReturn(tree);
4182         }
4183 
4184         @Override
4185         public void visitClassDef(JCClassDecl tree) {
4186             // don't descend any further
4187         }
4188     }
4189 
4190 /* *************************************************************************
4191  * Miscellaneous
4192  **************************************************************************/
4193 
4194     /**
4195      *  Check for division by integer constant zero
4196      *  @param pos           Position for error reporting.
4197      *  @param operator      The operator for the expression
4198      *  @param operand       The right hand operand for the expression
4199      */
4200     void checkDivZero(final DiagnosticPosition pos, Symbol operator, Type operand) {
4201         if (operand.constValue() != null
4202             && operand.getTag().isSubRangeOf(LONG)
4203             && ((Number) (operand.constValue())).longValue() == 0) {
4204             int opc = ((OperatorSymbol)operator).opcode;
4205             if (opc == ByteCodes.idiv || opc == ByteCodes.imod
4206                 || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) {
4207                 deferredLintHandler.report(() -> warnDivZero(pos));
4208             }
4209         }
< prev index next >