309 log.error(pos, Errors.CantAssignValToVar(Flags.toSource(v.flags() & (STATIC | FINAL)), v));
310 }
311 return;
312 }
313
314 // Check instance field assignments that appear in constructor prologues
315 if (rs.isEarlyReference(env, base, v)) {
316
317 // Field may not be inherited from a superclass
318 if (v.owner != env.enclClass.sym) {
319 log.error(pos, Errors.CantRefBeforeCtorCalled(v));
320 return;
321 }
322
323 // Field may not have an initializer
324 if ((v.flags() & HASINIT) != 0) {
325 log.error(pos, Errors.CantAssignInitializedBeforeCtorCalled(v));
326 return;
327 }
328 }
329 }
330
331 /** Does tree represent a static reference to an identifier?
332 * It is assumed that tree is either a SELECT or an IDENT.
333 * We have to weed out selects from non-type names here.
334 * @param tree The candidate tree.
335 */
336 boolean isStaticReference(JCTree tree) {
337 if (tree.hasTag(SELECT)) {
338 Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
339 if (lsym == null || lsym.kind != TYP) {
340 return false;
341 }
342 }
343 return true;
344 }
345
346 /** Is this symbol a type?
347 */
348 static boolean isType(Symbol sym) {
1186 }
1187 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1188 log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
1189 } else {
1190 if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1191 if ((owner.flags() & INTERFACE) != 0) {
1192 log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1193 } else {
1194 log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1195 }
1196 } else if ((tree.mods.flags & NATIVE) != 0) {
1197 log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1198 }
1199 // Add an implicit super() call unless an explicit call to
1200 // super(...) or this(...) is given
1201 // or we are compiling class java.lang.Object.
1202 if (isConstructor && owner.type != syms.objectType) {
1203 if (!TreeInfo.hasAnyConstructorCall(tree)) {
1204 JCStatement supCall = make.at(tree.body.pos).Exec(make.Apply(List.nil(),
1205 make.Ident(names._super), make.Idents(List.nil())));
1206 tree.body.stats = tree.body.stats.prepend(supCall);
1207 } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
1208 (tree.mods.flags & GENERATEDCONSTR) == 0 &&
1209 TreeInfo.hasConstructorCall(tree, names._super)) {
1210 // enum constructors are not allowed to call super
1211 // directly, so make sure there aren't any super calls
1212 // in enum constructors, except in the compiler
1213 // generated one.
1214 log.error(tree.body.stats.head.pos(),
1215 Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
1216 }
1217 if (env.enclClass.sym.isRecord() && (tree.sym.flags_field & RECORD) != 0) { // we are seeing the canonical constructor
1218 List<Name> recordComponentNames = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.name);
1219 List<Name> initParamNames = tree.sym.params.map(p -> p.name);
1220 if (!initParamNames.equals(recordComponentNames)) {
1221 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1222 Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalWithNameMismatch));
1223 }
1224 if (tree.sym.type.asMethodType().thrown != null && !tree.sym.type.asMethodType().thrown.isEmpty()) {
1225 log.error(tree,
1226 Errors.InvalidCanonicalConstructorInRecord(
1300 chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1301
1302 try {
1303 v.getConstValue(); // ensure compile-time constant initializer is evaluated
1304 deferredLintHandler.flush(tree.pos(), lint);
1305 chk.checkDeprecatedAnnotation(tree.pos(), v);
1306
1307 if (tree.init != null) {
1308 if ((v.flags_field & FINAL) == 0 ||
1309 !memberEnter.needsLazyConstValue(tree.init)) {
1310 // Not a compile-time constant
1311 // Attribute initializer in a new environment
1312 // with the declared variable as owner.
1313 // Check that initializer conforms to variable's declared type.
1314 Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
1315 initEnv.info.lint = lint;
1316 // In order to catch self-references, we set the variable's
1317 // declaration position to maximal possible value, effectively
1318 // marking the variable as undefined.
1319 initEnv.info.enclVar = v;
1320 attribExpr(tree.init, initEnv, v.type);
1321 if (tree.isImplicitlyTyped()) {
1322 //fixup local variable type
1323 v.type = chk.checkLocalVarType(tree, tree.init.type, tree.name);
1324 }
1325 }
1326 if (tree.isImplicitlyTyped()) {
1327 setSyntheticVariableType(tree, v.type);
1328 }
1329 }
1330 result = tree.type = v.type;
1331 if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1332 if (isNonArgsMethodInObject(v.name)) {
1333 log.error(tree, Errors.IllegalRecordComponentName(v));
1334 }
1335 }
1336 }
1337 finally {
1338 chk.setLint(prevLint);
1339 }
1340 }
1341
1342 private boolean isNonArgsMethodInObject(Name name) {
1343 for (Symbol s : syms.objectType.tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
1412 }
1413 }
1414 }
1415
1416 public void visitSkip(JCSkip tree) {
1417 result = null;
1418 }
1419
1420 public void visitBlock(JCBlock tree) {
1421 if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1422 // Block is a static or instance initializer;
1423 // let the owner of the environment be a freshly
1424 // created BLOCK-method.
1425 Symbol fakeOwner =
1426 new MethodSymbol(tree.flags | BLOCK |
1427 env.info.scope.owner.flags() & STRICTFP, names.empty, initBlockType,
1428 env.info.scope.owner);
1429 final Env<AttrContext> localEnv =
1430 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1431
1432 if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
1433 // Attribute all type annotations in the block
1434 annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
1435 annotate.flush();
1436 attribStats(tree.stats, localEnv);
1437
1438 {
1439 // Store init and clinit type annotations with the ClassSymbol
1440 // to allow output in Gen.normalizeDefs.
1441 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1442 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1443 if ((tree.flags & STATIC) != 0) {
1444 cs.appendClassInitTypeAttributes(tas);
1445 } else {
1446 cs.appendInitTypeAttributes(tas);
1447 }
1448 }
1449 } else {
1450 // Create a new local environment with a local scope.
1451 Env<AttrContext> localEnv =
1452 env.dup(tree, env.info.dup(env.info.scope.dup()));
1921 // where
1922 /** Return the selected enumeration constant symbol, or null. */
1923 private Symbol enumConstant(JCTree tree, Type enumType) {
1924 if (tree.hasTag(IDENT)) {
1925 JCIdent ident = (JCIdent)tree;
1926 Name name = ident.name;
1927 for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
1928 if (sym.kind == VAR) {
1929 Symbol s = ident.sym = sym;
1930 ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
1931 ident.type = s.type;
1932 return ((s.flags_field & Flags.ENUM) == 0)
1933 ? null : s;
1934 }
1935 }
1936 }
1937 return null;
1938 }
1939
1940 public void visitSynchronized(JCSynchronized tree) {
1941 chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
1942 if (isValueBased(tree.lock.type)) {
1943 env.info.lint.logIfEnabled(log, tree.pos(), LintWarnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1944 }
1945 attribStat(tree.body, env);
1946 result = null;
1947 }
1948 // where
1949 private boolean isValueBased(Type t) {
1950 return t != null && t.tsym != null && (t.tsym.flags() & VALUE_BASED) != 0;
1951 }
1952
1953
1954 public void visitTry(JCTry tree) {
1955 // Create a new local environment with a local
1956 Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
1957 try {
1958 boolean isTryWithResource = tree.resources.nonEmpty();
1959 // Create a nested environment for attributing the try block if needed
1960 Env<AttrContext> tryEnv = isTryWithResource ?
1961 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
1962 localEnv;
1963 try {
1964 // Attribute resource declarations
1965 for (JCTree resource : tree.resources) {
1966 CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
1967 @Override
1968 public void report(DiagnosticPosition pos, JCDiagnostic details) {
1969 chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
1970 }
1971 };
1972 ResultInfo twrResult =
1973 new ResultInfo(KindSelector.VAR,
4367 }
4368
4369 public void visitSelect(JCFieldAccess tree) {
4370 // Determine the expected kind of the qualifier expression.
4371 KindSelector skind = KindSelector.NIL;
4372 if (tree.name == names._this || tree.name == names._super ||
4373 tree.name == names._class)
4374 {
4375 skind = KindSelector.TYP;
4376 } else {
4377 if (pkind().contains(KindSelector.PCK))
4378 skind = KindSelector.of(skind, KindSelector.PCK);
4379 if (pkind().contains(KindSelector.TYP))
4380 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4381 if (pkind().contains(KindSelector.VAL_MTH))
4382 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4383 }
4384
4385 // Attribute the qualifier expression, and determine its symbol (if any).
4386 Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
4387 if (!pkind().contains(KindSelector.TYP_PCK))
4388 site = capture(site); // Capture field access
4389
4390 // don't allow T.class T[].class, etc
4391 if (skind == KindSelector.TYP) {
4392 Type elt = site;
4393 while (elt.hasTag(ARRAY))
4394 elt = ((ArrayType)elt).elemtype;
4395 if (elt.hasTag(TYPEVAR)) {
4396 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4397 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4398 tree.sym = tree.type.tsym;
4399 return ;
4400 }
4401 }
4402
4403 // If qualifier symbol is a type or `super', assert `selectSuper'
4404 // for the selection. This is relevant for determining whether
4405 // protected symbols are accessible.
4406 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, env.info.lint);
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) {
5645 sym.kind != VAR ||
5646 sym.getConstValue() == null)
5647 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5648 }
5649 }
5650
5651 // Check for proper placement of super()/this() calls.
5652 chk.checkSuperInitCalls(tree);
5653
5654 // Check for cycles among non-initial constructors.
5655 chk.checkCyclicConstructors(tree);
5656
5657 // Check for cycles among annotation elements.
5658 chk.checkNonCyclicElements(tree);
5659
5660 // Check for proper use of serialVersionUID and other
5661 // serialization-related fields and methods
5662 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5663 && rs.isSerializable(c.type)
5664 && !c.isAnonymous()) {
5665 chk.checkSerialStructure(tree, c);
5666 }
5667 // Correctly organize the positions of the type annotations
5668 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5669
5670 // Check type annotations applicability rules
5671 validateTypeAnnotations(tree, false);
5672 }
5673 // where
5674 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5675 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5676 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5677 if (types.isSameType(al.head.annotationType.type, t))
5678 return al.head.pos();
5679 }
5680
5681 return null;
5682 }
5683
5684 private Type capture(Type type) {
5685 return types.capture(type);
|
309 log.error(pos, Errors.CantAssignValToVar(Flags.toSource(v.flags() & (STATIC | FINAL)), v));
310 }
311 return;
312 }
313
314 // Check instance field assignments that appear in constructor prologues
315 if (rs.isEarlyReference(env, base, v)) {
316
317 // Field may not be inherited from a superclass
318 if (v.owner != env.enclClass.sym) {
319 log.error(pos, Errors.CantRefBeforeCtorCalled(v));
320 return;
321 }
322
323 // Field may not have an initializer
324 if ((v.flags() & HASINIT) != 0) {
325 log.error(pos, Errors.CantAssignInitializedBeforeCtorCalled(v));
326 return;
327 }
328 }
329
330 if (!env.info.ctorPrologue &&
331 v.owner.isValueClass() &&
332 v.owner.kind == TYP &&
333 v.owner == env.enclClass.sym &&
334 (v.flags() & STATIC) == 0 &&
335 (base == null ||
336 TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, base))) {
337 log.error(pos, Errors.CantRefAfterCtorCalled(v));
338 }
339 }
340
341 /** Does tree represent a static reference to an identifier?
342 * It is assumed that tree is either a SELECT or an IDENT.
343 * We have to weed out selects from non-type names here.
344 * @param tree The candidate tree.
345 */
346 boolean isStaticReference(JCTree tree) {
347 if (tree.hasTag(SELECT)) {
348 Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
349 if (lsym == null || lsym.kind != TYP) {
350 return false;
351 }
352 }
353 return true;
354 }
355
356 /** Is this symbol a type?
357 */
358 static boolean isType(Symbol sym) {
1196 }
1197 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1198 log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract);
1199 } else {
1200 if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1201 if ((owner.flags() & INTERFACE) != 0) {
1202 log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1203 } else {
1204 log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1205 }
1206 } else if ((tree.mods.flags & NATIVE) != 0) {
1207 log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1208 }
1209 // Add an implicit super() call unless an explicit call to
1210 // super(...) or this(...) is given
1211 // or we are compiling class java.lang.Object.
1212 if (isConstructor && owner.type != syms.objectType) {
1213 if (!TreeInfo.hasAnyConstructorCall(tree)) {
1214 JCStatement supCall = make.at(tree.body.pos).Exec(make.Apply(List.nil(),
1215 make.Ident(names._super), make.Idents(List.nil())));
1216 if (owner.isValueClass() || owner.hasStrict()) {
1217 tree.body.stats = tree.body.stats.append(supCall);
1218 } else {
1219 tree.body.stats = tree.body.stats.prepend(supCall);
1220 }
1221 } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
1222 (tree.mods.flags & GENERATEDCONSTR) == 0 &&
1223 TreeInfo.hasConstructorCall(tree, names._super)) {
1224 // enum constructors are not allowed to call super
1225 // directly, so make sure there aren't any super calls
1226 // in enum constructors, except in the compiler
1227 // generated one.
1228 log.error(tree.body.stats.head.pos(),
1229 Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
1230 }
1231 if (env.enclClass.sym.isRecord() && (tree.sym.flags_field & RECORD) != 0) { // we are seeing the canonical constructor
1232 List<Name> recordComponentNames = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.name);
1233 List<Name> initParamNames = tree.sym.params.map(p -> p.name);
1234 if (!initParamNames.equals(recordComponentNames)) {
1235 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1236 Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalWithNameMismatch));
1237 }
1238 if (tree.sym.type.asMethodType().thrown != null && !tree.sym.type.asMethodType().thrown.isEmpty()) {
1239 log.error(tree,
1240 Errors.InvalidCanonicalConstructorInRecord(
1314 chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1315
1316 try {
1317 v.getConstValue(); // ensure compile-time constant initializer is evaluated
1318 deferredLintHandler.flush(tree.pos(), lint);
1319 chk.checkDeprecatedAnnotation(tree.pos(), v);
1320
1321 if (tree.init != null) {
1322 if ((v.flags_field & FINAL) == 0 ||
1323 !memberEnter.needsLazyConstValue(tree.init)) {
1324 // Not a compile-time constant
1325 // Attribute initializer in a new environment
1326 // with the declared variable as owner.
1327 // Check that initializer conforms to variable's declared type.
1328 Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
1329 initEnv.info.lint = lint;
1330 // In order to catch self-references, we set the variable's
1331 // declaration position to maximal possible value, effectively
1332 // marking the variable as undefined.
1333 initEnv.info.enclVar = v;
1334 boolean previousCtorPrologue = initEnv.info.ctorPrologue;
1335 try {
1336 if (v.owner.kind == TYP && !v.isStatic() && v.isStrict()) {
1337 // strict instance initializer in a value class
1338 initEnv.info.ctorPrologue = true;
1339 }
1340 attribExpr(tree.init, initEnv, v.type);
1341 if (tree.isImplicitlyTyped()) {
1342 //fixup local variable type
1343 v.type = chk.checkLocalVarType(tree, tree.init.type, tree.name);
1344 }
1345 } finally {
1346 initEnv.info.ctorPrologue = previousCtorPrologue;
1347 }
1348 }
1349 if (tree.isImplicitlyTyped()) {
1350 setSyntheticVariableType(tree, v.type);
1351 }
1352 }
1353 result = tree.type = v.type;
1354 if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1355 if (isNonArgsMethodInObject(v.name)) {
1356 log.error(tree, Errors.IllegalRecordComponentName(v));
1357 }
1358 }
1359 }
1360 finally {
1361 chk.setLint(prevLint);
1362 }
1363 }
1364
1365 private boolean isNonArgsMethodInObject(Name name) {
1366 for (Symbol s : syms.objectType.tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
1435 }
1436 }
1437 }
1438
1439 public void visitSkip(JCSkip tree) {
1440 result = null;
1441 }
1442
1443 public void visitBlock(JCBlock tree) {
1444 if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1445 // Block is a static or instance initializer;
1446 // let the owner of the environment be a freshly
1447 // created BLOCK-method.
1448 Symbol fakeOwner =
1449 new MethodSymbol(tree.flags | BLOCK |
1450 env.info.scope.owner.flags() & STRICTFP, names.empty, initBlockType,
1451 env.info.scope.owner);
1452 final Env<AttrContext> localEnv =
1453 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1454
1455 if ((tree.flags & STATIC) != 0) {
1456 localEnv.info.staticLevel++;
1457 } else {
1458 localEnv.info.instanceInitializerBlock = true;
1459 }
1460 // Attribute all type annotations in the block
1461 annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner, null);
1462 annotate.flush();
1463 attribStats(tree.stats, localEnv);
1464
1465 {
1466 // Store init and clinit type annotations with the ClassSymbol
1467 // to allow output in Gen.normalizeDefs.
1468 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1469 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1470 if ((tree.flags & STATIC) != 0) {
1471 cs.appendClassInitTypeAttributes(tas);
1472 } else {
1473 cs.appendInitTypeAttributes(tas);
1474 }
1475 }
1476 } else {
1477 // Create a new local environment with a local scope.
1478 Env<AttrContext> localEnv =
1479 env.dup(tree, env.info.dup(env.info.scope.dup()));
1948 // where
1949 /** Return the selected enumeration constant symbol, or null. */
1950 private Symbol enumConstant(JCTree tree, Type enumType) {
1951 if (tree.hasTag(IDENT)) {
1952 JCIdent ident = (JCIdent)tree;
1953 Name name = ident.name;
1954 for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
1955 if (sym.kind == VAR) {
1956 Symbol s = ident.sym = sym;
1957 ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
1958 ident.type = s.type;
1959 return ((s.flags_field & Flags.ENUM) == 0)
1960 ? null : s;
1961 }
1962 }
1963 }
1964 return null;
1965 }
1966
1967 public void visitSynchronized(JCSynchronized tree) {
1968 boolean identityType = chk.checkIdentityType(tree.pos(), attribExpr(tree.lock, env));
1969 if (identityType && isValueBased(tree.lock.type)) {
1970 env.info.lint.logIfEnabled(log, tree.pos(), LintWarnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1971 }
1972 attribStat(tree.body, env);
1973 result = null;
1974 }
1975 // where
1976 private boolean isValueBased(Type t) {
1977 return t != null && t.tsym != null && (t.tsym.flags() & VALUE_BASED) != 0;
1978 }
1979
1980 public void visitTry(JCTry tree) {
1981 // Create a new local environment with a local
1982 Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
1983 try {
1984 boolean isTryWithResource = tree.resources.nonEmpty();
1985 // Create a nested environment for attributing the try block if needed
1986 Env<AttrContext> tryEnv = isTryWithResource ?
1987 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
1988 localEnv;
1989 try {
1990 // Attribute resource declarations
1991 for (JCTree resource : tree.resources) {
1992 CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
1993 @Override
1994 public void report(DiagnosticPosition pos, JCDiagnostic details) {
1995 chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details)));
1996 }
1997 };
1998 ResultInfo twrResult =
1999 new ResultInfo(KindSelector.VAR,
4393 }
4394
4395 public void visitSelect(JCFieldAccess tree) {
4396 // Determine the expected kind of the qualifier expression.
4397 KindSelector skind = KindSelector.NIL;
4398 if (tree.name == names._this || tree.name == names._super ||
4399 tree.name == names._class)
4400 {
4401 skind = KindSelector.TYP;
4402 } else {
4403 if (pkind().contains(KindSelector.PCK))
4404 skind = KindSelector.of(skind, KindSelector.PCK);
4405 if (pkind().contains(KindSelector.TYP))
4406 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4407 if (pkind().contains(KindSelector.VAL_MTH))
4408 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4409 }
4410
4411 // Attribute the qualifier expression, and determine its symbol (if any).
4412 Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
4413 Assert.check(site == tree.selected.type);
4414 if (!pkind().contains(KindSelector.TYP_PCK))
4415 site = capture(site); // Capture field access
4416
4417 // don't allow T.class T[].class, etc
4418 if (skind == KindSelector.TYP) {
4419 Type elt = site;
4420 while (elt.hasTag(ARRAY))
4421 elt = ((ArrayType)elt).elemtype;
4422 if (elt.hasTag(TYPEVAR)) {
4423 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4424 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4425 tree.sym = tree.type.tsym;
4426 return ;
4427 }
4428 }
4429
4430 // If qualifier symbol is a type or `super', assert `selectSuper'
4431 // for the selection. This is relevant for determining whether
4432 // protected symbols are accessible.
4433 Symbol sitesym = TreeInfo.symbol(tree.selected);
5492 .filter(s -> s.tsym.isSealed())
5493 .map(s -> (ClassSymbol) s.tsym)
5494 .collect(List.collector());
5495
5496 if (sealedSupers.isEmpty()) {
5497 if ((c.flags_field & Flags.NON_SEALED) != 0) {
5498 boolean hasErrorSuper = false;
5499
5500 hasErrorSuper |= types.directSupertypes(c.type)
5501 .stream()
5502 .anyMatch(s -> s.tsym.kind == Kind.ERR);
5503
5504 ClassType ct = (ClassType) c.type;
5505
5506 hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
5507
5508 if (!hasErrorSuper) {
5509 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
5510 }
5511 }
5512 } else if ((c.flags_field & Flags.COMPOUND) == 0) {
5513 if (c.isDirectlyOrIndirectlyLocal() && !c.isEnum()) {
5514 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
5515 }
5516
5517 if (!c.type.isCompound()) {
5518 for (ClassSymbol supertypeSym : sealedSupers) {
5519 if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
5520 log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
5521 }
5522 }
5523 if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
5524 log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
5525 c.isInterface() ?
5526 Errors.NonSealedOrSealedExpected :
5527 Errors.NonSealedSealedOrFinalExpected);
5528 }
5529 }
5530 }
5531
5532 deferredLintHandler.flush(env.tree, env.info.lint);
5533 env.info.returnResult = null;
5534 // java.lang.Enum may not be subclassed by a non-enum
5535 if (st.tsym == syms.enumSym &&
5536 ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
5537 log.error(env.tree.pos(), Errors.EnumNoSubclassing);
5538
5539 // Enums may not be extended by source-level classes
5540 if (st.tsym != null &&
5541 ((st.tsym.flags_field & Flags.ENUM) != 0) &&
5542 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
5543 log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
5544 }
5545
5546 if (rs.isSerializable(c.type)) {
5547 env.info.isSerializable = true;
5548 }
5549
5550 if (c.isValueClass()) {
5551 Assert.check(env.tree.hasTag(CLASSDEF));
5552 chk.checkConstraintsOfValueClass((JCClassDecl) env.tree, c);
5553 }
5554
5555 attribClassBody(env, c);
5556
5557 chk.checkDeprecatedAnnotation(env.tree.pos(), c);
5558 chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
5559 chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
5560 chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
5561
5562 if (c.isImplicit()) {
5563 chk.checkHasMain(env.tree.pos(), c);
5564 }
5565 } finally {
5566 env.info.returnResult = prevReturnRes;
5567 log.useSource(prev);
5568 chk.setLint(prevLint);
5569 }
5570
5571 }
5572 }
5573
5574 public void visitImport(JCImport tree) {
5677 sym.kind != VAR ||
5678 sym.getConstValue() == null)
5679 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5680 }
5681 }
5682
5683 // Check for proper placement of super()/this() calls.
5684 chk.checkSuperInitCalls(tree);
5685
5686 // Check for cycles among non-initial constructors.
5687 chk.checkCyclicConstructors(tree);
5688
5689 // Check for cycles among annotation elements.
5690 chk.checkNonCyclicElements(tree);
5691
5692 // Check for proper use of serialVersionUID and other
5693 // serialization-related fields and methods
5694 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5695 && rs.isSerializable(c.type)
5696 && !c.isAnonymous()) {
5697 chk.checkSerialStructure(env, tree, c);
5698 }
5699 // Correctly organize the positions of the type annotations
5700 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5701
5702 // Check type annotations applicability rules
5703 validateTypeAnnotations(tree, false);
5704 }
5705 // where
5706 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5707 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5708 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5709 if (types.isSameType(al.head.annotationType.type, t))
5710 return al.head.pos();
5711 }
5712
5713 return null;
5714 }
5715
5716 private Type capture(Type type) {
5717 return types.capture(type);
|