1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * ASM: a very small and fast Java bytecode manipulation framework
  32  * Copyright (c) 2000-2011 INRIA, France Telecom
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  * 1. Redistributions of source code must retain the above copyright
  39  *    notice, this list of conditions and the following disclaimer.
  40  * 2. Redistributions in binary form must reproduce the above copyright
  41  *    notice, this list of conditions and the following disclaimer in the
  42  *    documentation and/or other materials provided with the distribution.
  43  * 3. Neither the name of the copyright holders nor the names of its
  44  *    contributors may be used to endorse or promote products derived from
  45  *    this software without specific prior written permission.
  46  *
  47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  57  * THE POSSIBILITY OF SUCH DAMAGE.
  58  */
  59 
  60 package jdk.internal.org.objectweb.asm.commons;
  61 
  62 import java.util.Collections;
  63 import java.util.Comparator;
  64 
  65 import jdk.internal.org.objectweb.asm.MethodVisitor;
  66 import jdk.internal.org.objectweb.asm.Opcodes;
  67 import jdk.internal.org.objectweb.asm.tree.MethodNode;
  68 import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;
  69 
  70 /**
  71  * A {@link MethodVisitor} adapter to sort the exception handlers. The handlers
  72  * are sorted in a method innermost-to-outermost. This allows the programmer to
  73  * add handlers without worrying about ordering them correctly with respect to
  74  * existing, in-code handlers.
  75  *
  76  * Behavior is only defined for properly-nested handlers. If any "try" blocks
  77  * overlap (something that isn't possible in Java code) then this may not do
  78  * what you want. In fact, this adapter just sorts by the length of the "try"
  79  * block, taking advantage of the fact that a given try block must be larger
  80  * than any block it contains).
  81  *
  82  * @author Adrian Sampson
  83  */
  84 public class TryCatchBlockSorter extends MethodNode {
  85 
  86     public TryCatchBlockSorter(final MethodVisitor mv, final int access,
  87             final String name, final String desc, final String signature,
  88             final String[] exceptions) {
  89         this(Opcodes.ASM6, mv, access, name, desc, signature, exceptions);
  90     }
  91 
  92     protected TryCatchBlockSorter(final int api, final MethodVisitor mv,
  93             final int access, final String name, final String desc,
  94             final String signature, final String[] exceptions) {
  95         super(api, access, name, desc, signature, exceptions);
  96         this.mv = mv;
  97     }
  98 
  99     @Override
 100     public void visitEnd() {
 101         // Compares TryCatchBlockNodes by the length of their "try" block.
 102         Comparator<TryCatchBlockNode> comp = new Comparator<TryCatchBlockNode>() {
 103 
 104             public int compare(TryCatchBlockNode t1, TryCatchBlockNode t2) {
 105                 int len1 = blockLength(t1);
 106                 int len2 = blockLength(t2);
 107                 return len1 - len2;
 108             }
 109 
 110             private int blockLength(TryCatchBlockNode block) {
 111                 int startidx = instructions.indexOf(block.start);
 112                 int endidx = instructions.indexOf(block.end);
 113                 return endidx - startidx;
 114             }
 115         };
 116         Collections.sort(tryCatchBlocks, comp);
 117         // Updates the 'target' of each try catch block annotation.
 118         for (int i = 0; i < tryCatchBlocks.size(); ++i) {
 119             tryCatchBlocks.get(i).updateIndex(i);
 120         }
 121         if (mv != null) {
 122             accept(mv);
 123         }
 124     }
 125 }