< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 1999, 2025, 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
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 

  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  31 import java.lang.invoke.WrongMethodTypeException;
  32 import java.lang.module.ModuleDescriptor;
  33 import java.util.ArrayDeque;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.Deque;
  37 import java.util.HashMap;
  38 import java.util.HashSet;
  39 import java.util.IdentityHashMap;
  40 import java.util.List;
  41 import java.util.Map;
  42 import java.util.Objects;
  43 import java.util.Set;
  44 import java.util.concurrent.ConcurrentHashMap;
  45 import java.util.concurrent.atomic.AtomicInteger;
  46 import java.util.concurrent.atomic.AtomicLong;
  47 import java.util.function.BooleanSupplier;

 450                 }
 451             }
 452         }
 453 
 454         private static Class<?> defineProxyClass(ProxyClassContext context, List<Class<?>> interfaces) {
 455             /*
 456              * Choose a name for the proxy class to generate.
 457              */
 458             long num = nextUniqueNumber.getAndIncrement();
 459             String proxyName = context.packageName().isEmpty()
 460                                     ? proxyClassNamePrefix + num
 461                                     : context.packageName() + "." + proxyClassNamePrefix + num;
 462 
 463             ClassLoader loader = context.module().getClassLoader();
 464             trace(proxyName, context.module(), loader, interfaces);
 465 
 466             /*
 467              * Generate the specified proxy class.
 468              */
 469             byte[] proxyClassFile = ProxyGenerator.generateProxyClass(loader, proxyName, interfaces,
 470                                                                       context.accessFlags() | Modifier.FINAL);
 471             try {
 472                 Class<?> pc = JLA.defineClass(loader, proxyName, proxyClassFile,
 473                                               null, "__dynamic_proxy__");
 474                 reverseProxyCache.sub(pc).putIfAbsent(loader, Boolean.TRUE);
 475                 return pc;
 476             } catch (ClassFormatError e) {
 477                 /*
 478                  * A ClassFormatError here means that (barring bugs in the
 479                  * proxy class generation code) there was some other
 480                  * invalid aspect of the arguments supplied to the proxy
 481                  * class creation (such as virtual machine limitations
 482                  * exceeded).
 483                  */
 484                 throw new IllegalArgumentException(e.toString());
 485             }
 486         }
 487 
 488         /**
 489          * Test if given class is a class defined by
 490          * {@link #defineProxyClass(ProxyClassContext, List)}

   1 /*
   2  * Copyright (c) 1999, 2026, 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
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.classfile.ClassFile;
  29 import java.lang.invoke.MethodHandle;
  30 import java.lang.invoke.MethodHandles;
  31 import java.lang.invoke.MethodType;
  32 import java.lang.invoke.WrongMethodTypeException;
  33 import java.lang.module.ModuleDescriptor;
  34 import java.util.ArrayDeque;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.Deque;
  38 import java.util.HashMap;
  39 import java.util.HashSet;
  40 import java.util.IdentityHashMap;
  41 import java.util.List;
  42 import java.util.Map;
  43 import java.util.Objects;
  44 import java.util.Set;
  45 import java.util.concurrent.ConcurrentHashMap;
  46 import java.util.concurrent.atomic.AtomicInteger;
  47 import java.util.concurrent.atomic.AtomicLong;
  48 import java.util.function.BooleanSupplier;

 451                 }
 452             }
 453         }
 454 
 455         private static Class<?> defineProxyClass(ProxyClassContext context, List<Class<?>> interfaces) {
 456             /*
 457              * Choose a name for the proxy class to generate.
 458              */
 459             long num = nextUniqueNumber.getAndIncrement();
 460             String proxyName = context.packageName().isEmpty()
 461                                     ? proxyClassNamePrefix + num
 462                                     : context.packageName() + "." + proxyClassNamePrefix + num;
 463 
 464             ClassLoader loader = context.module().getClassLoader();
 465             trace(proxyName, context.module(), loader, interfaces);
 466 
 467             /*
 468              * Generate the specified proxy class.
 469              */
 470             byte[] proxyClassFile = ProxyGenerator.generateProxyClass(loader, proxyName, interfaces,
 471                                                                       context.accessFlags() | Modifier.FINAL | ClassFile.ACC_IDENTITY);
 472             try {
 473                 Class<?> pc = JLA.defineClass(loader, proxyName, proxyClassFile,
 474                                               null, "__dynamic_proxy__");
 475                 reverseProxyCache.sub(pc).putIfAbsent(loader, Boolean.TRUE);
 476                 return pc;
 477             } catch (ClassFormatError e) {
 478                 /*
 479                  * A ClassFormatError here means that (barring bugs in the
 480                  * proxy class generation code) there was some other
 481                  * invalid aspect of the arguments supplied to the proxy
 482                  * class creation (such as virtual machine limitations
 483                  * exceeded).
 484                  */
 485                 throw new IllegalArgumentException(e.toString());
 486             }
 487         }
 488 
 489         /**
 490          * Test if given class is a class defined by
 491          * {@link #defineProxyClass(ProxyClassContext, List)}
< prev index next >