rt/emul/compact/src/main/java/java/lang/invoke/ProxyClassesDumper.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 11:11:13 +0200
branchjdk8-b132
changeset 1646 c880a8a8803b
permissions -rw-r--r--
Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
jaroslav@1646
     1
/*
jaroslav@1646
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1646
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1646
     4
 *
jaroslav@1646
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1646
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1646
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1646
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1646
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1646
    10
 *
jaroslav@1646
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1646
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1646
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1646
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1646
    15
 * accompanied this code).
jaroslav@1646
    16
 *
jaroslav@1646
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1646
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1646
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1646
    20
 *
jaroslav@1646
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1646
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1646
    23
 * questions.
jaroslav@1646
    24
 */
jaroslav@1646
    25
package java.lang.invoke;
jaroslav@1646
    26
jaroslav@1646
    27
import sun.util.logging.PlatformLogger;
jaroslav@1646
    28
jaroslav@1646
    29
import java.io.FilePermission;
jaroslav@1646
    30
import java.nio.file.Files;
jaroslav@1646
    31
import java.nio.file.InvalidPathException;
jaroslav@1646
    32
import java.nio.file.Path;
jaroslav@1646
    33
import java.nio.file.Paths;
jaroslav@1646
    34
import java.security.AccessController;
jaroslav@1646
    35
import java.security.PrivilegedAction;
jaroslav@1646
    36
import java.util.Objects;
jaroslav@1646
    37
import java.util.concurrent.atomic.AtomicBoolean;
jaroslav@1646
    38
jaroslav@1646
    39
/**
jaroslav@1646
    40
 * Helper class used by InnerClassLambdaMetafactory to log generated classes
jaroslav@1646
    41
 *
jaroslav@1646
    42
 * @implNote
jaroslav@1646
    43
 * <p> Because this class is called by LambdaMetafactory, make use
jaroslav@1646
    44
 * of lambda lead to recursive calls cause stack overflow.
jaroslav@1646
    45
 */
jaroslav@1646
    46
final class ProxyClassesDumper {
jaroslav@1646
    47
    private static final char[] HEX = {
jaroslav@1646
    48
        '0', '1', '2', '3', '4', '5', '6', '7',
jaroslav@1646
    49
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
jaroslav@1646
    50
    };
jaroslav@1646
    51
    private static final char[] BAD_CHARS = {
jaroslav@1646
    52
        '\\', ':', '*', '?', '"', '<', '>', '|'
jaroslav@1646
    53
    };
jaroslav@1646
    54
    private static final String[] REPLACEMENT = {
jaroslav@1646
    55
        "%5C", "%3A", "%2A", "%3F", "%22", "%3C", "%3E", "%7C"
jaroslav@1646
    56
    };
jaroslav@1646
    57
jaroslav@1646
    58
    private final Path dumpDir;
jaroslav@1646
    59
jaroslav@1646
    60
    public static ProxyClassesDumper getInstance(String path) {
jaroslav@1646
    61
        if (null == path) {
jaroslav@1646
    62
            return null;
jaroslav@1646
    63
        }
jaroslav@1646
    64
        try {
jaroslav@1646
    65
            path = path.trim();
jaroslav@1646
    66
            final Path dir = Paths.get(path.length() == 0 ? "." : path);
jaroslav@1646
    67
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
jaroslav@1646
    68
                    @Override
jaroslav@1646
    69
                    public Void run() {
jaroslav@1646
    70
                        validateDumpDir(dir);
jaroslav@1646
    71
                        return null;
jaroslav@1646
    72
                    }
jaroslav@1646
    73
                }, null, new FilePermission("<<ALL FILES>>", "read, write"));
jaroslav@1646
    74
            return new ProxyClassesDumper(dir);
jaroslav@1646
    75
        } catch (InvalidPathException ex) {
jaroslav@1646
    76
            PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
jaroslav@1646
    77
                          .warning("Path " + path + " is not valid - dumping disabled", ex);
jaroslav@1646
    78
        } catch (IllegalArgumentException iae) {
jaroslav@1646
    79
            PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
jaroslav@1646
    80
                          .warning(iae.getMessage() + " - dumping disabled");
jaroslav@1646
    81
        }
