1 /*
   2  * Copyright (c) 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  * @bug 8239384
  27  * @library /test/lib
  28  * @build jdk.test.lib.Utils
  29  *        jdk.test.lib.compiler.CompilerUtils
  30  * @run testng/othervm InheritedProtectedMethod
  31  * @summary Test method reference to a method inherited from its
  32  *          superclass in a different package.  Such method's modifier
  33  *          is changed from public to protected.
  34  */
  35 
  36 import jdk.test.lib.compiler.CompilerUtils;
  37 import jdk.test.lib.Utils;
  38 
  39 import org.testng.annotations.BeforeTest;
  40 import org.testng.annotations.DataProvider;
  41 import org.testng.annotations.Test;
  42 
  43 import java.io.IOException;
  44 import java.lang.reflect.Method;
  45 import java.net.URL;
  46 import java.net.URLClassLoader;
  47 import java.nio.file.Path;
  48 import java.nio.file.Paths;
  49 
  50 import static org.testng.Assert.*;
  51 
  52 public class InheritedProtectedMethod {
  53     private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "src");
  54     private static final Path CLASSES_DIR = Paths.get("classes");
  55 
  56     @BeforeTest
  57     static void setup() throws IOException {
  58         assertTrue(CompilerUtils.compile(SRC_DIR, CLASSES_DIR));
  59 
  60         // compile the modified version of MethodSupplierOuter.java
  61         Path file = Paths.get(Utils.TEST_SRC, "modified", "MethodSupplierOuter.java");
  62         assertTrue(CompilerUtils.compile(file, CLASSES_DIR));
  63     }
  64 
  65     @Test
  66     public static void run() throws Exception {
  67         URLClassLoader loader = new URLClassLoader("loader", new URL[]{ CLASSES_DIR.toUri().toURL()},
  68                 ClassLoader.getPlatformClassLoader());
  69         Class<?> methodInvokeClass = Class.forName("MethodInvoker", false, loader);
  70         Method invokeMethod = methodInvokeClass.getMethod("invoke");
  71 
  72         String result = (String)invokeMethod.invoke(null);
  73         assertEquals(result, "protected inherited method");
  74     }
  75 }