Get rid of the MethodItem concept

This commit is contained in:
Ben Gruver 2013-09-01 09:37:49 -07:00
parent 897832aa15
commit bb7937fd30
10 changed files with 71 additions and 83 deletions

View File

@ -9,7 +9,7 @@ import javax.annotation.Nullable;
class BuilderExceptionHandler {
static ExceptionHandler newExceptionHandler(@Nullable final TypeReference exceptionType,
@Nonnull final LabelMethodItem handler) {
@Nonnull final Label handler) {
if (exceptionType == null) {
return newExceptionHandler(handler);
}
@ -28,7 +28,7 @@ class BuilderExceptionHandler {
};
}
static ExceptionHandler newExceptionHandler(@Nonnull final LabelMethodItem handler) {
static ExceptionHandler newExceptionHandler(@Nonnull final Label handler) {
return new BaseExceptionHandler() {
@Nullable @Override public String getExceptionType() {
return null;
@ -41,7 +41,7 @@ class BuilderExceptionHandler {
}
static ExceptionHandler newExceptionHandler(@Nullable final String exceptionType,
@Nonnull final LabelMethodItem handler) {
@Nonnull final Label handler) {
if (exceptionType == null) {
return newExceptionHandler(handler);
}

View File

@ -12,27 +12,26 @@ import java.util.List;
class BuilderTryBlock extends BaseTryBlock<ExceptionHandler> {
// We only ever have one exception handler per try block. They are later merged as needed in TryListBuilder
@Nonnull public final ExceptionHandler exceptionHandler;
@Nonnull public final LabelMethodItem start;
@Nonnull public final Label start;
// The end location is exclusive, it should point to the codeAddress of the instruction immediately after the last
// covered instruction.
@Nonnull public final LabelMethodItem end;
@Nonnull public final Label end;
public BuilderTryBlock(@Nonnull LabelMethodItem start, @Nonnull LabelMethodItem end,
@Nullable String exceptionType, @Nonnull LabelMethodItem handler) {
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable String exceptionType,
@Nonnull Label handler) {
this.start = start;
this.end = end;
this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler);
}
public BuilderTryBlock(@Nonnull LabelMethodItem start, @Nonnull LabelMethodItem end,
@Nullable TypeReference exceptionType, @Nonnull LabelMethodItem handler) {
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable TypeReference exceptionType,
@Nonnull Label handler) {
this.start = start;
this.end = end;
this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler);
}
public BuilderTryBlock(@Nonnull LabelMethodItem start, @Nonnull LabelMethodItem end,
@Nonnull LabelMethodItem handler) {
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label handler) {
this.start = start;
this.end = end;
this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(handler);

View File

@ -1,7 +0,0 @@
package org.jf.dexlib2.builder;
import org.jf.dexlib2.iface.instruction.Instruction;
class InstructionMethodItem extends MethodItem {
public Instruction instruction;
}

View File

@ -0,0 +1,31 @@
package org.jf.dexlib2.builder;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class Label {
@Nullable MethodLocation location;
Label() {
}
Label(MethodLocation location) {
this.location = location;
}
public int getCodeAddress() {
return getLocation().getCodeAddress();
}
@Nonnull
public MethodLocation getLocation() {
if (location == null) {
throw new IllegalStateException("Cannot get the location of a label that hasn't been placed yet.");
}
return location;
}
public boolean isPlaced() {
return location != null;
}
}

View File

@ -1,14 +0,0 @@
package org.jf.dexlib2.builder;
public class LabelMethodItem extends MethodItem {
LabelMethodItem() {
}
LabelMethodItem(MethodLocation location) {
this.location = location;
}
public int getCodeAddress() {
return getLocation().getCodeAddress();
}
}

View File

@ -13,7 +13,7 @@ import java.util.List;
public class MethodImplementationBuilder<ReferenceType extends Reference> {
// Contains all named labels - both placed and unplaced
private final HashMap<String, LabelMethodItem> labels = new HashMap<String, LabelMethodItem>();
private final HashMap<String, Label> labels = new HashMap<String, Label>();
@Nonnull
private final MutableMethodImplementation<ReferenceType> impl;
@ -34,8 +34,8 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
* @return A LabelRef representing the label
*/
@Nonnull
public LabelMethodItem addLabel(@Nonnull String name) {
LabelMethodItem label = labels.get(name);
public Label addLabel(@Nonnull String name) {
Label label = labels.get(name);
if (label != null) {
if (label.isPlaced()) {
@ -62,26 +62,26 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
* @return A LabelRef representing the label
*/
@Nonnull
public LabelMethodItem getLabel(@Nonnull String name) {
LabelMethodItem label = labels.get(name);
public Label getLabel(@Nonnull String name) {
Label label = labels.get(name);
if (label == null) {
label = new LabelMethodItem();
label = new Label();
labels.put(name, label);
}
return label;
}
public void addCatch(@Nullable TypeReference type, @Nonnull LabelMethodItem from,
@Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) {
public void addCatch(@Nullable TypeReference type, @Nonnull Label from,
@Nonnull Label to, @Nonnull Label handler) {
impl.addCatch(type, from, to, handler);
}
public void addCatch(@Nullable String type, @Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to,
@Nonnull LabelMethodItem handler) {
public void addCatch(@Nullable String type, @Nonnull Label from, @Nonnull Label to,
@Nonnull Label handler) {
impl.addCatch(type, from, to, handler);
}
public void addCatch(@Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) {
public void addCatch(@Nonnull Label from, @Nonnull Label to, @Nonnull Label handler) {
impl.addCatch(from, to, handler);
}
@ -108,7 +108,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
}
public void addInstruction10t(@Nonnull Opcode opcode,
@Nonnull LabelMethodItem label) {
@Nonnull Label label) {
}
public void addInstruction10x(@Nonnull Opcode opcode) {
@ -134,7 +134,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
}
public void addInstruction20t(@Nonnull Opcode opcode,
@Nonnull LabelMethodItem label) {
@Nonnull Label label) {
}
public void addInstruction21c(@Nonnull Opcode opcode,
@ -159,7 +159,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
public void addInstruction21t(@Nonnull Opcode opcode,
int registerA,
@Nonnull LabelMethodItem label) {
@Nonnull Label label) {
}
public void addInstruction22b(@Nonnull Opcode opcode,
@ -183,7 +183,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
public void addInstruction22t(@Nonnull Opcode opcode,
int registerA,
int registerB,
@Nonnull LabelMethodItem labelMethodItem) {
@Nonnull Label labelMethodItem) {
}
public void addInstruction22x(@Nonnull Opcode opcode,
@ -198,7 +198,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
}
public void addInstruction30t(@Nonnull Opcode opcode,
@Nonnull LabelMethodItem label) {
@Nonnull Label label) {
}
public void addInstruction31c(@Nonnull Opcode opcode,
@ -213,7 +213,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
public void addInstruction31t(@Nonnull Opcode opcode,
int registerA,
@Nonnull LabelMethodItem label) {
@Nonnull Label label) {
}
public void addInstruction32x(@Nonnull Opcode opcode,
@ -242,7 +242,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
long literal) {
}
public void addPackedSwitchPayload(int startKey, @Nullable List<? extends LabelMethodItem> switchElements) {
public void addPackedSwitchPayload(int startKey, @Nullable List<? extends Label> switchElements) {
}
public void addSparseSwitchPayload(@Nullable List<? extends SwitchLabelElement> switchElements) {

View File

@ -1,21 +0,0 @@
package org.jf.dexlib2.builder;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
class MethodItem {
@Nullable
MethodLocation location;
@Nonnull
public MethodLocation getLocation() {
if (location == null) {
throw new IllegalStateException("Cannot get the address of MethodItem that hasn't been added to a method.");
}
return location;
}
public boolean isPlaced() {
return location != null;
}
}

View File

@ -14,7 +14,7 @@ public class MethodLocation {
int codeAddress;
int index;
private List<LabelMethodItem> labels = Lists.newArrayList();
private List<Label> labels = Lists.newArrayList();
MethodLocation(@Nullable Instruction instruction,
int codeAddress, int index) {
@ -37,11 +37,11 @@ public class MethodLocation {
}
@Nonnull
public Collection<LabelMethodItem> getLabels() {
public Collection<Label> getLabels() {
return Collections.unmodifiableCollection(labels);
}
public void addLabel(@Nonnull LabelMethodItem label) {
public void addLabel(@Nonnull Label label) {
if (label.isPlaced()) {
label.getLocation().removeLabel(label);
}
@ -50,13 +50,13 @@ public class MethodLocation {
}
@Nonnull
public LabelMethodItem addNewLabel() {
LabelMethodItem label = new LabelMethodItem(this);
public Label addNewLabel() {
Label label = new Label(this);
labels.add(label);
return label;
}
public void removeLabel(@Nonnull LabelMethodItem label) {
public void removeLabel(@Nonnull Label label) {
for (int i=0; i<labels.size(); i++) {
labels.remove(label);
}

View File

@ -26,17 +26,17 @@ public class MutableMethodImplementation<ReferenceType extends Reference> {
return Collections.unmodifiableList(instructionList);
}
public void addCatch(@Nullable TypeReference type, @Nonnull LabelMethodItem from,
@Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) {
public void addCatch(@Nullable TypeReference type, @Nonnull Label from,
@Nonnull Label to, @Nonnull Label handler) {
tryBlocks.add(new BuilderTryBlock(from, to, type, handler));
}
public void addCatch(@Nullable String type, @Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to,
@Nonnull LabelMethodItem handler) {
public void addCatch(@Nullable String type, @Nonnull Label from, @Nonnull Label to,
@Nonnull Label handler) {
tryBlocks.add(new BuilderTryBlock(from, to, type, handler));
}
public void addCatch(@Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) {
public void addCatch(@Nonnull Label from, @Nonnull Label to, @Nonnull Label handler) {
tryBlocks.add(new BuilderTryBlock(from, to, handler));
}
}

View File

@ -3,6 +3,6 @@ package org.jf.dexlib2.builder;
import javax.annotation.Nonnull;
public class SwitchLabelElement {
public SwitchLabelElement(int key, @Nonnull LabelMethodItem dest) {
public SwitchLabelElement(int key, @Nonnull Label dest) {
}
}