< prev index next >

src/java.base/share/classes/java/lang/reflect/Proxy.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -357,14 +357,15 @@
      *             s.checkPackageAccess()} denies access to {@code intf}.</li>
      *          </ul>
      * @throws  NullPointerException if the {@code interfaces} array
      *          argument or any of its elements are {@code null}
      *
-     * @deprecated Proxy classes generated in a named module are encapsulated and not
-     *      accessible to code outside its module.
-     *      {@link Constructor#newInstance(Object...) Constructor.newInstance} will throw
-     *      {@code IllegalAccessException} when it is called on an inaccessible proxy class.
+     * @deprecated Proxy classes generated in a named module are encapsulated
+     *      and not accessible to code outside its module.
+     *      {@link Constructor#newInstance(Object...) Constructor.newInstance}
+     *      will throw {@code IllegalAccessException} when it is called on
+     *      an inaccessible proxy class.
      *      Use {@link #newProxyInstance(ClassLoader, Class[], InvocationHandler)}
      *      to create a proxy instance instead.
      *
      * @see <a href="#membership">Package and Module Membership of Proxy Class</a>
      * @revised 9

@@ -509,20 +510,22 @@
             } else if (proxyPkg.isEmpty() && m.isNamed()) {
                 throw new IllegalArgumentException(
                         "Unnamed package cannot be added to " + m);
             }
 
-            // add the package to the runtime module if not exists
             if (m.isNamed()) {
-                m.addPackage(proxyPkg);
+                if (!m.getDescriptor().packages().contains(proxyPkg)) {
+                    throw new InternalError(proxyPkg + " not exist in " + m.getName());
+                }
             }
 
             /*
              * Choose a name for the proxy class to generate.
              */
             long num = nextUniqueNumber.getAndIncrement();
-            String proxyName = proxyPkg.isEmpty() ? proxyClassNamePrefix + num
+            String proxyName = proxyPkg.isEmpty()
+                                    ? proxyClassNamePrefix + num
                                                   : proxyPkg + "." + proxyClassNamePrefix + num;
 
             ClassLoader loader = getLoader(m);
             trace(proxyName, m, loader, interfaces);
 

@@ -579,13 +582,17 @@
             ClassLoader ld = c.getClassLoader();
             return String.format("   %s/%s %s loader %s",
                     c.getModule().getName(), c.getName(), access, ld);
         }
 
-        static void trace(String cn, Module module, ClassLoader loader, List<Class<?>> interfaces) {
+        static void trace(String cn,
+                          Module module,
+                          ClassLoader loader,
+                          List<Class<?>> interfaces) {
             if (isDebug()) {
-                System.out.format("PROXY: %s/%s defined by %s%n", module.getName(), cn, loader);
+                System.err.format("PROXY: %s/%s defined by %s%n",
+                                  module.getName(), cn, loader);
             }
             if (isDebug("debug")) {
                 interfaces.stream()
                           .forEach(c -> System.out.println(toDetails(c)));
             }

@@ -601,27 +608,27 @@
             return DEBUG.equals(flag);
         }
 
         // ProxyBuilder instance members start here....
 
-        private final ClassLoader loader;
         private final List<Class<?>> interfaces;
         private final Module module;
         ProxyBuilder(ClassLoader loader, List<Class<?>> interfaces) {
             if (!VM.isModuleSystemInited()) {
-                throw new InternalError("Proxy is not supported until module system is fully initialized");
+                throw new InternalError("Proxy is not supported until "
+                        + "module system is fully initialized");
             }
             if (interfaces.size() > 65535) {
-                throw new IllegalArgumentException("interface limit exceeded: " + interfaces.size());
+                throw new IllegalArgumentException("interface limit exceeded: "
+                        + interfaces.size());
             }
 
             Set<Class<?>> refTypes = referencedTypes(loader, interfaces);
 
             // IAE if violates any restrictions specified in newProxyInstance
             validateProxyInterfaces(loader, interfaces, refTypes);
 
-            this.loader = loader;
             this.interfaces = interfaces;
             this.module = mapToModule(loader, interfaces, refTypes);
             assert getLoader(module) == loader;
         }
 

@@ -657,12 +664,12 @@
 
         /**
          * Validate the given proxy interfaces and the given referenced types
          * are visible to the defining loader.
          *
-         * @throws IllegalArgumentException if it violates the restrictions specified
-         *         in {@link Proxy#newProxyInstance}
+         * @throws IllegalArgumentException if it violates the restrictions
+         *         specified in {@link Proxy#newProxyInstance}
          */
         private static void validateProxyInterfaces(ClassLoader loader,
                                                     List<Class<?>> interfaces,
                                                     Set<Class<?>> refTypes)
         {

@@ -729,13 +736,13 @@
          *
          * If any of proxy interface is package-private, then the proxy class
          * is in the same module of the package-private interface.
          *
          * If all proxy interfaces are public and at least one in a non-exported
-         * package, then the proxy class is in a dynamic module in a non-exported
-         * package.  Reads edge and qualified exports are added for
-         * dynamic module to access.
+         * package, then the proxy class is in a dynamic module in a
+         * non-exported package.  Reads edge and qualified exports are added
+         * for dynamic module to access.
          */
         private static Module mapToModule(ClassLoader loader,
                                           List<Class<?>> interfaces,
                                           Set<Class<?>> refTypes) {
             Map<Class<?>, Module> modulePrivateTypes = new HashMap<>();

@@ -750,15 +757,16 @@
                 } else {
                     packagePrivateTypes.put(intf, m);
                 }
             }
 
-            // all proxy interfaces are public and exported, the proxy class is in unnamed module
-            // Such proxy class is accessible to any unnamed module and named module that
-            // can read unnamed module
+            // all proxy interfaces are public and exported, the proxy class
+            // is in unnamed module.  Such proxy class is accessible to
+            // any unnamed module and named module that can read unnamed module
             if (packagePrivateTypes.isEmpty() && modulePrivateTypes.isEmpty()) {
-                return loader != null ? loader.getUnnamedModule() : BootLoader.getUnnamedModule();
+                return loader != null ? loader.getUnnamedModule()
+                                      : BootLoader.getUnnamedModule();
             }
 
             if (packagePrivateTypes.size() > 0) {
                 // all package-private types must be in the same runtime package
                 // i.e. same package name and same module (named or unnamed)

@@ -776,11 +784,12 @@
 
                 // all package-private types are in the same module (named or unnamed)
                 Module target = null;
                 for (Module m : packagePrivateTypes.values()) {
                     if (getLoader(m) != loader) {
-                        // the specified loader is not the same class loader of the non-public interface
+                        // the specified loader is not the same class loader
+                        // of the non-public interface
                         throw new IllegalArgumentException(
                                 "non-public interface is not defined by the given loader");
                     }
                     target = m;
                 }

@@ -797,12 +806,13 @@
 
                 // return the module of the package-private interface
                 return target;
             }
 
-            // all proxy interfaces are public and at least one in a non-exported package
-            // map to dynamic proxy module and add reads edge and qualified exports, if necessary
+            // All proxy interfaces are public and at least one in a non-exported
+            // package.  So maps to a dynamic proxy module and add reads edge
+            // and qualified exports, if necessary
             Module target = getDynamicModule(loader);
 
             // set up proxy class access to proxy interfaces and types
             // referenced in the method signature
             Set<Class<?>> types = new HashSet<>(interfaces);

@@ -854,12 +864,12 @@
         private static final ClassLoaderValue<Module> dynProxyModules =
             new ClassLoaderValue<>();
         private static final AtomicInteger counter = new AtomicInteger();
 
         /*
-         * Define a dynamic module for the generated proxy classes in a non-exported package
-         * named com.sun.proxy.$MODULE.
+         * Define a dynamic module for the generated proxy classes in
+         * a non-exported package named com.sun.proxy.$MODULE.
          *
          * Each class loader will have one dynamic module.
          */
         private static Module getDynamicModule(ClassLoader loader) {
             return dynProxyModules.computeIfAbsent(loader, (ld, clv) -> {
< prev index next >