• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.dexgen.rop.code;
18 
19 import com.android.dexgen.rop.cst.Constant;
20 import com.android.dexgen.rop.type.Type;
21 import com.android.dexgen.rop.type.TypeList;
22 
23 /**
24  * Instruction which contains an explicit reference to a constant
25  * and which might throw an exception.
26  */
27 public 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      */
ThrowingCstInsn(Rop opcode, SourcePosition position, RegisterSpecList sources, TypeList catches, Constant cst)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
getInlineString()59     public String getInlineString() {
60         return getConstant().toHuman() + " " +
61                                  ThrowingInsn.toCatchString(catches);
62     }
63 
64     /** {@inheritDoc} */
65     @Override
getCatches()66     public TypeList getCatches() {
67         return catches;
68     }
69 
70     /** {@inheritDoc} */
71     @Override
accept(Visitor visitor)72     public void accept(Visitor visitor) {
73         visitor.visitThrowingCstInsn(this);
74     }
75 
76     /** {@inheritDoc} */
77     @Override
withAddedCatch(Type type)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
withRegisterOffset(int delta)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
withNewRegisters(RegisterSpec result, RegisterSpecList sources)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