langtools/test/tools/javac/T8029102/WarnSerializableLambdaTest.java
changeset 41636 086a3c7a6b56
parent 41627 66c9d5c2a08c
parent 41635 cb3d04878117
child 41637 7b24b4c32ee6
equal deleted inserted replaced
41627:66c9d5c2a08c 41636:086a3c7a6b56
     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=WarnSerializableLambdaTest.out -XDrawDiagnostics -Werror -XDwarnOnAccessToSensitiveMembers WarnSerializableLambdaTest.java
       
     8  */
       
     9 
       
    10 import java.io.Serializable;
       
    11 
       
    12 public class WarnSerializableLambdaTest {
       
    13 
       
    14     void warnLambda() throws Exception {
       
    15         SAM t3 = (SAM & Serializable)WarnSerializableLambdaTest::packageClassMethod;
       
    16         SAM t4 = (SAM & Serializable)WarnSerializableLambdaTest::protectedClassMethod;
       
    17         SAM t5 = (SAM & Serializable)WarnSerializableLambdaTest::privateClassMethod;
       
    18 
       
    19         WarnSerializableLambdaTest test = new WarnSerializableLambdaTest();
       
    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             WarnSerializableLambdaTest.staticPackageField = "";
       
    27             WarnSerializableLambdaTest.staticProtectedField = "";
       
    28             WarnSerializableLambdaTest.staticPrivateField = "";
       
    29 
       
    30             packageField = "";
       
    31             protectedField = "";
       
    32             privateField = "";
       
    33 
       
    34             WarnSerializableLambdaTest.packageClassMethod(null);
       
    35             WarnSerializableLambdaTest.protectedClassMethod(null);
       
    36             WarnSerializableLambdaTest.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                 WarnSerializableLambdaTest.staticPackageField = "";
       
    57                 WarnSerializableLambdaTest.staticProtectedField = "";
       
    58                 WarnSerializableLambdaTest.staticPrivateField = "";
       
    59 
       
    60                 packageField = "";
       
    61                 protectedField = "";
       
    62                 privateField = "";
       
    63 
       
    64                 WarnSerializableLambdaTest.packageClassMethod(null);
       
    65                 WarnSerializableLambdaTest.protectedClassMethod(null);
       
    66                 WarnSerializableLambdaTest.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)WarnSerializableLambdaTest::publicClassMethod;
       
    84 
       
    85         WarnSerializableLambdaTest test = new WarnSerializableLambdaTest();
       
    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             WarnSerializableLambdaTest.staticPublicField = "";
       
    96             publicField = "";
       
    97             WarnSerializableLambdaTest.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                 WarnSerializableLambdaTest.staticPublicField = "";
       
   122                 publicField = "";
       
   123                 WarnSerializableLambdaTest.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                 WarnSerializableLambdaTest.staticPackageField = "";
       
   142                 WarnSerializableLambdaTest.staticProtectedField = "";
       
   143                 WarnSerializableLambdaTest.staticPrivateField = "";
       
   144 
       
   145                 WarnSerializableLambdaTest test =
       
   146                         new WarnSerializableLambdaTest();
       
   147 
       
   148                 test.packageField = "";
       
   149                 test.protectedField = "";
       
   150                 test.privateField = "";
       
   151 
       
   152                 WarnSerializableLambdaTest.packageClassMethod(null);
       
   153                 WarnSerializableLambdaTest.protectedClassMethod(null);
       
   154                 WarnSerializableLambdaTest.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 }