jaroslav@1646
    82
        return null;
jaroslav@1646
    83
    }
jaroslav@1646
    84
jaroslav@1646
    85
    private ProxyClassesDumper(Path path) {
jaroslav@1646
    86
        dumpDir = Objects.requireNonNull(path);
jaroslav@1646
    87
    }
jaroslav@1646
    88
jaroslav@1646
    89
    private static void validateDumpDir(Path path) {
jaroslav@1646
    90
        if (!Files.exists(path)) {
jaroslav@1646
    91
            throw new IllegalArgumentException("Directory " + path + " does not exist");
jaroslav@1646
    92
        } else if (!Files.isDirectory(path)) {
jaroslav@1646
    93
            throw new IllegalArgumentException("Path " + path + " is not a directory");
jaroslav@1646
    94
        } else if (!Files.isWritable(path)) {
jaroslav@1646
    95
            throw new IllegalArgumentException("Directory " + path + " is not writable");
jaroslav@1646
    96
        }
jaroslav@1646
    97
    }
jaroslav@1646
    98
jaroslav@1646
    99
    public static String encodeForFilename(String className) {
jaroslav@1646
   100
        final int len = className.length();
jaroslav@1646
   101
        StringBuilder sb = new StringBuilder(len);
jaroslav@1646
   102
jaroslav@1646
   103
        for (int i = 0; i < len; i++) {
jaroslav@1646
   104
            char c = className.charAt(i);
jaroslav@1646
   105
            // control characters
jaroslav@1646
   106
            if (c <= 31) {
jaroslav@1646
   107
                sb.append('%');
jaroslav@1646
   108
                sb.append(HEX[c >> 4 & 0x0F]);
jaroslav@1646
   109
                sb.append(HEX[c & 0x0F]);
jaroslav@1646
   110
            } else {
jaroslav@1646
   111
                int j = 0;
jaroslav@1646
   112
                for (; j < BAD_CHARS.length; j++) {
jaroslav@1646
   113
                    if (c == BAD_CHARS[j]) {
jaroslav@1646
   114
                        sb.append(REPLACEMENT[j]);
jaroslav@1646
   115
                        break;
jaroslav@1646
   116
                    }
jaroslav@1646
   117
                }
jaroslav@1646
   118
                if (j >= BAD_CHARS.length) {
jaroslav@1646
   119
                    sb.append(c);
jaroslav@1646
   120
                }
jaroslav@1646
   121
            }
jaroslav@1646
   122
        }
jaroslav@1646
   123
jaroslav@1646
   124
        return sb.toString();
jaroslav@1646
   125
    }
jaroslav@1646
   126
jaroslav@1646
   127
    public void dumpClass(String className, final byte[] classBytes) {
jaroslav@1646
   128
        Path file;
jaroslav@1646
   129
        try {
jaroslav@1646
   130
            file = dumpDir.resolve(encodeForFilename(className) + ".class");
jaroslav@1646
   131
        } catch (InvalidPathException ex) {
jaroslav@1646
   132
            PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
jaroslav@1646
   133
                          .warning("Invalid path for class " + className);
jaroslav@1646
   134
            return;
jaroslav@1646
   135
        }
jaroslav@1646
   136
jaroslav@1646
   137
        try {
jaroslav@1646
   138
            Path dir = file.getParent();
jaroslav@1646
   139
            Files.createDirectories(dir);
jaroslav@1646
   140
            Files.write(file, classBytes);
jaroslav@1646
   141
        } catch (Exception ignore) {
jaroslav@1646
   142
            PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
jaroslav@1646
   143
                          .warning("Exception writing to path at " + file.toString());
jaroslav@1646
   144
            // simply don't care if this operation failed
jaroslav@1646
   145
        }
jaroslav@1646
   146
    }
jaroslav@1646
   147
}