# HG changeset patch # User igerasim # Date 1412835865 -14400 # Node ID 44ef857dd5e6799eecae0de61afc7be5909b7f9f # Parent e5831ecc079bbdd777cf8fca5fba0cf423d979ac 8059563: (proxy) sun.misc.ProxyGenerator.generateProxyClass should create intermediate directories Reviewed-by: mchung diff -r e5831ecc079b -r 44ef857dd5e6 src/share/classes/sun/misc/ProxyGenerator.java --- a/src/share/classes/sun/misc/ProxyGenerator.java Mon May 13 13:48:58 2013 -0700 +++ b/src/share/classes/sun/misc/ProxyGenerator.java Thu Oct 09 10:24:25 2014 +0400 @@ -27,11 +27,15 @@ import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Array; import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; @@ -327,10 +331,16 @@ new java.security.PrivilegedAction() { public Void run() { try { - FileOutputStream file = - new FileOutputStream(dotToSlash(name) + ".class"); - file.write(classFile); - file.close(); + int i = name.lastIndexOf('.'); + Path path; + if (i > 0) { + Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar)); + Files.createDirectories(dir); + path = dir.resolve(name.substring(i+1, name.length()) + ".class"); + } else { + path = Paths.get(name + ".class"); + } + Files.write(path, classFile); return null; } catch (IOException e) { throw new InternalError( diff -r e5831ecc079b -r 44ef857dd5e6 test/sun/misc/ProxyGenerator/SaveProxyClassFileTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/sun/misc/ProxyGenerator/SaveProxyClassFileTest.java Thu Oct 09 10:24:25 2014 +0400 @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014, 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. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8059563 + * @summary ProxyGenerator should create intermediate directories + * for the generated class file + * @build SaveProxyClassFileTest + * @run main/othervm -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true SaveProxyClassFileTest + */ + +import java.io.File; +import sun.misc.ProxyGenerator; + +public class SaveProxyClassFileTest { + + static final File dir1 = new File("a"); + static final File dir2 = new File(dir1, "b"); + static final File cf = new File(dir2, "c.class"); + + public static void main(String[] args) throws Throwable { + // remove the files in case they were left from + // the previous run + deleteFiles(); + + try { + ProxyGenerator.generateProxyClass("a.b.c", + new Class[] {Inf.class}); + + if (!cf.exists()) { + throw new RuntimeException(cf + " wasn't created"); + } + } finally { + deleteFiles(); + } + } + + static interface Inf { + } + + /** + * Removes generated file and directories + */ + private static void deleteFiles() { + cf.delete(); + dir2.delete(); + dir1.delete(); + } +}