1 /*
   2  * Copyright (c) 2015, 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 import java.lang.reflect.Constructor;
  25 import java.lang.reflect.InvocationHandler;
  26 import java.lang.reflect.Module;
  27 import java.lang.reflect.Proxy;
  28 
  29 /*
  30  * @test
  31  * @summary Basic test of proxy module mapping and the access to Proxy class
  32  * @modules java.base/sun.invoke
  33  */
  34 
  35 public class ProxyModuleMapping {
  36     public static void main(String... args) throws Exception {
  37         ClassLoader ld = ProxyModuleMapping.class.getClassLoader();
  38         Module unnamed = ld.getUnnamedModule();
  39         new ProxyModuleMapping(unnamed, Runnable.class).test();
  40 
  41         // unnamed module gets access to sun.invoke package (e.g. via --add-exports)
  42         new ProxyModuleMapping(sun.invoke.WrapperInstance.class).test();
  43 
  44         Class<?> modulePrivateIntf = Class.forName("sun.net.ProgressListener");
  45         new ProxyModuleMapping(modulePrivateIntf).test();
  46     }
  47 
  48     final Module target;
  49     final ClassLoader loader;
  50     final Class<?>[] interfaces;
  51     ProxyModuleMapping(Module m, Class<?>... interfaces) {
  52         this.target = m;
  53         this.loader = m.getClassLoader();
  54         this.interfaces = interfaces;
  55     }
  56 
  57     ProxyModuleMapping(Class<?>... interfaces) {
  58         this.target = null;  // expected to be dynamic module
  59         this.loader = interfaces[0].getClassLoader();   // same class loader
  60         this.interfaces = interfaces;
  61     }
  62 
  63     void test() throws Exception {
  64         verifyProxyClass();
  65         verifyNewProxyInstance();
  66     }
  67 
  68     void verifyProxyClass() throws Exception {
  69         Class<?> c = Proxy.getProxyClass(loader, interfaces);
  70         Module m = c.getModule();
  71         if (target != null && m != target) {
  72             throw new RuntimeException(c.getModule() + " not expected: " + target);
  73         }
  74         // expect dynamic module
  75         if (target == null && (!m.isNamed() || !m.getName().startsWith("jdk.proxy"))) {
  76             throw new RuntimeException("Unexpected:" + m);
  77         }
  78 
  79         Module module = c.getModule();
  80         try {
  81             Constructor<?> cons = c.getConstructor(InvocationHandler.class);
  82             cons.newInstance(ih);
  83             if (module.isNamed()) {
  84                 throw new RuntimeException("expected IAE not thrown");
  85             }
  86         } catch (IllegalAccessException e) {
  87             if (!module.isNamed()) {
  88                 throw e;
  89             }
  90         }
  91     }
  92 
  93     void verifyNewProxyInstance() throws Exception {
  94         Object o = Proxy.newProxyInstance(loader, interfaces, ih);
  95         Module m = o.getClass().getModule();
  96         if (target != null && m != target) {
  97             throw new RuntimeException(m + " not expected: " + target);
  98         }
  99         if (target == null && (!m.isNamed() || !m.getName().startsWith("jdk.proxy"))) {
 100             throw new RuntimeException(m + " not expected: dynamic module");
 101         }
 102     }
 103     private final static InvocationHandler ih =
 104         (proxy, m, params) -> { System.out.println(m); return null; };
 105 }