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