< prev index next >

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

Print this page

 288      *  @param v      The assigned variable
 289      *  @param base   If the variable is referred to in a Select, the part
 290      *                to the left of the `.', null otherwise.
 291      *  @param env    The current environment.
 292      */
 293     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
 294         if (v.name == names._this) {
 295             log.error(pos, Errors.CantAssignValToThis);
 296         } else if ((v.flags() & FINAL) != 0 &&
 297             ((v.flags() & HASINIT) != 0
 298              ||
 299              !((base == null ||
 300                TreeInfo.isThisQualifier(base)) &&
 301                isAssignableAsBlankFinal(v, env)))) {
 302             if (v.isResourceVariable()) { //TWR resource
 303                 log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
 304             } else {
 305                 log.error(pos, Errors.CantAssignValToVar(Flags.toSource(v.flags() & (STATIC | FINAL)), v));
 306             }
 307         }











 308     }
 309 
 310     /** Does tree represent a static reference to an identifier?
 311      *  It is assumed that tree is either a SELECT or an IDENT.
 312      *  We have to weed out selects from non-type names here.
 313      *  @param tree    The candidate tree.
 314      */
 315     boolean isStaticReference(JCTree tree) {
 316         if (tree.hasTag(SELECT)) {
 317             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
 318             if (lsym == null || lsym.kind != TYP) {
 319                 return false;
 320             }
 321         }
 322         return true;
 323     }
 324 
 325     /** Is this symbol a type?
 326      */
 327     static boolean isType(Symbol sym) {

1172                 }
1173                 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1174                     log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
1175             } else {
1176                 if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1177                     if ((owner.flags() & INTERFACE) != 0) {
1178                         log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1179                     } else {
1180                         log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1181                     }
1182                 } else if ((tree.mods.flags & NATIVE) != 0) {
1183                     log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1184                 }
1185                 // Add an implicit super() call unless an explicit call to
1186                 // super(...) or this(...) is given
1187                 // or we are compiling class java.lang.Object.
1188                 if (isConstructor && owner.type != syms.objectType) {
1189                     if (!TreeInfo.hasAnyConstructorCall(tree)) {
1190                         JCStatement supCall = make.at(tree.body.pos).Exec(make.Apply(List.nil(),
1191                                 make.Ident(names._super), make.Idents(List.nil())));
1192                         tree.body.stats = tree.body.stats.prepend(supCall);




1193                     } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
1194                             (tree.mods.flags & GENERATEDCONSTR) == 0 &&
1195                             TreeInfo.hasConstructorCall(tree, names._super)) {
1196                         // enum constructors are not allowed to call super
1197                         // directly, so make sure there aren't any super calls
1198                         // in enum constructors, except in the compiler
1199                         // generated one.
1200                         log.error(tree.body.stats.head.pos(),
1201                                   Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
1202                     }
1203                     if (env.enclClass.sym.isRecord() && (tree.sym.flags_field & RECORD) != 0) { // we are seeing the canonical constructor
1204                         List<Name> recordComponentNames = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.name);
1205                         List<Name> initParamNames = tree.sym.params.map(p -> p.name);
1206                         if (!initParamNames.equals(recordComponentNames)) {
1207                             log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1208                                     Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalWithNameMismatch));
1209                         }
1210                         if (tree.sym.type.asMethodType().thrown != null && !tree.sym.type.asMethodType().thrown.isEmpty()) {
1211                             log.error(tree,
1212                                     Errors.InvalidCanonicalConstructorInRecord(

1314                 }
1315             }
1316             result = tree.type = v.type;
1317             if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1318                 if (isNonArgsMethodInObject(v.name)) {
1319                     log.error(tree, Errors.IllegalRecordComponentName(v));
1320                 }
1321             }
1322         }
1323         finally {
1324             chk.setLint(prevLint);
1325         }
1326     }
1327 
1328     private boolean isNonArgsMethodInObject(Name name) {
1329         for (Symbol s : syms.objectType.tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
1330             if (s.type.getParameterTypes().isEmpty()) {
1331                 return true;
1332             }
1333         }
1334         return false;

1335     }
1336 
1337     Fragment canInferLocalVarType(JCVariableDecl tree) {
1338         LocalInitScanner lis = new LocalInitScanner();
1339         lis.scan(tree.init);
1340         return lis.badInferenceMsg;
1341     }
1342 
1343     static class LocalInitScanner extends TreeScanner {
1344         Fragment badInferenceMsg = null;
1345         boolean needsTarget = true;
1346 
1347         @Override
1348         public void visitNewArray(JCNewArray tree) {
1349             if (tree.elemtype == null && needsTarget) {
1350                 badInferenceMsg = Fragments.LocalArrayMissingTarget;
1351             }
1352         }
1353 
1354         @Override

1398             }
1399         }
1400     }
1401 
1402     public void visitSkip(JCSkip tree) {
1403         result = null;
1404     }
1405 
1406     public void visitBlock(JCBlock tree) {
1407         if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1408             // Block is a static or instance initializer;
1409             // let the owner of the environment be a freshly
1410             // created BLOCK-method.
1411             Symbol fakeOwner =
1412                 new MethodSymbol(tree.flags | BLOCK |
1413                     env.info.scope.owner.flags() & STRICTFP, names.empty, null,
1414                     env.info.scope.owner);
1415             final Env<AttrContext> localEnv =
1416                 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1417 
1418             if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;




1419             // Attribute all type annotations in the block
1420             annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
1421             annotate.flush();
1422             attribStats(tree.stats, localEnv);
1423 
1424             {
1425                 // Store init and clinit type annotations with the ClassSymbol
1426                 // to allow output in Gen.normalizeDefs.
1427                 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1428                 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1429                 if ((tree.flags & STATIC) != 0) {
1430                     cs.appendClassInitTypeAttributes(tas);
1431                 } else {
1432                     cs.appendInitTypeAttributes(tas);
1433                 }
1434             }
1435         } else {
1436             // Create a new local environment with a local scope.
1437             Env<AttrContext> localEnv =
1438                 env.dup(tree, env.info.dup(env.info.scope.dup()));

1907     // where
1908     /** Return the selected enumeration constant symbol, or null. */
1909     private Symbol enumConstant(JCTree tree, Type enumType) {
1910         if (tree.hasTag(IDENT)) {
1911             JCIdent ident = (JCIdent)tree;
1912             Name name = ident.name;
1913             for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
1914                 if (sym.kind == VAR) {
1915                     Symbol s = ident.sym = sym;
1916                     ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
1917                     ident.type = s.type;
1918                     return ((s.flags_field & Flags.ENUM) == 0)
1919                         ? null : s;
1920                 }
1921             }
1922         }
1923         return null;
1924     }
1925 
1926     public void visitSynchronized(JCSynchronized tree) {
1927         chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
1928         if (env.info.lint.isEnabled(LintCategory.SYNCHRONIZATION) && isValueBased(tree.lock.type)) {


1929             log.warning(LintCategory.SYNCHRONIZATION, tree.pos(), Warnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1930         }
1931         attribStat(tree.body, env);
1932         result = null;
1933     }
1934         // where
1935         private boolean isValueBased(Type t) {
1936             return t != null && t.tsym != null && (t.tsym.flags() & VALUE_BASED) != 0;
1937         }
1938 
1939 
1940     public void visitTry(JCTry tree) {
1941         // Create a new local environment with a local
1942         Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
1943         try {
1944             boolean isTryWithResource = tree.resources.nonEmpty();
1945             // Create a nested environment for attributing the try block if needed
1946             Env<AttrContext> tryEnv = isTryWithResource ?
1947                 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
1948                 localEnv;
1949             try {
1950                 // Attribute resource declarations
1951                 for (JCTree resource : tree.resources) {
1952                     CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
1953                         @Override
1954                         public void report(DiagnosticPosition pos, JCDiagnostic details) {
1955                             chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
1956                         }
1957                     };
1958                     ResultInfo twrResult =
1959                         new ResultInfo(KindSelector.VAR,

4366     }
4367 
4368     public void visitSelect(JCFieldAccess tree) {
4369         // Determine the expected kind of the qualifier expression.
4370         KindSelector skind = KindSelector.NIL;
4371         if (tree.name == names._this || tree.name == names._super ||
4372                 tree.name == names._class)
4373         {
4374             skind = KindSelector.TYP;
4375         } else {
4376             if (pkind().contains(KindSelector.PCK))
4377                 skind = KindSelector.of(skind, KindSelector.PCK);
4378             if (pkind().contains(KindSelector.TYP))
4379                 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4380             if (pkind().contains(KindSelector.VAL_MTH))
4381                 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4382         }
4383 
4384         // Attribute the qualifier expression, and determine its symbol (if any).
4385         Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));

4386         if (!pkind().contains(KindSelector.TYP_PCK))
4387             site = capture(site); // Capture field access
4388 
4389         // don't allow T.class T[].class, etc
4390         if (skind == KindSelector.TYP) {
4391             Type elt = site;
4392             while (elt.hasTag(ARRAY))
4393                 elt = ((ArrayType)elt).elemtype;
4394             if (elt.hasTag(TYPEVAR)) {
4395                 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4396                 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4397                 tree.sym = tree.type.tsym;
4398                 return ;
4399             }
4400         }
4401 
4402         // If qualifier symbol is a type or `super', assert `selectSuper'
4403         // for the selection. This is relevant for determining whether
4404         // protected symbols are accessible.
4405         Symbol sitesym = TreeInfo.symbol(tree.selected);

