107 final Analyzer analyzer;
108 final DeferredAttr deferredAttr;
109 final Check chk;
110 final Flow flow;
111 final MemberEnter memberEnter;
112 final TypeEnter typeEnter;
113 final TreeMaker make;
114 final ConstFold cfolder;
115 final Enter enter;
116 final Target target;
117 final Types types;
118 final Preview preview;
119 final JCDiagnostic.Factory diags;
120 final TypeAnnotations typeAnnotations;
121 final TypeEnvs typeEnvs;
122 final Dependencies dependencies;
123 final Annotate annotate;
124 final ArgumentAttr argumentAttr;
125 final MatchBindingsComputer matchBindingsComputer;
126 final AttrRecover attrRecover;
127 final boolean captureMRefReturnType;
128
129 public static Attr instance(Context context) {
130 Attr instance = context.get(attrKey);
131 if (instance == null)
132 instance = new Attr(context);
133 return instance;
134 }
135
136 @SuppressWarnings("this-escape")
137 protected Attr(Context context) {
138 context.put(attrKey, this);
139
140 names = Names.instance(context);
141 log = Log.instance(context);
142 lintMapper = LintMapper.instance(context);
143 syms = Symtab.instance(context);
144 rs = Resolve.instance(context);
145 operators = Operators.instance(context);
146 chk = Check.instance(context);
147 flow = Flow.instance(context);
148 memberEnter = MemberEnter.instance(context);
149 typeEnter = TypeEnter.instance(context);
150 make = TreeMaker.instance(context);
151 enter = Enter.instance(context);
152 infer = Infer.instance(context);
153 analyzer = Analyzer.instance(context);
154 deferredAttr = DeferredAttr.instance(context);
155 cfolder = ConstFold.instance(context);
156 target = Target.instance(context);
157 types = Types.instance(context);
158 preview = Preview.instance(context);
159 diags = JCDiagnostic.Factory.instance(context);
160 annotate = Annotate.instance(context);
161 typeAnnotations = TypeAnnotations.instance(context);
162 typeEnvs = TypeEnvs.instance(context);
163 dependencies = Dependencies.instance(context);
164 argumentAttr = ArgumentAttr.instance(context);
165 matchBindingsComputer = MatchBindingsComputer.instance(context);
166 attrRecover = AttrRecover.instance(context);
167
168 Options options = Options.instance(context);
169
170 Source source = Source.instance(context);
171 allowReifiableTypesInInstanceof = Feature.REIFIABLE_TYPES_INSTANCEOF.allowedInSource(source);
172 allowRecords = Feature.RECORDS.allowedInSource(source);
173 allowPatternSwitch = (preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH)) &&
174 Feature.PATTERN_SWITCH.allowedInSource(source);
175 allowUnconditionalPatternsInstanceOf =
176 Feature.UNCONDITIONAL_PATTERN_IN_INSTANCEOF.allowedInSource(source);
177 sourceName = source.name;
178 useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
179 captureMRefReturnType = Source.Feature.CAPTURE_MREF_RETURN_TYPE.allowedInSource(source);
180
181 statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
182 varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
183 unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
184 methodAttrInfo = new MethodAttrInfo();
185 unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
186 unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
187 recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
188 initBlockType = new MethodType(List.nil(), syms.voidType, List.nil(), syms.methodClass);
189 }
190
191 /** Switch: reifiable types in instanceof enabled?
192 */
193 boolean allowReifiableTypesInInstanceof;
194
195 /** Are records allowed
196 */
197 private final boolean allowRecords;
198
199 /** Are patterns in switch allowed
200 */
201 private final boolean allowPatternSwitch;
202
203 /** Are unconditional patterns in instanceof allowed
204 */
205 private final boolean allowUnconditionalPatternsInstanceOf;
206
207 /**
208 * Switch: warn about use of variable before declaration?
209 * RFE: 6425594
210 */
211 boolean useBeforeDeclarationWarning;
212
213 /**
214 * Switch: name of source level; used for error reporting.
215 */
216 String sourceName;
217
218 /** Check kind and type of given tree against protokind and prototype.
219 * If check succeeds, store type in tree and return it.
220 * If check fails, store errType in tree and return it.
221 * No checks are performed if the prototype is a method type.
222 * It is not necessary in this case since we know that kind and type
223 * are correct.
224 *
225 * @param tree The tree whose kind and type is checked
226 * @param found The computed type of the tree
280 owner.kind == VAR || // i.e. we are in a variable initializer
281 (owner.flags() & BLOCK) != 0) // i.e. we are in an initializer block
282 &&
283 v.owner == owner.owner
284 &&
285 ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
286 boolean insideCompactConstructor = env.enclMethod != null && TreeInfo.isCompactConstructor(env.enclMethod);
287 return isAssignable & !insideCompactConstructor;
288 }
289
290 /** Check that variable can be assigned to.
291 * @param pos The current source code position.
292 * @param v The assigned variable
293 * @param base If the variable is referred to in a Select, the part
294 * to the left of the `.', null otherwise.
295 * @param env The current environment.
296 */
297 void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
298 if (v.name == names._this) {
299 log.error(pos, Errors.CantAssignValToThis);
300 return;
301 }
302 if ((v.flags() & FINAL) != 0 &&
303 ((v.flags() & HASINIT) != 0
304 ||
305 !((base == null ||
306 TreeInfo.isThisQualifier(base)) &&
307 isAssignableAsBlankFinal(v, env)))) {
308 if (v.isResourceVariable()) { //TWR resource
309 log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
310 } else {
311 log.error(pos, Errors.CantAssignValToVar(Flags.toSource(v.flags() & (STATIC | FINAL)), v));
312 }
313 return;
314 }
315
316 // Check instance field assignments that appear in constructor prologues
317 if (rs.isEarlyReference(env, base, v)) {
318
319 // Field may not be inherited from a superclass
320 if (v.owner != env.enclClass.sym) {
321 log.error(pos, Errors.CantRefBeforeCtorCalled(v));
322 return;
323 }
324
325 // Field may not have an initializer
326 if ((v.flags() & HASINIT) != 0) {
327 log.error(pos, Errors.CantAssignInitializedBeforeCtorCalled(v));
328 return;
329 }
330 }
331 }
332
333 /** Does tree represent a static reference to an identifier?
334 * It is assumed that tree is either a SELECT or an IDENT.
335 * We have to weed out selects from non-type names here.
336 * @param tree The candidate tree.
337 */
338 boolean isStaticReference(JCTree tree) {
339 if (tree.hasTag(SELECT)) {
340 Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
341 if (lsym == null || lsym.kind != TYP) {
342 return false;
343 }
344 }
345 return true;
346 }
347
348 /** Is this symbol a type?
349 */
1104 * - have an accessibility stricter than that of the record type
1105 * - explicitly invoke any other constructor
1106 */
1107 if ((tree.sym.flags_field & GENERATEDCONSTR) == 0) {
1108 if (Check.protection(m.flags()) > Check.protection(env.enclClass.sym.flags())) {
1109 log.error(tree,
1110 (env.enclClass.sym.flags() & AccessFlags) == 0 ?
1111 Errors.InvalidCanonicalConstructorInRecord(
1112 Fragments.Canonical,
1113 env.enclClass.sym.name,
1114 Fragments.CanonicalMustNotHaveStrongerAccess("package")
1115 ) :
1116 Errors.InvalidCanonicalConstructorInRecord(
1117 Fragments.Canonical,
1118 env.enclClass.sym.name,
1119 Fragments.CanonicalMustNotHaveStrongerAccess(asFlagSet(env.enclClass.sym.flags() & AccessFlags))
1120 )
1121 );
1122 }
1123
1124 if (TreeInfo.hasAnyConstructorCall(tree)) {
1125 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1126 Fragments.Canonical, env.enclClass.sym.name,
1127 Fragments.CanonicalMustNotContainExplicitConstructorInvocation));
1128 }
1129 }
1130
1131 // also we want to check that no type variables have been defined
1132 if (!tree.typarams.isEmpty()) {
1133 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1134 Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalMustNotDeclareTypeVariables));
1135 }
1136
1137 /* and now we need to check that the constructor's arguments are exactly the same as those of the
1138 * record components
1139 */
1140 List<? extends RecordComponent> recordComponents = env.enclClass.sym.getRecordComponents();
1141 List<Type> recordFieldTypes = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.type);
1142 for (JCVariableDecl param: tree.params) {
1143 boolean paramIsVarArgs = (param.sym.flags_field & VARARGS) != 0;
1144 if (!types.isSameType(param.type, recordFieldTypes.head) ||
1182 if (tree.defaultValue != null) {
1183 if ((owner.flags() & ANNOTATION) == 0)
1184 log.error(tree.pos(),
1185 Errors.DefaultAllowedInIntfAnnotationMember);
1186 }
1187 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1188 log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract(tree.sym, owner));
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(
1227 TreeInfo.isCompactConstructor(tree) ? Fragments.Compact : Fragments.Canonical,
1228 env.enclClass.sym.name,
1229 Fragments.ThrowsClauseNotAllowedForCanonicalConstructor(
1230 TreeInfo.isCompactConstructor(tree) ? Fragments.Compact : Fragments.Canonical)));
1231 }
1232 }
1233 }
1234
1235 // Attribute all type annotations in the body
1236 annotate.queueScanTreeAndTypeAnnotate(tree.body, localEnv, m);
1237 annotate.flush();
1238
1239 // Start of constructor prologue (if not in java.lang.Object constructor)
1240 localEnv.info.ctorPrologue = isConstructor && owner.type != syms.objectType;
1241
1242 // Attribute method body.
1243 attribStat(tree.body, localEnv);
1244 }
1245
1246 localEnv.info.scope.leave();
1247 result = tree.type = m.type;
1248 } finally {
1249 chk.setLint(prevLint);
1250 chk.setMethod(prevMethod);
1251 env.info.ctorPrologue = ctorProloguePrev;
1252 }
1253 }
1254
1255 public void visitVarDef(JCVariableDecl tree) {
1256 // Local variables have not been entered yet, so we need to do it now:
1257 if (env.info.scope.owner.kind == MTH || env.info.scope.owner.kind == VAR) {
1258 if (tree.sym != null) {
1259 // parameters have already been entered
1260 env.info.scope.enter(tree.sym);
1261 } else {
1262 if (tree.isImplicitlyTyped() && (tree.getModifiers().flags & PARAMETER) == 0) {
1263 if (tree.init == null) {
1264 //cannot use 'var' without initializer
1265 log.error(tree, Errors.CantInferLocalVarType(tree.name, Fragments.LocalMissingInit));
1266 tree.vartype = make.at(tree.pos()).Erroneous();
1267 } else {
1268 Fragment msg = canInferLocalVarType(tree);
1269 if (msg != null) {
1270 //cannot use 'var' with initializer which require an explicit target
1271 //(e.g. lambda, method reference, array initializer).
1272 log.error(tree, Errors.CantInferLocalVarType(tree.name, msg));
1273 tree.vartype = make.at(tree.pos()).Erroneous();
1274 }
1283 }
1284 } else {
1285 doQueueScanTreeAndTypeAnnotateForVarInit(tree, env);
1286 }
1287
1288 VarSymbol v = tree.sym;
1289 Lint lint = env.info.lint.augment(v);
1290 Lint prevLint = chk.setLint(lint);
1291
1292 // Check that the variable's declared type is well-formed.
1293 boolean isImplicitLambdaParameter = env.tree.hasTag(LAMBDA) &&
1294 ((JCLambda)env.tree).paramKind == JCLambda.ParameterKind.IMPLICIT &&
1295 (tree.sym.flags() & PARAMETER) != 0;
1296 chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1297
1298 try {
1299 v.getConstValue(); // ensure compile-time constant initializer is evaluated
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 chk.checkRequiresIdentity(tree, env.info.lint);
1332 }
1333 finally {
1334 chk.setLint(prevLint);
1335 }
1336 }
1337
1338 private void doQueueScanTreeAndTypeAnnotateForVarInit(JCVariableDecl tree, Env<AttrContext> env) {
1339 if (tree.init != null &&
1340 (tree.mods.flags & Flags.FIELD_INIT_TYPE_ANNOTATIONS_QUEUED) == 0 &&
1419 }
1420 }
1421 }
1422
1423 public void visitSkip(JCSkip tree) {
1424 result = null;
1425 }
1426
1427 public void visitBlock(JCBlock tree) {
1428 if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1429 // Block is a static or instance initializer;
1430 // let the owner of the environment be a freshly
1431 // created BLOCK-method.
1432 Symbol fakeOwner =
1433 new MethodSymbol(tree.flags | BLOCK |
1434 env.info.scope.owner.flags() & STRICTFP, names.empty, initBlockType,
1435 env.info.scope.owner);
1436 final Env<AttrContext> localEnv =
1437 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1438
1439 if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
1440 // Attribute all type annotations in the block
1441 annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner);
1442 annotate.flush();
1443 attribStats(tree.stats, localEnv);
1444
1445 {
1446 // Store init and clinit type annotations with the ClassSymbol
1447 // to allow output in Gen.normalizeDefs.
1448 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1449 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1450 if ((tree.flags & STATIC) != 0) {
1451 cs.appendClassInitTypeAttributes(tas);
1452 } else {
1453 cs.appendInitTypeAttributes(tas);
1454 }
1455 }
1456 } else {
1457 // Create a new local environment with a local scope.
1458 Env<AttrContext> localEnv =
1459 env.dup(tree, env.info.dup(env.info.scope.dup()));
1932 // where
1933 /** Return the selected enumeration constant symbol, or null. */
1934 private Symbol enumConstant(JCTree tree, Type enumType) {
1935 if (tree.hasTag(IDENT)) {
1936 JCIdent ident = (JCIdent)tree;
1937 Name name = ident.name;
1938 for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
1939 if (sym.kind == VAR) {
1940 Symbol s = ident.sym = sym;
1941 ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
1942 ident.type = s.type;
1943 return ((s.flags_field & Flags.ENUM) == 0)
1944 ? null : s;
1945 }
1946 }
1947 }
1948 return null;
1949 }
1950
1951 public void visitSynchronized(JCSynchronized tree) {
1952 chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
1953 if (tree.lock.type != null && tree.lock.type.isValueBased()) {
1954 log.warning(tree.pos(), LintWarnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
1955 }
1956 attribStat(tree.body, env);
1957 result = null;
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
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 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);
5500 .filter(s -> s.tsym.isSealed())
5501 .map(s -> (ClassSymbol) s.tsym)
5502 .collect(List.collector());
5503
5504 if (sealedSupers.isEmpty()) {
5505 if ((c.flags_field & Flags.NON_SEALED) != 0) {
5506 boolean hasErrorSuper = false;
5507
5508 hasErrorSuper |= types.directSupertypes(c.type)
5509 .stream()
5510 .anyMatch(s -> s.tsym.kind == Kind.ERR);
5511
5512 ClassType ct = (ClassType) c.type;
5513
5514 hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
5515
5516 if (!hasErrorSuper) {
5517 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
5518 }
5519 }
5520 } else {
5521 if (c.isDirectlyOrIndirectlyLocal() && !c.isEnum()) {
5522 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
5523 }
5524
5525 if (!c.type.isCompound()) {
5526 for (ClassSymbol supertypeSym : sealedSupers) {
5527 if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
5528 log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
5529 }
5530 }
5531 if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
5532 log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
5533 c.isInterface() ?
5534 Errors.NonSealedOrSealedExpected :
5535 Errors.NonSealedSealedOrFinalExpected);
5536 }
5537 }
5538 }
5539
5540 env.info.returnResult = null;
5541 // java.lang.Enum may not be subclassed by a non-enum
5542 if (st.tsym == syms.enumSym &&
5543 ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
5544 log.error(env.tree.pos(), Errors.EnumNoSubclassing);
5545
5546 // Enums may not be extended by source-level classes
5547 if (st.tsym != null &&
5548 ((st.tsym.flags_field & Flags.ENUM) != 0) &&
5549 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
5550 log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
5551 }
5552
5553 if (rs.isSerializable(c.type)) {
5554 env.info.isSerializable = true;
5555 }
5556
5557 attribClassBody(env, c);
5558
5559 chk.checkDeprecatedAnnotation(env.tree.pos(), c);
5560 chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
5561 chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
5562 chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
5563
5564 if (c.isImplicit()) {
5565 chk.checkHasMain(env.tree.pos(), c);
5566 }
5567 } finally {
5568 env.info.returnResult = prevReturnRes;
5569 log.useSource(prev);
5570 chk.setLint(prevLint);
5571 }
5572
5573 }
5574 }
5575
5576 public void visitImport(JCImport tree) {
5683 sym.kind != VAR ||
5684 sym.getConstValue() == null)
5685 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
5686 }
5687 }
5688
5689 // Check for proper placement of super()/this() calls.
5690 chk.checkSuperInitCalls(tree);
5691
5692 // Check for cycles among non-initial constructors.
5693 chk.checkCyclicConstructors(tree);
5694
5695 // Check for cycles among annotation elements.
5696 chk.checkNonCyclicElements(tree);
5697
5698 // Check for proper use of serialVersionUID and other
5699 // serialization-related fields and methods
5700 if (env.info.lint.isEnabled(LintCategory.SERIAL)
5701 && rs.isSerializable(c.type)
5702 && !c.isAnonymous()) {
5703 chk.checkSerialStructure(tree, c);
5704 }
5705 // Correctly organize the positions of the type annotations
5706 typeAnnotations.organizeTypeAnnotationsBodies(tree);
5707
5708 // Check type annotations applicability rules
5709 validateTypeAnnotations(tree, false);
5710 }
5711 // where
5712 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
5713 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
5714 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
5715 if (types.isSameType(al.head.annotationType.type, t))
5716 return al.head.pos();
5717 }
5718
5719 return null;
5720 }
5721
5722 private Type capture(Type type) {
5723 return types.capture(type);
|
107 final Analyzer analyzer;
108 final DeferredAttr deferredAttr;
109 final Check chk;
110 final Flow flow;
111 final MemberEnter memberEnter;
112 final TypeEnter typeEnter;
113 final TreeMaker make;
114 final ConstFold cfolder;
115 final Enter enter;
116 final Target target;
117 final Types types;
118 final Preview preview;
119 final JCDiagnostic.Factory diags;
120 final TypeAnnotations typeAnnotations;
121 final TypeEnvs typeEnvs;
122 final Dependencies dependencies;
123 final Annotate annotate;
124 final ArgumentAttr argumentAttr;
125 final MatchBindingsComputer matchBindingsComputer;
126 final AttrRecover attrRecover;
127 final LocalProxyVarsGen localProxyVarsGen;
128 final boolean captureMRefReturnType;
129
130 public static Attr instance(Context context) {
131 Attr instance = context.get(attrKey);
132 if (instance == null)
133 instance = new Attr(context);
134 return instance;
135 }
136
137 @SuppressWarnings("this-escape")
138 protected Attr(Context context) {
139 context.put(attrKey, this);
140
141 names = Names.instance(context);
142 log = Log.instance(context);
143 lintMapper = LintMapper.instance(context);
144 syms = Symtab.instance(context);
145 rs = Resolve.instance(context);
146 operators = Operators.instance(context);
147 chk = Check.instance(context);
148 flow = Flow.instance(context);
149 memberEnter = MemberEnter.instance(context);
150 typeEnter = TypeEnter.instance(context);
151 make = TreeMaker.instance(context);
152 enter = Enter.instance(context);
153 infer = Infer.instance(context);
154 analyzer = Analyzer.instance(context);
155 deferredAttr = DeferredAttr.instance(context);
156 cfolder = ConstFold.instance(context);
157 target = Target.instance(context);
158 types = Types.instance(context);
159 preview = Preview.instance(context);
160 diags = JCDiagnostic.Factory.instance(context);
161 annotate = Annotate.instance(context);
162 typeAnnotations = TypeAnnotations.instance(context);
163 typeEnvs = TypeEnvs.instance(context);
164 dependencies = Dependencies.instance(context);
165 argumentAttr = ArgumentAttr.instance(context);
166 matchBindingsComputer = MatchBindingsComputer.instance(context);
167 attrRecover = AttrRecover.instance(context);
168 localProxyVarsGen = LocalProxyVarsGen.instance(context);
169
170 Options options = Options.instance(context);
171
172 Source source = Source.instance(context);
173 allowReifiableTypesInInstanceof = Feature.REIFIABLE_TYPES_INSTANCEOF.allowedInSource(source);
174 allowRecords = Feature.RECORDS.allowedInSource(source);
175 allowPatternSwitch = (preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH)) &&
176 Feature.PATTERN_SWITCH.allowedInSource(source);
177 allowUnconditionalPatternsInstanceOf =
178 Feature.UNCONDITIONAL_PATTERN_IN_INSTANCEOF.allowedInSource(source);
179 sourceName = source.name;
180 useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
181 captureMRefReturnType = Source.Feature.CAPTURE_MREF_RETURN_TYPE.allowedInSource(source);
182
183 statInfo = new ResultInfo(KindSelector.NIL, Type.noType);
184 varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType);
185 unknownExprInfo = new ResultInfo(KindSelector.VAL, Type.noType);
186 methodAttrInfo = new MethodAttrInfo();
187 unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
188 unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
189 recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
190 initBlockType = new MethodType(List.nil(), syms.voidType, List.nil(), syms.methodClass);
191 allowValueClasses = (!preview.isPreview(Feature.VALUE_CLASSES) || preview.isEnabled()) &&
192 Feature.VALUE_CLASSES.allowedInSource(source);
193 }
194
195 /** Switch: reifiable types in instanceof enabled?
196 */
197 boolean allowReifiableTypesInInstanceof;
198
199 /** Are records allowed
200 */
201 private final boolean allowRecords;
202
203 /** Are patterns in switch allowed
204 */
205 private final boolean allowPatternSwitch;
206
207 /** Are unconditional patterns in instanceof allowed
208 */
209 private final boolean allowUnconditionalPatternsInstanceOf;
210
211 /** Are value classes allowed
212 */
213 private final boolean allowValueClasses;
214
215 /**
216 * Switch: warn about use of variable before declaration?
217 * RFE: 6425594
218 */
219 boolean useBeforeDeclarationWarning;
220
221 /**
222 * Switch: name of source level; used for error reporting.
223 */
224 String sourceName;
225
226 /** Check kind and type of given tree against protokind and prototype.
227 * If check succeeds, store type in tree and return it.
228 * If check fails, store errType in tree and return it.
229 * No checks are performed if the prototype is a method type.
230 * It is not necessary in this case since we know that kind and type
231 * are correct.
232 *
233 * @param tree The tree whose kind and type is checked
234 * @param found The computed type of the tree
288 owner.kind == VAR || // i.e. we are in a variable initializer
289 (owner.flags() & BLOCK) != 0) // i.e. we are in an initializer block
290 &&
291 v.owner == owner.owner
292 &&
293 ((v.flags() & STATIC) != 0) == Resolve.isStatic(env));
294 boolean insideCompactConstructor = env.enclMethod != null && TreeInfo.isCompactConstructor(env.enclMethod);
295 return isAssignable & !insideCompactConstructor;
296 }
297
298 /** Check that variable can be assigned to.
299 * @param pos The current source code position.
300 * @param v The assigned variable
301 * @param base If the variable is referred to in a Select, the part
302 * to the left of the `.', null otherwise.
303 * @param env The current environment.
304 */
305 void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) {
306 if (v.name == names._this) {
307 log.error(pos, Errors.CantAssignValToThis);
308 } else if ((v.flags() & FINAL) != 0 &&
309 ((v.flags() & HASINIT) != 0
310 ||
311 !((base == null ||
312 TreeInfo.isThisQualifier(base)) &&
313 isAssignableAsBlankFinal(v, env)))) {
314 if (v.isResourceVariable()) { //TWR resource
315 log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
316 } else {
317 log.error(pos, Errors.CantAssignValToVar(Flags.toSource(v.flags() & (STATIC | FINAL)), v));
318 }
319 }
320 }
321
322 /** Does tree represent a static reference to an identifier?
323 * It is assumed that tree is either a SELECT or an IDENT.
324 * We have to weed out selects from non-type names here.
325 * @param tree The candidate tree.
326 */
327 boolean isStaticReference(JCTree tree) {
328 if (tree.hasTag(SELECT)) {
329 Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected);
330 if (lsym == null || lsym.kind != TYP) {
331 return false;
332 }
333 }
334 return true;
335 }
336
337 /** Is this symbol a type?
338 */
1093 * - have an accessibility stricter than that of the record type
1094 * - explicitly invoke any other constructor
1095 */
1096 if ((tree.sym.flags_field & GENERATEDCONSTR) == 0) {
1097 if (Check.protection(m.flags()) > Check.protection(env.enclClass.sym.flags())) {
1098 log.error(tree,
1099 (env.enclClass.sym.flags() & AccessFlags) == 0 ?
1100 Errors.InvalidCanonicalConstructorInRecord(
1101 Fragments.Canonical,
1102 env.enclClass.sym.name,
1103 Fragments.CanonicalMustNotHaveStrongerAccess("package")
1104 ) :
1105 Errors.InvalidCanonicalConstructorInRecord(
1106 Fragments.Canonical,
1107 env.enclClass.sym.name,
1108 Fragments.CanonicalMustNotHaveStrongerAccess(asFlagSet(env.enclClass.sym.flags() & AccessFlags))
1109 )
1110 );
1111 }
1112
1113 if ((!allowValueClasses || TreeInfo.isCompactConstructor(tree)) &&
1114 TreeInfo.hasAnyConstructorCall(tree)) {
1115 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1116 Fragments.Canonical, env.enclClass.sym.name,
1117 Fragments.CanonicalMustNotContainExplicitConstructorInvocation));
1118 }
1119 }
1120
1121 // also we want to check that no type variables have been defined
1122 if (!tree.typarams.isEmpty()) {
1123 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1124 Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalMustNotDeclareTypeVariables));
1125 }
1126
1127 /* and now we need to check that the constructor's arguments are exactly the same as those of the
1128 * record components
1129 */
1130 List<? extends RecordComponent> recordComponents = env.enclClass.sym.getRecordComponents();
1131 List<Type> recordFieldTypes = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.type);
1132 for (JCVariableDecl param: tree.params) {
1133 boolean paramIsVarArgs = (param.sym.flags_field & VARARGS) != 0;
1134 if (!types.isSameType(param.type, recordFieldTypes.head) ||
1172 if (tree.defaultValue != null) {
1173 if ((owner.flags() & ANNOTATION) == 0)
1174 log.error(tree.pos(),
1175 Errors.DefaultAllowedInIntfAnnotationMember);
1176 }
1177 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0)
1178 log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract(tree.sym, owner));
1179 } else {
1180 if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) {
1181 if ((owner.flags() & INTERFACE) != 0) {
1182 log.error(tree.body.pos(), Errors.IntfMethCantHaveBody);
1183 } else {
1184 log.error(tree.pos(), Errors.AbstractMethCantHaveBody);
1185 }
1186 } else if ((tree.mods.flags & NATIVE) != 0) {
1187 log.error(tree.pos(), Errors.NativeMethCantHaveBody);
1188 }
1189 // Add an implicit super() call unless an explicit call to
1190 // super(...) or this(...) is given
1191 // or we are compiling class java.lang.Object.
1192 boolean addedSuperInIdentityClass = false;
1193 if (isConstructor && owner.type != syms.objectType) {
1194 if (!TreeInfo.hasAnyConstructorCall(tree)) {
1195 JCStatement supCall = make.at(tree.body.pos).Exec(make.Apply(List.nil(),
1196 make.Ident(names._super), make.Idents(List.nil())));
1197 if (allowValueClasses && (owner.isValueClass() || owner.hasStrict() || ((owner.flags_field & RECORD) != 0))) {
1198 tree.body.stats = tree.body.stats.append(supCall);
1199 } else {
1200 tree.body.stats = tree.body.stats.prepend(supCall);
1201 addedSuperInIdentityClass = true;
1202 }
1203 } else if ((env.enclClass.sym.flags() & ENUM) != 0 &&
1204 (tree.mods.flags & GENERATEDCONSTR) == 0 &&
1205 TreeInfo.hasConstructorCall(tree, names._super)) {
1206 // enum constructors are not allowed to call super
1207 // directly, so make sure there aren't any super calls
1208 // in enum constructors, except in the compiler
1209 // generated one.
1210 log.error(tree.body.stats.head.pos(),
1211 Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym));
1212 }
1213 if (env.enclClass.sym.isRecord() && (tree.sym.flags_field & RECORD) != 0) { // we are seeing the canonical constructor
1214 List<Name> recordComponentNames = TreeInfo.recordFields(env.enclClass).map(vd -> vd.sym.name);
1215 List<Name> initParamNames = tree.sym.params.map(p -> p.name);
1216 if (!initParamNames.equals(recordComponentNames)) {
1217 log.error(tree, Errors.InvalidCanonicalConstructorInRecord(
1218 Fragments.Canonical, env.enclClass.sym.name, Fragments.CanonicalWithNameMismatch));
1219 }
1220 if (tree.sym.type.asMethodType().thrown != null && !tree.sym.type.asMethodType().thrown.isEmpty()) {
1221 log.error(tree,
1222 Errors.InvalidCanonicalConstructorInRecord(
1223 TreeInfo.isCompactConstructor(tree) ? Fragments.Compact : Fragments.Canonical,
1224 env.enclClass.sym.name,
1225 Fragments.ThrowsClauseNotAllowedForCanonicalConstructor(
1226 TreeInfo.isCompactConstructor(tree) ? Fragments.Compact : Fragments.Canonical)));
1227 }
1228 }
1229 }
1230
1231 // Attribute all type annotations in the body
1232 annotate.queueScanTreeAndTypeAnnotate(tree.body, localEnv, m);
1233 annotate.flush();
1234
1235 // Start of constructor prologue (if not in java.lang.Object constructor)
1236 localEnv.info.ctorPrologue = isConstructor && owner.type != syms.objectType;
1237
1238 // Attribute method body.
1239 attribStat(tree.body, localEnv);
1240 if (localEnv.info.ctorPrologue) {
1241 boolean thisInvocation = false;
1242 ListBuffer<JCTree> prologueCode = new ListBuffer<>();
1243 for (JCTree stat : tree.body.stats) {
1244 prologueCode.add(stat);
1245 /* gather all the stats in the body until a `super` or `this` constructor invocation is found,
1246 * including the constructor invocation, that way we don't need to worry in the visitor below if
1247 * if we are dealing or not with prologue code
1248 */
1249 if (stat instanceof JCExpressionStatement expStmt &&
1250 expStmt.expr instanceof JCMethodInvocation mi &&
1251 TreeInfo.isConstructorCall(mi)) {
1252 thisInvocation = TreeInfo.name(mi.meth) == names._this;
1253 if (!addedSuperInIdentityClass || !allowValueClasses) {
1254 break;
1255 }
1256 }
1257 }
1258 if (!prologueCode.isEmpty()) {
1259 CtorPrologueVisitor ctorPrologueVisitor = new CtorPrologueVisitor(localEnv,
1260 addedSuperInIdentityClass && allowValueClasses ?
1261 PrologueVisitorMode.WARNINGS_ONLY :
1262 thisInvocation ?
1263 PrologueVisitorMode.THIS_CONSTRUCTOR :
1264 PrologueVisitorMode.SUPER_CONSTRUCTOR,
1265 false);
1266 ctorPrologueVisitor.scan(prologueCode.toList());
1267 }
1268 }
1269 }
1270
1271 localEnv.info.scope.leave();
1272 result = tree.type = m.type;
1273 } finally {
1274 chk.setLint(prevLint);
1275 chk.setMethod(prevMethod);
1276 env.info.ctorPrologue = ctorProloguePrev;
1277 }
1278 }
1279
1280 enum PrologueVisitorMode {
1281 WARNINGS_ONLY,
1282 SUPER_CONSTRUCTOR,
1283 THIS_CONSTRUCTOR
1284 }
1285
1286 class CtorPrologueVisitor extends TreeScanner {
1287 Env<AttrContext> localEnv;
1288 PrologueVisitorMode mode;
1289 boolean isInitializer;
1290
1291 CtorPrologueVisitor(Env<AttrContext> localEnv, PrologueVisitorMode mode, boolean isInitializer) {
1292 this.localEnv = localEnv;
1293 currentClassSym = localEnv.enclClass.sym;
1294 this.mode = mode;
1295 this.isInitializer = isInitializer;
1296 }
1297
1298 boolean insideLambdaOrClassDef = false;
1299
1300 @Override
1301 public void visitLambda(JCLambda lambda) {
1302 boolean previousInsideLambdaOrClassDef = insideLambdaOrClassDef;
1303 try {
1304 insideLambdaOrClassDef = true;
1305 super.visitLambda(lambda);
1306 } finally {
1307 insideLambdaOrClassDef = previousInsideLambdaOrClassDef;
1308 }
1309 }
1310
1311 ClassSymbol currentClassSym;
1312
1313 @Override
1314 public void visitClassDef(JCClassDecl classDecl) {
1315 boolean previousInsideLambdaOrClassDef = insideLambdaOrClassDef;
1316 ClassSymbol previousClassSym = currentClassSym;
1317 try {
1318 insideLambdaOrClassDef = true;
1319 currentClassSym = classDecl.sym;
1320 super.visitClassDef(classDecl);
1321 } finally {
1322 insideLambdaOrClassDef = previousInsideLambdaOrClassDef;
1323 currentClassSym = previousClassSym;
1324 }
1325 }
1326
1327 private void reportPrologueError(JCTree tree, Symbol sym) {
1328 reportPrologueError(tree, sym, false);
1329 }
1330
1331 private void reportPrologueError(JCTree tree, Symbol sym, boolean hasInit) {
1332 preview.checkSourceLevel(tree, Feature.FLEXIBLE_CONSTRUCTORS);
1333 if (mode != PrologueVisitorMode.WARNINGS_ONLY) {
1334 if (hasInit) {
1335 log.error(tree, Errors.CantAssignInitializedBeforeCtorCalled(sym));
1336 } else {
1337 log.error(tree, Errors.CantRefBeforeCtorCalled(sym));
1338 }
1339 } else if (allowValueClasses) {
1340 // issue lint warning
1341 log.warning(tree, LintWarnings.WouldNotBeAllowedInPrologue(sym));
1342 }
1343 }
1344
1345 @Override
1346 public void visitApply(JCMethodInvocation tree) {
1347 super.visitApply(tree);
1348 Name name = TreeInfo.name(tree.meth);
1349 boolean isConstructorCall = name == names._this || name == names._super;
1350 Symbol msym = TreeInfo.symbolFor(tree.meth);
1351 // is this an instance method call or an illegal constructor invocation like: `this.super()`?
1352 if (msym != null && // for erroneous invocations msym can be null, ignore those
1353 (!isConstructorCall ||
1354 isConstructorCall && tree.meth.hasTag(SELECT))) {
1355 if (isEarlyReference(localEnv, tree.meth, msym))
1356 reportPrologueError(tree.meth, msym);
1357 }
1358 }
1359
1360 @Override
1361 public void visitIdent(JCIdent tree) {
1362 analyzeSymbol(tree);
1363 }
1364
1365 boolean isIndexed = false;
1366
1367 @Override
1368 public void visitIndexed(JCArrayAccess tree) {
1369 boolean previousIsIndexed = isIndexed;
1370 try {
1371 isIndexed = true;
1372 scan(tree.indexed);
1373 } finally {
1374 isIndexed = previousIsIndexed;
1375 }
1376 scan(tree.index);
1377 if (mode == PrologueVisitorMode.SUPER_CONSTRUCTOR && isInstanceField(tree.indexed)) {
1378 localProxyVarsGen.addFieldReadInPrologue(localEnv.enclMethod, TreeInfo.symbolFor(tree.indexed));
1379 }
1380 }
1381
1382 @Override
1383 public void visitSelect(JCFieldAccess tree) {
1384 SelectScanner ss = new SelectScanner();
1385 ss.scan(tree);
1386 if (ss.scanLater == null) {
1387 Symbol sym = TreeInfo.symbolFor(tree);
1388 // if this is a field access
1389 if (sym.kind == VAR && sym.owner.kind == TYP) {
1390 // Type.super.field or super.field expressions are forbidden in early construction contexts
1391 for (JCTree subtree : ss.selectorTrees) {
1392 if (TreeInfo.isSuperOrSelectorDotSuper(subtree)) {
1393 reportPrologueError(tree, sym);
1394 return;
1395 }
1396 }
1397 }
1398 analyzeSymbol(tree);
1399 } else {
1400 boolean prevLhs = isInLHS;
1401 try {
1402 isInLHS = false;
1403 scan(ss.scanLater);
1404 } finally {
1405 isInLHS = prevLhs;
1406 }
1407 }
1408 if (mode == PrologueVisitorMode.SUPER_CONSTRUCTOR) {
1409 for (JCTree subtree : ss.selectorTrees) {
1410 if (isInstanceField(subtree)) {
1411 // we need to add a proxy for this one
1412 localProxyVarsGen.addFieldReadInPrologue(localEnv.enclMethod, TreeInfo.symbolFor(subtree));
1413 }
1414 }
1415 }
1416 }
1417
1418 boolean isInstanceField(JCTree tree) {
1419 Symbol sym = TreeInfo.symbolFor(tree);
1420 return (sym != null &&
1421 !sym.isStatic() &&
1422 sym.kind == VAR &&
1423 sym.owner.kind == TYP &&
1424 sym.name != names._this &&
1425 sym.name != names._super &&
1426 isEarlyReference(localEnv, tree, sym));
1427 }
1428
1429 @Override
1430 public void visitNewClass(JCNewClass tree) {
1431 super.visitNewClass(tree);
1432 checkNewClassAndMethRefs(tree, tree.type);
1433 }
1434
1435 @Override
1436 public void visitReference(JCMemberReference tree) {
1437 super.visitReference(tree);
1438 if (tree.getMode() == JCMemberReference.ReferenceMode.NEW) {
1439 checkNewClassAndMethRefs(tree, tree.expr.type);
1440 }
1441 }
1442
1443 void checkNewClassAndMethRefs(JCTree tree, Type t) {
1444 if (t.tsym.isEnclosedBy(localEnv.enclClass.sym) &&
1445 !t.tsym.isStatic() &&
1446 !t.tsym.isDirectlyOrIndirectlyLocal()) {
1447 reportPrologueError(tree, t.getEnclosingType().tsym);
1448 }
1449 }
1450
1451 /* if a symbol is in the LHS of an assignment expression we won't consider it as a candidate
1452 * for a proxy local variable later on
1453 */
1454 boolean isInLHS = false;
1455
1456 @Override
1457 public void visitAssign(JCAssign tree) {
1458 boolean previousIsInLHS = isInLHS;
1459 try {
1460 isInLHS = true;
1461 scan(tree.lhs);
1462 } finally {
1463 isInLHS = previousIsInLHS;
1464 }
1465 scan(tree.rhs);
1466 }
1467
1468 @Override
1469 public void visitMethodDef(JCMethodDecl tree) {
1470 // ignore any declarative part, mainly to avoid scanning receiver parameters
1471 scan(tree.body);
1472 }
1473
1474 void analyzeSymbol(JCTree tree) {
1475 Symbol sym = TreeInfo.symbolFor(tree);
1476 // make sure that there is a symbol and it is not static
1477 if (sym == null || sym.isStatic()) {
1478 return;
1479 }
1480 if (isInLHS && !insideLambdaOrClassDef) {
1481 // Check instance field assignments that appear in constructor prologues
1482 if (isEarlyReference(localEnv, tree, sym)) {
1483 // Field may not be inherited from a superclass
1484 if (sym.owner != localEnv.enclClass.sym) {
1485 reportPrologueError(tree, sym);
1486 return;
1487 }
1488 // Field may not have an initializer
1489 if ((sym.flags() & HASINIT) != 0) {
1490 if (!localEnv.enclClass.sym.isValueClass() || !sym.type.hasTag(ARRAY) || !isIndexed) {
1491 reportPrologueError(tree, sym, true);
1492 }
1493 return;
1494 }
1495 // cant reference an instance field before a this constructor
1496 if (allowValueClasses && mode == PrologueVisitorMode.THIS_CONSTRUCTOR) {
1497 reportPrologueError(tree, sym);
1498 return;
1499 }
1500 }
1501 return;
1502 }
1503 tree = TreeInfo.skipParens(tree);
1504 if (sym.kind == VAR && sym.owner.kind == TYP) {
1505 if (sym.name == names._this || sym.name == names._super) {
1506 // are we seeing something like `this` or `CurrentClass.this` or `SuperClass.super::foo`?
1507 if (TreeInfo.isExplicitThisReference(
1508 types,
1509 (ClassType)localEnv.enclClass.sym.type,
1510 tree)) {
1511 reportPrologueError(tree, sym);
1512 }
1513 } else if (sym.kind == VAR && sym.owner.kind == TYP) { // now fields only
1514 if (sym.owner != localEnv.enclClass.sym) {
1515 if (localEnv.enclClass.sym.isSubClass(sym.owner, types) &&
1516 sym.isInheritedIn(localEnv.enclClass.sym, types)) {
1517 /* if we are dealing with a field that doesn't belong to the current class, but the
1518 * field is inherited, this is an error. Unless, the super class is also an outer
1519 * class and the field's qualifier refers to the outer class
1520 */
1521 if (tree.hasTag(IDENT) ||
1522 TreeInfo.isExplicitThisReference(
1523 types,
1524 (ClassType)localEnv.enclClass.sym.type,
1525 ((JCFieldAccess)tree).selected)) {
1526 reportPrologueError(tree, sym);
1527 }
1528 }
1529 } else if (isEarlyReference(localEnv, tree, sym)) {
1530 /* now this is a `proper` instance field of the current class
1531 * references to fields of identity classes which happen to have initializers are
1532 * not allowed in the prologue.
1533 * But it is OK for a field with initializer to refer to another field with initializer,
1534 * so no warning or error if we are analyzing a field initializer.
1535 */
1536 if (insideLambdaOrClassDef ||
1537 (!localEnv.enclClass.sym.isValueClass() &&
1538 (sym.flags_field & HASINIT) != 0 &&
1539 !isInitializer))
1540 reportPrologueError(tree, sym);
1541 // we will need to generate a proxy for this field later on
1542 if (!isInLHS) {
1543 if (!allowValueClasses) {
1544 reportPrologueError(tree, sym);
1545 } else {
1546 if (mode == PrologueVisitorMode.THIS_CONSTRUCTOR) {
1547 reportPrologueError(tree, sym);
1548 } else if (mode == PrologueVisitorMode.SUPER_CONSTRUCTOR) {
1549 localProxyVarsGen.addFieldReadInPrologue(localEnv.enclMethod, sym);
1550 }
1551 /* we do nothing in warnings only mode, as in that mode we are simulating what
1552 * the compiler would do in case the constructor code would be in the prologue
1553 * phase
1554 */
1555 }
1556 }
1557 }
1558 }
1559 }
1560 }
1561
1562 /**
1563 * Determine if the symbol appearance constitutes an early reference to the current class.
1564 *
1565 * <p>
1566 * This means the symbol is an instance field, or method, of the current class and it appears
1567 * in an early initialization context of it (i.e., one of its constructor prologues).
1568 *
1569 * @param env The current environment
1570 * @param tree the AST referencing the variable
1571 * @param sym The symbol
1572 */
1573 private boolean isEarlyReference(Env<AttrContext> env, JCTree tree, Symbol sym) {
1574 if ((sym.kind == VAR || sym.kind == MTH) &&
1575 sym.isMemberOf(env.enclClass.sym, types) &&
1576 ((sym.flags() & STATIC) == 0 ||
1577 (sym.kind == MTH && tree instanceof JCFieldAccess))) {
1578 // Allow "Foo.this.x" when "Foo" is (also) an outer class, as this refers to the outer instance
1579 if (tree instanceof JCFieldAccess fa) {
1580 return TreeInfo.isExplicitThisReference(types, (ClassType)env.enclClass.type, fa.selected);
1581 } else if (currentClassSym != env.enclClass.sym) {
1582 /* so we are inside a class, CI, in the prologue of an outer class, CO, and the symbol being
1583 * analyzed has no qualifier. So if the symbol is a member of CI the reference is allowed,
1584 * otherwise it is not.
1585 * It could be that the reference to CI's member happens inside CI's own prologue, but that
1586 * will be checked separately, when CI's prologue is analyzed.
1587 */
1588 return !sym.isMemberOf(currentClassSym, types);
1589 }
1590 return true;
1591 }
1592 return false;
1593 }
1594
1595 /* scanner for a select expression, anything that is not a select or identifier
1596 * will be stored for further analysis
1597 */
1598 class SelectScanner extends DeferredAttr.FilterScanner {
1599 JCTree scanLater;
1600 java.util.List<JCTree> selectorTrees = new ArrayList<>();
1601
1602 SelectScanner() {
1603 super(Set.of(IDENT, SELECT, PARENS));
1604 }
1605
1606 @Override
1607 public void visitSelect(JCFieldAccess tree) {
1608 super.visitSelect(tree);
1609 selectorTrees.add(tree.selected);
1610 }
1611
1612 @Override
1613 void skip(JCTree tree) {
1614 scanLater = tree;
1615 }
1616 }
1617 }
1618
1619 public void visitVarDef(JCVariableDecl tree) {
1620 // Local variables have not been entered yet, so we need to do it now:
1621 if (env.info.scope.owner.kind == MTH || env.info.scope.owner.kind == VAR) {
1622 if (tree.sym != null) {
1623 // parameters have already been entered
1624 env.info.scope.enter(tree.sym);
1625 } else {
1626 if (tree.isImplicitlyTyped() && (tree.getModifiers().flags & PARAMETER) == 0) {
1627 if (tree.init == null) {
1628 //cannot use 'var' without initializer
1629 log.error(tree, Errors.CantInferLocalVarType(tree.name, Fragments.LocalMissingInit));
1630 tree.vartype = make.at(tree.pos()).Erroneous();
1631 } else {
1632 Fragment msg = canInferLocalVarType(tree);
1633 if (msg != null) {
1634 //cannot use 'var' with initializer which require an explicit target
1635 //(e.g. lambda, method reference, array initializer).
1636 log.error(tree, Errors.CantInferLocalVarType(tree.name, msg));
1637 tree.vartype = make.at(tree.pos()).Erroneous();
1638 }
1647 }
1648 } else {
1649 doQueueScanTreeAndTypeAnnotateForVarInit(tree, env);
1650 }
1651
1652 VarSymbol v = tree.sym;
1653 Lint lint = env.info.lint.augment(v);
1654 Lint prevLint = chk.setLint(lint);
1655
1656 // Check that the variable's declared type is well-formed.
1657 boolean isImplicitLambdaParameter = env.tree.hasTag(LAMBDA) &&
1658 ((JCLambda)env.tree).paramKind == JCLambda.ParameterKind.IMPLICIT &&
1659 (tree.sym.flags() & PARAMETER) != 0;
1660 chk.validate(tree.vartype, env, !isImplicitLambdaParameter && !tree.isImplicitlyTyped());
1661
1662 try {
1663 v.getConstValue(); // ensure compile-time constant initializer is evaluated
1664 chk.checkDeprecatedAnnotation(tree.pos(), v);
1665
1666 if (tree.init != null) {
1667 Env<AttrContext> initEnv = memberEnter.initEnv(tree, env);
1668 if ((v.flags_field & FINAL) == 0 ||
1669 !memberEnter.needsLazyConstValue(tree.init)) {
1670 // Not a compile-time constant
1671 // Attribute initializer in a new environment
1672 // with the declared variable as owner.
1673 // Check that initializer conforms to variable's declared type.
1674 initEnv.info.lint = lint;
1675 // In order to catch self-references, we set the variable's
1676 // declaration position to maximal possible value, effectively
1677 // marking the variable as undefined.
1678 initEnv.info.enclVar = v;
1679 boolean previousCtorPrologue = initEnv.info.ctorPrologue;
1680 try {
1681 if (v.owner.kind == TYP && !v.isStatic() && v.isStrict()) {
1682 // strict instance initializer in a value class
1683 initEnv.info.ctorPrologue = true;
1684 }
1685 attribExpr(tree.init, initEnv, v.type);
1686 if (tree.isImplicitlyTyped()) {
1687 //fixup local variable type
1688 v.type = chk.checkLocalVarType(tree, tree.init.type, tree.name);
1689 }
1690 } finally {
1691 initEnv.info.ctorPrologue = previousCtorPrologue;
1692 }
1693 }
1694 if (allowValueClasses && v.owner.kind == TYP && !v.isStatic()) {
1695 // strict field initializers are inlined in constructor's prologues
1696 CtorPrologueVisitor ctorPrologueVisitor = new CtorPrologueVisitor(initEnv,
1697 !v.isStrict() ? PrologueVisitorMode.WARNINGS_ONLY : PrologueVisitorMode.SUPER_CONSTRUCTOR,
1698 true);
1699 ctorPrologueVisitor.scan(tree.init);
1700 }
1701 if (tree.isImplicitlyTyped()) {
1702 setSyntheticVariableType(tree, v.type);
1703 }
1704 }
1705 result = tree.type = v.type;
1706 if (env.enclClass.sym.isRecord() && tree.sym.owner.kind == TYP && !v.isStatic()) {
1707 if (isNonArgsMethodInObject(v.name)) {
1708 log.error(tree, Errors.IllegalRecordComponentName(v));
1709 }
1710 }
1711 chk.checkRequiresIdentity(tree, env.info.lint);
1712 }
1713 finally {
1714 chk.setLint(prevLint);
1715 }
1716 }
1717
1718 private void doQueueScanTreeAndTypeAnnotateForVarInit(JCVariableDecl tree, Env<AttrContext> env) {
1719 if (tree.init != null &&
1720 (tree.mods.flags & Flags.FIELD_INIT_TYPE_ANNOTATIONS_QUEUED) == 0 &&
1799 }
1800 }
1801 }
1802
1803 public void visitSkip(JCSkip tree) {
1804 result = null;
1805 }
1806
1807 public void visitBlock(JCBlock tree) {
1808 if (env.info.scope.owner.kind == TYP || env.info.scope.owner.kind == ERR) {
1809 // Block is a static or instance initializer;
1810 // let the owner of the environment be a freshly
1811 // created BLOCK-method.
1812 Symbol fakeOwner =
1813 new MethodSymbol(tree.flags | BLOCK |
1814 env.info.scope.owner.flags() & STRICTFP, names.empty, initBlockType,
1815 env.info.scope.owner);
1816 final Env<AttrContext> localEnv =
1817 env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
1818
1819 if ((tree.flags & STATIC) != 0) {
1820 localEnv.info.staticLevel++;
1821 } else {
1822 localEnv.info.instanceInitializerBlock = true;
1823 }
1824 // Attribute all type annotations in the block
1825 annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, localEnv.info.scope.owner);
1826 annotate.flush();
1827 attribStats(tree.stats, localEnv);
1828
1829 {
1830 // Store init and clinit type annotations with the ClassSymbol
1831 // to allow output in Gen.normalizeDefs.
1832 ClassSymbol cs = (ClassSymbol)env.info.scope.owner;
1833 List<Attribute.TypeCompound> tas = localEnv.info.scope.owner.getRawTypeAttributes();
1834 if ((tree.flags & STATIC) != 0) {
1835 cs.appendClassInitTypeAttributes(tas);
1836 } else {
1837 cs.appendInitTypeAttributes(tas);
1838 }
1839 }
1840 } else {
1841 // Create a new local environment with a local scope.
1842 Env<AttrContext> localEnv =
1843 env.dup(tree, env.info.dup(env.info.scope.dup()));
2316 // where
2317 /** Return the selected enumeration constant symbol, or null. */
2318 private Symbol enumConstant(JCTree tree, Type enumType) {
2319 if (tree.hasTag(IDENT)) {
2320 JCIdent ident = (JCIdent)tree;
2321 Name name = ident.name;
2322 for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
2323 if (sym.kind == VAR) {
2324 Symbol s = ident.sym = sym;
2325 ((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
2326 ident.type = s.type;
2327 return ((s.flags_field & Flags.ENUM) == 0)
2328 ? null : s;
2329 }
2330 }
2331 }
2332 return null;
2333 }
2334
2335 public void visitSynchronized(JCSynchronized tree) {
2336 boolean identityType = chk.checkIdentityType(tree.pos(), attribExpr(tree.lock, env));
2337 if (identityType && tree.lock.type != null && tree.lock.type.isValueBased()) {
2338 log.warning(tree.pos(), LintWarnings.AttemptToSynchronizeOnInstanceOfValueBasedClass);
2339 }
2340 attribStat(tree.body, env);
2341 result = null;
2342 }
2343
2344 public void visitTry(JCTry tree) {
2345 // Create a new local environment with a local
2346 Env<AttrContext> localEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
2347 try {
2348 boolean isTryWithResource = tree.resources.nonEmpty();
2349 // Create a nested environment for attributing the try block if needed
2350 Env<AttrContext> tryEnv = isTryWithResource ?
2351 env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup())) :
2352 localEnv;
2353 try {
2354 // Attribute resource declarations
2355 for (JCTree resource : tree.resources) {
2356 CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) {
2357 @Override
4773 }
4774
4775 public void visitSelect(JCFieldAccess tree) {
4776 // Determine the expected kind of the qualifier expression.
4777 KindSelector skind = KindSelector.NIL;
4778 if (tree.name == names._this || tree.name == names._super ||
4779 tree.name == names._class)
4780 {
4781 skind = KindSelector.TYP;
4782 } else {
4783 if (pkind().contains(KindSelector.PCK))
4784 skind = KindSelector.of(skind, KindSelector.PCK);
4785 if (pkind().contains(KindSelector.TYP))
4786 skind = KindSelector.of(skind, KindSelector.TYP, KindSelector.PCK);
4787 if (pkind().contains(KindSelector.VAL_MTH))
4788 skind = KindSelector.of(skind, KindSelector.VAL, KindSelector.TYP);
4789 }
4790
4791 // Attribute the qualifier expression, and determine its symbol (if any).
4792 Type site = attribTree(tree.selected, env, new ResultInfo(skind, Type.noType));
4793 Assert.check(site == tree.selected.type);
4794 if (!pkind().contains(KindSelector.TYP_PCK))
4795 site = capture(site); // Capture field access
4796
4797 // don't allow T.class T[].class, etc
4798 if (skind == KindSelector.TYP) {
4799 Type elt = site;
4800 while (elt.hasTag(ARRAY))
4801 elt = ((ArrayType)elt).elemtype;
4802 if (elt.hasTag(TYPEVAR)) {
4803 log.error(tree.pos(), Errors.TypeVarCantBeDeref);
4804 result = tree.type = types.createErrorType(tree.name, site.tsym, site);
4805 tree.sym = tree.type.tsym;
4806 return ;
4807 }
4808 }
4809
4810 // If qualifier symbol is a type or `super', assert `selectSuper'
4811 // for the selection. This is relevant for determining whether
4812 // protected symbols are accessible.
4813 Symbol sitesym = TreeInfo.symbol(tree.selected);
5885 .filter(s -> s.tsym.isSealed())
5886 .map(s -> (ClassSymbol) s.tsym)
5887 .collect(List.collector());
5888
5889 if (sealedSupers.isEmpty()) {
5890 if ((c.flags_field & Flags.NON_SEALED) != 0) {
5891 boolean hasErrorSuper = false;
5892
5893 hasErrorSuper |= types.directSupertypes(c.type)
5894 .stream()
5895 .anyMatch(s -> s.tsym.kind == Kind.ERR);
5896
5897 ClassType ct = (ClassType) c.type;
5898
5899 hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
5900
5901 if (!hasErrorSuper) {
5902 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
5903 }
5904 }
5905 } else if ((c.flags_field & Flags.COMPOUND) == 0) {
5906 if (c.isDirectlyOrIndirectlyLocal() && !c.isEnum()) {
5907 log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.LocalClassesCantExtendSealed(c.isAnonymous() ? Fragments.Anonymous : Fragments.Local));
5908 }
5909
5910 if (!c.type.isCompound()) {
5911 for (ClassSymbol supertypeSym : sealedSupers) {
5912 if (!supertypeSym.isPermittedSubclass(c.type.tsym)) {
5913 log.error(TreeInfo.diagnosticPositionFor(c.type.tsym, env.tree), Errors.CantInheritFromSealed(supertypeSym));
5914 }
5915 }
5916 if (!c.isNonSealed() && !c.isFinal() && !c.isSealed()) {
5917 log.error(TreeInfo.diagnosticPositionFor(c, env.tree),
5918 c.isInterface() ?
5919 Errors.NonSealedOrSealedExpected :
5920 Errors.NonSealedSealedOrFinalExpected);
5921 }
5922 }
5923 }
5924
5925 env.info.returnResult = null;
5926 // java.lang.Enum may not be subclassed by a non-enum
5927 if (st.tsym == syms.enumSym &&
5928 ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0))
5929 log.error(env.tree.pos(), Errors.EnumNoSubclassing);
5930
5931 // Enums may not be extended by source-level classes
5932 if (st.tsym != null &&
5933 ((st.tsym.flags_field & Flags.ENUM) != 0) &&
5934 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) {
5935 log.error(env.tree.pos(), Errors.EnumTypesNotExtensible);
5936 }
5937
5938 if (rs.isSerializable(c.type)) {
5939 env.info.isSerializable = true;
5940 }
5941
5942 if (c.isValueClass()) {
5943 Assert.check(env.tree.hasTag(CLASSDEF));
5944 chk.checkConstraintsOfValueClass((JCClassDecl) env.tree, c);
5945 }
5946
5947 attribClassBody(env, c);
5948
5949 chk.checkDeprecatedAnnotation(env.tree.pos(), c);
5950 chk.checkClassOverrideEqualsAndHashIfNeeded(env.tree.pos(), c);
5951 chk.checkFunctionalInterface((JCClassDecl) env.tree, c);
5952 chk.checkLeaksNotAccessible(env, (JCClassDecl) env.tree);
5953
5954 if (c.isImplicit()) {
5955 chk.checkHasMain(env.tree.pos(), c);
5956 }
5957 } finally {
5958 env.info.returnResult = prevReturnRes;
5959 log.useSource(prev);
5960 chk.setLint(prevLint);
5961 }
5962
5963 }
5964 }
5965
5966 public void visitImport(JCImport tree) {
6073 sym.kind != VAR ||
6074 sym.getConstValue() == null)
6075 log.error(l.head.pos(), Errors.IclsCantHaveStaticDecl(c));
6076 }
6077 }
6078
6079 // Check for proper placement of super()/this() calls.
6080 chk.checkSuperInitCalls(tree);
6081
6082 // Check for cycles among non-initial constructors.
6083 chk.checkCyclicConstructors(tree);
6084
6085 // Check for cycles among annotation elements.
6086 chk.checkNonCyclicElements(tree);
6087
6088 // Check for proper use of serialVersionUID and other
6089 // serialization-related fields and methods
6090 if (env.info.lint.isEnabled(LintCategory.SERIAL)
6091 && rs.isSerializable(c.type)
6092 && !c.isAnonymous()) {
6093 chk.checkSerialStructure(env, tree, c);
6094 }
6095 // Correctly organize the positions of the type annotations
6096 typeAnnotations.organizeTypeAnnotationsBodies(tree);
6097
6098 // Check type annotations applicability rules
6099 validateTypeAnnotations(tree, false);
6100 }
6101 // where
6102 /** get a diagnostic position for an attribute of Type t, or null if attribute missing */
6103 private DiagnosticPosition getDiagnosticPosition(JCClassDecl tree, Type t) {
6104 for(List<JCAnnotation> al = tree.mods.annotations; !al.isEmpty(); al = al.tail) {
6105 if (types.isSameType(al.head.annotationType.type, t))
6106 return al.head.pos();
6107 }
6108
6109 return null;
6110 }
6111
6112 private Type capture(Type type) {
6113 return types.capture(type);
|