src/share/classes/sun/misc/ProxyGenerator.java

Print this page
rev 7444 : 8059563: (proxy) sun.misc.ProxyGenerator.generateProxyClass should create intermediate directories


  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 sun.misc;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.DataOutputStream;

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


 310     /** debugging flag for saving generated class files */
 311     private final static boolean saveGeneratedFiles =
 312         java.security.AccessController.doPrivileged(
 313             new GetBooleanAction(
 314                 "sun.misc.ProxyGenerator.saveGeneratedFiles")).booleanValue();
 315 
 316     /**
 317      * Generate a proxy class given a name and a list of proxy interfaces.
 318      */
 319     public static byte[] generateProxyClass(final String name,
 320                                             Class[] interfaces)
 321     {
 322         ProxyGenerator gen = new ProxyGenerator(name, interfaces);
 323         final byte[] classFile = gen.generateClassFile();
 324 
 325         if (saveGeneratedFiles) {
 326             java.security.AccessController.doPrivileged(
 327             new java.security.PrivilegedAction<Void>() {
 328                 public Void run() {
 329                     try {
 330                         FileOutputStream file =
 331                             new FileOutputStream(dotToSlash(name) + ".class");
 332                         file.write(classFile);
 333                         file.close();






 334                         return null;
 335                     } catch (IOException e) {
 336                         throw new InternalError(
 337                             "I/O exception saving generated file: " + e);
 338                     }
 339                 }
 340             });
 341         }
 342 
 343         return classFile;
 344     }
 345 
 346     /* preloaded Method objects for methods in java.lang.Object */
 347     private static Method hashCodeMethod;
 348     private static Method equalsMethod;
 349     private static Method toStringMethod;
 350     static {
 351         try {
 352             hashCodeMethod = Object.class.getMethod("hashCode");
 353             equalsMethod =




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


 314     /** debugging flag for saving generated class files */
 315     private final static boolean saveGeneratedFiles =
 316         java.security.AccessController.doPrivileged(
 317             new GetBooleanAction(
 318                 "sun.misc.ProxyGenerator.saveGeneratedFiles")).booleanValue();
 319 
 320     /**
 321      * Generate a proxy class given a name and a list of proxy interfaces.
 322      */
 323     public static byte[] generateProxyClass(final String name,
 324                                             Class[] interfaces)
 325     {
 326         ProxyGenerator gen = new ProxyGenerator(name, interfaces);
 327         final byte[] classFile = gen.generateClassFile();
 328 
 329         if (saveGeneratedFiles) {
 330             java.security.AccessController.doPrivileged(
 331             new java.security.PrivilegedAction<Void>() {
 332                 public Void run() {
 333                     try {
 334                         int i = name.lastIndexOf('.');
 335                         Path path;
 336                         if (i > 0) {
 337                             Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));
 338                             Files.createDirectories(dir);
 339                             path = dir.resolve(name.substring(i+1, name.length()) + ".class");
 340                         } else {
 341                             path = Paths.get(name + ".class");
 342                         }
 343                         Files.write(path, classFile);
 344                         return null;
 345                     } catch (IOException e) {
 346                         throw new InternalError(
 347                             "I/O exception saving generated file: " + e);
 348                     }
 349                 }
 350             });
 351         }
 352 
 353         return classFile;
 354     }
 355 
 356     /* preloaded Method objects for methods in java.lang.Object */
 357     private static Method hashCodeMethod;
 358     private static Method equalsMethod;
 359     private static Method toStringMethod;
 360     static {
 361         try {
 362             hashCodeMethod = Object.class.getMethod("hashCode");
 363             equalsMethod =