Tue, 13 May 2014 14:18:34 +0100
8029102: Enhance compiler warnings for Lambda
Reviewed-by: briangoetz, jjg, jlahoda, ahgross
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Symbol.java Mon May 12 09:53:35 2014 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Symbol.java Tue May 13 14:18:34 2014 +0100 1.3 @@ -392,7 +392,7 @@ 1.4 /** A class is an inner class if it it has an enclosing instance class. 1.5 */ 1.6 public boolean isInner() { 1.7 - return type.getEnclosingType().hasTag(CLASS); 1.8 + return kind == TYP && type.getEnclosingType().hasTag(CLASS); 1.9 } 1.10 1.11 /** An inner class has an outer instance if it is not an interface
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Mon May 12 09:53:35 2014 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue May 13 14:18:34 2014 +0100 2.3 @@ -2143,6 +2143,11 @@ 2.4 cdef.extending = clazz; 2.5 } 2.6 2.7 + if (resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK && 2.8 + isSerializable(clazztype)) { 2.9 + localEnv.info.isSerializable = true; 2.10 + } 2.11 + 2.12 attribStat(cdef, localEnv); 2.13 2.14 checkLambdaCandidate(tree, cdef.sym, clazztype); 2.15 @@ -2298,6 +2303,9 @@ 2.16 resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK; 2.17 try { 2.18 Type currentTarget = pt(); 2.19 + if (needsRecovery && isSerializable(currentTarget)) { 2.20 + localEnv.info.isSerializable = true; 2.21 + } 2.22 List<Type> explicitParamTypes = null; 2.23 if (that.paramKind == JCLambda.ParameterKind.EXPLICIT) { 2.24 //attribute lambda parameters 2.25 @@ -2702,17 +2710,20 @@ 2.26 typeargtypes = attribTypes(that.typeargs, localEnv); 2.27 } 2.28 2.29 - Type target; 2.30 Type desc; 2.31 - if (pt() != Type.recoveryType) { 2.32 - target = targetChecker.visit(pt(), that); 2.33 - desc = types.findDescriptorType(target); 2.34 + Type currentTarget = pt(); 2.35 + boolean isTargetSerializable = 2.36 + resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.CHECK && 2.37 + isSerializable(currentTarget); 2.38 + if (currentTarget != Type.recoveryType) { 2.39 + currentTarget = targetChecker.visit(currentTarget, that); 2.40 + desc = types.findDescriptorType(currentTarget); 2.41 } else { 2.42 - target = Type.recoveryType; 2.43 + currentTarget = Type.recoveryType; 2.44 desc = fallbackDescriptorType(that); 2.45 } 2.46 2.47 - setFunctionalInfo(localEnv, that, pt(), desc, target, resultInfo.checkContext); 2.48 + setFunctionalInfo(localEnv, that, pt(), desc, currentTarget, resultInfo.checkContext); 2.49 List<Type> argtypes = desc.getParameterTypes(); 2.50 Resolve.MethodCheck referenceCheck = rs.resolveMethodCheck; 2.51 2.52 @@ -2763,10 +2774,10 @@ 2.53 JCDiagnostic diag = diags.create(diagKind, log.currentSource(), that, 2.54 "invalid.mref", Kinds.kindName(that.getMode()), detailsDiag); 2.55 2.56 - if (targetError && target == Type.recoveryType) { 2.57 + if (targetError && currentTarget == Type.recoveryType) { 2.58 //a target error doesn't make sense during recovery stage 2.59 //as we don't know what actual parameter types are 2.60 - result = that.type = target; 2.61 + result = that.type = currentTarget; 2.62 return; 2.63 } else { 2.64 if (targetError) { 2.65 @@ -2774,7 +2785,7 @@ 2.66 } else { 2.67 log.report(diag); 2.68 } 2.69 - result = that.type = types.createErrorType(target); 2.70 + result = that.type = types.createErrorType(currentTarget); 2.71 return; 2.72 } 2.73 } 2.74 @@ -2785,7 +2796,7 @@ 2.75 2.76 if (desc.getReturnType() == Type.recoveryType) { 2.77 // stop here 2.78 - result = that.type = target; 2.79 + result = that.type = currentTarget; 2.80 return; 2.81 } 2.82 2.83 @@ -2803,7 +2814,7 @@ 2.84 //static ref with class type-args 2.85 log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()), 2.86 diags.fragment("static.mref.with.targs")); 2.87 - result = that.type = types.createErrorType(target); 2.88 + result = that.type = types.createErrorType(currentTarget); 2.89 return; 2.90 } 2.91 2.92 @@ -2812,7 +2823,7 @@ 2.93 //no static bound mrefs 2.94 log.error(that.expr.pos(), "invalid.mref", Kinds.kindName(that.getMode()), 2.95 diags.fragment("static.bound.mref")); 2.96 - result = that.type = types.createErrorType(target); 2.97 + result = that.type = types.createErrorType(currentTarget); 2.98 return; 2.99 } 2.100 2.101 @@ -2820,6 +2831,10 @@ 2.102 // Check that super-qualified symbols are not abstract (JLS) 2.103 rs.checkNonAbstract(that.pos(), that.sym); 2.104 } 2.105 + 2.106 + if (isTargetSerializable) { 2.107 + chk.checkElemAccessFromSerializableLambda(that); 2.108 + } 2.109 } 2.110 2.111 ResultInfo checkInfo = 2.112 @@ -2851,9 +2866,9 @@ 2.113 resultInfo.checkContext.deferredAttrContext().mode == DeferredAttr.AttrMode.SPECULATIVE; 2.114 checkReferenceCompatible(that, desc, refType, resultInfo.checkContext, isSpeculativeRound); 2.115 if (!isSpeculativeRound) { 2.116 - checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), desc, target); 2.117 + checkAccessibleTypes(that, localEnv, resultInfo.checkContext.inferenceContext(), desc, currentTarget); 2.118 } 2.119 - result = check(that, target, VAL, resultInfo); 2.120 + result = check(that, currentTarget, VAL, resultInfo); 2.121 } catch (Types.FunctionDescriptorLookupError ex) { 2.122 JCDiagnostic cause = ex.getDiagnostic(); 2.123 resultInfo.checkContext.report(that, cause); 2.124 @@ -3193,6 +3208,11 @@ 2.125 while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym)) 2.126 env1 = env1.outer; 2.127 } 2.128 + 2.129 + if (env.info.isSerializable) { 2.130 + chk.checkElemAccessFromSerializableLambda(tree); 2.131 + } 2.132 + 2.133 result = checkId(tree, env1.enclClass.sym.type, sym, env, resultInfo); 2.134 } 2.135 2.136 @@ -3317,6 +3337,10 @@ 2.137 } 2.138 } 2.139 2.140 + if (env.info.isSerializable) { 2.141 + chk.checkElemAccessFromSerializableLambda(tree); 2.142 + } 2.143 + 2.144 env.info.selectSuper = selectSuperPrev; 2.145 result = checkId(tree, site, sym, env, resultInfo); 2.146 } 2.147 @@ -4195,6 +4219,11 @@ 2.148 ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0)) { 2.149 log.error(env.tree.pos(), "enum.types.not.extensible"); 2.150 } 2.151 + 2.152 + if (isSerializable(c.type)) { 2.153 + env.info.isSerializable = true; 2.154 + } 2.155 + 2.156 attribClassBody(env, c); 2.157 2.158 chk.checkDeprecatedAnnotation(env.tree.pos(), c); 2.159 @@ -4308,7 +4337,7 @@ 2.160 2.161 // Check for proper use of serialVersionUID 2.162 if (env.info.lint.isEnabled(LintCategory.SERIAL) && 2.163 - isSerializable(c) && 2.164 + isSerializable(c.type) && 2.165 (c.flags() & Flags.ENUM) == 0 && 2.166 checkForSerial(c)) { 2.167 checkSerialVersionUID(tree, c); 2.168 @@ -4348,15 +4377,15 @@ 2.169 return null; 2.170 } 2.171 2.172 - /** check if a class is a subtype of Serializable, if that is available. */ 2.173 - private boolean isSerializable(ClassSymbol c) { 2.174 + /** check if a type is a subtype of Serializable, if that is available. */ 2.175 + boolean isSerializable(Type t) { 2.176 try { 2.177 syms.serializableType.complete(); 2.178 } 2.179 catch (CompletionFailure e) { 2.180 return false; 2.181 } 2.182 - return types.isSubtype(c.type, syms.serializableType); 2.183 + return types.isSubtype(t, syms.serializableType); 2.184 } 2.185 2.186 /** Check that an appropriate serialVersionUID member is defined. */
3.1 --- a/src/share/classes/com/sun/tools/javac/comp/AttrContext.java Mon May 12 09:53:35 2014 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/comp/AttrContext.java Tue May 13 14:18:34 2014 +0100 3.3 @@ -1,5 +1,5 @@ 3.4 /* 3.5 - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 3.6 + * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 3.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 * 3.9 * This code is free software; you can redistribute it and/or modify it 3.10 @@ -54,6 +54,10 @@ 3.11 */ 3.12 boolean selectSuper = false; 3.13 3.14 + /** Is the current target of lambda expression or method reference serializable? 3.15 + */ 3.16 + boolean isSerializable = false; 3.17 + 3.18 /** Are arguments to current function applications boxed into an array for varargs? 3.19 */ 3.20 Resolve.MethodResolutionPhase pendingResolutionPhase = null; 3.21 @@ -89,6 +93,7 @@ 3.22 info.enclVar = enclVar; 3.23 info.returnResult = returnResult; 3.24 info.defaultSuperCallSite = defaultSuperCallSite; 3.25 + info.isSerializable = isSerializable; 3.26 return info; 3.27 } 3.28
4.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Mon May 12 09:53:35 2014 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Tue May 13 14:18:34 2014 +0100 4.3 @@ -82,6 +82,7 @@ 4.4 private final TreeInfo treeinfo; 4.5 private final JavaFileManager fileManager; 4.6 private final Profile profile; 4.7 + private final boolean warnOnAccessToSensitiveMembers; 4.8 4.9 // The set of lint options currently in effect. It is initialized 4.10 // from the context, and then is set/reset as needed by Attr as it 4.11 @@ -131,6 +132,7 @@ 4.12 warnOnSyntheticConflicts = options.isSet("warnOnSyntheticConflicts"); 4.13 suppressAbortOnBadClassFile = options.isSet("suppressAbortOnBadClassFile"); 4.14 enableSunApiLintControl = options.isSet("enableSunApiLintControl"); 4.15 + warnOnAccessToSensitiveMembers = options.isSet("warnOnAccessToSensitiveMembers"); 4.16 4.17 Target target = Target.instance(context); 4.18 syntheticNameChar = target.syntheticNameChar(); 4.19 @@ -2588,6 +2590,44 @@ 4.20 } 4.21 } 4.22 4.23 + void checkElemAccessFromSerializableLambda(final JCTree tree) { 4.24 + if (warnOnAccessToSensitiveMembers) { 4.25 + Symbol sym = TreeInfo.symbol(tree); 4.26 + if ((sym.kind & (VAR | MTH)) == 0) { 4.27 + return; 4.28 + } 4.29 + 4.30 + if (sym.kind == VAR) { 4.31 + if ((sym.flags() & PARAMETER) != 0 || 4.32 + sym.isLocal() || 4.33 + sym.name == names._this || 4.34 + sym.name == names._super) { 4.35 + return; 4.36 + } 4.37 + } 4.38 + 4.39 + if (!types.isSubtype(sym.owner.type, syms.serializableType) && 4.40 + isEffectivelyNonPublic(sym)) { 4.41 + log.warning(tree.pos(), 4.42 + "access.to.sensitive.member.from.serializable.element", sym); 4.43 + } 4.44 + } 4.45 + } 4.46 + 4.47 + private boolean isEffectivelyNonPublic(Symbol sym) { 4.48 + if (sym.packge() == syms.rootPackage) { 4.49 + return false; 4.50 + } 4.51 + 4.52 + while (sym.kind != Kinds.PCK) { 4.53 + if ((sym.flags() & PUBLIC) == 0) { 4.54 + return true; 4.55 + } 4.56 + sym = sym.owner; 4.57 + } 4.58 + return false; 4.59 + } 4.60 + 4.61 /** Report a conflict between a user symbol and a synthetic symbol. 4.62 */ 4.63 private void syntheticError(DiagnosticPosition pos, Symbol sym) {
5.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Mon May 12 09:53:35 2014 -0700 5.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue May 13 14:18:34 2014 +0100 5.3 @@ -1617,6 +1617,10 @@ 5.4 compiler.warn.varargs.redundant.trustme.anno=\ 5.5 Redundant {0} annotation. {1} 5.6 5.7 +# 0: symbol 5.8 +compiler.warn.access.to.sensitive.member.from.serializable.element=\ 5.9 + access to sensitive member {0} from serializable element can be publicly accessible to untrusted code 5.10 + 5.11 ##### 5.12 5.13 ## The following are tokens which are non-terminals in the language. They should
6.1 --- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Mon May 12 09:53:35 2014 -0700 6.2 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Tue May 13 14:18:34 2014 +0100 6.3 @@ -1,5 +1,5 @@ 6.4 /* 6.5 - * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 6.6 + * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 6.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.8 * 6.9 * This code is free software; you can redistribute it and/or modify it 6.10 @@ -831,6 +831,8 @@ 6.11 return symbol(((JCTypeApply) tree).clazz); 6.12 case ANNOTATED_TYPE: 6.13 return symbol(((JCAnnotatedType) tree).underlyingType); 6.14 + case REFERENCE: 6.15 + return ((JCMemberReference) tree).sym; 6.16 default: 6.17 return null; 6.18 }
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/T8029102/WarnSerializableLambdaTest.java Tue May 13 14:18:34 2014 +0100 7.3 @@ -0,0 +1,241 @@ 7.4 +/* 7.5 + * @test /nodynamiccopyright/ 7.6 + * @bug 8029102 7.7 + * @summary Enhance compiler warnings for Lambda 7.8 + * Checks that the warning for accessing non public members of a class is 7.9 + * fired correctly. 7.10 + * @compile/fail/ref=WarnSerializableLambdaTest.out -XDrawDiagnostics -Werror -XDwarnOnAccessToSensitiveMembers WarnSerializableLambdaTest.java 7.11 + */ 7.12 + 7.13 +import java.io.Serializable; 7.14 + 7.15 +public class WarnSerializableLambdaTest { 7.16 + 7.17 + void warnLambda() throws Exception { 7.18 + SAM t3 = (SAM & Serializable)WarnSerializableLambdaTest::packageClassMethod; 7.19 + SAM t4 = (SAM & Serializable)WarnSerializableLambdaTest::protectedClassMethod; 7.20 + SAM t5 = (SAM & Serializable)WarnSerializableLambdaTest::privateClassMethod; 7.21 + 7.22 + WarnSerializableLambdaTest test = new WarnSerializableLambdaTest(); 7.23 + SAM t6 = (SAM & Serializable)test::packageInstanceMethod; 7.24 + SAM t7 = (SAM & Serializable)test::protectedInstanceMethod; 7.25 + SAM t8 = (SAM & Serializable)test::privateInstanceMethod; 7.26 + 7.27 + SAM t9 = (SAM & Serializable) c -> { 7.28 + 7.29 + WarnSerializableLambdaTest.staticPackageField = ""; 7.30 + WarnSerializableLambdaTest.staticProtectedField = ""; 7.31 + WarnSerializableLambdaTest.staticPrivateField = ""; 7.32 + 7.33 + packageField = ""; 7.34 + protectedField = ""; 7.35 + privateField = ""; 7.36 + 7.37 + WarnSerializableLambdaTest.packageClassMethod(null); 7.38 + WarnSerializableLambdaTest.protectedClassMethod(null); 7.39 + WarnSerializableLambdaTest.privateClassMethod(null); 7.40 + 7.41 + packageInstanceMethod(null); 7.42 + protectedInstanceMethod(null); 7.43 + privateInstanceMethod(null); 7.44 + 7.45 + PrivateClass.effectivelyNonPublicStaticField = ""; 7.46 + PrivateClass.effectivelyNonPublicClassMethod(); 7.47 + 7.48 + PrivateClass p = new PrivateClass(); 7.49 + p.effectivelyNonPublicInstanceField = ""; 7.50 + p.effectivelyNonPublicInstanceMethod(); 7.51 + 7.52 + return null; 7.53 + }; 7.54 + } 7.55 + 7.56 + private void warnAnoInnerClass() throws Exception { 7.57 + new SerializableDesc() { 7.58 + public void m(Object param) throws Exception { 7.59 + WarnSerializableLambdaTest.staticPackageField = ""; 7.60 + WarnSerializableLambdaTest.staticProtectedField = ""; 7.61 + WarnSerializableLambdaTest.staticPrivateField = ""; 7.62 + 7.63 + packageField = ""; 7.64 + protectedField = ""; 7.65 + privateField = ""; 7.66 + 7.67 + WarnSerializableLambdaTest.packageClassMethod(null); 7.68 + WarnSerializableLambdaTest.protectedClassMethod(null); 7.69 + WarnSerializableLambdaTest.privateClassMethod(null); 7.70 + 7.71 + packageInstanceMethod(null); 7.72 + protectedInstanceMethod(null); 7.73 + privateInstanceMethod(null); 7.74 + 7.75 + PrivateClass.effectivelyNonPublicStaticField = ""; 7.76 + PrivateClass.effectivelyNonPublicClassMethod(); 7.77 + 7.78 + PrivateClass p = new PrivateClass(); 7.79 + p.effectivelyNonPublicInstanceField = ""; 7.80 + p.effectivelyNonPublicInstanceMethod(); 7.81 + } 7.82 + }; 7.83 + } 7.84 + 7.85 + void dontWarnLambda() throws Exception { 7.86 + SAM t1 = (SAM & Serializable)WarnSerializableLambdaTest::publicClassMethod; 7.87 + 7.88 + WarnSerializableLambdaTest test = new WarnSerializableLambdaTest(); 7.89 + SAM t2 = (SAM & Serializable)test::publicInstanceMethod; 7.90 + 7.91 + int[] buffer = {0}; 7.92 + 7.93 + SAM t3 = (SAM & Serializable) param -> { 7.94 + Object localVar; 7.95 + localVar = null; 7.96 + param = null; 7.97 + 7.98 + WarnSerializableLambdaTest.staticPublicField = ""; 7.99 + publicField = ""; 7.100 + WarnSerializableLambdaTest.publicClassMethod(null); 7.101 + publicInstanceMethod(null); 7.102 + 7.103 + PublicClass.effectivelyPublicStaticField = ""; 7.104 + PublicClass.effectivelyPublicClassMethod(); 7.105 + 7.106 + PublicClass p = new PublicClass(); 7.107 + p.effectivelyPublicInstanceField = ""; 7.108 + p.effectivelyPublicInstanceMethod(); 7.109 + 7.110 + int l = buffer.length; 7.111 + 7.112 + return null; 7.113 + }; 7.114 + } 7.115 + 7.116 + private void dontWarnAnoInnerClass() throws Exception { 7.117 + final int[] buffer = {0}; 7.118 + new SerializableDesc() { 7.119 + public void m(Object param) throws Exception { 7.120 + Object localVar; 7.121 + localVar = null; 7.122 + param = null; 7.123 + 7.124 + WarnSerializableLambdaTest.staticPublicField = ""; 7.125 + publicField = ""; 7.126 + WarnSerializableLambdaTest.publicClassMethod(null); 7.127 + publicInstanceMethod(null); 7.128 + 7.129 + PublicClass.effectivelyPublicStaticField = ""; 7.130 + PublicClass.effectivelyPublicClassMethod(); 7.131 + 7.132 + PublicClass p = new PublicClass(); 7.133 + p.effectivelyPublicInstanceField = ""; 7.134 + p.effectivelyPublicInstanceMethod(); 7.135 + 7.136 + int l = buffer.length; 7.137 + } 7.138 + }; 7.139 + } 7.140 + 7.141 + enum WarnEnum { 7.142 + A { 7.143 + public void m() throws Exception { 7.144 + WarnSerializableLambdaTest.staticPackageField = ""; 7.145 + WarnSerializableLambdaTest.staticProtectedField = ""; 7.146 + WarnSerializableLambdaTest.staticPrivateField = ""; 7.147 + 7.148 + WarnSerializableLambdaTest test = 7.149 + new WarnSerializableLambdaTest(); 7.150 + 7.151 + test.packageField = ""; 7.152 + test.protectedField = ""; 7.153 + test.privateField = ""; 7.154 + 7.155 + WarnSerializableLambdaTest.packageClassMethod(null); 7.156 + WarnSerializableLambdaTest.protectedClassMethod(null); 7.157 + WarnSerializableLambdaTest.privateClassMethod(null); 7.158 + 7.159 + test.packageInstanceMethod(null); 7.160 + test.protectedInstanceMethod(null); 7.161 + test.privateInstanceMethod(null); 7.162 + 7.163 + PrivateClass.effectivelyNonPublicStaticField = ""; 7.164 + PrivateClass.effectivelyNonPublicClassMethod(); 7.165 + 7.166 + PrivateClass p = new PrivateClass(); 7.167 + p.effectivelyNonPublicInstanceField = ""; 7.168 + p.effectivelyNonPublicInstanceMethod(); 7.169 + } 7.170 + }; 7.171 + 7.172 + public void m() throws Exception {} 7.173 + } 7.174 + 7.175 + static String staticPackageField; 7.176 + static private String staticPrivateField; 7.177 + static protected String staticProtectedField; 7.178 + static public String staticPublicField; 7.179 + 7.180 + String packageField; 7.181 + private String privateField; 7.182 + protected String protectedField; 7.183 + public String publicField; 7.184 + 7.185 + static Object packageClassMethod(String s) { 7.186 + return null; 7.187 + } 7.188 + 7.189 + static private Object privateClassMethod(String s) { 7.190 + return null; 7.191 + } 7.192 + 7.193 + static protected Object protectedClassMethod(String s) { 7.194 + return null; 7.195 + } 7.196 + 7.197 + static public Object publicClassMethod(String s) { 7.198 + return null; 7.199 + } 7.200 + 7.201 + Object packageInstanceMethod(String s) { 7.202 + return null; 7.203 + } 7.204 + 7.205 + protected Object protectedInstanceMethod(String s) { 7.206 + return null; 7.207 + } 7.208 + 7.209 + private Object privateInstanceMethod(String s) { 7.210 + return null; 7.211 + } 7.212 + 7.213 + public Object publicInstanceMethod(String s) { 7.214 + return null; 7.215 + } 7.216 + 7.217 + interface SAM { 7.218 + Object apply(String s) throws Exception; 7.219 + } 7.220 + 7.221 + interface SAM2 { 7.222 + Object apply(String arg1, String arg2); 7.223 + } 7.224 + 7.225 + class SerializableDesc implements Serializable { 7.226 + public void m(Object param) throws Exception {} 7.227 + } 7.228 + 7.229 + static private class PrivateClass { 7.230 + static public String effectivelyNonPublicStaticField; 7.231 + public String effectivelyNonPublicInstanceField; 7.232 + 7.233 + static public void effectivelyNonPublicClassMethod() {} 7.234 + public void effectivelyNonPublicInstanceMethod() {} 7.235 + } 7.236 + 7.237 + static public class PublicClass { 7.238 + static public String effectivelyPublicStaticField; 7.239 + public String effectivelyPublicInstanceField; 7.240 + 7.241 + static public void effectivelyPublicClassMethod() {} 7.242 + public void effectivelyPublicInstanceMethod() {} 7.243 + } 7.244 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/T8029102/WarnSerializableLambdaTest.out Tue May 13 14:18:34 2014 +0100 8.3 @@ -0,0 +1,57 @@ 8.4 +WarnSerializableLambdaTest.java:15:38: compiler.warn.access.to.sensitive.member.from.serializable.element: packageClassMethod(java.lang.String) 8.5 +WarnSerializableLambdaTest.java:16:38: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedClassMethod(java.lang.String) 8.6 +WarnSerializableLambdaTest.java:17:38: compiler.warn.access.to.sensitive.member.from.serializable.element: privateClassMethod(java.lang.String) 8.7 +WarnSerializableLambdaTest.java:20:38: compiler.warn.access.to.sensitive.member.from.serializable.element: packageInstanceMethod(java.lang.String) 8.8 +WarnSerializableLambdaTest.java:21:38: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedInstanceMethod(java.lang.String) 8.9 +WarnSerializableLambdaTest.java:22:38: compiler.warn.access.to.sensitive.member.from.serializable.element: privateInstanceMethod(java.lang.String) 8.10 +WarnSerializableLambdaTest.java:26:39: compiler.warn.access.to.sensitive.member.from.serializable.element: staticPackageField 8.11 +WarnSerializableLambdaTest.java:27:39: compiler.warn.access.to.sensitive.member.from.serializable.element: staticProtectedField 8.12 +WarnSerializableLambdaTest.java:28:39: compiler.warn.access.to.sensitive.member.from.serializable.element: staticPrivateField 8.13 +WarnSerializableLambdaTest.java:30:13: compiler.warn.access.to.sensitive.member.from.serializable.element: packageField 8.14 +WarnSerializableLambdaTest.java:31:13: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedField 8.15 +WarnSerializableLambdaTest.java:32:13: compiler.warn.access.to.sensitive.member.from.serializable.element: privateField 8.16 +WarnSerializableLambdaTest.java:34:39: compiler.warn.access.to.sensitive.member.from.serializable.element: packageClassMethod(java.lang.String) 8.17 +WarnSerializableLambdaTest.java:35:39: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedClassMethod(java.lang.String) 8.18 +WarnSerializableLambdaTest.java:36:39: compiler.warn.access.to.sensitive.member.from.serializable.element: privateClassMethod(java.lang.String) 8.19 +WarnSerializableLambdaTest.java:38:13: compiler.warn.access.to.sensitive.member.from.serializable.element: packageInstanceMethod(java.lang.String) 8.20 +WarnSerializableLambdaTest.java:39:13: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedInstanceMethod(java.lang.String) 8.21 +WarnSerializableLambdaTest.java:40:13: compiler.warn.access.to.sensitive.member.from.serializable.element: privateInstanceMethod(java.lang.String) 8.22 +WarnSerializableLambdaTest.java:42:25: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicStaticField 8.23 +WarnSerializableLambdaTest.java:43:25: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicClassMethod() 8.24 +WarnSerializableLambdaTest.java:46:14: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicInstanceField 8.25 +WarnSerializableLambdaTest.java:47:14: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicInstanceMethod() 8.26 +WarnSerializableLambdaTest.java:56:43: compiler.warn.access.to.sensitive.member.from.serializable.element: staticPackageField 8.27 +WarnSerializableLambdaTest.java:57:43: compiler.warn.access.to.sensitive.member.from.serializable.element: staticProtectedField 8.28 +WarnSerializableLambdaTest.java:58:43: compiler.warn.access.to.sensitive.member.from.serializable.element: staticPrivateField 8.29 +WarnSerializableLambdaTest.java:60:17: compiler.warn.access.to.sensitive.member.from.serializable.element: packageField 8.30 +WarnSerializableLambdaTest.java:61:17: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedField 8.31 +WarnSerializableLambdaTest.java:62:17: compiler.warn.access.to.sensitive.member.from.serializable.element: privateField 8.32 +WarnSerializableLambdaTest.java:64:43: compiler.warn.access.to.sensitive.member.from.serializable.element: packageClassMethod(java.lang.String) 8.33 +WarnSerializableLambdaTest.java:65:43: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedClassMethod(java.lang.String) 8.34 +WarnSerializableLambdaTest.java:66:43: compiler.warn.access.to.sensitive.member.from.serializable.element: privateClassMethod(java.lang.String) 8.35 +WarnSerializableLambdaTest.java:68:17: compiler.warn.access.to.sensitive.member.from.serializable.element: packageInstanceMethod(java.lang.String) 8.36 +WarnSerializableLambdaTest.java:69:17: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedInstanceMethod(java.lang.String) 8.37 +WarnSerializableLambdaTest.java:70:17: compiler.warn.access.to.sensitive.member.from.serializable.element: privateInstanceMethod(java.lang.String) 8.38 +WarnSerializableLambdaTest.java:72:29: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicStaticField 8.39 +WarnSerializableLambdaTest.java:73:29: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicClassMethod() 8.40 +WarnSerializableLambdaTest.java:76:18: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicInstanceField 8.41 +WarnSerializableLambdaTest.java:77:18: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicInstanceMethod() 8.42 +WarnSerializableLambdaTest.java:141:43: compiler.warn.access.to.sensitive.member.from.serializable.element: staticPackageField 8.43 +WarnSerializableLambdaTest.java:142:43: compiler.warn.access.to.sensitive.member.from.serializable.element: staticProtectedField 8.44 +WarnSerializableLambdaTest.java:143:43: compiler.warn.access.to.sensitive.member.from.serializable.element: staticPrivateField 8.45 +WarnSerializableLambdaTest.java:148:21: compiler.warn.access.to.sensitive.member.from.serializable.element: packageField 8.46 +WarnSerializableLambdaTest.java:149:21: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedField 8.47 +WarnSerializableLambdaTest.java:150:21: compiler.warn.access.to.sensitive.member.from.serializable.element: privateField 8.48 +WarnSerializableLambdaTest.java:152:43: compiler.warn.access.to.sensitive.member.from.serializable.element: packageClassMethod(java.lang.String) 8.49 +WarnSerializableLambdaTest.java:153:43: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedClassMethod(java.lang.String) 8.50 +WarnSerializableLambdaTest.java:154:43: compiler.warn.access.to.sensitive.member.from.serializable.element: privateClassMethod(java.lang.String) 8.51 +WarnSerializableLambdaTest.java:156:21: compiler.warn.access.to.sensitive.member.from.serializable.element: packageInstanceMethod(java.lang.String) 8.52 +WarnSerializableLambdaTest.java:157:21: compiler.warn.access.to.sensitive.member.from.serializable.element: protectedInstanceMethod(java.lang.String) 8.53 +WarnSerializableLambdaTest.java:158:21: compiler.warn.access.to.sensitive.member.from.serializable.element: privateInstanceMethod(java.lang.String) 8.54 +WarnSerializableLambdaTest.java:160:29: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicStaticField 8.55 +WarnSerializableLambdaTest.java:161:29: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicClassMethod() 8.56 +WarnSerializableLambdaTest.java:164:18: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicInstanceField 8.57 +WarnSerializableLambdaTest.java:165:18: compiler.warn.access.to.sensitive.member.from.serializable.element: effectivelyNonPublicInstanceMethod() 8.58 +- compiler.err.warnings.and.werror 8.59 +1 error 8.60 +54 warnings
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/T8029102/WarnSerializableLambdaTestb.java Tue May 13 14:18:34 2014 +0100 9.3 @@ -0,0 +1,56 @@ 9.4 +/* 9.5 + * @test /nodynamiccopyright/ 9.6 + * @bug 8029102 9.7 + * @summary Enhance compiler warnings for Lambda 9.8 + * Checks that the warning for accessing non public members of a class is 9.9 + * fired correctly. 9.10 + * @compile/fail/ref=WarnSerializableLambdaTestb.out -XDrawDiagnostics -Werror -XDwarnOnAccessToSensitiveMembers WarnSerializableLambdaTestb.java 9.11 + */ 9.12 + 9.13 +import java.io.Serializable; 9.14 + 9.15 +public class WarnSerializableLambdaTestb { 9.16 + public void foo(Secret1 secret) { 9.17 + Object o = (Runnable & java.io.Serializable) () -> { secret.test(); }; 9.18 + } 9.19 + 9.20 + public void bar(Secret2 secret) { 9.21 + Object o = (Runnable & java.io.Serializable) () -> { secret.test(); }; 9.22 + } 9.23 + 9.24 + private class Secret1 { 9.25 + public void test() {} 9.26 + } 9.27 + 9.28 + static private class Secret2 { 9.29 + public void test() {} 9.30 + } 9.31 + 9.32 + class TestInner { 9.33 + private int j = 0; 9.34 + void m() { 9.35 + Serializable s = new Serializable() { 9.36 + int i; 9.37 + void m() { 9.38 + i = 0; // don't warn 9.39 + System.out.println(j); //warn 9.40 + } 9.41 + }; 9.42 + } 9.43 + } 9.44 + 9.45 + class TestInner2 { 9.46 + class W implements Serializable { 9.47 + public int p = 0; 9.48 + class I { 9.49 + public int r = 0; 9.50 + class K implements Serializable { 9.51 + void m() { 9.52 + p = 1; // don't warn owner is serializable 9.53 + r = 2; // warn owner is not serializable 9.54 + } 9.55 + } 9.56 + } 9.57 + } 9.58 + } 9.59 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/T8029102/WarnSerializableLambdaTestb.out Tue May 13 14:18:34 2014 +0100 10.3 @@ -0,0 +1,7 @@ 10.4 +WarnSerializableLambdaTestb.java:14:69: compiler.warn.access.to.sensitive.member.from.serializable.element: test() 10.5 +WarnSerializableLambdaTestb.java:18:69: compiler.warn.access.to.sensitive.member.from.serializable.element: test() 10.6 +WarnSerializableLambdaTestb.java:36:40: compiler.warn.access.to.sensitive.member.from.serializable.element: j 10.7 +WarnSerializableLambdaTestb.java:50:25: compiler.warn.access.to.sensitive.member.from.serializable.element: r 10.8 +- compiler.err.warnings.and.werror 10.9 +1 error 10.10 +4 warnings
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/test/tools/javac/diags/examples/WarnSerializableLambda.java Tue May 13 14:18:34 2014 +0100 11.3 @@ -0,0 +1,41 @@ 11.4 +/* 11.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 + * 11.8 + * This code is free software; you can redistribute it and/or modify it 11.9 + * under the terms of the GNU General Public License version 2 only, as 11.10 + * published by the Free Software Foundation. 11.11 + * 11.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 11.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.15 + * version 2 for more details (a copy is included in the LICENSE file that 11.16 + * accompanied this code). 11.17 + * 11.18 + * You should have received a copy of the GNU General Public License version 11.19 + * 2 along with this work; if not, write to the Free Software Foundation, 11.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.21 + * 11.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 11.23 + * or visit www.oracle.com if you need additional information or have any 11.24 + * questions. 11.25 + */ 11.26 + 11.27 +// key: compiler.warn.access.to.sensitive.member.from.serializable.element 11.28 +// options: -XDwarnOnAccessToSensitiveMembers 11.29 + 11.30 +import java.io.Serializable; 11.31 + 11.32 +public class WarnSerializableLambda { 11.33 + interface SAM { 11.34 + void apply(String s); 11.35 + } 11.36 + 11.37 + private void m1() { 11.38 + SAM s = (SAM & Serializable) c -> { 11.39 + packageField = ""; 11.40 + }; 11.41 + } 11.42 + 11.43 + String packageField; 11.44 +}