MethodImplementationBuilder.java revision 897832aa150cdf53ed7fa2f17dee132d2408e2f3
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, LabelMethodItem> labels = new HashMap<String, LabelMethodItem>();
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 LabelMethodItem addLabel(@Nonnull String name) {
38        LabelMethodItem 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.addLabel(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 LabelMethodItem getLabel(@Nonnull String name) {
66        LabelMethodItem label = labels.get(name);
67        if (label == null) {
68            label = new LabelMethodItem();
69            labels.put(name, label);
70        }
71        return label;
72    }
73
74    public void addCatch(@Nullable TypeReference type, @Nonnull LabelMethodItem from,
75                         @Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) {
76        impl.addCatch(type, from, to, handler);
77    }
78
79    public void addCatch(@Nullable String type, @Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to,
80                         @Nonnull LabelMethodItem handler) {
81        impl.addCatch(type, from, to, handler);
82    }
83
84    public void addCatch(@Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) {
85        impl.addCatch(from, to, handler);
86    }
87
88    public void addLineNumber(int lineNumber) {
89    }
90
91    public void addStartLocal(int registerNumber, @Nullable StringReference name, @Nullable TypeReference type,
92                              @Nullable StringReference signature) {
93    }
94
95    public void addEndLocal(int registerNumber) {
96    }
97
98    public void addRestartLocal(int registerNumber) {
99    }
100
101    public void addPrologue() {
102    }
103
104    public void addEpilogue() {
105    }
106
107    public void addSetSourceFile(@Nullable BuilderStringReference sourceFile) {
108    }
109
110    public void addInstruction10t(@Nonnull Opcode opcode,
111                                  @Nonnull LabelMethodItem label) {
112    }
113
114    public void addInstruction10x(@Nonnull Opcode opcode) {
115    }
116
117    public void addInstruction11n(@Nonnull Opcode opcode,
118                                  int registerA,
119                                  int literal) {
120    }
121
122    public void addInstruction11x(@Nonnull Opcode opcode,
123                                  int registerA) {
124    }
125
126    public void addInstruction12x(@Nonnull Opcode opcode,
127                                  int registerA,
128                                  int registerB) {
129    }
130
131    public void addInstruction20bc(@Nonnull Opcode opcode,
132                                   int verificationError,
133                                   @Nonnull ReferenceType reference) {
134    }
135
136    public void addInstruction20t(@Nonnull Opcode opcode,
137                                  @Nonnull LabelMethodItem label) {
138    }
139
140    public void addInstruction21c(@Nonnull Opcode opcode,
141                                  int registerA,
142                                  @Nonnull ReferenceType reference) {
143    }
144
145    public void addInstruction21ih(@Nonnull Opcode opcode,
146                                   int registerA,
147                                   int literal) {
148    }
149
150    public void addInstruction21lh(@Nonnull Opcode opcode,
151                                   int registerA,
152                                   long literal) {
153    }
154
155    public void addInstruction21s(@Nonnull Opcode opcode,
156                                  int registerA,
157                                  int literal) {
158    }
159
160    public void addInstruction21t(@Nonnull Opcode opcode,
161                                  int registerA,
162                                  @Nonnull LabelMethodItem label) {
163    }
164
165    public void addInstruction22b(@Nonnull Opcode opcode,
166                                  int registerA,
167                                  int registerB,
168                                  int literal) {
169    }
170
171    public void addInstruction22c(@Nonnull Opcode opcode,
172                                  int registerA,
173                                  int registerB,
174                                  @Nonnull ReferenceType reference) {
175    }
176
177    public void addInstruction22s(@Nonnull Opcode opcode,
178                                  int registerA,
179                                  int registerB,
180                                  int literal) {
181    }
182
183    public void addInstruction22t(@Nonnull Opcode opcode,
184                                  int registerA,
185                                  int registerB,
186                                  @Nonnull LabelMethodItem labelMethodItem) {
187    }
188
189    public void addInstruction22x(@Nonnull Opcode opcode,
190                                  int registerA,
191                                  int registerB) {
192    }
193
194    public void addInstruction23x(@Nonnull Opcode opcode,
195                                  int registerA,
196                                  int registerB,
197                                  int registerC) {
198    }
199
200    public void addInstruction30t(@Nonnull Opcode opcode,
201                                  @Nonnull LabelMethodItem label) {
202    }
203
204    public void addInstruction31c(@Nonnull Opcode opcode,
205                                  int registerA,
206                                  @Nonnull ReferenceType reference) {
207    }
208
209    public void addInstruction31i(@Nonnull Opcode opcode,
210                                  int registerA,
211                                  int literal) {
212    }
213
214    public void addInstruction31t(@Nonnull Opcode opcode,
215                                  int registerA,
216                                  @Nonnull LabelMethodItem label) {
217    }
218
219    public void addInstruction32x(@Nonnull Opcode opcode,
220                                  int registerA,
221                                  int registerB) {
222    }
223
224    public void addInstruction35c(@Nonnull Opcode opcode,
225                                  int registerCount,
226                                  int registerC,
227                                  int registerD,
228                                  int registerE,
229                                  int registerF,
230                                  int registerG,
231                                  @Nonnull ReferenceType reference) {
232    }
233
234    public void addInstruction3rc(@Nonnull Opcode opcode,
235                                  int startRegister,
236                                  int registerCount,
237                                  @Nonnull ReferenceType reference) {
238    }
239
240    public void addInstruction51l(@Nonnull Opcode opcode,
241                                  int registerA,
242                                  long literal) {
243    }
244
245    public void addPackedSwitchPayload(int startKey, @Nullable List<? extends LabelMethodItem> switchElements) {
246    }
247
248    public void addSparseSwitchPayload(@Nullable List<? extends SwitchLabelElement> switchElements) {
249    }
250
251    public void addArrayPayload(int elementWidth, @Nullable List<Number> arrayElements) {
252    }
253}
254