5465                                                       .filter(s -> s.tsym.isSealed())
5466                                                       .map(s -> (ClassSymbol) s.tsym)
5467                                                       .collect(List.collector());
5468 
5469                 if (sealedSupers.isEmpty()) {
5470                     if ((c.flags_field & Flags.NON_SEALED) != 0) {
5471                         boolean hasErrorSuper = false;
5472 
5473                         hasErrorSuper |= types.directSupertypes(c.type)
5474                                               .stream()
5475                                               .anyMatch(s -> s.tsym.kind == Kind.ERR);
5476 
5477                         ClassType ct = (ClassType) c.type;
5478 
5479                         hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
5480 
5481                         if (!hasErrorSuper) {
5482                             log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
5483                         }
5484                     }
5485                 } else {
5486                     if (c.isDirectlyOrIndirectlyLocal() && !c.isEnum()) {
5487                         log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
5488                     }
5489 
5490                     if (!c.type.isCompound()) {
5491                         for (ClassSymbol supertypeSym : sealedSupers) {
5492                             if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
5493                                 log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
5494                             }
5495                         }
5496                         if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
5497                             log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
5498                                     c.isInterface() ?
5499                                             Errors.NonSealedOrSealedExpected :
5500                                             Errors.NonSealedSealedOrFinalExpected);
5501                         }
5502                     }
5503                 }
5504 
5505                 deferredLintHandler.flush(env.tree);
5506                 env.info.returnResult = null;
5507                 // java.lang.Enum may not be subclassed by a non-enum
5508                 if (st.tsym == syms.enumSym &&
5509                     ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
5510                     log.error(env.tree.pos(), Errors.EnumNoSubclassing);
5511 
5512                 // Enums may not be extended by source-level classes
5513                 if (st.tsym != null &&
5514                     ((st.tsym.flags_field & Flags.ENUM) != 0) &&
5515                     ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
5516                     log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
5517                 }
5518 
5519                 if (rs.isSerializable(c.type)) {
5520                     env.info.isSerializable = true;
5521                 }
5522 





5523                 attribClassBody(env, c);
5524 
5525                 chk.checkDeprecatedAnnotation(env.tree.pos(), c);
5526                 chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
5527                 chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
5528                 chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
5529 
5530                 if (c.isImplicit()) {
5531                     chk.checkHasMain(env.tree.pos(), c);
5532                 }
5533             } finally {
5534                 env.info.returnResult = prevReturnRes;
5535                 log.useSource(prev);
5536                 chk.setLint(prevLint);
5537             }
5538 
5539         }
5540     }
5541 
5542     public void visitImport(JCImport tree) {

 288      *  @param v      The assigned variable
 289      *  @param base   If the variable is referred to in a Select, the part
 290      *                to the left of the `.', null otherwise.
 291      *  @param env    The current environment.
 292      */
 293     void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
 294         if (v.name == names._this) {
 295             log.error(pos, Errors.CantAssignValToThis);
 296         } else if ((v.flags() & FINAL) != 0 &&
 297             ((v.flags() & HASINIT) != 0
 298              ||
 299              !((base == null ||
 300                TreeInfo.isThisQualifier(base)) &&
 301                isAssignableAsBlankFinal(v, env)))) {
 302             if (v.isResourceVariable()) { //TWR resource
 303                 log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
 304             } else {
 305                 log.error(pos, Errors.CantAssignValToVar(Flags.toSource(v.flags() & (STATIC | FINAL)), v));
 306             }
 307         }
 308 
 309         if (!env.info.ctorPrologue &&
 310                 v.owner.isValueClass() &&
 311                 !env.info.instanceInitializerBlock && // it is OK instance initializer blocks will go after super() anyways
 312                 v.owner.kind == TYP &&
 313                 v.owner == env.enclClass.sym &&
 314                 (v.flags() & STATIC) == 0 &&
 315                 (base == null ||
 316                         TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, base))) {
 317             log.error(pos, Errors.CantRefAfterCtorCalled(v));
 318         }
 319     }
 320 
 321     /** Does tree represent a static reference to an identifier?
 322      *  It is assumed that tree is either a SELECT or an IDENT.
 323      *  We have to weed out selects from non-type names here.
 324      *  @param tree    The candidate tree.
 325      */
 326     boolean isStaticReference(JCTree tree) {
 327         if (tree.hasTag(SELECT)) {
 328             Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
 329             if (lsym == null || lsym.kind != TYP) {
 330                 return false;
 331             }
 332         }
 333         return true;
 334     }
 335 
 336     /** Is this symbol a type?
 337      */
 338     static boolean isType(Symbol sym) {

1183                 }
1184                 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1185                     log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
1186             } else {
1187                 if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1188                     if ((owner.flags() & INTERFACE) != 0) {
1189                         log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1190                     } else {
1191                         log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1192                     }
1193                 } else if ((tree.mods.flags & NATIVE) != 0) {
1194                     log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1195                 }
1196                 // Add an implicit super() call unless an explicit call to
1197                 // super(...) or this(...) is given
1198                 // or we are compiling class java.lang.Object.
1199                 if (isConstructor && owner.type != syms.objectType) {
1200                     if (!TreeInfo.hasAnyConstructorCall(tree)) {
1201                         JCStatement supCall = make.at(tree.body.pos).Exec(make.Apply(List.nil(),
1202                                 make.Ident(names._super), make.Idents(List.nil())));
1203                         if (owner.isValueClass()) {
1204                             tree.body.stats = tree.body.stats.append(supCall);
1205                         } else {
1206                             tree.body.stats = tree.body.stats.prepend(supCall);
1207                         }
1208                     } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
1209                             (tree.mods.flags & GENERATEDCONSTR) == 0 &&
1210                             TreeInfo.hasConstructorCall(tree, names._super)) {
1211                         // enum constructors are not allowed to call super
1212                         // directly, so make sure there aren't any super calls
1213                         // in enum constructors, except in the compiler
1214                         // generated one.
1215                         log.error(tree.body.stats.head.pos(),
1216                                   Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
1217                     }
1218                     if (env.enclClass.sym.isRecord() && (tree.sym.flags_field & RECORD) != 0) { // we are seeing the canonical constructor
1219                         List<Name> recordComponentNames = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.name);
1220                         List<Name> initParamNames = tree.sym.params.map(p -> p.name);
1221                         if (!initParamNames.equals(recordComponentNames)) {
1222                             log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1223                                     Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalWithNameMismatch));
1224                         }
1225                         if (tree.sym.type.asMethodType().thrown != null && !tree.sym.type.asMethodType().thrown.isEmpty()) {
1226                             log.error(tree,
1227                                     Errors.InvalidCanonicalConstructorInRecord(

1329                 }
1330             }
1331             result = tree.type = v.type;
1332             if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1333                 if (isNonArgsMethodInObject(v.name)) {
1334                     log.error(tree, Errors.IllegalRecordComponentName(v));
1335                 }
1336             }
1337         }
1338         finally {
1339             chk.setLint(prevLint);
1340         }
1341     }
1342 
1343     private boolean isNonArgsMethodInObject(Name name) {
1344         for (Symbol s : syms.objectType.tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
1345             if (s.type.getParameterTypes().isEmpty()) {
1346                 return true;
1347             }
1348         }
1349         // isValueObject is not included in Object yet so we need a work around
1350         return name == names.isValueObject;
1351     }
1352 
1353     Fragment canInferLocalVarType(JCVariableDecl tree) {
1354         LocalInitScanner lis = new LocalInitScanner();
1355         lis.scan(tree.init);
1356         return lis.badInferenceMsg;
1357     }
1358 
1359     static class LocalInitScanner extends TreeScanner {
1360         Fragment badInferenceMsg = null;
1361         boolean needsTarget = true;
1362 
1363         @Override
1364         public void visitNewArray(JCNewArray tree) {
1365             if (tree.elemtype == null && needsTarget) {
1366                 badInferenceMsg = Fragments.LocalArrayMissingTarget;
1367             }
1368         }
1369 
1370         @Override

1414             }
1415         }
1416     }
1417 
1418     public void visitSkip(JCSkip tree) {
1419         result = null;
1420     }
1421 
1422     public void visitBlock(JCBlock tree) {
1423         if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1424             // Block is a static or instance initializer;
1425             // let the owner of the environment be a freshly
1426             // created BLOCK-method.
1427             Symbol fakeOwner =
1428                 new MethodSymbol(tree.flags | BLOCK |
1429                     env.info.scope.owner.flags() & STRICTFP, names.empty, null,
1430                     env.info.scope.owner);
1431             final Env<AttrContext> localEnv =
1432                 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1433 
1434             if ((tree.flags & STATIC) != 0) {
1435                 localEnv.info.staticLevel++;
1436             } else {
1437                 localEnv.info.instanceInitializerBlock = true;
1438             }
1439             // Attribute all type annotations in the block
1440             annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
1441             annotate.flush();
1442             attribStats(tree.stats, localEnv);
1443 
1444             {
1445                 // Store init and clinit type annotations with the ClassSymbol
1446                 // to allow output in Gen.normalizeDefs.
1447                 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1448                 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1449                 if ((tree.flags & STATIC) != 0) {
1450                     cs.appendClassInitTypeAttributes(tas);
1451                 } else {
1452                     cs.appendInitTypeAttributes(tas);
1453                 }
1454             }
1455         } else {
1456             // Create a new local environment with a local scope.
1457             Env<AttrContext> localEnv =
1458                 env.dup(tree, env.info.dup(env.info.scope.dup()));

1927     // where
1928     /** Return the selected enumeration constant symbol, or null. */
1929     private Symbol enumConstant(JCTree tree, Type enumType) {
1930         if (tree.hasTag(IDENT)) {
1931             JCIdent ident = (JCIdent)tree;
1932             Name name = ident.name;
1933             for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
1934                 if (sym.kind == VAR) {
1935                     Symbol s = ident.sym = sym;
1936                     ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
1937                     ident.type = s.type;
1938                     return ((s.flags_field & Flags.ENUM) == 0)
1939                         ? null : s;
1940                 }
1941             }
1942         }
1943         return null;
1944     }
1945 
1946     public void visitSynchronized(JCSynchronized tree) {
1947         boolean identityType = chk.checkIdentityType(tree.pos(), attribExpr(tree.lock, env));
1948         if (env.info.lint.isEnabled(LintCategory.SYNCHRONIZATION) &&
1949                 identityType &&
1950                 isValueBased(tree.lock.type)) {
1951             log.warning(LintCategory.SYNCHRONIZATION, tree.pos(), Warnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1952         }
1953         attribStat(tree.body, env);
1954         result = null;
1955     }
1956         // where
1957         private boolean isValueBased(Type t) {
1958             return t != null && t.tsym != null && (t.tsym.flags() & VALUE_BASED) != 0;
1959         }
1960 

1961     public void visitTry(JCTry tree) {
1962         // Create a new local environment with a local
1963         Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
1964         try {
1965             boolean isTryWithResource = tree.resources.nonEmpty();
1966             // Create a nested environment for attributing the try block if needed
1967             Env<AttrContext> tryEnv = isTryWithResource ?
1968                 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
1969                 localEnv;
1970             try {
1971                 // Attribute resource declarations
1972                 for (JCTree resource : tree.resources) {
1973                     CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
1974                         @Override
1975                         public void report(DiagnosticPosition pos, JCDiagnostic details) {
1976                             chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
1977                         }
1978                     };
1979                     ResultInfo twrResult =
1980                         new ResultInfo(KindSelector.VAR,

4387     }
4388 
4389     public void visitSelect(JCFieldAccess tree) {
4390         // Determine the expected kind of the qualifier expression.
4391         KindSelector skind = KindSelector.NIL;
4392         if (tree.name == names._this || tree.name == names._super ||
4393                 tree.name == names._class)
4394         {
4395             skind = KindSelector.TYP;
4396         } else {
4397             if (pkind().contains(KindSelector.PCK))
4398                 skind = KindSelector.of(skind, KindSelector.PCK);
4399             if (pkind().contains(KindSelector.TYP))
4400                 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4401             if (pkind().contains(KindSelector.VAL_MTH))
4402                 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4403         }
4404 
4405         // Attribute the qualifier expression, and determine its symbol (if any).
4406         Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
4407         Assert.check(site == tree.selected.type);
4408         if (!pkind().contains(KindSelector.TYP_PCK))
4409             site = capture(site); // Capture field access
4410 
4411         // don't allow T.class T[].class, etc
4412         if (skind == KindSelector.TYP) {
4413             Type elt = site;
4414             while (elt.hasTag(ARRAY))
4415                 elt = ((ArrayType)elt).elemtype;
4416             if (elt.hasTag(TYPEVAR)) {
4417                 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4418                 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4419                 tree.sym = tree.type.tsym;
4420                 return ;
4421             }
4422         }
4423 
4424         // If qualifier symbol is a type or `super', assert `selectSuper'
4425         // for the selection. This is relevant for determining whether
4426         // protected symbols are accessible.
4427         Symbol sitesym = TreeInfo.symbol(tree.selected);

5487                                                       .filter(s -> s.tsym.isSealed())
5488                                                       .map(s -> (ClassSymbol) s.tsym)
5489                                                       .collect(List.collector());
5490 
5491                 if (sealedSupers.isEmpty()) {
5492                     if ((c.flags_field & Flags.NON_SEALED) != 0) {
5493                         boolean hasErrorSuper = false;
5494 
5495                         hasErrorSuper |= types.directSupertypes(c.type)
5496                                               .stream()
5497                                               .anyMatch(s -> s.tsym.kind == Kind.ERR);
5498 
5499                         ClassType ct = (ClassType) c.type;
5500 
5501                         hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
5502 
5503                         if (!hasErrorSuper) {
5504                             log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
5505                         }
5506                     }
5507                 } else if ((c.flags_field & Flags.COMPOUND) == 0) {
5508                     if (c.isDirectlyOrIndirectlyLocal() && !c.isEnum()) {
5509                         log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
5510                     }
5511 
5512                     if (!c.type.isCompound()) {
5513                         for (ClassSymbol supertypeSym : sealedSupers) {
5514                             if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
5515                                 log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
5516                             }
5517                         }
5518                         if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
5519                             log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
5520                                     c.isInterface() ?
5521                                             Errors.NonSealedOrSealedExpected :
5522                                             Errors.NonSealedSealedOrFinalExpected);
5523                         }
5524                     }
5525                 }
5526 
5527                 deferredLintHandler.flush(env.tree);
5528                 env.info.returnResult = null;
5529                 // java.lang.Enum may not be subclassed by a non-enum
5530                 if (st.tsym == syms.enumSym &&
5531                     ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
5532                     log.error(env.tree.pos(), Errors.EnumNoSubclassing);
5533 
5534                 // Enums may not be extended by source-level classes
5535                 if (st.tsym != null &&
5536                     ((st.tsym.flags_field & Flags.ENUM) != 0) &&
5537                     ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
5538                     log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
5539                 }
5540 
5541                 if (rs.isSerializable(c.type)) {
5542                     env.info.isSerializable = true;
5543                 }
5544 
5545                 if (c.isValueClass()) {
5546                     Assert.check(env.tree.hasTag(CLASSDEF));
5547                     chk.checkConstraintsOfValueClass((JCClassDecl) env.tree, c);
5548                 }
5549 
5550                 attribClassBody(env, c);
5551 
5552                 chk.checkDeprecatedAnnotation(env.tree.pos(), c);
5553                 chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
5554                 chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
5555                 chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
5556 
5557                 if (c.isImplicit()) {
5558                     chk.checkHasMain(env.tree.pos(), c);
5559                 }
5560             } finally {
5561                 env.info.returnResult = prevReturnRes;
5562                 log.useSource(prev);
5563                 chk.setLint(prevLint);
5564             }
5565 
5566         }
5567     }
5568 
5569     public void visitImport(JCImport tree) {
< prev index next >