• 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 package org.objectweb.asm.tree;
29 
30 import java.util.List;
31 import org.objectweb.asm.MethodVisitor;
32 
33 /**
34  * A node that represents a try catch block.
35  *
36  * @author Eric Bruneton
37  */
38 public class TryCatchBlockNode {
39 
40   /** The beginning of the exception handler's scope (inclusive). */
41   public LabelNode start;
42 
43   /** The end of the exception handler's scope (exclusive). */
44   public LabelNode end;
45 
46   /** The beginning of the exception handler's code. */
47   public LabelNode handler;
48 
49   /**
50    * The internal name of the type of exceptions handled by the handler. May be {@literal null} to
51    * catch any exceptions (for "finally" blocks).
52    */
53   public String type;
54 
55   /** The runtime visible type annotations on the exception handler type. May be {@literal null}. */
56   public List<TypeAnnotationNode> visibleTypeAnnotations;
57 
58   /**
59    * The runtime invisible type annotations on the exception handler type. May be {@literal null}.
60    */
61   public List<TypeAnnotationNode> invisibleTypeAnnotations;
62 
63   /**
64    * Constructs a new {@link TryCatchBlockNode}.
65    *
66    * @param start the beginning of the exception handler's scope (inclusive).
67    * @param end the end of the exception handler's scope (exclusive).
68    * @param handler the beginning of the exception handler's code.
69    * @param type the internal name of the type of exceptions handled by the handler (see {@link
70    *     org.objectweb.asm.Type#getInternalName()}), or {@literal null} to catch any exceptions (for
71    *     "finally" blocks).
72    */
TryCatchBlockNode( final LabelNode start, final LabelNode end, final LabelNode handler, final String type)73   public TryCatchBlockNode(
74       final LabelNode start, final LabelNode end, final LabelNode handler, final String type) {
75     this.start = start;
76     this.end = end;
77     this.handler = handler;
78     this.type = type;
79   }
80 
81   /**
82    * Updates the index of this try catch block in the method's list of try catch block nodes. This
83    * index maybe stored in the 'target' field of the type annotations of this block.
84    *
85    * @param index the new index of this try catch block in the method's list of try catch block
86    *     nodes.
87    */
updateIndex(final int index)88   public void updateIndex(final int index) {
89     int newTypeRef = 0x42000000 | (index << 8);
90     if (visibleTypeAnnotations != null) {
91       for (int i = 0, n = visibleTypeAnnotations.size(); i < n; ++i) {
92         visibleTypeAnnotations.get(i).typeRef = newTypeRef;
93       }
94     }
95     if (invisibleTypeAnnotations != null) {
96       for (int i = 0, n = invisibleTypeAnnotations.size(); i < n; ++i) {
97         invisibleTypeAnnotations.get(i).typeRef = newTypeRef;
98       }
99     }
100   }
101 
102   /**
103    * Makes the given visitor visit this try catch block.
104    *
105    * @param methodVisitor a method visitor.
106    */
accept(final MethodVisitor methodVisitor)107   public void accept(final MethodVisitor methodVisitor) {
108     methodVisitor.visitTryCatchBlock(
109         start.getLabel(), end.getLabel(), handler == null ? null : handler.getLabel(), type);
110     if (visibleTypeAnnotations != null) {
111       for (int i = 0, n = visibleTypeAnnotations.size(); i < n; ++i) {
112         TypeAnnotationNode typeAnnotation = visibleTypeAnnotations.get(i);
113         typeAnnotation.accept(
114             methodVisitor.visitTryCatchAnnotation(
115                 typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, true));
116       }
117     }
118     if (invisibleTypeAnnotations != null) {
119       for (int i = 0, n = invisibleTypeAnnotations.size(); i < n; ++i) {
120         TypeAnnotationNode typeAnnotation = invisibleTypeAnnotations.get(i);
121         typeAnnotation.accept(
122             methodVisitor.visitTryCatchAnnotation(
123                 typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, false));
124       }
125     }
126   }
127 }
128