CatchMethodItem.java revision 6eae34831fee1f116f3a453bdc5e143d68e05e03
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)/*
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * [The "BSD licence"]
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * Copyright (c) 2010 Ben Gruver (JesusFreke)
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * All rights reserved.
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) *
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * modification, are permitted provided that the following conditions
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * are met:
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *    documentation and/or other materials provided with the distribution.
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * 3. The name of the author may not be used to endorse or promote products
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *    derived from this software without specific prior written permission.
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) *
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.baksmali.Adaptors;
30
31import org.jf.baksmali.IndentingPrintWriter;
32import org.jf.dexlib.TypeIdItem;
33
34public class CatchMethodItem extends MethodItem {
35    private final TypeIdItem exceptionType;
36
37    private final LabelMethodItem tryStartLabel;
38    private final LabelMethodItem tryEndLabel;
39    private final LabelMethodItem handlerLabel;
40
41    public CatchMethodItem(MethodDefinition.LabelCache labelCache, int codeAddress, TypeIdItem exceptionType,
42                           int startAddress, int endAddress, int handlerAddress) {
43        super(codeAddress);
44        this.exceptionType = exceptionType;
45
46        tryStartLabel = labelCache.internLabel(new LabelMethodItem(startAddress, "try_start_"));
47        tryStartLabel.setUncommented();
48        //use the address from the last covered instruction, but make the label
49        //name refer to the address of the next instruction
50        tryEndLabel = labelCache.internLabel(new EndTryLabelMethodItem(codeAddress, endAddress));
51        tryEndLabel.setUncommented();
52
53        if (exceptionType == null) {
54            handlerLabel = labelCache.internLabel(new LabelMethodItem(handlerAddress, "catchall_"));
55        } else {
56            handlerLabel = labelCache.internLabel(new LabelMethodItem(handlerAddress, "catch_"));
57        }
58        handlerLabel.setUncommented();
59    }
60
61    public LabelMethodItem getTryStartLabel() {
62        return tryStartLabel;
63    }
64
65    public LabelMethodItem getTryEndLabel() {
66        return tryEndLabel;
67    }
68
69    public LabelMethodItem getHandlerLabel() {
70        return handlerLabel;
71    }
72
73    public double getSortOrder() {
74        //sort after instruction and end_try label
75        return 102;
76    }
77
78    @Override
79    public boolean writeTo(IndentingPrintWriter writer) {
80        if (exceptionType == null) {
81            writer.write(".catchall");
82        } else {
83            writer.write(".catch ");
84            ReferenceFormatter.writeTypeReference(writer, exceptionType);
85        }
86        writer.write(" {");
87        tryStartLabel.writeTo(writer);
88        writer.write(" .. ");
89        tryEndLabel.writeTo(writer);
90        writer.write("} ");
91        handlerLabel.writeTo(writer);
92        return true;
93    }
94}
95