< prev index next >

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

Print this page




  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.io.ByteArrayOutputStream;
  29 import java.io.DataOutputStream;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.io.OutputStream;
  33 import java.lang.reflect.Array;
  34 import java.lang.reflect.Method;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.ArrayList;
  39 import java.util.HashMap;
  40 import java.util.LinkedList;
  41 import java.util.List;
  42 import java.util.ListIterator;
  43 import java.util.Map;
  44 import sun.security.action.GetBooleanAction;
  45 
  46 /**
  47  * ProxyGenerator contains the code to generate a dynamic proxy class
  48  * for the java.lang.reflect.Proxy API.
  49  *
  50  * The external interfaces to ProxyGenerator is the static
  51  * "generateProxyClass" method.
  52  *
  53  * @author      Peter Jones
  54  * @since       1.3
  55  */
  56 class ProxyGenerator {
  57     /*


 329      *
 330      * @param name        the class name of the proxy class
 331      * @param interfaces  proxy interfaces
 332      * @param accessFlags access flags of the proxy class
 333     */
 334     static byte[] generateProxyClass(final String name,
 335                                      Class<?>[] interfaces,
 336                                      int accessFlags)
 337     {
 338         ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
 339         final byte[] classFile = gen.generateClassFile();
 340 
 341         if (saveGeneratedFiles) {
 342             java.security.AccessController.doPrivileged(
 343             new java.security.PrivilegedAction<Void>() {
 344                 public Void run() {
 345                     try {
 346                         int i = name.lastIndexOf('.');
 347                         Path path;
 348                         if (i > 0) {
 349                             Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));
 350                             Files.createDirectories(dir);
 351                             path = dir.resolve(name.substring(i+1, name.length()) + ".class");
 352                         } else {
 353                             path = Paths.get(name + ".class");
 354                         }
 355                         Files.write(path, classFile);
 356                         return null;
 357                     } catch (IOException e) {
 358                         throw new InternalError(
 359                             "I/O exception saving generated file: " + e);
 360                     }
 361                 }
 362             });
 363         }
 364 
 365         return classFile;
 366     }
 367 
 368     /* preloaded Method objects for methods in java.lang.Object */
 369     private static Method hashCodeMethod;
 370     private static Method equalsMethod;
 371     private static Method toStringMethod;
 372     static {
 373         try {




  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.io.ByteArrayOutputStream;
  29 import java.io.DataOutputStream;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.io.OutputStream;
  33 import java.lang.reflect.Array;
  34 import java.lang.reflect.Method;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;

  37 import java.util.ArrayList;
  38 import java.util.HashMap;
  39 import java.util.LinkedList;
  40 import java.util.List;
  41 import java.util.ListIterator;
  42 import java.util.Map;
  43 import sun.security.action.GetBooleanAction;
  44 
  45 /**
  46  * ProxyGenerator contains the code to generate a dynamic proxy class
  47  * for the java.lang.reflect.Proxy API.
  48  *
  49  * The external interfaces to ProxyGenerator is the static
  50  * "generateProxyClass" method.
  51  *
  52  * @author      Peter Jones
  53  * @since       1.3
  54  */
  55 class ProxyGenerator {
  56     /*


 328      *
 329      * @param name        the class name of the proxy class
 330      * @param interfaces  proxy interfaces
 331      * @param accessFlags access flags of the proxy class
 332     */
 333     static byte[] generateProxyClass(final String name,
 334                                      Class<?>[] interfaces,
 335                                      int accessFlags)
 336     {
 337         ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
 338         final byte[] classFile = gen.generateClassFile();
 339 
 340         if (saveGeneratedFiles) {
 341             java.security.AccessController.doPrivileged(
 342             new java.security.PrivilegedAction<Void>() {
 343                 public Void run() {
 344                     try {
 345                         int i = name.lastIndexOf('.');
 346                         Path path;
 347                         if (i > 0) {
 348                             Path dir = Path.get(name.substring(0, i).replace('.', File.separatorChar));
 349                             Files.createDirectories(dir);
 350                             path = dir.resolve(name.substring(i+1, name.length()) + ".class");
 351                         } else {
 352                             path = Path.get(name + ".class");
 353                         }
 354                         Files.write(path, classFile);
 355                         return null;
 356                     } catch (IOException e) {
 357                         throw new InternalError(
 358                             "I/O exception saving generated file: " + e);
 359                     }
 360                 }
 361             });
 362         }
 363 
 364         return classFile;
 365     }
 366 
 367     /* preloaded Method objects for methods in java.lang.Object */
 368     private static Method hashCodeMethod;
 369     private static Method equalsMethod;
 370     private static Method toStringMethod;
 371     static {
 372         try {


< prev index next >