CatchMethodItem.java revision 93aa50139c4641d931b05608f73af8879c0de1c2
1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2010 Ben Gruver (JesusFreke)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * 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.baksmaliOptions;
32import org.jf.util.IndentingWriter;
33
34import javax.annotation.Nonnull;
35import javax.annotation.Nullable;
36import java.io.IOException;
37
38public class CatchMethodItem extends MethodItem {
39    private final String exceptionType;
40
41    private final LabelMethodItem tryStartLabel;
42    private final LabelMethodItem tryEndLabel;
43    private final LabelMethodItem handlerLabel;
44
45    public CatchMethodItem(@Nonnull baksmaliOptions options, @Nonnull MethodDefinition.LabelCache labelCache,
46                           int codeAddress, @Nullable String exceptionType, int startAddress, int endAddress,
47                           int handlerAddress) {
48        super(codeAddress);
49        this.exceptionType = exceptionType;
50
51        tryStartLabel = labelCache.internLabel(new LabelMethodItem(options, startAddress, "try_start_"));
52
53        //use the address from the last covered instruction, but make the label
54        //name refer to the address of the next instruction
55        tryEndLabel = labelCache.internLabel(new EndTryLabelMethodItem(options, codeAddress, endAddress));
56
57        if (exceptionType == null) {
58            handlerLabel = labelCache.internLabel(new LabelMethodItem(options, handlerAddress, "catchall_"));
59        } else {
60            handlerLabel = labelCache.internLabel(new LabelMethodItem(options, handlerAddress, "catch_"));
61        }
62    }
63
64    public LabelMethodItem getTryStartLabel() {
65        return tryStartLabel;
66    }
67
68    public LabelMethodItem getTryEndLabel() {
69        return tryEndLabel;
70    }
71
72    public LabelMethodItem getHandlerLabel() {
73        return handlerLabel;
74    }
75
76    public double getSortOrder() {
77        //sort after instruction and end_try label
78        return 102;
79    }
80
81    @Override
82    public boolean writeTo(IndentingWriter writer) throws IOException {
83        if (exceptionType == null) {
84            writer.write(".catchall");
85        } else {
86            writer.write(".catch ");
87            writer.write(exceptionType);
88        }
89        writer.write(" {");
90        tryStartLabel.writeTo(writer);
91        writer.write(" .. ");
92        tryEndLabel.writeTo(writer);
93        writer.write("} ");
94        handlerLabel.writeTo(writer);
95        return true;
96    }
97}
98