< prev index next >

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

Print this page

1185                 }
1186                 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1187                     log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
1188             } else {
1189                 if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1190                     if ((owner.flags() & INTERFACE) != 0) {
1191                         log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1192                     } else {
1193                         log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1194                     }
1195                 } else if ((tree.mods.flags & NATIVE) != 0) {
1196                     log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1197                 }
1198                 // Add an implicit super() call unless an explicit call to
1199                 // super(...) or this(...) is given
1200                 // or we are compiling class java.lang.Object.
1201                 if (isConstructor && owner.type != syms.objectType) {
1202                     if (!TreeInfo.hasAnyConstructorCall(tree)) {
1203                         JCStatement supCall = make.at(tree.body.pos).Exec(make.Apply(List.nil(),
1204                                 make.Ident(names._super), make.Idents(List.nil())));
1205                         tree.body.stats = tree.body.stats.prepend(supCall);




1206                     } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
1207                             (tree.mods.flags & GENERATEDCONSTR) == 0 &&
1208                             TreeInfo.hasConstructorCall(tree, names._super)) {
1209                         // enum constructors are not allowed to call super
1210                         // directly, so make sure there aren't any super calls
1211                         // in enum constructors, except in the compiler
1212                         // generated one.
1213                         log.error(tree.body.stats.head.pos(),
1214                                   Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
1215                     }
1216                     if (env.enclClass.sym.isRecord() && (tree.sym.flags_field & RECORD) != 0) { // we are seeing the canonical constructor
1217                         List<Name> recordComponentNames = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.name);
1218                         List<Name> initParamNames = tree.sym.params.map(p -> p.name);
1219                         if (!initParamNames.equals(recordComponentNames)) {
1220                             log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1221                                     Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalWithNameMismatch));
1222                         }
1223                         if (tree.sym.type.asMethodType().thrown != null && !tree.sym.type.asMethodType().thrown.isEmpty()) {
1224                             log.error(tree,
1225                                     Errors.InvalidCanonicalConstructorInRecord(

1295         chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1296 
1297         try {
1298             v.getConstValue(); // ensure compile-time constant initializer is evaluated
1299             deferredLintHandler.flush(tree, lint);
1300             chk.checkDeprecatedAnnotation(tree.pos(), v);
1301 
1302             if (tree.init != null) {
1303                 if ((v.flags_field & FINAL) == 0 ||
1304                     !memberEnter.needsLazyConstValue(tree.init)) {
1305                     // Not a compile-time constant
1306                     // Attribute initializer in a new environment
1307                     // with the declared variable as owner.
1308                     // Check that initializer conforms to variable's declared type.
1309                     Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
1310                     initEnv.info.lint = lint;
1311                     // In order to catch self-references, we set the variable's
1312                     // declaration position to maximal possible value, effectively
1313                     // marking the variable as undefined.
1314                     initEnv.info.enclVar = v;
1315                     attribExpr(tree.init, initEnv, v.type);
1316                     if (tree.isImplicitlyTyped()) {
1317                         //fixup local variable type
1318                         v.type = chk.checkLocalVarType(tree, tree.init.type, tree.name);









1319                     }
1320                 }
1321                 if (tree.isImplicitlyTyped()) {
1322                     setSyntheticVariableType(tree, v.type);
1323                 }
1324             }
1325             result = tree.type = v.type;
1326             if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1327                 if (isNonArgsMethodInObject(v.name)) {
1328                     log.error(tree, Errors.IllegalRecordComponentName(v));
1329                 }
1330             }
1331         }
1332         finally {
1333             chk.setLint(prevLint);
1334         }
1335     }
1336 
1337     private void doQueueScanTreeAndTypeAnnotateForVarInit(JCVariableDecl tree, Env<AttrContext> env) {
1338         if (tree.init != null &&

1418             }
1419         }
1420     }
1421 
1422     public void visitSkip(JCSkip tree) {
1423         result = null;
1424     }
1425 
1426     public void visitBlock(JCBlock tree) {
1427         if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1428             // Block is a static or instance initializer;
1429             // let the owner of the environment be a freshly
1430             // created BLOCK-method.
1431             Symbol fakeOwner =
1432                 new MethodSymbol(tree.flags | BLOCK |
1433                     env.info.scope.owner.flags() & STRICTFP, names.empty, initBlockType,
1434                     env.info.scope.owner);
1435             final Env<AttrContext> localEnv =
1436                 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1437 
1438             if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;




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         chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
1948         if (isValueBased(tree.lock.type)) {
1949             env.info.lint.logIfEnabled(tree.pos(), LintWarnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1950         }
1951         attribStat(tree.body, env);
1952         result = null;
1953     }
1954         // where
1955         private boolean isValueBased(Type t) {
1956             return t != null && t.tsym != null && (t.tsym.flags() & VALUE_BASED) != 0;
1957         }
1958 
1959 
1960     public void visitTry(JCTry tree) {
1961         // Create a new local environment with a local
1962         Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
1963         try {
1964             boolean isTryWithResource = tree.resources.nonEmpty();
1965             // Create a nested environment for attributing the try block if needed
1966             Env<AttrContext> tryEnv = isTryWithResource ?
1967                 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
1968                 localEnv;
1969             try {
1970                 // Attribute resource declarations
1971                 for (JCTree resource : tree.resources) {
1972                     CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
1973                         @Override
1974                         public void report(DiagnosticPosition pos, JCDiagnostic details) {
1975                             chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
1976                         }
1977                     };
1978                     ResultInfo twrResult =
1979                         new ResultInfo(KindSelector.VAR,

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

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

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





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

5651                         sym.kind != VAR ||
5652                         sym.getConstValue() == null)
5653                     log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5654             }
5655         }
5656 
5657         // Check for proper placement of super()/this() calls.
5658         chk.checkSuperInitCalls(tree);
5659 
5660         // Check for cycles among non-initial constructors.
5661         chk.checkCyclicConstructors(tree);
5662 
5663         // Check for cycles among annotation elements.
5664         chk.checkNonCyclicElements(tree);
5665 
5666         // Check for proper use of serialVersionUID and other
5667         // serialization-related fields and methods
5668         if (env.info.lint.isEnabled(LintCategory.SERIAL)
5669                 && rs.isSerializable(c.type)
5670                 && !c.isAnonymous()) {
5671             chk.checkSerialStructure(tree, c);
5672         }
5673         // Correctly organize the positions of the type annotations
5674         typeAnnotations.organizeTypeAnnotationsBodies(tree);
5675 
5676         // Check type annotations applicability rules
5677         validateTypeAnnotations(tree, false);
5678     }
5679         // where
5680         /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5681         private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5682             for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5683                 if (types.isSameType(al.head.annotationType.type, t))
5684                     return al.head.pos();
5685             }
5686 
5687             return null;
5688         }
5689 
5690     private Type capture(Type type) {
5691         return types.capture(type);

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

1299         chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1300 
1301         try {
1302             v.getConstValue(); // ensure compile-time constant initializer is evaluated
1303             deferredLintHandler.flush(tree, lint);
1304             chk.checkDeprecatedAnnotation(tree.pos(), v);
1305 
1306             if (tree.init != null) {
1307                 if ((v.flags_field & FINAL) == 0 ||
1308                     !memberEnter.needsLazyConstValue(tree.init)) {
1309                     // Not a compile-time constant
1310                     // Attribute initializer in a new environment
1311                     // with the declared variable as owner.
1312                     // Check that initializer conforms to variable's declared type.
1313                     Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
1314                     initEnv.info.lint = lint;
1315                     // In order to catch self-references, we set the variable's
1316                     // declaration position to maximal possible value, effectively
1317                     // marking the variable as undefined.
1318                     initEnv.info.enclVar = v;
1319                     boolean previousCtorPrologue = initEnv.info.ctorPrologue;
1320                     try {
1321                         if (v.owner.kind == TYP && !v.isStatic() && v.isStrict()) {
1322                             // strict instance initializer in a value class
1323                             initEnv.info.ctorPrologue = true;
1324                         }
1325                         attribExpr(tree.init, initEnv, v.type);
1326                         if (tree.isImplicitlyTyped()) {
1327                             //fixup local variable type
1328                             v.type = chk.checkLocalVarType(tree, tree.init.type, tree.name);
1329                         }
1330                     } finally {
1331                         initEnv.info.ctorPrologue = previousCtorPrologue;
1332                     }
1333                 }
1334                 if (tree.isImplicitlyTyped()) {
1335                     setSyntheticVariableType(tree, v.type);
1336                 }
1337             }
1338             result = tree.type = v.type;
1339             if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1340                 if (isNonArgsMethodInObject(v.name)) {
1341                     log.error(tree, Errors.IllegalRecordComponentName(v));
1342                 }
1343             }
1344         }
1345         finally {
1346             chk.setLint(prevLint);
1347         }
1348     }
1349 
1350     private void doQueueScanTreeAndTypeAnnotateForVarInit(JCVariableDecl tree, Env<AttrContext> env) {
1351         if (tree.init != null &&

1431             }
1432         }
1433     }
1434 
1435     public void visitSkip(JCSkip tree) {
1436         result = null;
1437     }
1438 
1439     public void visitBlock(JCBlock tree) {
1440         if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1441             // Block is a static or instance initializer;
1442             // let the owner of the environment be a freshly
1443             // created BLOCK-method.
1444             Symbol fakeOwner =
1445                 new MethodSymbol(tree.flags | BLOCK |
1446                     env.info.scope.owner.flags() & STRICTFP, names.empty, initBlockType,
1447                     env.info.scope.owner);
1448             final Env<AttrContext> localEnv =
1449                 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1450 
1451             if ((tree.flags & STATIC) != 0) {
1452                 localEnv.info.staticLevel++;
1453             } else {
1454                 localEnv.info.instanceInitializerBlock = true;
1455             }
1456             // Attribute all type annotations in the block
1457             annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
1458             annotate.flush();
1459             attribStats(tree.stats, localEnv);
1460 
1461             {
1462                 // Store init and clinit type annotations with the ClassSymbol
1463                 // to allow output in Gen.normalizeDefs.
1464                 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1465                 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1466                 if ((tree.flags & STATIC) != 0) {
1467                     cs.appendClassInitTypeAttributes(tas);
1468                 } else {
1469                     cs.appendInitTypeAttributes(tas);
1470                 }
1471             }
1472         } else {
1473             // Create a new local environment with a local scope.
1474             Env<AttrContext> localEnv =
1475                 env.dup(tree, env.info.dup(env.info.scope.dup()));

1944     // where
1945     /** Return the selected enumeration constant symbol, or null. */
1946     private Symbol enumConstant(JCTree tree, Type enumType) {
1947         if (tree.hasTag(IDENT)) {
1948             JCIdent ident = (JCIdent)tree;
1949             Name name = ident.name;
1950             for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
1951                 if (sym.kind == VAR) {
1952                     Symbol s = ident.sym = sym;
1953                     ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
1954                     ident.type = s.type;
1955                     return ((s.flags_field & Flags.ENUM) == 0)
1956                         ? null : s;
1957                 }
1958             }
1959         }
1960         return null;
1961     }
1962 
1963     public void visitSynchronized(JCSynchronized tree) {
1964         boolean identityType = chk.checkIdentityType(tree.pos(), attribExpr(tree.lock, env));
1965         if (identityType && isValueBased(tree.lock.type)) {
1966             env.info.lint.logIfEnabled(tree.pos(), LintWarnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1967         }
1968         attribStat(tree.body, env);
1969         result = null;
1970     }
1971         // where
1972         private boolean isValueBased(Type t) {
1973             return t != null && t.tsym != null && (t.tsym.flags() & VALUE_BASED) != 0;
1974         }
1975 

1976     public void visitTry(JCTry tree) {
1977         // Create a new local environment with a local
1978         Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
1979         try {
1980             boolean isTryWithResource = tree.resources.nonEmpty();
1981             // Create a nested environment for attributing the try block if needed
1982             Env<AttrContext> tryEnv = isTryWithResource ?
1983                 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
1984                 localEnv;
1985             try {
1986                 // Attribute resource declarations
1987                 for (JCTree resource : tree.resources) {
1988                     CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
1989                         @Override
1990                         public void report(DiagnosticPosition pos, JCDiagnostic details) {
1991                             chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
1992                         }
1993                     };
1994                     ResultInfo twrResult =
1995                         new ResultInfo(KindSelector.VAR,

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

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

5673                         sym.kind != VAR ||
5674                         sym.getConstValue() == null)
5675                     log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5676             }
5677         }
5678 
5679         // Check for proper placement of super()/this() calls.
5680         chk.checkSuperInitCalls(tree);
5681 
5682         // Check for cycles among non-initial constructors.
5683         chk.checkCyclicConstructors(tree);
5684 
5685         // Check for cycles among annotation elements.
5686         chk.checkNonCyclicElements(tree);
5687 
5688         // Check for proper use of serialVersionUID and other
5689         // serialization-related fields and methods
5690         if (env.info.lint.isEnabled(LintCategory.SERIAL)
5691                 && rs.isSerializable(c.type)
5692                 && !c.isAnonymous()) {
5693             chk.checkSerialStructure(env, tree, c);
5694         }
5695         // Correctly organize the positions of the type annotations
5696         typeAnnotations.organizeTypeAnnotationsBodies(tree);
5697 
5698         // Check type annotations applicability rules
5699         validateTypeAnnotations(tree, false);
5700     }
5701         // where
5702         /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5703         private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5704             for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5705                 if (types.isSameType(al.head.annotationType.type, t))
5706                     return al.head.pos();
5707             }
5708 
5709             return null;
5710         }
5711 
5712     private Type capture(Type type) {
5713         return types.capture(type);
< prev index next >