1/* 2 * @test /nodynamiccopyright/ 3 * @bug 8029102 4 * @summary Enhance compiler warnings for Lambda 5 * Checks that the warning for accessing non public members of a class is 6 * fired correctly. 7 * @compile/fail/ref=WarnSerializableElementTest.out -XDrawDiagnostics -Werror -XDwarnOnAccessToMembers WarnSerializableElementTest.java 8 */ 9 10import java.io.Serializable; 11 12public class WarnSerializableElementTest { 13 14 void warnLambda() throws Exception { 15 SAM t3 = (SAM & Serializable)WarnSerializableElementTest::packageClassMethod; 16 SAM t4 = (SAM & Serializable)WarnSerializableElementTest::protectedClassMethod; 17 SAM t5 = (SAM & Serializable)WarnSerializableElementTest::privateClassMethod; 18 19 WarnSerializableElementTest test = new WarnSerializableElementTest(); 20 SAM t6 = (SAM & Serializable)test::packageInstanceMethod; 21 SAM t7 = (SAM & Serializable)test::protectedInstanceMethod; 22 SAM t8 = (SAM & Serializable)test::privateInstanceMethod; 23 24 SAM t9 = (SAM & Serializable) c -> { 25 26 WarnSerializableElementTest.staticPackageField = ""; 27 WarnSerializableElementTest.staticProtectedField = ""; 28 WarnSerializableElementTest.staticPrivateField = ""; 29 30 packageField = ""; 31 protectedField = ""; 32 privateField = ""; 33 34 WarnSerializableElementTest.packageClassMethod(null); 35 WarnSerializableElementTest.protectedClassMethod(null); 36 WarnSerializableElementTest.privateClassMethod(null); 37 38 packageInstanceMethod(null); 39 protectedInstanceMethod(null); 40 privateInstanceMethod(null); 41 42 PrivateClass.effectivelyNonPublicStaticField = ""; 43 PrivateClass.effectivelyNonPublicClassMethod(); 44 45 PrivateClass p = new PrivateClass(); 46 p.effectivelyNonPublicInstanceField = ""; 47 p.effectivelyNonPublicInstanceMethod(); 48 49 return null; 50 }; 51 } 52 53 private void warnAnoInnerClass() throws Exception { 54 new SerializableDesc() { 55 public void m(Object param) throws Exception { 56 WarnSerializableElementTest.staticPackageField = ""; 57 WarnSerializableElementTest.staticProtectedField = ""; 58 WarnSerializableElementTest.staticPrivateField = ""; 59 60 packageField = ""; 61 protectedField = ""; 62 privateField = ""; 63 64 WarnSerializableElementTest.packageClassMethod(null); 65 WarnSerializableElementTest.protectedClassMethod(null); 66 WarnSerializableElementTest.privateClassMethod(null); 67 68 packageInstanceMethod(null); 69 protectedInstanceMethod(null); 70 privateInstanceMethod(null); 71 72 PrivateClass.effectivelyNonPublicStaticField = ""; 73 PrivateClass.effectivelyNonPublicClassMethod(); 74 75 PrivateClass p = new PrivateClass(); 76 p.effectivelyNonPublicInstanceField = ""; 77 p.effectivelyNonPublicInstanceMethod(); 78 } 79 }; 80 } 81 82 void dontWarnLambda() throws Exception { 83 SAM t1 = (SAM & Serializable)WarnSerializableElementTest::publicClassMethod; 84 85 WarnSerializableElementTest test = new WarnSerializableElementTest(); 86 SAM t2 = (SAM & Serializable)test::publicInstanceMethod; 87 88 int[] buffer = {0}; 89 90 SAM t3 = (SAM & Serializable) param -> { 91 Object localVar; 92 localVar = null; 93 param = null; 94 95 WarnSerializableElementTest.staticPublicField = ""; 96 publicField = ""; 97 WarnSerializableElementTest.publicClassMethod(null); 98 publicInstanceMethod(null); 99 100 PublicClass.effectivelyPublicStaticField = ""; 101 PublicClass.effectivelyPublicClassMethod(); 102 103 PublicClass p = new PublicClass(); 104 p.effectivelyPublicInstanceField = ""; 105 p.effectivelyPublicInstanceMethod(); 106 107 int l = buffer.length; 108 109 return null; 110 }; 111 } 112 113 private void dontWarnAnoInnerClass() throws Exception { 114 final int[] buffer = {0}; 115 new SerializableDesc() { 116 public void m(Object param) throws Exception { 117 Object localVar; 118 localVar = null; 119 param = null; 120 121 WarnSerializableElementTest.staticPublicField = ""; 122 publicField = ""; 123 WarnSerializableElementTest.publicClassMethod(null); 124 publicInstanceMethod(null); 125 126 PublicClass.effectivelyPublicStaticField = ""; 127 PublicClass.effectivelyPublicClassMethod(); 128 129 PublicClass p = new PublicClass(); 130 p.effectivelyPublicInstanceField = ""; 131 p.effectivelyPublicInstanceMethod(); 132 133 int l = buffer.length; 134 } 135 }; 136 } 137 138 enum WarnEnum { 139 A { 140 public void m() throws Exception { 141 WarnSerializableElementTest.staticPackageField = ""; 142 WarnSerializableElementTest.staticProtectedField = ""; 143 WarnSerializableElementTest.staticPrivateField = ""; 144 145 WarnSerializableElementTest test = 146 new WarnSerializableElementTest(); 147 148 test.packageField = ""; 149 test.protectedField = ""; 150 test.privateField = ""; 151 152 WarnSerializableElementTest.packageClassMethod(null); 153 WarnSerializableElementTest.protectedClassMethod(null); 154 WarnSerializableElementTest.privateClassMethod(null); 155 156 test.packageInstanceMethod(null); 157 test.protectedInstanceMethod(null); 158 test.privateInstanceMethod(null); 159 160 PrivateClass.effectivelyNonPublicStaticField = ""; 161 PrivateClass.effectivelyNonPublicClassMethod(); 162 163 PrivateClass p = new PrivateClass(); 164 p.effectivelyNonPublicInstanceField = ""; 165 p.effectivelyNonPublicInstanceMethod(); 166 } 167 }; 168 169 public void m() throws Exception {} 170 } 171 172 static String staticPackageField; 173 static private String staticPrivateField; 174 static protected String staticProtectedField; 175 static public String staticPublicField; 176 177 String packageField; 178 private String privateField; 179 protected String protectedField; 180 public String publicField; 181 182 static Object packageClassMethod(String s) { 183 return null; 184 } 185 186 static private Object privateClassMethod(String s) { 187 return null; 188 } 189 190 static protected Object protectedClassMethod(String s) { 191 return null; 192 } 193 194 static public Object publicClassMethod(String s) { 195 return null; 196 } 197 198 Object packageInstanceMethod(String s) { 199 return null; 200 } 201 202 protected Object protectedInstanceMethod(String s) { 203 return null; 204 } 205 206 private Object privateInstanceMethod(String s) { 207 return null; 208 } 209 210 public Object publicInstanceMethod(String s) { 211 return null; 212 } 213 214 interface SAM { 215 Object apply(String s) throws Exception; 216 } 217 218 interface SAM2 { 219 Object apply(String arg1, String arg2); 220 } 221 222 class SerializableDesc implements Serializable { 223 public void m(Object param) throws Exception {} 224 } 225 226 static private class PrivateClass { 227 static public String effectivelyNonPublicStaticField; 228 public String effectivelyNonPublicInstanceField; 229 230 static public void effectivelyNonPublicClassMethod() {} 231 public void effectivelyNonPublicInstanceMethod() {} 232 } 233 234 static public class PublicClass { 235 static public String effectivelyPublicStaticField; 236 public String effectivelyPublicInstanceField; 237 238 static public void effectivelyPublicClassMethod() {} 239 public void effectivelyPublicInstanceMethod() {} 240 } 241} 242