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,
4372 }
4373
4374 public void visitSelect(JCFieldAccess tree) {
4375 // Determine the expected kind of the qualifier expression.
4376 KindSelector skind = KindSelector.NIL;
4377 if (tree.name == names._this || tree.name == names._super ||
4378 tree.name == names._class)
4379 {
4380 skind = KindSelector.TYP;
4381 } else {
4382 if (pkind().contains(KindSelector.PCK))
4383 skind = KindSelector.of(skind, KindSelector.PCK);
4384 if (pkind().contains(KindSelector.TYP))
4385 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4386 if (pkind().contains(KindSelector.VAL_MTH))
4387 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4388 }
4389
4390 // Attribute the qualifier expression, and determine its symbol (if any).
4391 Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
4392 if (!pkind().contains(KindSelector.TYP_PCK))
4393 site = capture(site); // Capture field access
4394
4395 // don't allow T.class T[].class, etc
4396 if (skind == KindSelector.TYP) {
4397 Type elt = site;
4398 while (elt.hasTag(ARRAY))
4399 elt = ((ArrayType)elt).elemtype;
4400 if (elt.hasTag(TYPEVAR)) {
4401 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4402 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4403 tree.sym = tree.type.tsym;
4404 return ;
4405 }
4406 }
4407
4408 // If qualifier symbol is a type or `super', assert `selectSuper'
4409 // for the selection. This is relevant for determining whether
4410 // protected symbols are accessible.
4411 Symbol sitesym = TreeInfo.symbol(tree.selected);
5470 .filter(s -> s.tsym.isSealed())
5471 .map(s -> (ClassSymbol) s.tsym)
5472 .collect(List.collector());
5473
5474 if (sealedSupers.isEmpty()) {
5475 if ((c.flags_field & Flags.NON_SEALED) != 0) {
5476 boolean hasErrorSuper = false;
5477
5478 hasErrorSuper |= types.directSupertypes(c.type)
5479 .stream()
5480 .anyMatch(s -> s.tsym.kind == Kind.ERR);
5481
5482 ClassType ct = (ClassType) c.type;
5483
5484 hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
5485
5486 if (!hasErrorSuper) {
5487 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
5488 }
5489 }
5490 } else {
5491 if (c.isDirectlyOrIndirectlyLocal() && !c.isEnum()) {
5492 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
5493 }
5494
5495 if (!c.type.isCompound()) {
5496 for (ClassSymbol supertypeSym : sealedSupers) {
5497 if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
5498 log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
5499 }
5500 }
5501 if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
5502 log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
5503 c.isInterface() ?
5504 Errors.NonSealedOrSealedExpected :
5505 Errors.NonSealedSealedOrFinalExpected);
5506 }
5507 }
5508 }
5509
5510 deferredLintHandler.flush(env.tree, env.info.lint);
5511 env.info.returnResult = null;
5512 // java.lang.Enum may not be subclassed by a non-enum
5513 if (st.tsym == syms.enumSym &&
5514 ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
5515 log.error(env.tree.pos(), Errors.EnumNoSubclassing);
5516
5517 // Enums may not be extended by source-level classes
5518 if (st.tsym != null &&
5519 ((st.tsym.flags_field & Flags.ENUM) != 0) &&
5520 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
5521 log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
5522 }
5523
5524 if (rs.isSerializable(c.type)) {
5525 env.info.isSerializable = true;
5526 }
5527
5528 attribClassBody(env, c);
5529
5530 chk.checkDeprecatedAnnotation(env.tree.pos(), c);
5531 chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
5532 chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
5533 chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
5534
5535 if (c.isImplicit()) {
5536 chk.checkHasMain(env.tree.pos(), c);
5537 }
5538 } finally {
5539 env.info.returnResult = prevReturnRes;
5540 log.useSource(prev);
5541 chk.setLint(prevLint);
5542 }
5543
5544 }
5545 }
5546
5547 public void visitImport(JCImport tree) {
5650 sym.kind != VAR ||
5651 sym.getConstValue() == null)
5652 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5653 }
5654 }
5655
5656 // Check for proper placement of super()/this() calls.
5657 chk.checkSuperInitCalls(tree);
5658
5659 // Check for cycles among non-initial constructors.
5660 chk.checkCyclicConstructors(tree);
5661
5662 // Check for cycles among annotation elements.
5663 chk.checkNonCyclicElements(tree);
5664
5665 // Check for proper use of serialVersionUID and other
5666 // serialization-related fields and methods
5667 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5668 && rs.isSerializable(c.type)
5669 && !c.isAnonymous()) {
5670 chk.checkSerialStructure(tree, c);
5671 }
5672 // Correctly organize the positions of the type annotations
5673 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5674
5675 // Check type annotations applicability rules
5676 validateTypeAnnotations(tree, false);
5677 }
5678 // where
5679 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5680 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5681 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5682 if (types.isSameType(al.head.annotationType.type, t))
5683 return al.head.pos();
5684 }
5685
5686 return null;
5687 }
5688
5689 private Type capture(Type type) {
5690 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,
4388 }
4389
4390 public void visitSelect(JCFieldAccess tree) {
4391 // Determine the expected kind of the qualifier expression.
4392 KindSelector skind = KindSelector.NIL;
4393 if (tree.name == names._this || tree.name == names._super ||
4394 tree.name == names._class)
4395 {
4396 skind = KindSelector.TYP;
4397 } else {
4398 if (pkind().contains(KindSelector.PCK))
4399 skind = KindSelector.of(skind, KindSelector.PCK);
4400 if (pkind().contains(KindSelector.TYP))
4401 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4402 if (pkind().contains(KindSelector.VAL_MTH))
4403 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4404 }
4405
4406 // Attribute the qualifier expression, and determine its symbol (if any).
4407 Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
4408 Assert.check(site == tree.selected.type);
4409 if (!pkind().contains(KindSelector.TYP_PCK))
4410 site = capture(site); // Capture field access
4411
4412 // don't allow T.class T[].class, etc
4413 if (skind == KindSelector.TYP) {
4414 Type elt = site;
4415 while (elt.hasTag(ARRAY))
4416 elt = ((ArrayType)elt).elemtype;
4417 if (elt.hasTag(TYPEVAR)) {
4418 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4419 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4420 tree.sym = tree.type.tsym;
4421 return ;
4422 }
4423 }
4424
4425 // If qualifier symbol is a type or `super', assert `selectSuper'
4426 // for the selection. This is relevant for determining whether
4427 // protected symbols are accessible.
4428 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, env.info.lint);
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) {
5672 sym.kind != VAR ||
5673 sym.getConstValue() == null)
5674 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5675 }
5676 }
5677
5678 // Check for proper placement of super()/this() calls.
5679 chk.checkSuperInitCalls(tree);
5680
5681 // Check for cycles among non-initial constructors.
5682 chk.checkCyclicConstructors(tree);
5683
5684 // Check for cycles among annotation elements.
5685 chk.checkNonCyclicElements(tree);
5686
5687 // Check for proper use of serialVersionUID and other
5688 // serialization-related fields and methods
5689 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5690 && rs.isSerializable(c.type)
5691 && !c.isAnonymous()) {
5692 chk.checkSerialStructure(env, tree, c);
5693 }
5694 // Correctly organize the positions of the type annotations
5695 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5696
5697 // Check type annotations applicability rules
5698 validateTypeAnnotations(tree, false);
5699 }
5700 // where
5701 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5702 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5703 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5704 if (types.isSameType(al.head.annotationType.type, t))
5705 return al.head.pos();
5706 }
5707
5708 return null;
5709 }
5710
5711 private Type capture(Type type) {
5712 return types.capture(type);
|