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.util.Iterator; 28 import java.util.Set; 29 import jdk.internal.org.objectweb.asm.ClassReader; 30 import jdk.internal.org.objectweb.asm.tree.AbstractInsnNode; 31 import jdk.internal.org.objectweb.asm.tree.ClassNode; 32 import jdk.internal.org.objectweb.asm.tree.MethodInsnNode; 33 import jdk.internal.org.objectweb.asm.tree.MethodNode; 34 import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode; 35 36 /** 37 * Optimization utility methods 38 */ 39 public class Utils { 40 41 public static boolean canThrowCheckedException(ReflectionOptimizer.TypeResolver cch, 42 ClassNode classNode, MethodNode m, TryCatchBlockNode bn) throws Exception { 43 int istart = m.instructions.indexOf(bn.start); 44 int iend = m.instructions.indexOf(bn.end); 45 for (int i = istart; i < iend - 1; i++) { 46 AbstractInsnNode instr = m.instructions.get(i); 47 if (instr instanceof MethodInsnNode) { 48 MethodInsnNode meth = (MethodInsnNode) instr; 49 ClassReader reader = cch.resolve(classNode, m, meth.owner); 50 if (reader != null) { 51 ClassNode cn = new ClassNode(); 52 reader.accept(cn, ClassReader.EXPAND_FRAMES); 53 for (MethodNode method : cn.methods) { 54 if (method.name.equals(meth.name)) { 55 for (String e : method.exceptions) { 56 if (e.equals(bn.type)) { 57 return true; 58 } 59 } 60 } 61 } 62 } else { 63 return true; 64 } 65 } 66 } 67 return false; 68 } 69 70 public static void suppressBlocks(MethodNode m, Set<ControlFlow.Block> toRemove) throws Exception { 71 m.instructions.resetLabels(); 72 Iterator<AbstractInsnNode> it = m.instructions.iterator(); 73 while (it.hasNext()) { 74 AbstractInsnNode n = it.next(); 75 Iterator<TryCatchBlockNode> handlers = m.tryCatchBlocks.iterator(); 76 boolean cont = false; 77 // Do not delete instructions that are end of other try block. 78 while (handlers.hasNext()) { 79 TryCatchBlockNode handler = handlers.next(); 80 if (handler.end == n) { 81 cont = true; 82 } 83 } 84 if (cont) { 85 continue; 86 } 87 88 for (ControlFlow.Block b : toRemove) { 89 for (ControlFlow.InstructionNode ins : b.getInstructions()) { 90 if (ins.getInstr() == n) { 91 it.remove(); 92 } 93 } 94 } 95 } 96 } 97 }