< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1999, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 281  * @since       1.3
 282  * @revised 9
 283  * @spec JPMS
 284  */
 285 public class Proxy implements java.io.Serializable {
 286     @java.io.Serial
 287     private static final long serialVersionUID = -2222568056686623797L;
 288 
 289     /** parameter types of a proxy class constructor */
 290     private static final Class<?>[] constructorParams =
 291         { InvocationHandler.class };
 292 
 293     /**
 294      * a cache of proxy constructors with
 295      * {@link Constructor#setAccessible(boolean) accessible} flag already set
 296      */
 297     private static final ClassLoaderValue<Constructor<?>> proxyCache =
 298         new ClassLoaderValue<>();
 299 
 300     /**
 301      * System property to revert to generation of proxy class files for version 1.5 (V49).
 302      * Set to "true" to generate v49 class file format.
 303      */
 304     private static final boolean PROXY_GENERATOR_V49 =
 305             GetBooleanAction.privilegedGetProperty("jdk.proxy.ProxyGenerator.v49");
 306 
 307     /**
 308      * the invocation handler for this proxy instance.
 309      * @serial
 310      */
 311     @SuppressWarnings("serial") // Not statically typed as Serializable
 312     protected InvocationHandler h;
 313 
 314     /**
 315      * Prohibits instantiation.
 316      */
 317     private Proxy() {
 318     }
 319 
 320     /**
 321      * Constructs a new {@code Proxy} instance from a subclass
 322      * (typically, a dynamic proxy class) with the specified value
 323      * for its invocation handler.
 324      *
 325      * @param  h the invocation handler for this proxy instance
 326      *
 327      * @throws NullPointerException if the given invocation handler, {@code h},


 524             if (m.isNamed()) {
 525                 if (!m.getDescriptor().packages().contains(proxyPkg)) {
 526                     throw new InternalError(proxyPkg + " not exist in " + m.getName());
 527                 }
 528             }
 529 
 530             /*
 531              * Choose a name for the proxy class to generate.
 532              */
 533             long num = nextUniqueNumber.getAndIncrement();
 534             String proxyName = proxyPkg.isEmpty()
 535                                     ? proxyClassNamePrefix + num
 536                                     : proxyPkg + "." + proxyClassNamePrefix + num;
 537 
 538             ClassLoader loader = getLoader(m);
 539             trace(proxyName, m, loader, interfaces);
 540 
 541             /*
 542              * Generate the specified proxy class.
 543              */
 544             byte[] proxyClassFile = PROXY_GENERATOR_V49
 545                     ? ProxyGenerator_v49.generateProxyClass(proxyName, interfaces, accessFlags)
 546                     : ProxyGenerator.generateProxyClass(loader, proxyName, interfaces, accessFlags);
 547             try {
 548                 Class<?> pc = JLA.defineClass(loader, proxyName, proxyClassFile,
 549                                               null, "__dynamic_proxy__");
 550                 reverseProxyCache.sub(pc).putIfAbsent(loader, Boolean.TRUE);
 551                 return pc;
 552             } catch (ClassFormatError e) {
 553                 /*
 554                  * A ClassFormatError here means that (barring bugs in the
 555                  * proxy class generation code) there was some other
 556                  * invalid aspect of the arguments supplied to the proxy
 557                  * class creation (such as virtual machine limitations
 558                  * exceeded).
 559                  */
 560                 throw new IllegalArgumentException(e.toString());
 561             }
 562         }
 563 
 564         /**
 565          * Test if given class is a class defined by
 566          * {@link #defineProxyClass(Module, List)}


   1 /*
   2  * Copyright (c) 1999, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 281  * @since       1.3
 282  * @revised 9
 283  * @spec JPMS
 284  */
 285 public class Proxy implements java.io.Serializable {
 286     @java.io.Serial
 287     private static final long serialVersionUID = -2222568056686623797L;
 288 
 289     /** parameter types of a proxy class constructor */
 290     private static final Class<?>[] constructorParams =
 291         { InvocationHandler.class };
 292 
 293     /**
 294      * a cache of proxy constructors with
 295      * {@link Constructor#setAccessible(boolean) accessible} flag already set
 296      */
 297     private static final ClassLoaderValue<Constructor<?>> proxyCache =
 298         new ClassLoaderValue<>();
 299 
 300     /**







 301      * the invocation handler for this proxy instance.
 302      * @serial
 303      */
 304     @SuppressWarnings("serial") // Not statically typed as Serializable
 305     protected InvocationHandler h;
 306 
 307     /**
 308      * Prohibits instantiation.
 309      */
 310     private Proxy() {
 311     }
 312 
 313     /**
 314      * Constructs a new {@code Proxy} instance from a subclass
 315      * (typically, a dynamic proxy class) with the specified value
 316      * for its invocation handler.
 317      *
 318      * @param  h the invocation handler for this proxy instance
 319      *
 320      * @throws NullPointerException if the given invocation handler, {@code h},


 517             if (m.isNamed()) {
 518                 if (!m.getDescriptor().packages().contains(proxyPkg)) {
 519                     throw new InternalError(proxyPkg + " not exist in " + m.getName());
 520                 }
 521             }
 522 
 523             /*
 524              * Choose a name for the proxy class to generate.
 525              */
 526             long num = nextUniqueNumber.getAndIncrement();
 527             String proxyName = proxyPkg.isEmpty()
 528                                     ? proxyClassNamePrefix + num
 529                                     : proxyPkg + "." + proxyClassNamePrefix + num;
 530 
 531             ClassLoader loader = getLoader(m);
 532             trace(proxyName, m, loader, interfaces);
 533 
 534             /*
 535              * Generate the specified proxy class.
 536              */
 537             byte[] proxyClassFile = ProxyGenerator.generateProxyClass(loader, proxyName, interfaces, accessFlags);


 538             try {
 539                 Class<?> pc = JLA.defineClass(loader, proxyName, proxyClassFile,
 540                                               null, "__dynamic_proxy__");
 541                 reverseProxyCache.sub(pc).putIfAbsent(loader, Boolean.TRUE);
 542                 return pc;
 543             } catch (ClassFormatError e) {
 544                 /*
 545                  * A ClassFormatError here means that (barring bugs in the
 546                  * proxy class generation code) there was some other
 547                  * invalid aspect of the arguments supplied to the proxy
 548                  * class creation (such as virtual machine limitations
 549                  * exceeded).
 550                  */
 551                 throw new IllegalArgumentException(e.toString());
 552             }
 553         }
 554 
 555         /**
 556          * Test if given class is a class defined by
 557          * {@link #defineProxyClass(Module, List)}


< prev index next >