jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import sun.util.logging.PlatformLogger; jaroslav@1646: jaroslav@1646: import java.io.FilePermission; jaroslav@1646: import java.nio.file.Files; jaroslav@1646: import java.nio.file.InvalidPathException; jaroslav@1646: import java.nio.file.Path; jaroslav@1646: import java.nio.file.Paths; jaroslav@1646: import java.security.AccessController; jaroslav@1646: import java.security.PrivilegedAction; jaroslav@1646: import java.util.Objects; jaroslav@1646: import java.util.concurrent.atomic.AtomicBoolean; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Helper class used by InnerClassLambdaMetafactory to log generated classes jaroslav@1646: * jaroslav@1646: * @implNote jaroslav@1646: *

Because this class is called by LambdaMetafactory, make use jaroslav@1646: * of lambda lead to recursive calls cause stack overflow. jaroslav@1646: */ jaroslav@1646: final class ProxyClassesDumper { jaroslav@1646: private static final char[] HEX = { jaroslav@1646: '0', '1', '2', '3', '4', '5', '6', '7', jaroslav@1646: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' jaroslav@1646: }; jaroslav@1646: private static final char[] BAD_CHARS = { jaroslav@1646: '\\', ':', '*', '?', '"', '<', '>', '|' jaroslav@1646: }; jaroslav@1646: private static final String[] REPLACEMENT = { jaroslav@1646: "%5C", "%3A", "%2A", "%3F", "%22", "%3C", "%3E", "%7C" jaroslav@1646: }; jaroslav@1646: jaroslav@1646: private final Path dumpDir; jaroslav@1646: jaroslav@1646: public static ProxyClassesDumper getInstance(String path) { jaroslav@1646: if (null == path) { jaroslav@1646: return null; jaroslav@1646: } jaroslav@1646: try { jaroslav@1646: path = path.trim(); jaroslav@1646: final Path dir = Paths.get(path.length() == 0 ? "." : path); jaroslav@1646: AccessController.doPrivileged(new PrivilegedAction() { jaroslav@1646: @Override jaroslav@1646: public Void run() { jaroslav@1646: validateDumpDir(dir); jaroslav@1646: return null; jaroslav@1646: } jaroslav@1646: }, null, new FilePermission("<>", "read, write")); jaroslav@1646: return new ProxyClassesDumper(dir); jaroslav@1646: } catch (InvalidPathException ex) { jaroslav@1646: PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) jaroslav@1646: .warning("Path " + path + " is not valid - dumping disabled", ex); jaroslav@1646: } catch (IllegalArgumentException iae) { jaroslav@1646: PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) jaroslav@1646: .warning(iae.getMessage() + " - dumping disabled"); jaroslav@1646: } jaroslav@1646: return null; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private ProxyClassesDumper(Path path) { jaroslav@1646: dumpDir = Objects.requireNonNull(path); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static void validateDumpDir(Path path) { jaroslav@1646: if (!Files.exists(path)) { jaroslav@1646: throw new IllegalArgumentException("Directory " + path + " does not exist"); jaroslav@1646: } else if (!Files.isDirectory(path)) { jaroslav@1646: throw new IllegalArgumentException("Path " + path + " is not a directory"); jaroslav@1646: } else if (!Files.isWritable(path)) { jaroslav@1646: throw new IllegalArgumentException("Directory " + path + " is not writable"); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: public static String encodeForFilename(String className) { jaroslav@1646: final int len = className.length(); jaroslav@1646: StringBuilder sb = new StringBuilder(len); jaroslav@1646: jaroslav@1646: for (int i = 0; i < len; i++) { jaroslav@1646: char c = className.charAt(i); jaroslav@1646: // control characters jaroslav@1646: if (c <= 31) { jaroslav@1646: sb.append('%'); jaroslav@1646: sb.append(HEX[c >> 4 & 0x0F]); jaroslav@1646: sb.append(HEX[c & 0x0F]); jaroslav@1646: } else { jaroslav@1646: int j = 0; jaroslav@1646: for (; j < BAD_CHARS.length; j++) { jaroslav@1646: if (c == BAD_CHARS[j]) { jaroslav@1646: sb.append(REPLACEMENT[j]); jaroslav@1646: break; jaroslav@1646: } jaroslav@1646: } jaroslav@1646: if (j >= BAD_CHARS.length) { jaroslav@1646: sb.append(c); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: return sb.toString(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: public void dumpClass(String className, final byte[] classBytes) { jaroslav@1646: Path file; jaroslav@1646: try { jaroslav@1646: file = dumpDir.resolve(encodeForFilename(className) + ".class"); jaroslav@1646: } catch (InvalidPathException ex) { jaroslav@1646: PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) jaroslav@1646: .warning("Invalid path for class " + className); jaroslav@1646: return; jaroslav@1646: } jaroslav@1646: jaroslav@1646: try { jaroslav@1646: Path dir = file.getParent(); jaroslav@1646: Files.createDirectories(dir); jaroslav@1646: Files.write(file, classBytes); jaroslav@1646: } catch (Exception ignore) { jaroslav@1646: PlatformLogger.getLogger(ProxyClassesDumper.class.getName()) jaroslav@1646: .warning("Exception writing to path at " + file.toString()); jaroslav@1646: // simply don't care if this operation failed jaroslav@1646: } jaroslav@1646: } jaroslav@1646: }