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 
  82     @BeforeTest
  83     public void setup() throws IOException {
  84         String filename = INNER_CLASSNAME.replace('.', File.separatorChar) + ".class";
  85         Path src = Paths.get(System.getProperty("test.classes"), filename);
  86         Path dest = Paths.get(DIR, filename);
  87         Files.createDirectories(dest.getParent());
  88         Files.copy(src, dest, REPLACE_EXISTING);
  89     }
  90 
  91     @Test
  92     public void test() throws Exception {
  93         Class<?> inner = Class.forName(INNER_CLASSNAME);
  94         // inner class is a nest member of LambdaNestedInnerTest
  95         Class<?> nestHost = inner.getNestHost();
  96         assertTrue(nestHost == LambdaNestedInnerTest.class);
  97         Set<Class<?>> members = Arrays.stream(nestHost.getNestMembers()).collect(Collectors.toSet());
  98         assertEquals(members, Set.of(nestHost, inner, TestLoader.class));
  99 
 100         // spin lambda proxy hidden class
 101         Runnable runnable = (Runnable) inner.newInstance();
 102         runnable.run();
 103     }
 104 
 105     @Test
 106     public void nestHostNotExist() throws Exception {
 107         URL[] urls = new URL[] { Paths.get(DIR).toUri().toURL() };
 108         URLClassLoader loader = new URLClassLoader(urls, null);
 109         Class<?> inner = loader.loadClass(INNER_CLASSNAME);
 110         assertTrue(inner.getClassLoader() == loader);
 111         assertTrue(inner.getNestHost() == inner);   // linkage error ignored
 112 
 113         Runnable runnable = (Runnable) inner.newInstance();
 114         // this validates the lambda proxy class
 115         runnable.run();
 116     }
 117 
 118     /*
 119      * Tests IncompatibleClassChangeError thrown since the true nest host is not
 120      * in the same runtime package as the hidden class
 121      */
 122     @Test
 123     public void nestHostNotSamePackage() throws Exception {
 124         URL[] urls = new URL[] { Paths.get(DIR).toUri().toURL() };
 125         TestLoader loader = new TestLoader(urls);
 126 
 127         Class<?> inner = loader.loadClass(INNER_CLASSNAME);
 128         assertTrue(inner.getClassLoader() == loader);
 129         assertTrue(inner.getNestHost() == inner);   // linkage error ignored.
 130 
 131         Runnable runnable = (Runnable) inner.newInstance();
 132         // this validates the lambda proxy class
 133         runnable.run();
 134     }
 135 
 136     static class TestLoader extends URLClassLoader {
 137         TestLoader(URL[] urls) {
 138             super(urls, TestLoader.class.getClassLoader());
 139         }
 140         public Class<?> loadClass(String name) throws ClassNotFoundException {
 141             if (INNER_CLASSNAME.equals(name)) {
 142                 return findClass(name);
 143             } else {
 144                 // delegate to its parent
 145                 return loadClass(name, false);
 146             }
 147         }
 148     }
 149 }