< prev index next >

src/java.base/share/classes/java/lang/invoke/ProxyClassesDumper.java

Print this page




  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 package java.lang.invoke;
  26 
  27 import sun.util.logging.PlatformLogger;
  28 
  29 import java.io.FilePermission;
  30 import java.nio.file.Files;
  31 import java.nio.file.InvalidPathException;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.security.AccessController;
  35 import java.security.PrivilegedAction;
  36 import java.util.Objects;
  37 import java.util.concurrent.atomic.AtomicBoolean;
  38 
  39 /**
  40  * Helper class used by InnerClassLambdaMetafactory to log generated classes
  41  *
  42  * @implNote
  43  * <p> Because this class is called by LambdaMetafactory, make use
  44  * of lambda lead to recursive calls cause stack overflow.
  45  */
  46 final class ProxyClassesDumper {
  47     private static final char[] HEX = {
  48         '0', '1', '2', '3', '4', '5', '6', '7',
  49         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  50     };
  51     private static final char[] BAD_CHARS = {
  52         '\\', ':', '*', '?', '"', '<', '>', '|'
  53     };
  54     private static final String[] REPLACEMENT = {
  55         "%5C", "%3A", "%2A", "%3F", "%22", "%3C", "%3E", "%7C"
  56     };
  57 
  58     private final Path dumpDir;
  59 
  60     public static ProxyClassesDumper getInstance(String path) {
  61         if (null == path) {
  62             return null;
  63         }
  64         try {
  65             path = path.trim();
  66             final Path dir = Paths.get(path.length() == 0 ? "." : path);
  67             AccessController.doPrivileged(new PrivilegedAction<>() {
  68                     @Override
  69                     public Void run() {
  70                         validateDumpDir(dir);
  71                         return null;
  72                     }
  73                 }, null, new FilePermission("<<ALL FILES>>", "read, write"));
  74             return new ProxyClassesDumper(dir);
  75         } catch (InvalidPathException ex) {
  76             PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
  77                           .warning("Path " + path + " is not valid - dumping disabled", ex);
  78         } catch (IllegalArgumentException iae) {
  79             PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
  80                           .warning(iae.getMessage() + " - dumping disabled");
  81         }
  82         return null;
  83     }
  84 
  85     private ProxyClassesDumper(Path path) {
  86         dumpDir = Objects.requireNonNull(path);




  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 package java.lang.invoke;
  26 
  27 import sun.util.logging.PlatformLogger;
  28 
  29 import java.io.FilePermission;
  30 import java.nio.file.Files;
  31 import java.nio.file.InvalidPathException;
  32 import java.nio.file.Path;

  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.util.Objects;
  36 import java.util.concurrent.atomic.AtomicBoolean;
  37 
  38 /**
  39  * Helper class used by InnerClassLambdaMetafactory to log generated classes
  40  *
  41  * @implNote
  42  * <p> Because this class is called by LambdaMetafactory, make use
  43  * of lambda lead to recursive calls cause stack overflow.
  44  */
  45 final class ProxyClassesDumper {
  46     private static final char[] HEX = {
  47         '0', '1', '2', '3', '4', '5', '6', '7',
  48         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  49     };
  50     private static final char[] BAD_CHARS = {
  51         '\\', ':', '*', '?', '"', '<', '>', '|'
  52     };
  53     private static final String[] REPLACEMENT = {
  54         "%5C", "%3A", "%2A", "%3F", "%22", "%3C", "%3E", "%7C"
  55     };
  56 
  57     private final Path dumpDir;
  58 
  59     public static ProxyClassesDumper getInstance(String path) {
  60         if (null == path) {
  61             return null;
  62         }
  63         try {
  64             path = path.trim();
  65             final Path dir = Path.get(path.length() == 0 ? "." : path);
  66             AccessController.doPrivileged(new PrivilegedAction<>() {
  67                     @Override
  68                     public Void run() {
  69                         validateDumpDir(dir);
  70                         return null;
  71                     }
  72                 }, null, new FilePermission("<<ALL FILES>>", "read, write"));
  73             return new ProxyClassesDumper(dir);
  74         } catch (InvalidPathException ex) {
  75             PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
  76                           .warning("Path " + path + " is not valid - dumping disabled", ex);
  77         } catch (IllegalArgumentException iae) {
  78             PlatformLogger.getLogger(ProxyClassesDumper.class.getName())
  79                           .warning(iae.getMessage() + " - dumping disabled");
  80         }
  81         return null;
  82     }
  83 
  84     private ProxyClassesDumper(Path path) {
  85         dumpDir = Objects.requireNonNull(path);


< prev index next >