ThrowingCstInsn.java revision 99409883d9c4c0ffb49b070ce307bb33a9dfe9f1
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.rop.code;
18
19import com.android.dx.rop.cst.Constant;
20import com.android.dx.rop.type.Type;
21import com.android.dx.rop.type.TypeList;
22
23/**
24 * Instruction which contains an explicit reference to a constant
25 * and which might throw an exception.
26 */
27public final class ThrowingCstInsn
28        extends CstInsn {
29    /** {@code non-null;} list of exceptions caught */
30    private final TypeList catches;
31
32    /**
33     * Constructs an instance.
34     *
35     * @param opcode {@code non-null;} the opcode
36     * @param position {@code non-null;} source position
37     * @param sources {@code non-null;} specs for all the sources
38     * @param catches {@code non-null;} list of exceptions caught
39     * @param cst {@code non-null;} the constant
40     */
41    public ThrowingCstInsn(Rop opcode, SourcePosition position,
42                           RegisterSpecList sources,
43                           TypeList catches, Constant cst) {
44        super(opcode, position, null, sources, cst);
45
46        if (opcode.getBranchingness() != Rop.BRANCH_THROW) {
47            throw new IllegalArgumentException("bogus branchingness");
48        }
49
50        if (catches == null) {
51            throw new NullPointerException("catches == null");
52        }
53
54        this.catches = catches;
55    }
56
57    /** {@inheritDoc} */
58    @Override
59    public String getInlineString() {
60        return getConstant().toHuman() + " " +
61                                 ThrowingInsn.toCatchString(catches);
62    }
63
64    /** {@inheritDoc} */
65    @Override
66    public TypeList getCatches() {
67        return catches;
68    }
69
70    /** {@inheritDoc} */
71    @Override
72    public void accept(Visitor visitor) {
73        visitor.visitThrowingCstInsn(this);
74    }
75
76    /** {@inheritDoc} */
77    @Override
78    public Insn withAddedCatch(Type type) {
79        return new ThrowingCstInsn(getOpcode(), getPosition(),
80                                   getSources(), catches.withAddedType(type),
81                                   getConstant());
82    }
83
84    /** {@inheritDoc} */
85    @Override
86    public Insn withRegisterOffset(int delta) {
87        return new ThrowingCstInsn(getOpcode(), getPosition(),
88                                   getSources().withOffset(delta),
89                                   catches,
90                                   getConstant());
91    }
92
93    /** {@inheritDoc} */
94    @Override
95    public Insn withNewRegisters(RegisterSpec result,
96            RegisterSpecList sources) {
97
98        return new ThrowingCstInsn(getOpcode(), getPosition(),
99                                   sources,
100                                   catches,
101                                   getConstant());
102    }
103
104
105}
106