51 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Diamond; |
51 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Diamond; |
52 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArg; |
52 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArg; |
53 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArgs; |
53 import static com.sun.tools.javac.resources.CompilerProperties.Fragments.DiamondInvalidArgs; |
54 import com.sun.tools.javac.resources.CompilerProperties.Errors; |
54 import com.sun.tools.javac.resources.CompilerProperties.Errors; |
55 import com.sun.tools.javac.resources.CompilerProperties.Fragments; |
55 import com.sun.tools.javac.resources.CompilerProperties.Fragments; |
|
56 import com.sun.tools.javac.resources.CompilerProperties.Warnings; |
56 import com.sun.tools.javac.tree.*; |
57 import com.sun.tools.javac.tree.*; |
57 import com.sun.tools.javac.tree.JCTree.*; |
58 import com.sun.tools.javac.tree.JCTree.*; |
58 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*; |
59 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*; |
59 import com.sun.tools.javac.util.*; |
60 import com.sun.tools.javac.util.*; |
60 import com.sun.tools.javac.util.DefinedBy.Api; |
61 import com.sun.tools.javac.util.DefinedBy.Api; |
225 Type owntype; |
226 Type owntype; |
226 boolean shouldCheck = !found.hasTag(ERROR) && |
227 boolean shouldCheck = !found.hasTag(ERROR) && |
227 !resultInfo.pt.hasTag(METHOD) && |
228 !resultInfo.pt.hasTag(METHOD) && |
228 !resultInfo.pt.hasTag(FORALL); |
229 !resultInfo.pt.hasTag(FORALL); |
229 if (shouldCheck && !ownkind.subset(resultInfo.pkind)) { |
230 if (shouldCheck && !ownkind.subset(resultInfo.pkind)) { |
230 log.error(tree.pos(), "unexpected.type", |
231 log.error(tree.pos(), |
231 resultInfo.pkind.kindNames(), |
232 Errors.UnexpectedType(resultInfo.pkind.kindNames(), |
232 ownkind.kindNames()); |
233 ownkind.kindNames())); |
233 owntype = types.createErrorType(found); |
234 owntype = types.createErrorType(found); |
234 } else if (allowPoly && inferenceContext.free(found)) { |
235 } else if (allowPoly && inferenceContext.free(found)) { |
235 //delay the check if there are inference variables in the found type |
236 //delay the check if there are inference variables in the found type |
236 //this means we are dealing with a partially inferred poly expression |
237 //this means we are dealing with a partially inferred poly expression |
237 owntype = shouldCheck ? resultInfo.pt : found; |
238 owntype = shouldCheck ? resultInfo.pt : found; |
290 || |
291 || |
291 !((base == null || |
292 !((base == null || |
292 (base.hasTag(IDENT) && TreeInfo.name(base) == names._this)) && |
293 (base.hasTag(IDENT) && TreeInfo.name(base) == names._this)) && |
293 isAssignableAsBlankFinal(v, env)))) { |
294 isAssignableAsBlankFinal(v, env)))) { |
294 if (v.isResourceVariable()) { //TWR resource |
295 if (v.isResourceVariable()) { //TWR resource |
295 log.error(pos, "try.resource.may.not.be.assigned", v); |
296 log.error(pos, Errors.TryResourceMayNotBeAssigned(v)); |
296 } else { |
297 } else { |
297 log.error(pos, "cant.assign.val.to.final.var", v); |
298 log.error(pos, Errors.CantAssignValToFinalVar(v)); |
298 } |
299 } |
299 } |
300 } |
300 } |
301 } |
301 |
302 |
302 /** Does tree represent a static reference to an identifier? |
303 /** Does tree represent a static reference to an identifier? |
866 boolean interfaceExpected, |
867 boolean interfaceExpected, |
867 boolean checkExtensible) { |
868 boolean checkExtensible) { |
868 final DiagnosticPosition pos = tree.hasTag(TYPEAPPLY) ? |
869 final DiagnosticPosition pos = tree.hasTag(TYPEAPPLY) ? |
869 (((JCTypeApply) tree).clazz).pos() : tree.pos(); |
870 (((JCTypeApply) tree).clazz).pos() : tree.pos(); |
870 if (t.tsym.isAnonymous()) { |
871 if (t.tsym.isAnonymous()) { |
871 log.error(pos, "cant.inherit.from.anon"); |
872 log.error(pos, Errors.CantInheritFromAnon); |
872 return types.createErrorType(t); |
873 return types.createErrorType(t); |
873 } |
874 } |
874 if (t.isErroneous()) |
875 if (t.isErroneous()) |
875 return t; |
876 return t; |
876 if (t.hasTag(TYPEVAR) && !classExpected && !interfaceExpected) { |
877 if (t.hasTag(TYPEVAR) && !classExpected && !interfaceExpected) { |
877 // check that type variable is already visible |
878 // check that type variable is already visible |
878 if (t.getUpperBound() == null) { |
879 if (t.getUpperBound() == null) { |
879 log.error(pos, "illegal.forward.ref"); |
880 log.error(pos, Errors.IllegalForwardRef); |
880 return types.createErrorType(t); |
881 return types.createErrorType(t); |
881 } |
882 } |
882 } else { |
883 } else { |
883 t = chk.checkClassType(pos, t, checkExtensible); |
884 t = chk.checkClassType(pos, t, checkExtensible); |
884 } |
885 } |
885 if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) { |
886 if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) { |
886 log.error(pos, "intf.expected.here"); |
887 log.error(pos, Errors.IntfExpectedHere); |
887 // return errType is necessary since otherwise there might |
888 // return errType is necessary since otherwise there might |
888 // be undetected cycles which cause attribution to loop |
889 // be undetected cycles which cause attribution to loop |
889 return types.createErrorType(t); |
890 return types.createErrorType(t); |
890 } else if (checkExtensible && |
891 } else if (checkExtensible && |
891 classExpected && |
892 classExpected && |
892 (t.tsym.flags() & INTERFACE) != 0) { |
893 (t.tsym.flags() & INTERFACE) != 0) { |
893 log.error(pos, "no.intf.expected.here"); |
894 log.error(pos, Errors.NoIntfExpectedHere); |
894 return types.createErrorType(t); |
895 return types.createErrorType(t); |
895 } |
896 } |
896 if (checkExtensible && |
897 if (checkExtensible && |
897 ((t.tsym.flags() & FINAL) != 0)) { |
898 ((t.tsym.flags() & FINAL) != 0)) { |
898 log.error(pos, |
899 log.error(pos, |
899 "cant.inherit.from.final", t.tsym); |
900 Errors.CantInheritFromFinal(t.tsym)); |
900 } |
901 } |
901 chk.checkNonCyclic(pos, t); |
902 chk.checkNonCyclic(pos, t); |
902 return t; |
903 return t; |
903 } |
904 } |
904 |
905 |
978 chk.checkOverrideClashes(tree.pos(), env.enclClass.type, m); |
979 chk.checkOverrideClashes(tree.pos(), env.enclClass.type, m); |
979 } |
980 } |
980 chk.checkOverride(env, tree, m); |
981 chk.checkOverride(env, tree, m); |
981 |
982 |
982 if (isDefaultMethod && types.overridesObjectMethod(m.enclClass(), m)) { |
983 if (isDefaultMethod && types.overridesObjectMethod(m.enclClass(), m)) { |
983 log.error(tree, "default.overrides.object.member", m.name, Kinds.kindName(m.location()), m.location()); |
984 log.error(tree, Errors.DefaultOverridesObjectMember(m.name, Kinds.kindName(m.location()), m.location())); |
984 } |
985 } |
985 |
986 |
986 // Enter all type parameters into the local method scope. |
987 // Enter all type parameters into the local method scope. |
987 for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
988 for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
988 localEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
989 localEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
992 (tree.params.nonEmpty() || |
993 (tree.params.nonEmpty() || |
993 tree.recvparam != null)) |
994 tree.recvparam != null)) |
994 log.error(tree.params.nonEmpty() ? |
995 log.error(tree.params.nonEmpty() ? |
995 tree.params.head.pos() : |
996 tree.params.head.pos() : |
996 tree.recvparam.pos(), |
997 tree.recvparam.pos(), |
997 "intf.annotation.members.cant.have.params"); |
998 Errors.IntfAnnotationMembersCantHaveParams); |
998 |
999 |
999 // Attribute all value parameters. |
1000 // Attribute all value parameters. |
1000 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
1001 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
1001 attribStat(l.head, localEnv); |
1002 attribStat(l.head, localEnv); |
1002 } |
1003 } |
1023 // annotation method checks |
1024 // annotation method checks |
1024 if ((owner.flags() & ANNOTATION) != 0) { |
1025 if ((owner.flags() & ANNOTATION) != 0) { |
1025 // annotation method cannot have throws clause |
1026 // annotation method cannot have throws clause |
1026 if (tree.thrown.nonEmpty()) { |
1027 if (tree.thrown.nonEmpty()) { |
1027 log.error(tree.thrown.head.pos(), |
1028 log.error(tree.thrown.head.pos(), |
1028 "throws.not.allowed.in.intf.annotation"); |
1029 Errors.ThrowsNotAllowedInIntfAnnotation); |
1029 } |
1030 } |
1030 // annotation method cannot declare type-parameters |
1031 // annotation method cannot declare type-parameters |
1031 if (tree.typarams.nonEmpty()) { |
1032 if (tree.typarams.nonEmpty()) { |
1032 log.error(tree.typarams.head.pos(), |
1033 log.error(tree.typarams.head.pos(), |
1033 "intf.annotation.members.cant.have.type.params"); |
1034 Errors.IntfAnnotationMembersCantHaveTypeParams); |
1034 } |
1035 } |
1035 // validate annotation method's return type (could be an annotation type) |
1036 // validate annotation method's return type (could be an annotation type) |
1036 chk.validateAnnotationType(tree.restype); |
1037 chk.validateAnnotationType(tree.restype); |
1037 // ensure that annotation method does not clash with members of Object/Annotation |
1038 // ensure that annotation method does not clash with members of Object/Annotation |
1038 chk.validateAnnotationMethod(tree.pos(), m); |
1039 chk.validateAnnotationMethod(tree.pos(), m); |
1046 // abstract, native, or interface methods, or for methods |
1047 // abstract, native, or interface methods, or for methods |
1047 // in a retrofit signature class. |
1048 // in a retrofit signature class. |
1048 if (tree.defaultValue != null) { |
1049 if (tree.defaultValue != null) { |
1049 if ((owner.flags() & ANNOTATION) == 0) |
1050 if ((owner.flags() & ANNOTATION) == 0) |
1050 log.error(tree.pos(), |
1051 log.error(tree.pos(), |
1051 "default.allowed.in.intf.annotation.member"); |
1052 Errors.DefaultAllowedInIntfAnnotationMember); |
1052 } |
1053 } |
1053 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0) |
1054 if (isDefaultMethod || (tree.sym.flags() & (ABSTRACT | NATIVE)) == 0) |
1054 log.error(tree.pos(), "missing.meth.body.or.decl.abstract"); |
1055 log.error(tree.pos(), Errors.MissingMethBodyOrDeclAbstract); |
1055 } else if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) { |
1056 } else if ((tree.sym.flags() & (ABSTRACT|DEFAULT|PRIVATE)) == ABSTRACT) { |
1056 if ((owner.flags() & INTERFACE) != 0) { |
1057 if ((owner.flags() & INTERFACE) != 0) { |
1057 log.error(tree.body.pos(), "intf.meth.cant.have.body"); |
1058 log.error(tree.body.pos(), Errors.IntfMethCantHaveBody); |
1058 } else { |
1059 } else { |
1059 log.error(tree.pos(), "abstract.meth.cant.have.body"); |
1060 log.error(tree.pos(), Errors.AbstractMethCantHaveBody); |
1060 } |
1061 } |
1061 } else if ((tree.mods.flags & NATIVE) != 0) { |
1062 } else if ((tree.mods.flags & NATIVE) != 0) { |
1062 log.error(tree.pos(), "native.meth.cant.have.body"); |
1063 log.error(tree.pos(), Errors.NativeMethCantHaveBody); |
1063 } else { |
1064 } else { |
1064 // Add an implicit super() call unless an explicit call to |
1065 // Add an implicit super() call unless an explicit call to |
1065 // super(...) or this(...) is given |
1066 // super(...) or this(...) is given |
1066 // or we are compiling class java.lang.Object. |
1067 // or we are compiling class java.lang.Object. |
1067 if (tree.name == names.init && owner.type != syms.objectType) { |
1068 if (tree.name == names.init && owner.type != syms.objectType) { |
1079 // enum constructors are not allowed to call super |
1080 // enum constructors are not allowed to call super |
1080 // directly, so make sure there aren't any super calls |
1081 // directly, so make sure there aren't any super calls |
1081 // in enum constructors, except in the compiler |
1082 // in enum constructors, except in the compiler |
1082 // generated one. |
1083 // generated one. |
1083 log.error(tree.body.stats.head.pos(), |
1084 log.error(tree.body.stats.head.pos(), |
1084 "call.to.super.not.allowed.in.enum.ctor", |
1085 Errors.CallToSuperNotAllowedInEnumCtor(env.enclClass.sym)); |
1085 env.enclClass.sym); |
|
1086 } |
1086 } |
1087 } |
1087 } |
1088 |
1088 |
1089 // Attribute all type annotations in the body |
1089 // Attribute all type annotations in the body |
1090 annotate.queueScanTreeAndTypeAnnotate(tree.body, localEnv, m, null); |
1090 annotate.queueScanTreeAndTypeAnnotate(tree.body, localEnv, m, null); |
1250 if (elemtype == null) { |
1250 if (elemtype == null) { |
1251 // or perhaps expr implements Iterable<T>? |
1251 // or perhaps expr implements Iterable<T>? |
1252 Type base = types.asSuper(exprType, syms.iterableType.tsym); |
1252 Type base = types.asSuper(exprType, syms.iterableType.tsym); |
1253 if (base == null) { |
1253 if (base == null) { |
1254 log.error(tree.expr.pos(), |
1254 log.error(tree.expr.pos(), |
1255 "foreach.not.applicable.to.type", |
1255 Errors.ForeachNotApplicableToType(exprType, |
1256 exprType, |
1256 Fragments.TypeReqArrayOrIterable)); |
1257 diags.fragment("type.req.array.or.iterable")); |
|
1258 elemtype = types.createErrorType(exprType); |
1257 elemtype = types.createErrorType(exprType); |
1259 } else { |
1258 } else { |
1260 List<Type> iterableParams = base.allparams(); |
1259 List<Type> iterableParams = base.allparams(); |
1261 elemtype = iterableParams.isEmpty() |
1260 elemtype = iterableParams.isEmpty() |
1262 ? syms.objectType |
1261 ? syms.objectType |
1277 // Check that label is not used in an enclosing statement |
1276 // Check that label is not used in an enclosing statement |
1278 Env<AttrContext> env1 = env; |
1277 Env<AttrContext> env1 = env; |
1279 while (env1 != null && !env1.tree.hasTag(CLASSDEF)) { |
1278 while (env1 != null && !env1.tree.hasTag(CLASSDEF)) { |
1280 if (env1.tree.hasTag(LABELLED) && |
1279 if (env1.tree.hasTag(LABELLED) && |
1281 ((JCLabeledStatement) env1.tree).label == tree.label) { |
1280 ((JCLabeledStatement) env1.tree).label == tree.label) { |
1282 log.error(tree.pos(), "label.already.in.use", |
1281 log.error(tree.pos(), |
1283 tree.label); |
1282 Errors.LabelAlreadyInUse(tree.label)); |
1284 break; |
1283 break; |
1285 } |
1284 } |
1286 env1 = env1.next; |
1285 env1 = env1.next; |
1287 } |
1286 } |
1288 |
1287 |
1299 try { |
1298 try { |
1300 |
1299 |
1301 boolean enumSwitch = (seltype.tsym.flags() & Flags.ENUM) != 0; |
1300 boolean enumSwitch = (seltype.tsym.flags() & Flags.ENUM) != 0; |
1302 boolean stringSwitch = types.isSameType(seltype, syms.stringType); |
1301 boolean stringSwitch = types.isSameType(seltype, syms.stringType); |
1303 if (stringSwitch && !allowStringsInSwitch) { |
1302 if (stringSwitch && !allowStringsInSwitch) { |
1304 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.selector.pos(), "string.switch.not.supported.in.source", sourceName); |
1303 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.selector.pos(), Errors.StringSwitchNotSupportedInSource(sourceName)); |
1305 } |
1304 } |
1306 if (!enumSwitch && !stringSwitch) |
1305 if (!enumSwitch && !stringSwitch) |
1307 seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); |
1306 seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); |
1308 |
1307 |
1309 // Attribute all cases and |
1308 // Attribute all cases and |
1314 JCCase c = l.head; |
1313 JCCase c = l.head; |
1315 if (c.pat != null) { |
1314 if (c.pat != null) { |
1316 if (enumSwitch) { |
1315 if (enumSwitch) { |
1317 Symbol sym = enumConstant(c.pat, seltype); |
1316 Symbol sym = enumConstant(c.pat, seltype); |
1318 if (sym == null) { |
1317 if (sym == null) { |
1319 log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum"); |
1318 log.error(c.pat.pos(), Errors.EnumLabelMustBeUnqualifiedEnum); |
1320 } else if (!labels.add(sym)) { |
1319 } else if (!labels.add(sym)) { |
1321 log.error(c.pos(), "duplicate.case.label"); |
1320 log.error(c.pos(), Errors.DuplicateCaseLabel); |
1322 } |
1321 } |
1323 } else { |
1322 } else { |
1324 Type pattype = attribExpr(c.pat, switchEnv, seltype); |
1323 Type pattype = attribExpr(c.pat, switchEnv, seltype); |
1325 if (!pattype.hasTag(ERROR)) { |
1324 if (!pattype.hasTag(ERROR)) { |
1326 if (pattype.constValue() == null) { |
1325 if (pattype.constValue() == null) { |
1327 log.error(c.pat.pos(), |
1326 log.error(c.pat.pos(), |
1328 (stringSwitch ? "string.const.req" : "const.expr.req")); |
1327 (stringSwitch ? "string.const.req" : "const.expr.req")); |
1329 } else if (!labels.add(pattype.constValue())) { |
1328 } else if (!labels.add(pattype.constValue())) { |
1330 log.error(c.pos(), "duplicate.case.label"); |
1329 log.error(c.pos(), Errors.DuplicateCaseLabel); |
1331 } |
1330 } |
1332 } |
1331 } |
1333 } |
1332 } |
1334 } else if (hasDefault) { |
1333 } else if (hasDefault) { |
1335 log.error(c.pos(), "duplicate.default.label"); |
1334 log.error(c.pos(), Errors.DuplicateDefaultLabel); |
1336 } else { |
1335 } else { |
1337 hasDefault = true; |
1336 hasDefault = true; |
1338 } |
1337 } |
1339 Env<AttrContext> caseEnv = |
1338 Env<AttrContext> caseEnv = |
1340 switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup())); |
1339 switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup())); |
1399 // Attribute resource declarations |
1398 // Attribute resource declarations |
1400 for (JCTree resource : tree.resources) { |
1399 for (JCTree resource : tree.resources) { |
1401 CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) { |
1400 CheckContext twrContext = new Check.NestedCheckContext(resultInfo.checkContext) { |
1402 @Override |
1401 @Override |
1403 public void report(DiagnosticPosition pos, JCDiagnostic details) { |
1402 public void report(DiagnosticPosition pos, JCDiagnostic details) { |
1404 chk.basicHandler.report(pos, diags.fragment("try.not.applicable.to.type", details)); |
1403 chk.basicHandler.report(pos, diags.fragment(Fragments.TryNotApplicableToType(details))); |
1405 } |
1404 } |
1406 }; |
1405 }; |
1407 ResultInfo twrResult = |
1406 ResultInfo twrResult = |
1408 new ResultInfo(KindSelector.VAR, |
1407 new ResultInfo(KindSelector.VAR, |
1409 syms.autoCloseableType, |
1408 syms.autoCloseableType, |
1479 } |
1478 } |
1480 if (close.kind == MTH && |
1479 if (close.kind == MTH && |
1481 close.overrides(syms.autoCloseableClose, resource.tsym, types, true) && |
1480 close.overrides(syms.autoCloseableClose, resource.tsym, types, true) && |
1482 chk.isHandled(syms.interruptedExceptionType, types.memberType(resource, close).getThrownTypes()) && |
1481 chk.isHandled(syms.interruptedExceptionType, types.memberType(resource, close).getThrownTypes()) && |
1483 env.info.lint.isEnabled(LintCategory.TRY)) { |
1482 env.info.lint.isEnabled(LintCategory.TRY)) { |
1484 log.warning(LintCategory.TRY, pos, "try.resource.throws.interrupted.exc", resource); |
1483 log.warning(LintCategory.TRY, pos, Warnings.TryResourceThrowsInterruptedExc(resource)); |
1485 } |
1484 } |
1486 } |
1485 } |
1487 } |
1486 } |
1488 |
1487 |
1489 public void visitConditional(JCConditional tree) { |
1488 public void visitConditional(JCConditional tree) { |
1494 isBooleanOrNumeric(env, tree)) ? |
1493 isBooleanOrNumeric(env, tree)) ? |
1495 PolyKind.STANDALONE : PolyKind.POLY; |
1494 PolyKind.STANDALONE : PolyKind.POLY; |
1496 |
1495 |
1497 if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) { |
1496 if (tree.polyKind == PolyKind.POLY && resultInfo.pt.hasTag(VOID)) { |
1498 //this means we are returning a poly conditional from void-compatible lambda expression |
1497 //this means we are returning a poly conditional from void-compatible lambda expression |
1499 resultInfo.checkContext.report(tree, diags.fragment("conditional.target.cant.be.void")); |
1498 resultInfo.checkContext.report(tree, diags.fragment(Fragments.ConditionalTargetCantBeVoid)); |
1500 result = tree.type = types.createErrorType(resultInfo.pt); |
1499 result = tree.type = types.createErrorType(resultInfo.pt); |
1501 return; |
1500 return; |
1502 } |
1501 } |
1503 |
1502 |
1504 ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ? |
1503 ResultInfo condInfo = tree.polyKind == PolyKind.STANDALONE ? |
1572 //this will use enclosing check context to check compatibility of |
1571 //this will use enclosing check context to check compatibility of |
1573 //subexpression against target type; if we are in a method check context, |
1572 //subexpression against target type; if we are in a method check context, |
1574 //depending on whether boxing is allowed, we could have incompatibilities |
1573 //depending on whether boxing is allowed, we could have incompatibilities |
1575 @Override |
1574 @Override |
1576 public void report(DiagnosticPosition pos, JCDiagnostic details) { |
1575 public void report(DiagnosticPosition pos, JCDiagnostic details) { |
1577 enclosingContext.report(pos, diags.fragment("incompatible.type.in.conditional", details)); |
1576 enclosingContext.report(pos, diags.fragment(Fragments.IncompatibleTypeInConditional(details))); |
1578 } |
1577 } |
1579 }; |
1578 }; |
1580 } |
1579 } |
1581 |
1580 |
1582 /** Compute the type of a conditional expression, after |
1581 /** Compute the type of a conditional expression, after |
1638 return elsetype.baseType(); |
1637 return elsetype.baseType(); |
1639 if (types.isSubtype(elsetype, thentype)) |
1638 if (types.isSubtype(elsetype, thentype)) |
1640 return thentype.baseType(); |
1639 return thentype.baseType(); |
1641 |
1640 |
1642 if (thentype.hasTag(VOID) || elsetype.hasTag(VOID)) { |
1641 if (thentype.hasTag(VOID) || elsetype.hasTag(VOID)) { |
1643 log.error(pos, "neither.conditional.subtype", |
1642 log.error(pos, |
1644 thentype, elsetype); |
1643 Errors.NeitherConditionalSubtype(thentype, |
|
1644 elsetype)); |
1645 return thentype.baseType(); |
1645 return thentype.baseType(); |
1646 } |
1646 } |
1647 |
1647 |
1648 // both are known to be reference types. The result is |
1648 // both are known to be reference types. The result is |
1649 // lub(thentype,elsetype). This cannot fail, as it will |
1649 // lub(thentype,elsetype). This cannot fail, as it will |
1718 if (tag == CONTINUE) { |
1718 if (tag == CONTINUE) { |
1719 if (!labelled.body.hasTag(DOLOOP) && |
1719 if (!labelled.body.hasTag(DOLOOP) && |
1720 !labelled.body.hasTag(WHILELOOP) && |
1720 !labelled.body.hasTag(WHILELOOP) && |
1721 !labelled.body.hasTag(FORLOOP) && |
1721 !labelled.body.hasTag(FORLOOP) && |
1722 !labelled.body.hasTag(FOREACHLOOP)) |
1722 !labelled.body.hasTag(FOREACHLOOP)) |
1723 log.error(pos, "not.loop.label", label); |
1723 log.error(pos, Errors.NotLoopLabel(label)); |
1724 // Found labelled statement target, now go inwards |
1724 // Found labelled statement target, now go inwards |
1725 // to next non-labelled tree. |
1725 // to next non-labelled tree. |
1726 return TreeInfo.referencedStatement(labelled); |
1726 return TreeInfo.referencedStatement(labelled); |
1727 } else { |
1727 } else { |
1728 return labelled; |
1728 return labelled; |
1745 default: |
1745 default: |
1746 } |
1746 } |
1747 env1 = env1.next; |
1747 env1 = env1.next; |
1748 } |
1748 } |
1749 if (label != null) |
1749 if (label != null) |
1750 log.error(pos, "undef.label", label); |
1750 log.error(pos, Errors.UndefLabel(label)); |
1751 else if (tag == CONTINUE) |
1751 else if (tag == CONTINUE) |
1752 log.error(pos, "cont.outside.loop"); |
1752 log.error(pos, Errors.ContOutsideLoop); |
1753 else |
1753 else |
1754 log.error(pos, "break.outside.switch.loop"); |
1754 log.error(pos, Errors.BreakOutsideSwitchLoop); |
1755 return null; |
1755 return null; |
1756 } |
1756 } |
1757 |
1757 |
1758 public void visitReturn(JCReturn tree) { |
1758 public void visitReturn(JCReturn tree) { |
1759 // Check that there is an enclosing method which is |
1759 // Check that there is an enclosing method which is |
1760 // nested within than the enclosing class. |
1760 // nested within than the enclosing class. |
1761 if (env.info.returnResult == null) { |
1761 if (env.info.returnResult == null) { |
1762 log.error(tree.pos(), "ret.outside.meth"); |
1762 log.error(tree.pos(), Errors.RetOutsideMeth); |
1763 } else { |
1763 } else { |
1764 // Attribute return expression, if it exists, and check that |
1764 // Attribute return expression, if it exists, and check that |
1765 // it conforms to result type of enclosing method. |
1765 // it conforms to result type of enclosing method. |
1766 if (tree.expr != null) { |
1766 if (tree.expr != null) { |
1767 if (env.info.returnResult.pt.hasTag(VOID)) { |
1767 if (env.info.returnResult.pt.hasTag(VOID)) { |
1768 env.info.returnResult.checkContext.report(tree.expr.pos(), |
1768 env.info.returnResult.checkContext.report(tree.expr.pos(), |
1769 diags.fragment("unexpected.ret.val")); |
1769 diags.fragment(Fragments.UnexpectedRetVal)); |
1770 } |
1770 } |
1771 attribTree(tree.expr, env, env.info.returnResult); |
1771 attribTree(tree.expr, env, env.info.returnResult); |
1772 } else if (!env.info.returnResult.pt.hasTag(VOID) && |
1772 } else if (!env.info.returnResult.pt.hasTag(VOID) && |
1773 !env.info.returnResult.pt.hasTag(NONE)) { |
1773 !env.info.returnResult.pt.hasTag(NONE)) { |
1774 env.info.returnResult.checkContext.report(tree.pos(), |
1774 env.info.returnResult.checkContext.report(tree.pos(), |
1775 diags.fragment("missing.ret.val")); |
1775 diags.fragment(Fragments.MissingRetVal(env.info.returnResult.pt))); |
1776 } |
1776 } |
1777 } |
1777 } |
1778 result = null; |
1778 result = null; |
1779 } |
1779 } |
1780 |
1780 |
1832 // Variable `site' points to the class in which the called |
1832 // Variable `site' points to the class in which the called |
1833 // constructor is defined. |
1833 // constructor is defined. |
1834 Type site = env.enclClass.sym.type; |
1834 Type site = env.enclClass.sym.type; |
1835 if (methName == names._super) { |
1835 if (methName == names._super) { |
1836 if (site == syms.objectType) { |
1836 if (site == syms.objectType) { |
1837 log.error(tree.meth.pos(), "no.superclass", site); |
1837 log.error(tree.meth.pos(), Errors.NoSuperclass(site)); |
1838 site = types.createErrorType(syms.objectType); |
1838 site = types.createErrorType(syms.objectType); |
1839 } else { |
1839 } else { |
1840 site = types.supertype(site); |
1840 site = types.supertype(site); |
1841 } |
1841 } |
1842 } |
1842 } |
1863 // of an appropriate implicit qualifier. |
1863 // of an appropriate implicit qualifier. |
1864 rs.resolveImplicitThis(tree.meth.pos(), |
1864 rs.resolveImplicitThis(tree.meth.pos(), |
1865 localEnv, site, true); |
1865 localEnv, site, true); |
1866 } |
1866 } |
1867 } else if (tree.meth.hasTag(SELECT)) { |
1867 } else if (tree.meth.hasTag(SELECT)) { |
1868 log.error(tree.meth.pos(), "illegal.qual.not.icls", |
1868 log.error(tree.meth.pos(), |
1869 site.tsym); |
1869 Errors.IllegalQualNotIcls(site.tsym)); |
1870 } |
1870 } |
1871 |
1871 |
1872 // if we're calling a java.lang.Enum constructor, |
1872 // if we're calling a java.lang.Enum constructor, |
1873 // prefix the implicit String and int parameters |
1873 // prefix the implicit String and int parameters |
1874 if (site.tsym == syms.enumSym) |
1874 if (site.tsym == syms.enumSym) |
1966 JCBlock body = enclMethod.body; |
1966 JCBlock body = enclMethod.body; |
1967 if (body.stats.head.hasTag(EXEC) && |
1967 if (body.stats.head.hasTag(EXEC) && |
1968 ((JCExpressionStatement) body.stats.head).expr == tree) |
1968 ((JCExpressionStatement) body.stats.head).expr == tree) |
1969 return true; |
1969 return true; |
1970 } |
1970 } |
1971 log.error(tree.pos(),"call.must.be.first.stmt.in.ctor", |
1971 log.error(tree.pos(), |
1972 TreeInfo.name(tree.meth)); |
1972 Errors.CallMustBeFirstStmtInCtor(TreeInfo.name(tree.meth))); |
1973 return false; |
1973 return false; |
1974 } |
1974 } |
1975 |
1975 |
1976 /** Obtain a method type with given argument types. |
1976 /** Obtain a method type with given argument types. |
1977 */ |
1977 */ |
2078 if (annoclazzid != null) { |
2078 if (annoclazzid != null) { |
2079 annoclazzid.type = clazzid.type; |
2079 annoclazzid.type = clazzid.type; |
2080 } |
2080 } |
2081 if (!clazztype.isErroneous()) { |
2081 if (!clazztype.isErroneous()) { |
2082 if (cdef != null && clazztype.tsym.isInterface()) { |
2082 if (cdef != null && clazztype.tsym.isInterface()) { |
2083 log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new"); |
2083 log.error(tree.encl.pos(), Errors.AnonClassImplIntfNoQualForNew); |
2084 } else if (clazztype.tsym.isStatic()) { |
2084 } else if (clazztype.tsym.isStatic()) { |
2085 log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym); |
2085 log.error(tree.encl.pos(), Errors.QualifiedNewOfStaticClass(clazztype.tsym)); |
2086 } |
2086 } |
2087 } |
2087 } |
2088 } else if (!clazztype.tsym.isInterface() && |
2088 } else if (!clazztype.tsym.isInterface() && |
2089 clazztype.getEnclosingType().hasTag(CLASS)) { |
2089 clazztype.getEnclosingType().hasTag(CLASS)) { |
2090 // Check for the existence of an apropos outer instance |
2090 // Check for the existence of an apropos outer instance |
2103 // Enums may not be instantiated except implicitly |
2103 // Enums may not be instantiated except implicitly |
2104 if ((clazztype.tsym.flags_field & Flags.ENUM) != 0 && |
2104 if ((clazztype.tsym.flags_field & Flags.ENUM) != 0 && |
2105 (!env.tree.hasTag(VARDEF) || |
2105 (!env.tree.hasTag(VARDEF) || |
2106 (((JCVariableDecl) env.tree).mods.flags & Flags.ENUM) == 0 || |
2106 (((JCVariableDecl) env.tree).mods.flags & Flags.ENUM) == 0 || |
2107 ((JCVariableDecl) env.tree).init != tree)) |
2107 ((JCVariableDecl) env.tree).init != tree)) |
2108 log.error(tree.pos(), "enum.cant.be.instantiated"); |
2108 log.error(tree.pos(), Errors.EnumCantBeInstantiated); |
2109 |
2109 |
2110 boolean isSpeculativeDiamondInferenceRound = TreeInfo.isDiamond(tree) && |
2110 boolean isSpeculativeDiamondInferenceRound = TreeInfo.isDiamond(tree) && |
2111 resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE; |
2111 resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE; |
2112 boolean skipNonDiamondPath = false; |
2112 boolean skipNonDiamondPath = false; |
2113 // Check that class is not abstract |
2113 // Check that class is not abstract |
2114 if (cdef == null && !isSpeculativeDiamondInferenceRound && // class body may be nulled out in speculative tree copy |
2114 if (cdef == null && !isSpeculativeDiamondInferenceRound && // class body may be nulled out in speculative tree copy |
2115 (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
2115 (clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
2116 log.error(tree.pos(), "abstract.cant.be.instantiated", |
2116 log.error(tree.pos(), |
2117 clazztype.tsym); |
2117 Errors.AbstractCantBeInstantiated(clazztype.tsym)); |
2118 skipNonDiamondPath = true; |
2118 skipNonDiamondPath = true; |
2119 } else if (cdef != null && clazztype.tsym.isInterface()) { |
2119 } else if (cdef != null && clazztype.tsym.isInterface()) { |
2120 // Check that no constructor arguments are given to |
2120 // Check that no constructor arguments are given to |
2121 // anonymous classes implementing an interface |
2121 // anonymous classes implementing an interface |
2122 if (!argtypes.isEmpty()) |
2122 if (!argtypes.isEmpty()) |
2123 log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args"); |
2123 log.error(tree.args.head.pos(), Errors.AnonClassImplIntfNoArgs); |
2124 |
2124 |
2125 if (!typeargtypes.isEmpty()) |
2125 if (!typeargtypes.isEmpty()) |
2126 log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs"); |
2126 log.error(tree.typeargs.head.pos(), Errors.AnonClassImplIntfNoTypeargs); |
2127 |
2127 |
2128 // Error recovery: pretend no arguments were supplied. |
2128 // Error recovery: pretend no arguments were supplied. |
2129 argtypes = List.nil(); |
2129 argtypes = List.nil(); |
2130 typeargtypes = List.nil(); |
2130 typeargtypes = List.nil(); |
2131 skipNonDiamondPath = true; |
2131 skipNonDiamondPath = true; |
2340 CheckContext diamondContext(JCNewClass clazz, TypeSymbol tsym, CheckContext checkContext) { |
2340 CheckContext diamondContext(JCNewClass clazz, TypeSymbol tsym, CheckContext checkContext) { |
2341 return new Check.NestedCheckContext(checkContext) { |
2341 return new Check.NestedCheckContext(checkContext) { |
2342 @Override |
2342 @Override |
2343 public void report(DiagnosticPosition _unused, JCDiagnostic details) { |
2343 public void report(DiagnosticPosition _unused, JCDiagnostic details) { |
2344 enclosingContext.report(clazz.clazz, |
2344 enclosingContext.report(clazz.clazz, |
2345 diags.fragment("cant.apply.diamond.1", diags.fragment("diamond", tsym), details)); |
2345 diags.fragment(Fragments.CantApplyDiamond1(Fragments.Diamond(tsym), details))); |
2346 } |
2346 } |
2347 }; |
2347 }; |
2348 } |
2348 } |
2349 |
2349 |
2350 /** Make an attributed null check tree. |
2350 /** Make an attributed null check tree. |
2381 // this is allowed only if the prototype is an array |
2381 // this is allowed only if the prototype is an array |
2382 if (pt().hasTag(ARRAY)) { |
2382 if (pt().hasTag(ARRAY)) { |
2383 elemtype = types.elemtype(pt()); |
2383 elemtype = types.elemtype(pt()); |
2384 } else { |
2384 } else { |
2385 if (!pt().hasTag(ERROR)) { |
2385 if (!pt().hasTag(ERROR)) { |
2386 log.error(tree.pos(), "illegal.initializer.for.type", |
2386 log.error(tree.pos(), |
2387 pt()); |
2387 Errors.IllegalInitializerForType(pt())); |
2388 } |
2388 } |
2389 elemtype = types.createErrorType(pt()); |
2389 elemtype = types.createErrorType(pt()); |
2390 } |
2390 } |
2391 } |
2391 } |
2392 if (tree.elems != null) { |
2392 if (tree.elems != null) { |
2393 attribExprs(tree.elems, localEnv, elemtype); |
2393 attribExprs(tree.elems, localEnv, elemtype); |
2394 owntype = new ArrayType(elemtype, syms.arrayClass); |
2394 owntype = new ArrayType(elemtype, syms.arrayClass); |
2395 } |
2395 } |
2396 if (!types.isReifiable(elemtype)) |
2396 if (!types.isReifiable(elemtype)) |
2397 log.error(tree.pos(), "generic.array.creation"); |
2397 log.error(tree.pos(), Errors.GenericArrayCreation); |
2398 result = check(tree, owntype, KindSelector.VAL, resultInfo); |
2398 result = check(tree, owntype, KindSelector.VAL, resultInfo); |
2399 } |
2399 } |
2400 |
2400 |
2401 /* |
2401 /* |
2402 * A lambda expression can only be attributed when a target-type is available. |
2402 * A lambda expression can only be attributed when a target-type is available. |
2407 @Override |
2407 @Override |
2408 public void visitLambda(final JCLambda that) { |
2408 public void visitLambda(final JCLambda that) { |
2409 if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) { |
2409 if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) { |
2410 if (pt().hasTag(NONE)) { |
2410 if (pt().hasTag(NONE)) { |
2411 //lambda only allowed in assignment or method invocation/cast context |
2411 //lambda only allowed in assignment or method invocation/cast context |
2412 log.error(that.pos(), "unexpected.lambda"); |
2412 log.error(that.pos(), Errors.UnexpectedLambda); |
2413 } |
2413 } |
2414 result = that.type = types.createErrorType(pt()); |
2414 result = that.type = types.createErrorType(pt()); |
2415 return; |
2415 return; |
2416 } |
2416 } |
2417 //create an environment for attribution of the lambda expression |
2417 //create an environment for attribution of the lambda expression |
2441 |
2441 |
2442 setFunctionalInfo(localEnv, that, pt(), lambdaType, currentTarget, resultInfo.checkContext); |
2442 setFunctionalInfo(localEnv, that, pt(), lambdaType, currentTarget, resultInfo.checkContext); |
2443 |
2443 |
2444 if (lambdaType.hasTag(FORALL)) { |
2444 if (lambdaType.hasTag(FORALL)) { |
2445 //lambda expression target desc cannot be a generic method |
2445 //lambda expression target desc cannot be a generic method |
2446 resultInfo.checkContext.report(that, diags.fragment("invalid.generic.lambda.target", |
2446 Fragment msg = Fragments.InvalidGenericLambdaTarget(lambdaType, |
2447 lambdaType, kindName(currentTarget.tsym), currentTarget.tsym)); |
2447 kindName(currentTarget.tsym), |
|
2448 currentTarget.tsym); |
|
2449 resultInfo.checkContext.report(that, diags.fragment(msg)); |
2448 result = that.type = types.createErrorType(pt()); |
2450 result = that.type = types.createErrorType(pt()); |
2449 return; |
2451 return; |
2450 } |
2452 } |
2451 |
2453 |
2452 if (that.paramKind == JCLambda.ParameterKind.IMPLICIT) { |
2454 if (that.paramKind == JCLambda.ParameterKind.IMPLICIT) { |
2574 currentTarget = Type.recoveryType; |
2576 currentTarget = Type.recoveryType; |
2575 lambdaType = fallbackDescriptorType(that); |
2577 lambdaType = fallbackDescriptorType(that); |
2576 } |
2578 } |
2577 if (that.hasTag(LAMBDA) && lambdaType.hasTag(FORALL)) { |
2579 if (that.hasTag(LAMBDA) && lambdaType.hasTag(FORALL)) { |
2578 //lambda expression target desc cannot be a generic method |
2580 //lambda expression target desc cannot be a generic method |
2579 resultInfo.checkContext.report(that, diags.fragment("invalid.generic.lambda.target", |
2581 Fragment msg = Fragments.InvalidGenericLambdaTarget(lambdaType, |
2580 lambdaType, kindName(currentTarget.tsym), currentTarget.tsym)); |
2582 kindName(currentTarget.tsym), |
|
2583 currentTarget.tsym); |
|
2584 resultInfo.checkContext.report(that, diags.fragment(msg)); |
2581 currentTarget = types.createErrorType(pt()); |
2585 currentTarget = types.createErrorType(pt()); |
2582 } |
2586 } |
2583 return new TargetInfo(currentTarget, lambdaType); |
2587 return new TargetInfo(currentTarget, lambdaType); |
2584 } |
2588 } |
2585 |
2589 |
2638 notionalIntf.tsym.flags_field |= INTERFACE; |
2642 notionalIntf.tsym.flags_field |= INTERFACE; |
2639 return notionalIntf.tsym; |
2643 return notionalIntf.tsym; |
2640 } |
2644 } |
2641 |
2645 |
2642 private void reportIntersectionError(DiagnosticPosition pos, String key, Object... args) { |
2646 private void reportIntersectionError(DiagnosticPosition pos, String key, Object... args) { |
2643 resultInfo.checkContext.report(pos, diags.fragment("bad.intersection.target.for.functional.expr", |
2647 resultInfo.checkContext.report(pos, |
2644 diags.fragment(key, args))); |
2648 diags.fragment(Fragments.BadIntersectionTargetForFunctionalExpr(diags.fragment(key, args)))); |
2645 } |
2649 } |
2646 }; |
2650 }; |
2647 |
2651 |
2648 private Type fallbackDescriptorType(JCExpression tree) { |
2652 private Type fallbackDescriptorType(JCExpression tree) { |
2649 switch (tree.getTag()) { |
2653 switch (tree.getTag()) { |
2701 return chk.basicHandler.compatible(inferenceContext().asUndetVar(found), inferenceContext().asUndetVar(req), warn); |
2705 return chk.basicHandler.compatible(inferenceContext().asUndetVar(found), inferenceContext().asUndetVar(req), warn); |
2702 } |
2706 } |
2703 |
2707 |
2704 @Override |
2708 @Override |
2705 public void report(DiagnosticPosition pos, JCDiagnostic details) { |
2709 public void report(DiagnosticPosition pos, JCDiagnostic details) { |
2706 enclosingContext.report(pos, diags.fragment("incompatible.ret.type.in.lambda", details)); |
2710 enclosingContext.report(pos, diags.fragment(Fragments.IncompatibleRetTypeInLambda(details))); |
2707 } |
2711 } |
2708 } |
2712 } |
2709 |
2713 |
2710 class ExpressionLambdaReturnContext extends FunctionalReturnContext { |
2714 class ExpressionLambdaReturnContext extends FunctionalReturnContext { |
2711 |
2715 |
2762 //values, we must ensure that void/value compatibility is correct; |
2766 //values, we must ensure that void/value compatibility is correct; |
2763 //this amounts at checking that, if a lambda body can complete normally, |
2767 //this amounts at checking that, if a lambda body can complete normally, |
2764 //the descriptor's return type must be void |
2768 //the descriptor's return type must be void |
2765 if (tree.getBodyKind() == JCLambda.BodyKind.STATEMENT && tree.canCompleteNormally && |
2769 if (tree.getBodyKind() == JCLambda.BodyKind.STATEMENT && tree.canCompleteNormally && |
2766 !returnType.hasTag(VOID) && returnType != Type.recoveryType) { |
2770 !returnType.hasTag(VOID) && returnType != Type.recoveryType) { |
2767 checkContext.report(tree, diags.fragment("incompatible.ret.type.in.lambda", |
2771 Fragment msg = |
2768 diags.fragment("missing.ret.val", returnType))); |
2772 Fragments.IncompatibleRetTypeInLambda(Fragments.MissingRetVal(returnType)); |
|
2773 checkContext.report(tree, |
|
2774 diags.fragment(msg)); |
2769 } |
2775 } |
2770 |
2776 |
2771 List<Type> argTypes = checkContext.inferenceContext().asUndetVars(descriptor.getParameterTypes()); |
2777 List<Type> argTypes = checkContext.inferenceContext().asUndetVars(descriptor.getParameterTypes()); |
2772 if (!types.isSameTypes(argTypes, TreeInfo.types(tree.params))) { |
2778 if (!types.isSameTypes(argTypes, TreeInfo.types(tree.params))) { |
2773 checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda")); |
2779 checkContext.report(tree, diags.fragment(Fragments.IncompatibleArgTypesInLambda)); |
2774 } |
2780 } |
2775 } |
2781 } |
2776 |
2782 |
2777 /* Map to hold 'fake' clinit methods. If a lambda is used to initialize a |
2783 /* Map to hold 'fake' clinit methods. If a lambda is used to initialize a |
2778 * static field and that lambda has type annotations, these annotations will |
2784 * static field and that lambda has type annotations, these annotations will |
2839 @Override |
2845 @Override |
2840 public void visitReference(final JCMemberReference that) { |
2846 public void visitReference(final JCMemberReference that) { |
2841 if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) { |
2847 if (pt().isErroneous() || (pt().hasTag(NONE) && pt() != Type.recoveryType)) { |
2842 if (pt().hasTag(NONE)) { |
2848 if (pt().hasTag(NONE)) { |
2843 //method reference only allowed in assignment or method invocation/cast context |
2849 //method reference only allowed in assignment or method invocation/cast context |
2844 log.error(that.pos(), "unexpected.mref"); |
2850 log.error(that.pos(), Errors.UnexpectedMref); |
2845 } |
2851 } |
2846 result = that.type = types.createErrorType(pt()); |
2852 result = that.type = types.createErrorType(pt()); |
2847 return; |
2853 return; |
2848 } |
2854 } |
2849 final Env<AttrContext> localEnv = env.dup(that); |
2855 final Env<AttrContext> localEnv = env.dup(that); |
2855 if (that.getMode() == JCMemberReference.ReferenceMode.NEW) { |
2861 if (that.getMode() == JCMemberReference.ReferenceMode.NEW) { |
2856 exprType = chk.checkConstructorRefType(that.expr, exprType); |
2862 exprType = chk.checkConstructorRefType(that.expr, exprType); |
2857 if (!exprType.isErroneous() && |
2863 if (!exprType.isErroneous() && |
2858 exprType.isRaw() && |
2864 exprType.isRaw() && |
2859 that.typeargs != null) { |
2865 that.typeargs != null) { |
2860 log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()), |
2866 log.error(that.expr.pos(), |
2861 diags.fragment("mref.infer.and.explicit.params")); |
2867 Errors.InvalidMref(Kinds.kindName(that.getMode()), |
|
2868 Fragments.MrefInferAndExplicitParams)); |
2862 exprType = types.createErrorType(exprType); |
2869 exprType = types.createErrorType(exprType); |
2863 } |
2870 } |
2864 } |
2871 } |
2865 |
2872 |
2866 if (exprType.isErroneous()) { |
2873 if (exprType.isErroneous()) { |
2988 } |
2995 } |
2989 |
2996 |
2990 if (that.sym.isStatic() && TreeInfo.isStaticSelector(that.expr, names) && |
2997 if (that.sym.isStatic() && TreeInfo.isStaticSelector(that.expr, names) && |
2991 exprType.getTypeArguments().nonEmpty()) { |
2998 exprType.getTypeArguments().nonEmpty()) { |
2992 //static ref with class type-args |
2999 //static ref with class type-args |
2993 log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()), |
3000 log.error(that.expr.pos(), |
2994 diags.fragment("static.mref.with.targs")); |
3001 Errors.InvalidMref(Kinds.kindName(that.getMode()), |
|
3002 Fragments.StaticMrefWithTargs)); |
2995 result = that.type = types.createErrorType(currentTarget); |
3003 result = that.type = types.createErrorType(currentTarget); |
2996 return; |
3004 return; |
2997 } |
3005 } |
2998 |
3006 |
2999 if (!refSym.isStatic() && that.kind == JCMemberReference.ReferenceKind.SUPER) { |
3007 if (!refSym.isStatic() && that.kind == JCMemberReference.ReferenceKind.SUPER) { |
3086 incompatibleReturnType = null; |
3094 incompatibleReturnType = null; |
3087 } |
3095 } |
3088 } |
3096 } |
3089 |
3097 |
3090 if (incompatibleReturnType != null) { |
3098 if (incompatibleReturnType != null) { |
3091 checkContext.report(tree, diags.fragment("incompatible.ret.type.in.mref", |
3099 Fragment msg = |
3092 diags.fragment("inconvertible.types", resType, descriptor.getReturnType()))); |
3100 Fragments.IncompatibleRetTypeInMref(Fragments.InconvertibleTypes(resType, descriptor.getReturnType())); |
|
3101 checkContext.report(tree, diags.fragment(msg)); |
3093 } else { |
3102 } else { |
3094 if (inferenceContext.free(refType)) { |
3103 if (inferenceContext.free(refType)) { |
3095 // we need to wait for inference to finish and then replace inference vars in the referent type |
3104 // we need to wait for inference to finish and then replace inference vars in the referent type |
3096 inferenceContext.addFreeTypeListener(List.of(refType), |
3105 inferenceContext.addFreeTypeListener(List.of(refType), |
3097 instantiatedContext -> { |
3106 instantiatedContext -> { |
3103 } |
3112 } |
3104 |
3113 |
3105 if (!speculativeAttr) { |
3114 if (!speculativeAttr) { |
3106 List<Type> thrownTypes = inferenceContext.asUndetVars(descriptor.getThrownTypes()); |
3115 List<Type> thrownTypes = inferenceContext.asUndetVars(descriptor.getThrownTypes()); |
3107 if (chk.unhandled(refType.getThrownTypes(), thrownTypes).nonEmpty()) { |
3116 if (chk.unhandled(refType.getThrownTypes(), thrownTypes).nonEmpty()) { |
3108 log.error(tree, "incompatible.thrown.types.in.mref", refType.getThrownTypes()); |
3117 log.error(tree, Errors.IncompatibleThrownTypesInMref(refType.getThrownTypes())); |
3109 } |
3118 } |
3110 //18.2.5: "In addition, for all j (1 <= j <= n), the constraint reduces to the bound throws Ej" |
3119 //18.2.5: "In addition, for all j (1 <= j <= n), the constraint reduces to the bound throws Ej" |
3111 thrownTypes.stream() |
3120 thrownTypes.stream() |
3112 .filter(t -> t.hasTag(UNDETVAR)) |
3121 .filter(t -> t.hasTag(UNDETVAR)) |
3113 .forEach(t -> ((UndetVar)t).setThrow()); |
3122 .forEach(t -> ((UndetVar)t).setThrow()); |
3174 public void visitParens(JCParens tree) { |
3183 public void visitParens(JCParens tree) { |
3175 Type owntype = attribTree(tree.expr, env, resultInfo); |
3184 Type owntype = attribTree(tree.expr, env, resultInfo); |
3176 result = check(tree, owntype, pkind(), resultInfo); |
3185 result = check(tree, owntype, pkind(), resultInfo); |
3177 Symbol sym = TreeInfo.symbol(tree); |
3186 Symbol sym = TreeInfo.symbol(tree); |
3178 if (sym != null && sym.kind.matches(KindSelector.TYP_PCK)) |
3187 if (sym != null && sym.kind.matches(KindSelector.TYP_PCK)) |
3179 log.error(tree.pos(), "illegal.start.of.type"); |
3188 log.error(tree.pos(), Errors.IllegalStartOfType); |
3180 } |
3189 } |
3181 |
3190 |
3182 public void visitAssign(JCAssign tree) { |
3191 public void visitAssign(JCAssign tree) { |
3183 Type owntype = attribTree(tree.lhs, env.dup(tree), varAssignmentInfo); |
3192 Type owntype = attribTree(tree.lhs, env.dup(tree), varAssignmentInfo); |
3184 Type capturedType = capture(owntype); |
3193 Type capturedType = capture(owntype); |
3253 // Check that argument types of a reference ==, != are |
3262 // Check that argument types of a reference ==, != are |
3254 // castable to each other, (JLS 15.21). Note: unboxing |
3263 // castable to each other, (JLS 15.21). Note: unboxing |
3255 // comparisons will not have an acmp* opc at this point. |
3264 // comparisons will not have an acmp* opc at this point. |
3256 if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) { |
3265 if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) { |
3257 if (!types.isCastable(left, right, new Warner(tree.pos()))) { |
3266 if (!types.isCastable(left, right, new Warner(tree.pos()))) { |
3258 log.error(tree.pos(), "incomparable.types", left, right); |
3267 log.error(tree.pos(), Errors.IncomparableTypes(left, right)); |
3259 } |
3268 } |
3260 } |
3269 } |
3261 |
3270 |
3262 chk.checkDivZero(tree.rhs.pos(), operator, right); |
3271 chk.checkDivZero(tree.rhs.pos(), operator, right); |
3263 } |
3272 } |
3302 Type clazztype = attribType(tree.clazz, env); |
3311 Type clazztype = attribType(tree.clazz, env); |
3303 if (!clazztype.hasTag(TYPEVAR)) { |
3312 if (!clazztype.hasTag(TYPEVAR)) { |
3304 clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype); |
3313 clazztype = chk.checkClassOrArrayType(tree.clazz.pos(), clazztype); |
3305 } |
3314 } |
3306 if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) { |
3315 if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) { |
3307 log.error(tree.clazz.pos(), "illegal.generic.type.for.instof"); |
3316 log.error(tree.clazz.pos(), Errors.IllegalGenericTypeForInstof); |
3308 clazztype = types.createErrorType(clazztype); |
3317 clazztype = types.createErrorType(clazztype); |
3309 } |
3318 } |
3310 chk.validate(tree.clazz, env, false); |
3319 chk.validate(tree.clazz, env, false); |
3311 chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
3320 chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
3312 result = check(tree, syms.booleanType, KindSelector.VAL, resultInfo); |
3321 result = check(tree, syms.booleanType, KindSelector.VAL, resultInfo); |
3317 Type atype = attribExpr(tree.indexed, env); |
3326 Type atype = attribExpr(tree.indexed, env); |
3318 attribExpr(tree.index, env, syms.intType); |
3327 attribExpr(tree.index, env, syms.intType); |
3319 if (types.isArray(atype)) |
3328 if (types.isArray(atype)) |
3320 owntype = types.elemtype(atype); |
3329 owntype = types.elemtype(atype); |
3321 else if (!atype.hasTag(ERROR)) |
3330 else if (!atype.hasTag(ERROR)) |
3322 log.error(tree.pos(), "array.req.but.found", atype); |
3331 log.error(tree.pos(), Errors.ArrayReqButFound(atype)); |
3323 if (!pkind().contains(KindSelector.VAL)) |
3332 if (!pkind().contains(KindSelector.VAL)) |
3324 owntype = capture(owntype); |
3333 owntype = capture(owntype); |
3325 result = check(tree, owntype, KindSelector.VAR, resultInfo); |
3334 result = check(tree, owntype, KindSelector.VAR, resultInfo); |
3326 } |
3335 } |
3327 |
3336 |
3430 if (skind == KindSelector.TYP) { |
3439 if (skind == KindSelector.TYP) { |
3431 Type elt = site; |
3440 Type elt = site; |
3432 while (elt.hasTag(ARRAY)) |
3441 while (elt.hasTag(ARRAY)) |
3433 elt = ((ArrayType)elt).elemtype; |
3442 elt = ((ArrayType)elt).elemtype; |
3434 if (elt.hasTag(TYPEVAR)) { |
3443 if (elt.hasTag(TYPEVAR)) { |
3435 log.error(tree.pos(), "type.var.cant.be.deref"); |
3444 log.error(tree.pos(), Errors.TypeVarCantBeDeref); |
3436 result = tree.type = types.createErrorType(tree.name, site.tsym, site); |
3445 result = tree.type = types.createErrorType(tree.name, site.tsym, site); |
3437 tree.sym = tree.type.tsym; |
3446 tree.sym = tree.type.tsym; |
3438 return ; |
3447 return ; |
3439 } |
3448 } |
3440 } |
3449 } |
3450 |
3459 |
3451 // Determine the symbol represented by the selection. |
3460 // Determine the symbol represented by the selection. |
3452 env.info.pendingResolutionPhase = null; |
3461 env.info.pendingResolutionPhase = null; |
3453 Symbol sym = selectSym(tree, sitesym, site, env, resultInfo); |
3462 Symbol sym = selectSym(tree, sitesym, site, env, resultInfo); |
3454 if (sym.kind == VAR && sym.name != names._super && env.info.defaultSuperCallSite != null) { |
3463 if (sym.kind == VAR && sym.name != names._super && env.info.defaultSuperCallSite != null) { |
3455 log.error(tree.selected.pos(), "not.encl.class", site.tsym); |
3464 log.error(tree.selected.pos(), Errors.NotEnclClass(site.tsym)); |
3456 sym = syms.errSymbol; |
3465 sym = syms.errSymbol; |
3457 } |
3466 } |
3458 if (sym.exists() && !isType(sym) && pkind().contains(KindSelector.TYP_PCK)) { |
3467 if (sym.exists() && !isType(sym) && pkind().contains(KindSelector.TYP_PCK)) { |
3459 site = capture(site); |
3468 site = capture(site); |
3460 sym = selectSym(tree, sitesym, site, env, resultInfo); |
3469 sym = selectSym(tree, sitesym, site, env, resultInfo); |
3485 ((VarSymbol)sitesym).isResourceVariable() && |
3494 ((VarSymbol)sitesym).isResourceVariable() && |
3486 sym.kind == MTH && |
3495 sym.kind == MTH && |
3487 sym.name.equals(names.close) && |
3496 sym.name.equals(names.close) && |
3488 sym.overrides(syms.autoCloseableClose, sitesym.type.tsym, types, true) && |
3497 sym.overrides(syms.autoCloseableClose, sitesym.type.tsym, types, true) && |
3489 env.info.lint.isEnabled(LintCategory.TRY)) { |
3498 env.info.lint.isEnabled(LintCategory.TRY)) { |
3490 log.warning(LintCategory.TRY, tree, "try.explicit.close.call"); |
3499 log.warning(LintCategory.TRY, tree, Warnings.TryExplicitCloseCall); |
3491 } |
3500 } |
3492 |
3501 |
3493 // Disallow selecting a type from an expression |
3502 // Disallow selecting a type from an expression |
3494 if (isType(sym) && (sitesym == null || !sitesym.kind.matches(KindSelector.TYP_PCK))) { |
3503 if (isType(sym) && (sitesym == null || !sitesym.kind.matches(KindSelector.TYP_PCK))) { |
3495 tree.type = check(tree.selected, pt(), |
3504 tree.type = check(tree.selected, pt(), |
3515 tree.pos(), site, sym.name, true); |
3524 tree.pos(), site, sym.name, true); |
3516 } |
3525 } |
3517 } |
3526 } |
3518 if (!allowStaticInterfaceMethods && sitesym.isInterface() && |
3527 if (!allowStaticInterfaceMethods && sitesym.isInterface() && |
3519 sym.isStatic() && sym.kind == MTH) { |
3528 sym.isStatic() && sym.kind == MTH) { |
3520 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.pos(), "static.intf.method.invoke.not.supported.in.source", sourceName); |
3529 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.pos(), Errors.StaticIntfMethodInvokeNotSupportedInSource(sourceName)); |
3521 } |
3530 } |
3522 } else if (sym.kind != ERR && |
3531 } else if (sym.kind != ERR && |
3523 (sym.flags() & STATIC) != 0 && |
3532 (sym.flags() & STATIC) != 0 && |
3524 sym.name != names._class) { |
3533 sym.name != names._class) { |
3525 // If the qualified item is not a type and the selected item is static, report |
3534 // If the qualified item is not a type and the selected item is static, report |
3600 // class B<T> extends A<T.foo> {} |
3609 // class B<T> extends A<T.foo> {} |
3601 Symbol sym = (site.getUpperBound() != null) |
3610 Symbol sym = (site.getUpperBound() != null) |
3602 ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo) |
3611 ? selectSym(tree, location, capture(site.getUpperBound()), env, resultInfo) |
3603 : null; |
3612 : null; |
3604 if (sym == null) { |
3613 if (sym == null) { |
3605 log.error(pos, "type.var.cant.be.deref"); |
3614 log.error(pos, Errors.TypeVarCantBeDeref); |
3606 return syms.errSymbol; |
3615 return syms.errSymbol; |
3607 } else { |
3616 } else { |
3608 Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ? |
3617 Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ? |
3609 rs.new AccessError(env, site, sym) : |
3618 rs.new AccessError(env, site, sym) : |
3610 sym; |
3619 sym; |
3624 Type arg = types.boxedClass(site).type; |
3633 Type arg = types.boxedClass(site).type; |
3625 t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym); |
3634 t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym); |
3626 return new VarSymbol( |
3635 return new VarSymbol( |
3627 STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
3636 STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
3628 } else { |
3637 } else { |
3629 log.error(pos, "cant.deref", site); |
3638 log.error(pos, Errors.CantDeref(site)); |
3630 return syms.errSymbol; |
3639 return syms.errSymbol; |
3631 } |
3640 } |
3632 } |
3641 } |
3633 } |
3642 } |
3634 |
3643 |
3924 // If the reference isn't from an initializer, then |
3933 // If the reference isn't from an initializer, then |
3925 // the reference is OK. |
3934 // the reference is OK. |
3926 if (!Resolve.isInitializer(env)) |
3935 if (!Resolve.isInitializer(env)) |
3927 return; |
3936 return; |
3928 |
3937 |
3929 log.error(tree.pos(), "illegal.enum.static.ref"); |
3938 log.error(tree.pos(), Errors.IllegalEnumStaticRef); |
3930 } |
3939 } |
3931 } |
3940 } |
3932 |
3941 |
3933 /** Is the given symbol a static, non-constant field of an Enum? |
3942 /** Is the given symbol a static, non-constant field of an Enum? |
3934 * Note: enum literals should not be regarded as such |
3943 * Note: enum literals should not be regarded as such |
3971 List<MethodSymbol> icand_sup = |
3980 List<MethodSymbol> icand_sup = |
3972 types.interfaceCandidates(sup, (MethodSymbol)sym); |
3981 types.interfaceCandidates(sup, (MethodSymbol)sym); |
3973 if (icand_sup.nonEmpty() && |
3982 if (icand_sup.nonEmpty() && |
3974 icand_sup.head != sym && |
3983 icand_sup.head != sym && |
3975 icand_sup.head.overrides(sym, icand_sup.head.enclClass(), types, true)) { |
3984 icand_sup.head.overrides(sym, icand_sup.head.enclClass(), types, true)) { |
3976 log.error(env.tree.pos(), "illegal.default.super.call", env.info.defaultSuperCallSite, |
3985 log.error(env.tree.pos(), |
3977 diags.fragment("overridden.default", sym, sup)); |
3986 Errors.IllegalDefaultSuperCall(env.info.defaultSuperCallSite, Fragments.OverriddenDefault(sym, sup))); |
3978 break; |
3987 break; |
3979 } |
3988 } |
3980 } |
3989 } |
3981 env.info.defaultSuperCallSite = null; |
3990 env.info.defaultSuperCallSite = null; |
3982 } |
3991 } |
3983 |
3992 |
3984 if (sym.isStatic() && site.isInterface() && env.tree.hasTag(APPLY)) { |
3993 if (sym.isStatic() && site.isInterface() && env.tree.hasTag(APPLY)) { |
3985 JCMethodInvocation app = (JCMethodInvocation)env.tree; |
3994 JCMethodInvocation app = (JCMethodInvocation)env.tree; |
3986 if (app.meth.hasTag(SELECT) && |
3995 if (app.meth.hasTag(SELECT) && |
3987 !TreeInfo.isStaticSelector(((JCFieldAccess)app.meth).selected, names)) { |
3996 !TreeInfo.isStaticSelector(((JCFieldAccess)app.meth).selected, names)) { |
3988 log.error(env.tree.pos(), "illegal.static.intf.meth.call", site); |
3997 log.error(env.tree.pos(), Errors.IllegalStaticIntfMethCall(site)); |
3989 } |
3998 } |
3990 } |
3999 } |
3991 |
4000 |
3992 // Compute the identifier's instantiated type. |
4001 // Compute the identifier's instantiated type. |
3993 // For methods, we need to compute the instance type by |
4002 // For methods, we need to compute the instance type by |
4128 } |
4137 } |
4129 owntype = new ClassType(clazzOuter, actuals, clazztype.tsym, |
4138 owntype = new ClassType(clazzOuter, actuals, clazztype.tsym, |
4130 clazztype.getMetadata()); |
4139 clazztype.getMetadata()); |
4131 } else { |
4140 } else { |
4132 if (formals.length() != 0) { |
4141 if (formals.length() != 0) { |
4133 log.error(tree.pos(), "wrong.number.type.args", |
4142 log.error(tree.pos(), |
4134 Integer.toString(formals.length())); |
4143 Errors.WrongNumberTypeArgs(Integer.toString(formals.length()))); |
4135 } else { |
4144 } else { |
4136 log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym); |
4145 log.error(tree.pos(), Errors.TypeDoesntTakeParams(clazztype.tsym)); |
4137 } |
4146 } |
4138 owntype = types.createErrorType(tree.type); |
4147 owntype = types.createErrorType(tree.type); |
4139 } |
4148 } |
4140 } |
4149 } |
4141 result = check(tree, owntype, KindSelector.TYP, resultInfo); |
4150 result = check(tree, owntype, KindSelector.TYP, resultInfo); |
4158 boolean sup = types.isSubtype(t, ctype); |
4167 boolean sup = types.isSubtype(t, ctype); |
4159 if (sub || sup) { |
4168 if (sub || sup) { |
4160 //assume 'a' <: 'b' |
4169 //assume 'a' <: 'b' |
4161 Type a = sub ? ctype : t; |
4170 Type a = sub ? ctype : t; |
4162 Type b = sub ? t : ctype; |
4171 Type b = sub ? t : ctype; |
4163 log.error(typeTree.pos(), "multicatch.types.must.be.disjoint", a, b); |
4172 log.error(typeTree.pos(), Errors.MulticatchTypesMustBeDisjoint(a, b)); |
4164 } |
4173 } |
4165 } |
4174 } |
4166 } |
4175 } |
4167 multicatchTypes.append(ctype); |
4176 multicatchTypes.append(ctype); |
4168 if (all_multicatchTypes != null) |
4177 if (all_multicatchTypes != null) |
4214 } |
4223 } |
4215 else if (bounds.head.type.hasTag(TYPEVAR)) { |
4224 else if (bounds.head.type.hasTag(TYPEVAR)) { |
4216 // if first bound was a typevar, do not accept further bounds. |
4225 // if first bound was a typevar, do not accept further bounds. |
4217 if (bounds.tail.nonEmpty()) { |
4226 if (bounds.tail.nonEmpty()) { |
4218 log.error(bounds.tail.head.pos(), |
4227 log.error(bounds.tail.head.pos(), |
4219 "type.var.may.not.be.followed.by.other.bounds"); |
4228 Errors.TypeVarMayNotBeFollowedByOtherBounds); |
4220 return bounds.head.type; |
4229 return bounds.head.type; |
4221 } |
4230 } |
4222 } else { |
4231 } else { |
4223 // if first bound was a class or interface, accept only interfaces |
4232 // if first bound was a class or interface, accept only interfaces |
4224 // as further bounds. |
4233 // as further bounds. |
4436 deferredLintHandler.flush(env.tree); |
4445 deferredLintHandler.flush(env.tree); |
4437 env.info.returnResult = null; |
4446 env.info.returnResult = null; |
4438 // java.lang.Enum may not be subclassed by a non-enum |
4447 // java.lang.Enum may not be subclassed by a non-enum |
4439 if (st.tsym == syms.enumSym && |
4448 if (st.tsym == syms.enumSym && |
4440 ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0)) |
4449 ((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0)) |
4441 log.error(env.tree.pos(), "enum.no.subclassing"); |
4450 log.error(env.tree.pos(), Errors.EnumNoSubclassing); |
4442 |
4451 |
4443 // Enums may not be extended by source-level classes |
4452 // Enums may not be extended by source-level classes |
4444 if (st.tsym != null && |
4453 if (st.tsym != null && |
4445 ((st.tsym.flags_field & Flags.ENUM) != 0) && |
4454 ((st.tsym.flags_field & Flags.ENUM) != 0) && |
4446 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) { |
4455 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) { |
4447 log.error(env.tree.pos(), "enum.types.not.extensible"); |
4456 log.error(env.tree.pos(), Errors.EnumTypesNotExtensible); |
4448 } |
4457 } |
4449 |
4458 |
4450 if (isSerializable(c.type)) { |
4459 if (isSerializable(c.type)) { |
4451 env.info.isSerializable = true; |
4460 env.info.isSerializable = true; |
4452 } |
4461 } |
4508 } |
4517 } |
4509 |
4518 |
4510 if ((c.flags() & ANNOTATION) != 0) { |
4519 if ((c.flags() & ANNOTATION) != 0) { |
4511 if (tree.implementing.nonEmpty()) |
4520 if (tree.implementing.nonEmpty()) |
4512 log.error(tree.implementing.head.pos(), |
4521 log.error(tree.implementing.head.pos(), |
4513 "cant.extend.intf.annotation"); |
4522 Errors.CantExtendIntfAnnotation); |
4514 if (tree.typarams.nonEmpty()) |
4523 if (tree.typarams.nonEmpty()) { |
4515 log.error(tree.typarams.head.pos(), |
4524 log.error(tree.typarams.head.pos(), |
4516 "intf.annotation.cant.have.type.params"); |
4525 Errors.IntfAnnotationCantHaveTypeParams(c)); |
|
4526 } |
4517 |
4527 |
4518 // If this annotation type has a @Repeatable, validate |
4528 // If this annotation type has a @Repeatable, validate |
4519 Attribute.Compound repeatable = c.getAnnotationTypeMetadata().getRepeatable(); |
4529 Attribute.Compound repeatable = c.getAnnotationTypeMetadata().getRepeatable(); |
4520 // If this annotation type has a @Repeatable, validate |
4530 // If this annotation type has a @Repeatable, validate |
4521 if (repeatable != null) { |
4531 if (repeatable != null) { |
4546 Assert.checkNonNull(env.info.scope.findFirst(l.head.name)); |
4556 Assert.checkNonNull(env.info.scope.findFirst(l.head.name)); |
4547 } |
4557 } |
4548 |
4558 |
4549 // Check that a generic class doesn't extend Throwable |
4559 // Check that a generic class doesn't extend Throwable |
4550 if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType)) |
4560 if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType)) |
4551 log.error(tree.extending.pos(), "generic.throwable"); |
4561 log.error(tree.extending.pos(), Errors.GenericThrowable); |
4552 |
4562 |
4553 // Check that all methods which implement some |
4563 // Check that all methods which implement some |
4554 // method conform to the method they implement. |
4564 // method conform to the method they implement. |
4555 chk.checkImplementations(tree); |
4565 chk.checkImplementations(tree); |
4556 |
4566 |
4641 } |
4651 } |
4642 } |
4652 } |
4643 |
4653 |
4644 if (svuid == null) { |
4654 if (svuid == null) { |
4645 log.warning(LintCategory.SERIAL, |
4655 log.warning(LintCategory.SERIAL, |
4646 tree.pos(), "missing.SVUID", c); |
4656 tree.pos(), Warnings.MissingSVUID(c)); |
4647 return; |
4657 return; |
4648 } |
4658 } |
4649 |
4659 |
4650 // check that it is static final |
4660 // check that it is static final |
4651 if ((svuid.flags() & (STATIC | FINAL)) != |
4661 if ((svuid.flags() & (STATIC | FINAL)) != |
4652 (STATIC | FINAL)) |
4662 (STATIC | FINAL)) |
4653 log.warning(LintCategory.SERIAL, |
4663 log.warning(LintCategory.SERIAL, |
4654 TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c); |
4664 TreeInfo.diagnosticPositionFor(svuid, tree), Warnings.ImproperSVUID(c)); |
4655 |
4665 |
4656 // check that it is long |
4666 // check that it is long |
4657 else if (!svuid.type.hasTag(LONG)) |
4667 else if (!svuid.type.hasTag(LONG)) |
4658 log.warning(LintCategory.SERIAL, |
4668 log.warning(LintCategory.SERIAL, |
4659 TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c); |
4669 TreeInfo.diagnosticPositionFor(svuid, tree), Warnings.LongSVUID(c)); |
4660 |
4670 |
4661 // check constant |
4671 // check constant |
4662 else if (svuid.getConstValue() == null) |
4672 else if (svuid.getConstValue() == null) |
4663 log.warning(LintCategory.SERIAL, |
4673 log.warning(LintCategory.SERIAL, |
4664 TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c); |
4674 TreeInfo.diagnosticPositionFor(svuid, tree), Warnings.ConstantSVUID(c)); |
4665 } |
4675 } |
4666 |
4676 |
4667 private Type capture(Type type) { |
4677 private Type capture(Type type) { |
4668 return types.capture(type); |
4678 return types.capture(type); |
4669 } |
4679 } |
4829 } |
4839 } |
4830 } else if (enclTr.hasTag(ANNOTATED_TYPE)) { |
4840 } else if (enclTr.hasTag(ANNOTATED_TYPE)) { |
4831 JCAnnotatedType at = (JCTree.JCAnnotatedType) enclTr; |
4841 JCAnnotatedType at = (JCTree.JCAnnotatedType) enclTr; |
4832 if (enclTy == null || enclTy.hasTag(NONE)) { |
4842 if (enclTy == null || enclTy.hasTag(NONE)) { |
4833 if (at.getAnnotations().size() == 1) { |
4843 if (at.getAnnotations().size() == 1) { |
4834 log.error(at.underlyingType.pos(), "cant.type.annotate.scoping.1", at.getAnnotations().head.attribute); |
4844 log.error(at.underlyingType.pos(), Errors.CantTypeAnnotateScoping1(at.getAnnotations().head.attribute)); |
4835 } else { |
4845 } else { |
4836 ListBuffer<Attribute.Compound> comps = new ListBuffer<>(); |
4846 ListBuffer<Attribute.Compound> comps = new ListBuffer<>(); |
4837 for (JCAnnotation an : at.getAnnotations()) { |
4847 for (JCAnnotation an : at.getAnnotations()) { |
4838 comps.add(an.attribute); |
4848 comps.add(an.attribute); |
4839 } |
4849 } |
4840 log.error(at.underlyingType.pos(), "cant.type.annotate.scoping", comps.toList()); |
4850 log.error(at.underlyingType.pos(), Errors.CantTypeAnnotateScoping(comps.toList())); |
4841 } |
4851 } |
4842 repeat = false; |
4852 repeat = false; |
4843 } |
4853 } |
4844 enclTr = at.underlyingType; |
4854 enclTr = at.underlyingType; |
4845 // enclTy doesn't need to be changed |
4855 // enclTy doesn't need to be changed |