31 |
31 |
32 import com.sun.tools.javac.code.*; |
32 import com.sun.tools.javac.code.*; |
33 import com.sun.tools.javac.code.Attribute.Compound; |
33 import com.sun.tools.javac.code.Attribute.Compound; |
34 import com.sun.tools.javac.code.Directive.ExportsDirective; |
34 import com.sun.tools.javac.code.Directive.ExportsDirective; |
35 import com.sun.tools.javac.code.Directive.RequiresDirective; |
35 import com.sun.tools.javac.code.Directive.RequiresDirective; |
|
36 import com.sun.tools.javac.code.Source.Feature; |
36 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata; |
37 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata; |
37 import com.sun.tools.javac.jvm.*; |
38 import com.sun.tools.javac.jvm.*; |
38 import com.sun.tools.javac.resources.CompilerProperties.Errors; |
39 import com.sun.tools.javac.resources.CompilerProperties.Errors; |
39 import com.sun.tools.javac.resources.CompilerProperties.Fragments; |
40 import com.sun.tools.javac.resources.CompilerProperties.Fragments; |
40 import com.sun.tools.javac.resources.CompilerProperties.Warnings; |
41 import com.sun.tools.javac.resources.CompilerProperties.Warnings; |
125 Options options = Options.instance(context); |
126 Options options = Options.instance(context); |
126 lint = Lint.instance(context); |
127 lint = Lint.instance(context); |
127 fileManager = context.get(JavaFileManager.class); |
128 fileManager = context.get(JavaFileManager.class); |
128 |
129 |
129 source = Source.instance(context); |
130 source = Source.instance(context); |
130 allowSimplifiedVarargs = source.allowSimplifiedVarargs(); |
|
131 allowDefaultMethods = source.allowDefaultMethods(); |
|
132 allowStrictMethodClashCheck = source.allowStrictMethodClashCheck(); |
|
133 allowPrivateSafeVarargs = source.allowPrivateSafeVarargs(); |
|
134 allowDiamondWithAnonymousClassCreation = source.allowDiamondWithAnonymousClassCreation(); |
|
135 warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers"); |
131 warnOnAnyAccessToMembers = options.isSet("warnOnAccessToMembers"); |
136 |
132 |
137 Target target = Target.instance(context); |
133 Target target = Target.instance(context); |
138 syntheticNameChar = target.syntheticNameChar(); |
134 syntheticNameChar = target.syntheticNameChar(); |
139 |
135 |
154 enforceMandatoryWarnings, "sunapi", null); |
150 enforceMandatoryWarnings, "sunapi", null); |
155 |
151 |
156 deferredLintHandler = DeferredLintHandler.instance(context); |
152 deferredLintHandler = DeferredLintHandler.instance(context); |
157 } |
153 } |
158 |
154 |
159 /** Switch: simplified varargs enabled? |
|
160 */ |
|
161 boolean allowSimplifiedVarargs; |
|
162 |
|
163 /** Switch: default methods enabled? |
|
164 */ |
|
165 boolean allowDefaultMethods; |
|
166 |
|
167 /** Switch: should unrelated return types trigger a method clash? |
|
168 */ |
|
169 boolean allowStrictMethodClashCheck; |
|
170 |
|
171 /** Switch: can the @SafeVarargs annotation be applied to private methods? |
|
172 */ |
|
173 boolean allowPrivateSafeVarargs; |
|
174 |
|
175 /** Switch: can diamond inference be used in anonymous instance creation ? |
|
176 */ |
|
177 boolean allowDiamondWithAnonymousClassCreation; |
|
178 |
|
179 /** Character for synthetic names |
155 /** Character for synthetic names |
180 */ |
156 */ |
181 char syntheticNameChar; |
157 char syntheticNameChar; |
182 |
158 |
183 /** A table mapping flat names of all compiled classes for each module in this run |
159 /** A table mapping flat names of all compiled classes for each module in this run |
254 |
230 |
255 /** Warn about unsafe vararg method decl. |
231 /** Warn about unsafe vararg method decl. |
256 * @param pos Position to be used for error reporting. |
232 * @param pos Position to be used for error reporting. |
257 */ |
233 */ |
258 void warnUnsafeVararg(DiagnosticPosition pos, String key, Object... args) { |
234 void warnUnsafeVararg(DiagnosticPosition pos, String key, Object... args) { |
259 if (lint.isEnabled(LintCategory.VARARGS) && allowSimplifiedVarargs) |
235 if (lint.isEnabled(LintCategory.VARARGS) && Feature.SIMPLIFIED_VARARGS.allowedInSource(source)) |
260 log.warning(LintCategory.VARARGS, pos, key, args); |
236 log.warning(LintCategory.VARARGS, pos, key, args); |
261 } |
237 } |
262 |
238 |
263 public void warnStatic(DiagnosticPosition pos, String msg, Object... args) { |
239 public void warnStatic(DiagnosticPosition pos, String msg, Object... args) { |
264 if (lint.isEnabled(LintCategory.STATIC)) |
240 if (lint.isEnabled(LintCategory.STATIC)) |
812 Type checkDiamond(JCNewClass tree, Type t) { |
788 Type checkDiamond(JCNewClass tree, Type t) { |
813 if (!TreeInfo.isDiamond(tree) || |
789 if (!TreeInfo.isDiamond(tree) || |
814 t.isErroneous()) { |
790 t.isErroneous()) { |
815 return checkClassType(tree.clazz.pos(), t, true); |
791 return checkClassType(tree.clazz.pos(), t, true); |
816 } else { |
792 } else { |
817 if (tree.def != null && !allowDiamondWithAnonymousClassCreation) { |
793 if (tree.def != null && !Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.allowedInSource(source)) { |
818 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.clazz.pos(), |
794 log.error(DiagnosticFlag.SOURCE_LEVEL, tree.clazz.pos(), |
819 Errors.CantApplyDiamond1(t, Fragments.DiamondAndAnonClassNotSupportedInSource(source.name))); |
795 Errors.CantApplyDiamond1(t, Feature.DIAMOND_WITH_ANONYMOUS_CLASS_CREATION.fragment(source.name))); |
820 } |
796 } |
821 if (t.tsym.type.getTypeArguments().isEmpty()) { |
797 if (t.tsym.type.getTypeArguments().isEmpty()) { |
822 log.error(tree.clazz.pos(), |
798 log.error(tree.clazz.pos(), |
823 Errors.CantApplyDiamond1(t, |
799 Errors.CantApplyDiamond1(t, |
824 Fragments.DiamondNonGeneric(t))); |
800 Fragments.DiamondNonGeneric(t))); |
904 } |
880 } |
905 }; |
881 }; |
906 |
882 |
907 void checkVarargsMethodDecl(Env<AttrContext> env, JCMethodDecl tree) { |
883 void checkVarargsMethodDecl(Env<AttrContext> env, JCMethodDecl tree) { |
908 MethodSymbol m = tree.sym; |
884 MethodSymbol m = tree.sym; |
909 if (!allowSimplifiedVarargs) return; |
885 if (!Feature.SIMPLIFIED_VARARGS.allowedInSource(source)) return; |
910 boolean hasTrustMeAnno = m.attribute(syms.trustMeType.tsym) != null; |
886 boolean hasTrustMeAnno = m.attribute(syms.trustMeType.tsym) != null; |
911 Type varargElemType = null; |
887 Type varargElemType = null; |
912 if (m.isVarArgs()) { |
888 if (m.isVarArgs()) { |
913 varargElemType = types.elemtype(tree.params.last().type); |
889 varargElemType = types.elemtype(tree.params.last().type); |
914 } |
890 } |
915 if (hasTrustMeAnno && !isTrustMeAllowedOnMethod(m)) { |
891 if (hasTrustMeAnno && !isTrustMeAllowedOnMethod(m)) { |
916 if (varargElemType != null) { |
892 if (varargElemType != null) { |
917 JCDiagnostic msg = allowPrivateSafeVarargs ? |
893 JCDiagnostic msg = Feature.PRIVATE_SAFE_VARARGS.allowedInSource(source) ? |
918 diags.fragment(Fragments.VarargsTrustmeOnVirtualVarargs(m)) : |
894 diags.fragment(Fragments.VarargsTrustmeOnVirtualVarargs(m)) : |
919 diags.fragment(Fragments.VarargsTrustmeOnVirtualVarargsFinalOnly(m)); |
895 diags.fragment(Fragments.VarargsTrustmeOnVirtualVarargsFinalOnly(m)); |
920 log.error(tree, |
896 log.error(tree, |
921 Errors.VarargsInvalidTrustmeAnno(syms.trustMeType.tsym, |
897 Errors.VarargsInvalidTrustmeAnno(syms.trustMeType.tsym, |
922 msg)); |
898 msg)); |
940 //where |
916 //where |
941 private boolean isTrustMeAllowedOnMethod(Symbol s) { |
917 private boolean isTrustMeAllowedOnMethod(Symbol s) { |
942 return (s.flags() & VARARGS) != 0 && |
918 return (s.flags() & VARARGS) != 0 && |
943 (s.isConstructor() || |
919 (s.isConstructor() || |
944 (s.flags() & (STATIC | FINAL | |
920 (s.flags() & (STATIC | FINAL | |
945 (allowPrivateSafeVarargs ? PRIVATE : 0) )) != 0); |
921 (Feature.PRIVATE_SAFE_VARARGS.allowedInSource(source) ? PRIVATE : 0) )) != 0); |
946 } |
922 } |
947 |
923 |
948 Type checkLocalVarType(DiagnosticPosition pos, Type t, Name name) { |
924 Type checkLocalVarType(DiagnosticPosition pos, Type t, Name name) { |
949 //check that resulting type is not the null type |
925 //check that resulting type is not the null type |
950 if (t.hasTag(BOT)) { |
926 if (t.hasTag(BOT)) { |
1017 } |
993 } |
1018 } |
994 } |
1019 if (useVarargs) { |
995 if (useVarargs) { |
1020 Type argtype = owntype.getParameterTypes().last(); |
996 Type argtype = owntype.getParameterTypes().last(); |
1021 if (!types.isReifiable(argtype) && |
997 if (!types.isReifiable(argtype) && |
1022 (!allowSimplifiedVarargs || |
998 (!Feature.SIMPLIFIED_VARARGS.allowedInSource(source) || |
1023 sym.baseSymbol().attribute(syms.trustMeType.tsym) == null || |
999 sym.baseSymbol().attribute(syms.trustMeType.tsym) == null || |
1024 !isTrustMeAllowedOnMethod(sym))) { |
1000 !isTrustMeAllowedOnMethod(sym))) { |
1025 warnUnchecked(env.tree.pos(), |
1001 warnUnchecked(env.tree.pos(), |
1026 "unchecked.generic.array.creation", |
1002 "unchecked.generic.array.creation", |
1027 argtype); |
1003 argtype); |
2487 //...check each method m2 that is a member of 'site' |
2463 //...check each method m2 that is a member of 'site' |
2488 for (Symbol m2 : types.membersClosure(site, false).getSymbolsByName(sym.name, cf)) { |
2464 for (Symbol m2 : types.membersClosure(site, false).getSymbolsByName(sym.name, cf)) { |
2489 if (m2 == m1) continue; |
2465 if (m2 == m1) continue; |
2490 //if (i) the signature of 'sym' is not a subsignature of m1 (seen as |
2466 //if (i) the signature of 'sym' is not a subsignature of m1 (seen as |
2491 //a member of 'site') and (ii) m1 has the same erasure as m2, issue an error |
2467 //a member of 'site') and (ii) m1 has the same erasure as m2, issue an error |
2492 if (!types.isSubSignature(sym.type, types.memberType(site, m2), allowStrictMethodClashCheck) && |
2468 if (!types.isSubSignature(sym.type, types.memberType(site, m2), Feature.STRICT_METHOD_CLASH_CHECK.allowedInSource(source)) && |
2493 types.hasSameArgs(m2.erasure(types), m1.erasure(types))) { |
2469 types.hasSameArgs(m2.erasure(types), m1.erasure(types))) { |
2494 sym.flags_field |= CLASH; |
2470 sym.flags_field |= CLASH; |
2495 if (m1 == sym) { |
2471 if (m1 == sym) { |
2496 log.error(pos, Errors.NameClashSameErasureNoOverride( |
2472 log.error(pos, Errors.NameClashSameErasureNoOverride( |
2497 m1.name, types.memberType(site, m1).asMethodType().getParameterTypes(), m1.location(), |
2473 m1.name, types.memberType(site, m1).asMethodType().getParameterTypes(), m1.location(), |
2532 ClashFilter cf = new ClashFilter(site); |
2508 ClashFilter cf = new ClashFilter(site); |
2533 //for each method m1 that is a member of 'site'... |
2509 //for each method m1 that is a member of 'site'... |
2534 for (Symbol s : types.membersClosure(site, true).getSymbolsByName(sym.name, cf)) { |
2510 for (Symbol s : types.membersClosure(site, true).getSymbolsByName(sym.name, cf)) { |
2535 //if (i) the signature of 'sym' is not a subsignature of m1 (seen as |
2511 //if (i) the signature of 'sym' is not a subsignature of m1 (seen as |
2536 //a member of 'site') and (ii) 'sym' has the same erasure as m1, issue an error |
2512 //a member of 'site') and (ii) 'sym' has the same erasure as m1, issue an error |
2537 if (!types.isSubSignature(sym.type, types.memberType(site, s), allowStrictMethodClashCheck)) { |
2513 if (!types.isSubSignature(sym.type, types.memberType(site, s), Feature.STRICT_METHOD_CLASH_CHECK.allowedInSource(source))) { |
2538 if (types.hasSameArgs(s.erasure(types), sym.erasure(types))) { |
2514 if (types.hasSameArgs(s.erasure(types), sym.erasure(types))) { |
2539 log.error(pos, |
2515 log.error(pos, |
2540 Errors.NameClashSameErasureNoHide(sym, sym.location(), s, s.location())); |
2516 Errors.NameClashSameErasureNoHide(sym, sym.location(), s, s.location())); |
2541 return; |
2517 return; |
2542 } else { |
2518 } else { |
2632 * lambda would be ambiguous). |
2608 * lambda would be ambiguous). |
2633 */ |
2609 */ |
2634 void checkPotentiallyAmbiguousOverloads(DiagnosticPosition pos, Type site, |
2610 void checkPotentiallyAmbiguousOverloads(DiagnosticPosition pos, Type site, |
2635 MethodSymbol msym1, MethodSymbol msym2) { |
2611 MethodSymbol msym1, MethodSymbol msym2) { |
2636 if (msym1 != msym2 && |
2612 if (msym1 != msym2 && |
2637 allowDefaultMethods && |
2613 Feature.DEFAULT_METHODS.allowedInSource(source) && |
2638 lint.isEnabled(LintCategory.OVERLOADS) && |
2614 lint.isEnabled(LintCategory.OVERLOADS) && |
2639 (msym1.flags() & POTENTIALLY_AMBIGUOUS) == 0 && |
2615 (msym1.flags() & POTENTIALLY_AMBIGUOUS) == 0 && |
2640 (msym2.flags() & POTENTIALLY_AMBIGUOUS) == 0) { |
2616 (msym2.flags() & POTENTIALLY_AMBIGUOUS) == 0) { |
2641 Type mt1 = types.memberType(site, msym1); |
2617 Type mt1 = types.memberType(site, msym1); |
2642 Type mt2 = types.memberType(site, msym2); |
2618 Type mt2 = types.memberType(site, msym2); |