1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 jdk.tools.jlink.internal.plugins.optim; 26 27 import java.io.IOException; 28 import java.util.Map.Entry; 29 import java.util.Set; 30 import java.util.function.Consumer; 31 import jdk.internal.org.objectweb.asm.ClassReader; 32 import jdk.tools.jlink.internal.plugins.asm.AsmPools; 33 import jdk.internal.org.objectweb.asm.tree.ClassNode; 34 import jdk.internal.org.objectweb.asm.tree.MethodNode; 35 import jdk.tools.jlink.internal.plugins.OptimizationPlugin.MethodOptimizer; 36 import jdk.tools.jlink.internal.plugins.asm.AsmModulePool; 37 import jdk.tools.jlink.internal.plugins.optim.ControlFlow.Block; 38 import jdk.tools.jlink.internal.plugins.optim.ReflectionOptimizer.Data; 39 import jdk.tools.jlink.internal.plugins.optim.ReflectionOptimizer.TypeResolver; 40 41 42 /** 43 * MethodOptimizer that removes Class.forName when possible. 44 * WARNING: This code is experimental. 45 * TODO: Need to check that the type is accessible prior to replace with a constant. 46 */ 47 public class ForNameFolding implements MethodOptimizer { 48 49 private int numNotReplaced; 50 private int numReplacement; 51 private int numRemovedHandlers; 52 private int instructionsRemoved; 53 54 private Consumer<String> logger; 55 56 @Override 57 public boolean optimize(Consumer<String> logger, AsmPools pools, 58 AsmModulePool modulePool, 59 ClassNode cn, MethodNode m, TypeResolver resolver) throws Exception { 60 this.logger = logger; 61 Data data = ReflectionOptimizer.replaceWithClassConstant(cn, m, createResolver(resolver)); 62 instructionsRemoved += data.removedInstructions(); 63 numRemovedHandlers += data.removedHandlers().size(); 64 for (Entry<String, Set<Block>> entry : data.removedHandlers().entrySet()) { 65 logRemoval(cn.name + "." + m.name + "removed block for " + entry.getKey() 66 + " : " + entry.getValue()); 67 } 68 return data.removedInstructions() > 0; 69 } 70 71 public TypeResolver createResolver(TypeResolver resolver) { 72 return (ClassNode cn, MethodNode mn, String type) -> { 73 ClassReader reader = resolver.resolve(cn, mn, type); 74 if (reader == null) { 75 logNotReplaced(type); 76 } else { 77 logReplaced(type); 78 } 79 return reader; 80 }; 81 } 82 83 private void logReplaced(String type) { 84 numReplacement += 1; 85 } 86 87 private void logNotReplaced(String type) { 88 numNotReplaced += 1; 89 if (logger != null) { 90 logger.accept(type + " not resolved"); 91 } 92 } 93 94 private void logRemoval(String content) { 95 numRemovedHandlers += 1; 96 if (logger != null) { 97 logger.accept(content); 98 } 99 } 100 101 @Override 102 public void close() throws IOException { 103 if (logger != null) { 104 logger.accept("Class.forName Folding results:\n " + numReplacement 105 + " removed reflection. " + numRemovedHandlers 106 + " removed exception handlers." 107 + numNotReplaced + " types unknown. " 108 + instructionsRemoved + " instructions removed\n"); 109 } 110 } 111 }