1.1 --- a/rt/emul/compact/src/main/java/java/lang/invoke/ProxyClassesDumper.java Sun Aug 10 05:56:32 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,147 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -package java.lang.invoke;
1.29 -
1.30 -import sun.util.logging.PlatformLogger;
1.31 -
1.32 -import java.io.FilePermission;
1.33 -import java.nio.file.Files;
1.34 -import java.nio.file.InvalidPathException;
1.35 -import java.nio.file.Path;
1.36 -import java.nio.file.Paths;
1.37 -import java.security.AccessController;
1.38 -import java.security.PrivilegedAction;
1.39 -import java.util.Objects;
1.40 -import java.util.concurrent.atomic.AtomicBoolean;
1.41 -
1.42 -/**
1.43 - * Helper class used by InnerClassLambdaMetafactory to log generated classes
1.44 - *
1.45 - * @implNote
1.46 - * <p> Because this class is called by LambdaMetafactory, make use
1.47 - * of lambda lead to recursive calls cause stack overflow.
1.48 - */
1.49 -final class ProxyClassesDumper {
1.50 - private static final char[] HEX = {
1.51 - '0', '1', '2', '3', '4', '5', '6', '7',
1.52 - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
1.53 - };
1.54 - private static final char[] BAD_CHARS = {
1.55 - '\\', ':', '*', '?', '"', '<', '>', '|'
1.56 - };
1.57 - private static final String[] REPLACEMENT = {
1.58 - "%5C", "%3A", "%2A", "%3F", "%22", "%3C", "%3E", "%7C"
1.59 - };
1.60 -
1.61 - private final Path dumpDir;
1.62 -
1.63 - public static ProxyClassesDumper getInstance(String path) {
1.64 - if (null == path) {
1.65 - return null;
1.66 - }
1.67 - try {
1.68 - path = path.trim();
1.69 - final Path dir = Paths.get(path.length() == 0 ? "." : path);
1.70 - AccessController.doPrivileged(new PrivilegedAction<Void>() {
1.71 - @Override
1.72 - public Void run() {
1.73 - validateDumpDir(dir);
1.74 - return null;
1.75 - }
1.76 - }, null, new FilePermission("<<ALL FILES>>", "read, write"));
1.77 - return new ProxyClassesDumper(dir);
1.78 - } catch (InvalidPathException ex) {
1.79 - PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
1.80 - .warning("Path " + path + " is not valid - dumping disabled", ex);
1.81 - } catch (IllegalArgumentException iae) {
1.82 - PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
1.83 - .warning(iae.getMessage() + " - dumping disabled");
1.84 - }
1.85 - return null;
1.86 - }
1.87 -
1.88 - private ProxyClassesDumper(Path path) {
1.89 - dumpDir = Objects.requireNonNull(path);
1.90 - }
1.91 -
1.92 - private static void validateDumpDir(Path path) {
1.93 - if (!Files.exists(path)) {
1.94 - throw new IllegalArgumentException("Directory " + path + " does not exist");
1.95 - } else if (!Files.isDirectory(path)) {
1.96 - throw new IllegalArgumentException("Path " + path + " is not a directory");
1.97 - } else if (!Files.isWritable(path)) {
1.98 - throw new IllegalArgumentException("Directory " + path + " is not writable");
1.99 - }
1.100 - }
1.101 -
1.102 - public static String encodeForFilename(String className) {
1.103 - final int len = className.length();
1.104 - StringBuilder sb = new StringBuilder(len);
1.105 -
1.106 - for (int i = 0; i < len; i++) {
1.107 - char c = className.charAt(i);
1.108 - // control characters
1.109 - if (c <= 31) {
1.110 - sb.append('%');
1.111 - sb.append(HEX[c >> 4 & 0x0F]);
1.112 - sb.append(HEX[c & 0x0F]);
1.113 - } else {
1.114 - int j = 0;
1.115 - for (; j < BAD_CHARS.length; j++) {
1.116 - if (c == BAD_CHARS[j]) {
1.117 - sb.append(REPLACEMENT[j]);
1.118 - break;
1.119 - }
1.120 - }
1.121 - if (j >= BAD_CHARS.length) {
1.122 - sb.append(c);
1.123 - }
1.124 - }
1.125 - }
1.126 -
1.127 - return sb.toString();
1.128 - }
1.129 -
1.130 - public void dumpClass(String className, final byte[] classBytes) {
1.131 - Path file;
1.132 - try {
1.133 - file = dumpDir.resolve(encodeForFilename(className) + ".class");
1.134 - } catch (InvalidPathException ex) {
1.135 - PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
1.136 - .warning("Invalid path for class " + className);
1.137 - return;
1.138 - }
1.139 -
1.140 - try {
1.141 - Path dir = file.getParent();
1.142 - Files.createDirectories(dir);
1.143 - Files.write(file, classBytes);
1.144 - } catch (Exception ignore) {
1.145 - PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
1.146 - .warning("Exception writing to path at " + file.toString());
1.147 - // simply don't care if this operation failed
1.148 - }
1.149 - }
1.150 -}