MethodImplementationBuilder.java revision 688611814ddff6babff935e81dcf51aff903563a
1package org.jf.dexlib2.builder;
2
3import org.jf.dexlib2.Opcode;
4import org.jf.dexlib2.iface.reference.Reference;
5import org.jf.dexlib2.iface.reference.StringReference;
6import org.jf.dexlib2.iface.reference.TypeReference;
7import org.jf.dexlib2.writer.builder.BuilderStringReference;
8
9import javax.annotation.Nonnull;
10import javax.annotation.Nullable;
11import java.util.HashMap;
12import java.util.List;
13
14public class MethodImplementationBuilder<ReferenceType extends Reference> {
15    // Contains all named labels - both placed and unplaced
16    private final HashMap<String, Label> labels = new HashMap<String, Label>();
17
18    @Nonnull
19    private final MutableMethodImplementation<ReferenceType> impl;
20
21    private MethodLocation currentLocation;
22
23    public MethodImplementationBuilder() {
24        this.impl = new MutableMethodImplementation<ReferenceType>();
25        this.currentLocation = impl.instructionList.get(0);
26    }
27
28    /**
29     * Adds a new named label at the current location.
30     *
31     * Any previous unplaced references to a label of this name will now refer to this label/location
32     *
33     * @param name The name of the label to add
34     * @return A LabelRef representing the label
35     */
36    @Nonnull
37    public Label addLabel(@Nonnull String name) {
38        Label label = labels.get(name);
39
40        if (label != null) {
41            if (label.isPlaced()) {
42                throw new IllegalArgumentException("There is already a label with that name.");
43            } else {
44                currentLocation.getLabels().add(label);
45            }
46        } else {
47            label = currentLocation.addNewLabel();
48            labels.put(name, label);
49        }
50
51        return label;
52    }
53
54    /**
55     * Get a reference to a label with the given name.
56     *
57     * If a label with that name has not been added yet, a new one is created, but is left
58     * in an unplaced state. It is assumed that addLabel(name) will be called at a later
59     * point to define the location of the label.
60     *
61     * @param name The name of the label to get
62     * @return A LabelRef representing the label
63     */
64    @Nonnull
65    public Label getLabel(@Nonnull String name) {
66        Label label = labels.get(name);
67        if (label == null) {
68            label = new Label();
69            labels.put(name, label);
70        }
71        return label;
72    }
73
74    public void addCatch(@Nullable TypeReference type, @Nonnull Label from,
75                         @Nonnull Label to, @Nonnull Label handler) {
76        impl.addCatch(type, from, to, handler);
77    }
78
79    public void addCatch(@Nullable String type, @Nonnull Label from, @Nonnull Label to,
80                         @Nonnull Label handler) {
81        impl.addCatch(type, from, to, handler);
82    }
83
84    public void addCatch(@Nonnull Label from, @Nonnull Label to, @Nonnull Label handler) {
85        impl.addCatch(from, to, handler);
86    }
87
88    public void addLineNumber(int lineNumber) {
89        currentLocation.addLineNumber(lineNumber);
90    }
91
92    public void addStartLocal(int registerNumber, @Nullable StringReference name, @Nullable TypeReference type,
93                              @Nullable StringReference signature) {
94        currentLocation.addStartLocal(registerNumber, name, type, signature);
95    }
96
97    public void addEndLocal(int registerNumber) {
98        currentLocation.addEndLocal(registerNumber);
99    }
100
101    public void addRestartLocal(int registerNumber) {
102        currentLocation.addRestartLocal(registerNumber);
103    }
104
105    public void addPrologue() {
106        currentLocation.addPrologue();
107    }
108
109    public void addEpilogue() {
110        currentLocation.addEpilogue();
111    }
112
113    public void addSetSourceFile(@Nullable BuilderStringReference sourceFile) {
114        currentLocation.addSetSourceFile(sourceFile);
115    }
116
117    public void addInstruction10t(@Nonnull Opcode opcode,
118                                  @Nonnull Label label) {
119    }
120
121    public void addInstruction10x(@Nonnull Opcode opcode) {
122    }
123
124    public void addInstruction11n(@Nonnull Opcode opcode,
125                                  int registerA,
126                                  int literal) {
127    }
128
129    public void addInstruction11x(@Nonnull Opcode opcode,
130                                  int registerA) {
131    }
132
133    public void addInstruction12x(@Nonnull Opcode opcode,
134                                  int registerA,
135                                  int registerB) {
136    }
137
138    public void addInstruction20bc(@Nonnull Opcode opcode,
139                                   int verificationError,
140                                   @Nonnull ReferenceType reference) {
141    }
142
143    public void addInstruction20t(@Nonnull Opcode opcode,
144                                  @Nonnull Label label) {
145    }
146
147    public void addInstruction21c(@Nonnull Opcode opcode,
148                                  int registerA,
149                                  @Nonnull ReferenceType reference) {
150    }
151
152    public void addInstruction21ih(@Nonnull Opcode opcode,
153                                   int registerA,
154                                   int literal) {
155    }
156
157    public void addInstruction21lh(@Nonnull Opcode opcode,
158                                   int registerA,
159                                   long literal) {
160    }
161
162    public void addInstruction21s(@Nonnull Opcode opcode,
163                                  int registerA,
164                                  int literal) {
165    }
166
167    public void addInstruction21t(@Nonnull Opcode opcode,
168                                  int registerA,
169                                  @Nonnull Label label) {
170    }
171
172    public void addInstruction22b(@Nonnull Opcode opcode,
173                                  int registerA,
174                                  int registerB,
175                                  int literal) {
176    }
177
178    public void addInstruction22c(@Nonnull Opcode opcode,
179                                  int registerA,
180                                  int registerB,
181                                  @Nonnull ReferenceType reference) {
182    }
183
184    public void addInstruction22s(@Nonnull Opcode opcode,
185                                  int registerA,
186                                  int registerB,
187                                  int literal) {
188    }
189
190    public void addInstruction22t(@Nonnull Opcode opcode,
191                                  int registerA,
192                                  int registerB,
193                                  @Nonnull Label labelMethodItem) {
194    }
195
196    public void addInstruction22x(@Nonnull Opcode opcode,
197                                  int registerA,
198                                  int registerB) {
199    }
200
201    public void addInstruction23x(@Nonnull Opcode opcode,
202                                  int registerA,
203                                  int registerB,
204                                  int registerC) {
205    }
206
207    public void addInstruction30t(@Nonnull Opcode opcode,
208                                  @Nonnull Label label) {
209    }
210
211    public void addInstruction31c(@Nonnull Opcode opcode,
212                                  int registerA,
213                                  @Nonnull ReferenceType reference) {
214    }
215
216    public void addInstruction31i(@Nonnull Opcode opcode,
217                                  int registerA,
218                                  int literal) {
219    }
220
221    public void addInstruction31t(@Nonnull Opcode opcode,
222                                  int registerA,
223                                  @Nonnull Label label) {
224    }
225
226    public void addInstruction32x(@Nonnull Opcode opcode,
227                                  int registerA,
228                                  int registerB) {
229    }
230
231    public void addInstruction35c(@Nonnull Opcode opcode,
232                                  int registerCount,
233                                  int registerC,
234                                  int registerD,
235                                  int registerE,
236                                  int registerF,
237                                  int registerG,
238                                  @Nonnull ReferenceType reference) {
239    }
240
241    public void addInstruction3rc(@Nonnull Opcode opcode,
242                                  int startRegister,
243                                  int registerCount,
244                                  @Nonnull ReferenceType reference) {
245    }
246
247    public void addInstruction51l(@Nonnull Opcode opcode,
248                                  int registerA,
249                                  long literal) {
250    }
251
252    public void addPackedSwitchPayload(int startKey, @Nullable List<? extends Label> switchElements) {
253    }
254
255    public void addSparseSwitchPayload(@Nullable List<? extends SwitchLabelElement> switchElements) {
256    }
257
258    public void addArrayPayload(int elementWidth, @Nullable List<Number> arrayElements) {
259    }
260}
261