1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T8029102/WarnSerializableLambdaTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,241 @@ 1.4 +/* 1.5 + * @test /nodynamiccopyright/ 1.6 + * @bug 8029102 1.7 + * @summary Enhance compiler warnings for Lambda 1.8 + * Checks that the warning for accessing non public members of a class is 1.9 + * fired correctly. 1.10 + * @compile/fail/ref=WarnSerializableLambdaTest.out -XDrawDiagnostics -Werror -XDwarnOnAccessToSensitiveMembers WarnSerializableLambdaTest.java 1.11 + */ 1.12 + 1.13 +import java.io.Serializable; 1.14 + 1.15 +public class WarnSerializableLambdaTest { 1.16 + 1.17 + void warnLambda() throws Exception { 1.18 + SAM t3 = (SAM & Serializable)WarnSerializableLambdaTest::packageClassMethod; 1.19 + SAM t4 = (SAM & Serializable)WarnSerializableLambdaTest::protectedClassMethod; 1.20 + SAM t5 = (SAM & Serializable)WarnSerializableLambdaTest::privateClassMethod; 1.21 + 1.22 + WarnSerializableLambdaTest test = new WarnSerializableLambdaTest(); 1.23 + SAM t6 = (SAM & Serializable)test::packageInstanceMethod; 1.24 + SAM t7 = (SAM & Serializable)test::protectedInstanceMethod; 1.25 + SAM t8 = (SAM & Serializable)test::privateInstanceMethod; 1.26 + 1.27 + SAM t9 = (SAM & Serializable) c -> { 1.28 + 1.29 + WarnSerializableLambdaTest.staticPackageField = ""; 1.30 + WarnSerializableLambdaTest.staticProtectedField = ""; 1.31 + WarnSerializableLambdaTest.staticPrivateField = ""; 1.32 + 1.33 + packageField = ""; 1.34 + protectedField = ""; 1.35 + privateField = ""; 1.36 + 1.37 + WarnSerializableLambdaTest.packageClassMethod(null); 1.38 + WarnSerializableLambdaTest.protectedClassMethod(null); 1.39 + WarnSerializableLambdaTest.privateClassMethod(null); 1.40 + 1.41 + packageInstanceMethod(null); 1.42 + protectedInstanceMethod(null); 1.43 + privateInstanceMethod(null); 1.44 + 1.45 + PrivateClass.effectivelyNonPublicStaticField = ""; 1.46 + PrivateClass.effectivelyNonPublicClassMethod(); 1.47 + 1.48 + PrivateClass p = new PrivateClass(); 1.49 + p.effectivelyNonPublicInstanceField = ""; 1.50 + p.effectivelyNonPublicInstanceMethod(); 1.51 + 1.52 + return null; 1.53 + }; 1.54 + } 1.55 + 1.56 + private void warnAnoInnerClass() throws Exception { 1.57 + new SerializableDesc() { 1.58 + public void m(Object param) throws Exception { 1.59 + WarnSerializableLambdaTest.staticPackageField = ""; 1.60 + WarnSerializableLambdaTest.staticProtectedField = ""; 1.61 + WarnSerializableLambdaTest.staticPrivateField = ""; 1.62 + 1.63 + packageField = ""; 1.64 + protectedField = ""; 1.65 + privateField = ""; 1.66 + 1.67 + WarnSerializableLambdaTest.packageClassMethod(null); 1.68 + WarnSerializableLambdaTest.protectedClassMethod(null); 1.69 + WarnSerializableLambdaTest.privateClassMethod(null); 1.70 + 1.71 + packageInstanceMethod(null); 1.72 + protectedInstanceMethod(null); 1.73 + privateInstanceMethod(null); 1.74 + 1.75 + PrivateClass.effectivelyNonPublicStaticField = ""; 1.76 + PrivateClass.effectivelyNonPublicClassMethod(); 1.77 + 1.78 + PrivateClass p = new PrivateClass(); 1.79 + p.effectivelyNonPublicInstanceField = ""; 1.80 + p.effectivelyNonPublicInstanceMethod(); 1.81 + } 1.82 + }; 1.83 + } 1.84 + 1.85 + void dontWarnLambda() throws Exception { 1.86 + SAM t1 = (SAM & Serializable)WarnSerializableLambdaTest::publicClassMethod; 1.87 + 1.88 + WarnSerializableLambdaTest test = new WarnSerializableLambdaTest(); 1.89 + SAM t2 = (SAM & Serializable)test::publicInstanceMethod; 1.90 + 1.91 + int[] buffer = {0}; 1.92 + 1.93 + SAM t3 = (SAM & Serializable) param -> { 1.94 + Object localVar; 1.95 + localVar = null; 1.96 + param = null; 1.97 + 1.98 + WarnSerializableLambdaTest.staticPublicField = ""; 1.99 + publicField = ""; 1.100 + WarnSerializableLambdaTest.publicClassMethod(null); 1.101 + publicInstanceMethod(null); 1.102 + 1.103 + PublicClass.effectivelyPublicStaticField = ""; 1.104 + PublicClass.effectivelyPublicClassMethod(); 1.105 + 1.106 + PublicClass p = new PublicClass(); 1.107 + p.effectivelyPublicInstanceField = ""; 1.108 + p.effectivelyPublicInstanceMethod(); 1.109 + 1.110 + int l = buffer.length; 1.111 + 1.112 + return null; 1.113 + }; 1.114 + } 1.115 + 1.116 + private void dontWarnAnoInnerClass() throws Exception { 1.117 + final int[] buffer = {0}; 1.118 + new SerializableDesc() { 1.119 + public void m(Object param) throws Exception { 1.120 + Object localVar; 1.121 + localVar = null; 1.122 + param = null; 1.123 + 1.124 + WarnSerializableLambdaTest.staticPublicField = ""; 1.125 + publicField = ""; 1.126 + WarnSerializableLambdaTest.publicClassMethod(null); 1.127 + publicInstanceMethod(null); 1.128 + 1.129 + PublicClass.effectivelyPublicStaticField = ""; 1.130 + PublicClass.effectivelyPublicClassMethod(); 1.131 + 1.132 + PublicClass p = new PublicClass(); 1.133 + p.effectivelyPublicInstanceField = ""; 1.134 + p.effectivelyPublicInstanceMethod(); 1.135 + 1.136 + int l = buffer.length; 1.137 + } 1.138 + }; 1.139 + } 1.140 + 1.141 + enum WarnEnum { 1.142 + A { 1.143 + public void m() throws Exception { 1.144 + WarnSerializableLambdaTest.staticPackageField = ""; 1.145 + WarnSerializableLambdaTest.staticProtectedField = ""; 1.146 + WarnSerializableLambdaTest.staticPrivateField = ""; 1.147 + 1.148 + WarnSerializableLambdaTest test = 1.149 + new WarnSerializableLambdaTest(); 1.150 + 1.151 + test.packageField = ""; 1.152 + test.protectedField = ""; 1.153 + test.privateField = ""; 1.154 + 1.155 + WarnSerializableLambdaTest.packageClassMethod(null); 1.156 + WarnSerializableLambdaTest.protectedClassMethod(null); 1.157 + WarnSerializableLambdaTest.privateClassMethod(null); 1.158 + 1.159 + test.packageInstanceMethod(null); 1.160 + test.protectedInstanceMethod(null); 1.161 + test.privateInstanceMethod(null); 1.162 + 1.163 + PrivateClass.effectivelyNonPublicStaticField = ""; 1.164 + PrivateClass.effectivelyNonPublicClassMethod(); 1.165 + 1.166 + PrivateClass p = new PrivateClass(); 1.167 + p.effectivelyNonPublicInstanceField = ""; 1.168 + p.effectivelyNonPublicInstanceMethod(); 1.169 + } 1.170 + }; 1.171 + 1.172 + public void m() throws Exception {} 1.173 + } 1.174 + 1.175 + static String staticPackageField; 1.176 + static private String staticPrivateField; 1.177 + static protected String staticProtectedField; 1.178 + static public String staticPublicField; 1.179 + 1.180 + String packageField; 1.181 + private String privateField; 1.182 + protected String protectedField; 1.183 + public String publicField; 1.184 + 1.185 + static Object packageClassMethod(String s) { 1.186 + return null; 1.187 + } 1.188 + 1.189 + static private Object privateClassMethod(String s) { 1.190 + return null; 1.191 + } 1.192 + 1.193 + static protected Object protectedClassMethod(String s) { 1.194 + return null; 1.195 + } 1.196 + 1.197 + static public Object publicClassMethod(String s) { 1.198 + return null; 1.199 + } 1.200 + 1.201 + Object packageInstanceMethod(String s) { 1.202 + return null; 1.203 + } 1.204 + 1.205 + protected Object protectedInstanceMethod(String s) { 1.206 + return null; 1.207 + } 1.208 + 1.209 + private Object privateInstanceMethod(String s) { 1.210 + return null; 1.211 + } 1.212 + 1.213 + public Object publicInstanceMethod(String s) { 1.214 + return null; 1.215 + } 1.216 + 1.217 + interface SAM { 1.218 + Object apply(String s) throws Exception; 1.219 + } 1.220 + 1.221 + interface SAM2 { 1.222 + Object apply(String arg1, String arg2); 1.223 + } 1.224 + 1.225 + class SerializableDesc implements Serializable { 1.226 + public void m(Object param) throws Exception {} 1.227 + } 1.228 + 1.229 + static private class PrivateClass { 1.230 + static public String effectivelyNonPublicStaticField; 1.231 + public String effectivelyNonPublicInstanceField; 1.232 + 1.233 + static public void effectivelyNonPublicClassMethod() {} 1.234 + public void effectivelyNonPublicInstanceMethod() {} 1.235 + } 1.236 + 1.237 + static public class PublicClass { 1.238 + static public String effectivelyPublicStaticField; 1.239 + public String effectivelyPublicInstanceField; 1.240 + 1.241 + static public void effectivelyPublicClassMethod() {} 1.242 + public void effectivelyPublicInstanceMethod() {} 1.243 + } 1.244 +}