< prev index next >

test/jdk/java/lang/invoke/defineHiddenClass/LambdaNestedInnerTest.java

Print this page
rev 58565 : 8238358: Implementation of JEP 371: Hidden Classes
Reviewed-by: duke
Contributed-by: mandy.chung@oracle.com, lois.foltan@oracle.com, david.holmes@oracle.com, harold.seigel@oracle.com, serguei.spitsyn@oracle.com, alex.buckley@oracle.com, jamsheed.c.m@oracle.com
rev 58567 : [mq]: rename-isHidden
rev 58568 : [mq]: hidden-class-4
   1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @build  LambdaNestedInnerTest
  27  * @run testng/othervm p.LambdaNestedInnerTest
  28  * @summary define a lambda proxy class whose target class has an invalid
  29  *          nest membership

  30  */
  31 
  32 package p;
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.net.URL;
  37 import java.net.URLClassLoader;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.Arrays;
  42 import java.util.Set;
  43 import java.util.stream.Collectors;
  44 
  45 import org.testng.annotations.BeforeTest;
  46 import org.testng.annotations.Test;
  47 
  48 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  49 import static org.testng.Assert.*;
  50 
  51 public class LambdaNestedInnerTest {
  52     private static final String INNER_CLASSNAME = "p.LambdaNestedInnerTest$Inner";
  53     private static final String DIR = "missingOuter";
  54     public static class Inner implements Runnable {
  55         // generate lambda proxy class
  56         private Runnable lambda1 = this::doit;
  57 
  58         @Override
  59         public void run() {
  60             // validate the lambda proxy class
  61             Class<?> lambdaProxyClass = lambda1.getClass();
  62             assertTrue(lambdaProxyClass.isHiddenClass());
  63             System.out.format("%s nest host %s nestmate of Inner class %s%n",
  64                     lambdaProxyClass, lambdaProxyClass.getNestHost(),
  65                     lambdaProxyClass.isNestmateOf(Inner.class));
  66             assertTrue(lambdaProxyClass.getNestHost() == Inner.class.getNestHost());
  67             assertTrue(Arrays.equals(lambdaProxyClass.getNestMembers(), Inner.class.getNestMembers()));
  68             assertTrue(lambdaProxyClass.isNestmateOf(Inner.class));
  69             lambda1.run();
  70         }
  71 
  72         // testng may not be visible to this class
  73         private static void assertTrue(boolean x) {
  74             if (!x) {
  75                 throw new AssertionError("expected true but found false");
  76             }
  77         }
  78 
  79         private void doit() {
  80         }
  81     }
  82 


   1 /*
   2  * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test


  26  * @summary define a lambda proxy class whose target class has an invalid
  27  *          nest membership
  28  * @run testng/othervm p.LambdaNestedInnerTest
  29  */
  30 
  31 package p;
  32 
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.net.URL;
  36 import java.net.URLClassLoader;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Arrays;
  41 import java.util.Set;
  42 import java.util.stream.Collectors;
  43 
  44 import org.testng.annotations.BeforeTest;
  45 import org.testng.annotations.Test;
  46 
  47 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  48 import static org.testng.Assert.*;
  49 
  50 public class LambdaNestedInnerTest {
  51     private static final String INNER_CLASSNAME = "p.LambdaNestedInnerTest$Inner";
  52     private static final String DIR = "missingOuter";
  53     public static class Inner implements Runnable {
  54         // generate lambda proxy class
  55         private Runnable lambda1 = this::doit;
  56 
  57         @Override
  58         public void run() {
  59             // validate the lambda proxy class
  60             Class<?> lambdaProxyClass = lambda1.getClass();
  61             assertTrue(lambdaProxyClass.isHidden());
  62             System.out.format("%s nest host %s nestmate of Inner class %s%n",
  63                     lambdaProxyClass, lambdaProxyClass.getNestHost(),
  64                     lambdaProxyClass.isNestmateOf(Inner.class));
  65             assertTrue(lambdaProxyClass.getNestHost() == Inner.class.getNestHost());
  66             assertTrue(Arrays.equals(lambdaProxyClass.getNestMembers(), Inner.class.getNestMembers()));
  67             assertTrue(lambdaProxyClass.isNestmateOf(Inner.class));
  68             lambda1.run();
  69         }
  70 
  71         // testng may not be visible to this class
  72         private static void assertTrue(boolean x) {
  73             if (!x) {
  74                 throw new AssertionError("expected true but found false");
  75             }
  76         }
  77 
  78         private void doit() {
  79         }
  80     }
  81 


< prev index next >