test/tools/javac/T8029102/WarnSerializableLambdaTest.java

Mon, 26 Oct 2015 13:23:30 -0700

author
asaha
date
Mon, 26 Oct 2015 13:23:30 -0700
changeset 2999
683b3e7e05a7
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8u76-b00 for changeset 10ffafaf5340

     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  */
    10 import java.io.Serializable;
    12 public class WarnSerializableLambdaTest {
    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;
    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;
    24         SAM t9 = (SAM & Serializable) c -> {
    26             WarnSerializableLambdaTest.staticPackageField = "";
    27             WarnSerializableLambdaTest.staticProtectedField = "";
    28             WarnSerializableLambdaTest.staticPrivateField = "";
    30             packageField = "";
    31             protectedField = "";
    32             privateField = "";
    34             WarnSerializableLambdaTest.packageClassMethod(null);
    35             WarnSerializableLambdaTest.protectedClassMethod(null);
    36             WarnSerializableLambdaTest.privateClassMethod(null);
    38             packageInstanceMethod(null);
    39             protectedInstanceMethod(null);
    40             privateInstanceMethod(null);
    42             PrivateClass.effectivelyNonPublicStaticField = "";
    43             PrivateClass.effectivelyNonPublicClassMethod();
    45             PrivateClass p = new PrivateClass();
    46             p.effectivelyNonPublicInstanceField = "";
    47             p.effectivelyNonPublicInstanceMethod();
    49             return null;
    50         };
    51     }
    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 = "";
    60                 packageField = "";
    61                 protectedField = "";
    62                 privateField = "";
    64                 WarnSerializableLambdaTest.packageClassMethod(null);
    65                 WarnSerializableLambdaTest.protectedClassMethod(null);
    66                 WarnSerializableLambdaTest.privateClassMethod(null);
    68                 packageInstanceMethod(null);
    69                 protectedInstanceMethod(null);
    70                 privateInstanceMethod(null);
    72                 PrivateClass.effectivelyNonPublicStaticField = "";
    73                 PrivateClass.effectivelyNonPublicClassMethod();
    75                 PrivateClass p = new PrivateClass();
    76                 p.effectivelyNonPublicInstanceField = "";
    77                 p.effectivelyNonPublicInstanceMethod();
    78             }
    79         };
    80     }
    82     void dontWarnLambda() throws Exception {
    83         SAM t1 = (SAM & Serializable)WarnSerializableLambdaTest::publicClassMethod;
    85         WarnSerializableLambdaTest test = new WarnSerializableLambdaTest();
    86         SAM t2 = (SAM & Serializable)test::publicInstanceMethod;
    88         int[] buffer = {0};
    90         SAM t3 = (SAM & Serializable) param -> {
    91             Object localVar;
    92             localVar = null;
    93             param = null;
    95             WarnSerializableLambdaTest.staticPublicField = "";
    96             publicField = "";
    97             WarnSerializableLambdaTest.publicClassMethod(null);
    98             publicInstanceMethod(null);
   100             PublicClass.effectivelyPublicStaticField = "";
   101             PublicClass.effectivelyPublicClassMethod();
   103             PublicClass p = new PublicClass();
   104             p.effectivelyPublicInstanceField = "";
   105             p.effectivelyPublicInstanceMethod();
   107             int l = buffer.length;
   109             return null;
   110         };
   111     }
   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;
   121                 WarnSerializableLambdaTest.staticPublicField = "";
   122                 publicField = "";
   123                 WarnSerializableLambdaTest.publicClassMethod(null);
   124                 publicInstanceMethod(null);
   126                 PublicClass.effectivelyPublicStaticField = "";
   127                 PublicClass.effectivelyPublicClassMethod();
   129                 PublicClass p = new PublicClass();
   130                 p.effectivelyPublicInstanceField = "";
   131                 p.effectivelyPublicInstanceMethod();
   133                 int l = buffer.length;
   134             }
   135         };
   136     }
   138     enum WarnEnum {
   139         A {
   140             public void m() throws Exception {
   141                 WarnSerializableLambdaTest.staticPackageField = "";
   142                 WarnSerializableLambdaTest.staticProtectedField = "";
   143                 WarnSerializableLambdaTest.staticPrivateField = "";
   145                 WarnSerializableLambdaTest test =
   146                         new WarnSerializableLambdaTest();
   148                 test.packageField = "";
   149                 test.protectedField = "";
   150                 test.privateField = "";
   152                 WarnSerializableLambdaTest.packageClassMethod(null);
   153                 WarnSerializableLambdaTest.protectedClassMethod(null);
   154                 WarnSerializableLambdaTest.privateClassMethod(null);
   156                 test.packageInstanceMethod(null);
   157                 test.protectedInstanceMethod(null);
   158                 test.privateInstanceMethod(null);
   160                 PrivateClass.effectivelyNonPublicStaticField = "";
   161                 PrivateClass.effectivelyNonPublicClassMethod();
   163                 PrivateClass p = new PrivateClass();
   164                 p.effectivelyNonPublicInstanceField = "";
   165                 p.effectivelyNonPublicInstanceMethod();
   166             }
   167         };
   169         public void m() throws Exception {}
   170     }
   172     static String staticPackageField;
   173     static private String staticPrivateField;
   174     static protected String staticProtectedField;
   175     static public String staticPublicField;
   177     String packageField;
   178     private String privateField;
   179     protected String protectedField;
   180     public String publicField;
   182     static Object packageClassMethod(String s) {
   183         return null;
   184     }
   186     static private Object privateClassMethod(String s) {
   187         return null;
   188     }
   190     static protected Object protectedClassMethod(String s) {
   191         return null;
   192     }
   194     static public Object publicClassMethod(String s) {
   195         return null;
   196     }
   198     Object packageInstanceMethod(String s) {
   199         return null;
   200     }
   202     protected Object protectedInstanceMethod(String s) {
   203         return null;
   204     }
   206     private Object privateInstanceMethod(String s) {
   207         return null;
   208     }
   210     public Object publicInstanceMethod(String s) {
   211         return null;
   212     }
   214     interface SAM {
   215         Object apply(String s) throws Exception;
   216     }
   218     interface SAM2 {
   219         Object apply(String arg1, String arg2);
   220     }
   222     class SerializableDesc implements Serializable {
   223         public void m(Object param) throws Exception {}
   224     }
   226     static private class PrivateClass {
   227         static public String effectivelyNonPublicStaticField;
   228         public String effectivelyNonPublicInstanceField;
   230         static public void effectivelyNonPublicClassMethod() {}
   231         public void effectivelyNonPublicInstanceMethod() {}
   232     }
   234     static public class PublicClass {
   235         static public String effectivelyPublicStaticField;
   236         public String effectivelyPublicInstanceField;
   238         static public void effectivelyPublicClassMethod() {}
   239         public void effectivelyPublicInstanceMethod() {}
   240     }
   241 }

mercurial