• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ASM: a very small and fast Java bytecode manipulation framework
2 // Copyright (c) 2000-2011 INRIA, France Telecom
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holders nor the names of its
14 //    contributors may be used to endorse or promote products derived from
15 //    this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 // THE POSSIBILITY OF SUCH DAMAGE.
28 
29 package org.objectweb.asm.commons;
30 
31 import java.util.Collections;
32 import java.util.Comparator;
33 import org.objectweb.asm.MethodVisitor;
34 import org.objectweb.asm.Opcodes;
35 import org.objectweb.asm.tree.MethodNode;
36 import org.objectweb.asm.tree.TryCatchBlockNode;
37 
38 /**
39  * A {@link MethodVisitor} adapter to sort the exception handlers. The handlers are sorted in a
40  * method innermost-to-outermost. This allows the programmer to add handlers without worrying about
41  * ordering them correctly with respect to existing, in-code handlers.
42  *
43  * <p>Behavior is only defined for properly-nested handlers. If any "try" blocks overlap (something
44  * that isn't possible in Java code) then this may not do what you want. In fact, this adapter just
45  * sorts by the length of the "try" block, taking advantage of the fact that a given try block must
46  * be larger than any block it contains).
47  *
48  * @author Adrian Sampson
49  */
50 public class TryCatchBlockSorter extends MethodNode {
51 
52   /**
53    * Constructs a new {@link TryCatchBlockSorter}.
54    *
55    * @param methodVisitor the method visitor to which this visitor must delegate method calls. May
56    *     be {@literal null}.
57    * @param access the method's access flags (see {@link Opcodes}). This parameter also indicates if
58    *     the method is synthetic and/or deprecated.
59    * @param name the method's name.
60    * @param descriptor the method's descriptor (see {@link org.objectweb.asm.Type}).
61    * @param signature the method's signature. May be {@literal null} if the method parameters,
62    *     return type and exceptions do not use generic types.
63    * @param exceptions the internal names of the method's exception classes (see {@link
64    *     org.objectweb.asm.Type#getInternalName()}). May be {@literal null}.
65    */
TryCatchBlockSorter( final MethodVisitor methodVisitor, final int access, final String name, final String descriptor, final String signature, final String[] exceptions)66   public TryCatchBlockSorter(
67       final MethodVisitor methodVisitor,
68       final int access,
69       final String name,
70       final String descriptor,
71       final String signature,
72       final String[] exceptions) {
73     this(
74         /* latest api = */ Opcodes.ASM9,
75         methodVisitor,
76         access,
77         name,
78         descriptor,
79         signature,
80         exceptions);
81     if (getClass() != TryCatchBlockSorter.class) {
82       throw new IllegalStateException();
83     }
84   }
85 
TryCatchBlockSorter( final int api, final MethodVisitor methodVisitor, final int access, final String name, final String descriptor, final String signature, final String[] exceptions)86   protected TryCatchBlockSorter(
87       final int api,
88       final MethodVisitor methodVisitor,
89       final int access,
90       final String name,
91       final String descriptor,
92       final String signature,
93       final String[] exceptions) {
94     super(api, access, name, descriptor, signature, exceptions);
95     this.mv = methodVisitor;
96   }
97 
98   @Override
visitEnd()99   public void visitEnd() {
100     // Sort the TryCatchBlockNode elements by the length of their "try" block.
101     Collections.sort(
102         tryCatchBlocks,
103         new Comparator<TryCatchBlockNode>() {
104 
105           @Override
106           public int compare(
107               final TryCatchBlockNode tryCatchBlockNode1,
108               final TryCatchBlockNode tryCatchBlockNode2) {
109             return blockLength(tryCatchBlockNode1) - blockLength(tryCatchBlockNode2);
110           }
111 
112           private int blockLength(final TryCatchBlockNode tryCatchBlockNode) {
113             int startIndex = instructions.indexOf(tryCatchBlockNode.start);
114             int endIndex = instructions.indexOf(tryCatchBlockNode.end);
115             return endIndex - startIndex;
116           }
117         });
118     // Update the 'target' of each try catch block annotation.
119     for (int i = 0; i < tryCatchBlocks.size(); ++i) {
120       tryCatchBlocks.get(i).updateIndex(i);
121     }
122     if (mv != null) {
123       accept(mv);
124     }
125   }
126 }
127