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 { class BuilderExceptionHandler {
static ExceptionHandler newExceptionHandler(@Nullable final TypeReference exceptionType, static ExceptionHandler newExceptionHandler(@Nullable final TypeReference exceptionType,
@Nonnull final LabelMethodItem handler) { @Nonnull final Label handler) {
if (exceptionType == null) { if (exceptionType == null) {
return newExceptionHandler(handler); 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() { return new BaseExceptionHandler() {
@Nullable @Override public String getExceptionType() { @Nullable @Override public String getExceptionType() {
return null; return null;
@ -41,7 +41,7 @@ class BuilderExceptionHandler {
} }
static ExceptionHandler newExceptionHandler(@Nullable final String exceptionType, static ExceptionHandler newExceptionHandler(@Nullable final String exceptionType,
@Nonnull final LabelMethodItem handler) { @Nonnull final Label handler) {
if (exceptionType == null) { if (exceptionType == null) {
return newExceptionHandler(handler); return newExceptionHandler(handler);
} }

View File

@ -12,27 +12,26 @@ import java.util.List;
class BuilderTryBlock extends BaseTryBlock<ExceptionHandler> { class BuilderTryBlock extends BaseTryBlock<ExceptionHandler> {
// We only ever have one exception handler per try block. They are later merged as needed in TryListBuilder // 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 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 // The end location is exclusive, it should point to the codeAddress of the instruction immediately after the last
// covered instruction. // covered instruction.
@Nonnull public final LabelMethodItem end; @Nonnull public final Label end;
public BuilderTryBlock(@Nonnull LabelMethodItem start, @Nonnull LabelMethodItem end, public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable String exceptionType,
@Nullable String exceptionType, @Nonnull LabelMethodItem handler) { @Nonnull Label handler) {
this.start = start; this.start = start;
this.end = end; this.end = end;
this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler);
} }
public BuilderTryBlock(@Nonnull LabelMethodItem start, @Nonnull LabelMethodItem end, public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable TypeReference exceptionType,
@Nullable TypeReference exceptionType, @Nonnull LabelMethodItem handler) { @Nonnull Label handler) {
this.start = start; this.start = start;
this.end = end; this.end = end;
this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler);
} }
public BuilderTryBlock(@Nonnull LabelMethodItem start, @Nonnull LabelMethodItem end, public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label handler) {
@Nonnull LabelMethodItem handler) {
this.start = start; this.start = start;
this.end = end; this.end = end;
this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(handler); 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> { public class MethodImplementationBuilder<ReferenceType extends Reference> {
// Contains all named labels - both placed and unplaced // 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 @Nonnull
private final MutableMethodImplementation<ReferenceType> impl; private final MutableMethodImplementation<ReferenceType> impl;
@ -34,8 +34,8 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
* @return A LabelRef representing the label * @return A LabelRef representing the label
*/ */
@Nonnull @Nonnull
public LabelMethodItem addLabel(@Nonnull String name) { public Label addLabel(@Nonnull String name) {
LabelMethodItem label = labels.get(name); Label label = labels.get(name);
if (label != null) { if (label != null) {
if (label.isPlaced()) { if (label.isPlaced()) {
@ -62,26 +62,26 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
* @return A LabelRef representing the label * @return A LabelRef representing the label
*/ */
@Nonnull @Nonnull
public LabelMethodItem getLabel(@Nonnull String name) { public Label getLabel(@Nonnull String name) {
LabelMethodItem label = labels.get(name); Label label = labels.get(name);
if (label == null) { if (label == null) {
label = new LabelMethodItem(); label = new Label();
labels.put(name, label); labels.put(name, label);
} }
return label; return label;
} }
public void addCatch(@Nullable TypeReference type, @Nonnull LabelMethodItem from, public void addCatch(@Nullable TypeReference type, @Nonnull Label from,
@Nonnull LabelMethodItem to, @Nonnull LabelMethodItem handler) { @Nonnull Label to, @Nonnull Label handler) {
impl.addCatch(type, from, to, handler); impl.addCatch(type, from, to, handler);
} }
public void addCatch(@Nullable String type, @Nonnull LabelMethodItem from, @Nonnull LabelMethodItem to, public void addCatch(@Nullable String type, @Nonnull Label from, @Nonnull Label to,
@Nonnull LabelMethodItem handler) { @Nonnull Label handler) {
impl.addCatch(type, from, to, 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); impl.addCatch(from, to, handler);
} }
@ -108,7 +108,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
} }
public void addInstruction10t(@Nonnull Opcode opcode, public void addInstruction10t(@Nonnull Opcode opcode,
@Nonnull LabelMethodItem label) { @Nonnull Label label) {
} }
public void addInstruction10x(@Nonnull Opcode opcode) { public void addInstruction10x(@Nonnull Opcode opcode) {
@ -134,7 +134,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
} }
public void addInstruction20t(@Nonnull Opcode opcode, public void addInstruction20t(@Nonnull Opcode opcode,
@Nonnull LabelMethodItem label) { @Nonnull Label label) {
} }
public void addInstruction21c(@Nonnull Opcode opcode, public void addInstruction21c(@Nonnull Opcode opcode,
@ -159,7 +159,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
public void addInstruction21t(@Nonnull Opcode opcode, public void addInstruction21t(@Nonnull Opcode opcode,
int registerA, int registerA,
@Nonnull LabelMethodItem label) { @Nonnull Label label) {
} }
public void addInstruction22b(@Nonnull Opcode opcode, public void addInstruction22b(@Nonnull Opcode opcode,
@ -183,7 +183,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
public void addInstruction22t(@Nonnull Opcode opcode, public void addInstruction22t(@Nonnull Opcode opcode,
int registerA, int registerA,
int registerB, int registerB,
@Nonnull LabelMethodItem labelMethodItem) { @Nonnull Label labelMethodItem) {
} }
public void addInstruction22x(@Nonnull Opcode opcode, public void addInstruction22x(@Nonnull Opcode opcode,
@ -198,7 +198,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
} }
public void addInstruction30t(@Nonnull Opcode opcode, public void addInstruction30t(@Nonnull Opcode opcode,
@Nonnull LabelMethodItem label) { @Nonnull Label label) {
} }
public void addInstruction31c(@Nonnull Opcode opcode, public void addInstruction31c(@Nonnull Opcode opcode,
@ -213,7 +213,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
public void addInstruction31t(@Nonnull Opcode opcode, public void addInstruction31t(@Nonnull Opcode opcode,
int registerA, int registerA,
@Nonnull LabelMethodItem label) { @Nonnull Label label) {
} }
public void addInstruction32x(@Nonnull Opcode opcode, public void addInstruction32x(@Nonnull Opcode opcode,
@ -242,7 +242,7 @@ public class MethodImplementationBuilder<ReferenceType extends Reference> {
long literal) { 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) { 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 codeAddress;
int index; int index;
private List<LabelMethodItem> labels = Lists.newArrayList(); private List<Label> labels = Lists.newArrayList();
MethodLocation(@Nullable Instruction instruction, MethodLocation(@Nullable Instruction instruction,
int codeAddress, int index) { int codeAddress, int index) {
@ -37,11 +37,11 @@ public class MethodLocation {
} }
@Nonnull @Nonnull
public Collection<LabelMethodItem> getLabels() { public Collection<Label> getLabels() {
return Collections.unmodifiableCollection(labels); return Collections.unmodifiableCollection(labels);
} }
public void addLabel(@Nonnull LabelMethodItem label) { public void addLabel(@Nonnull Label label) {
if (label.isPlaced()) { if (label.isPlaced()) {
label.getLocation().removeLabel(label); label.getLocation().removeLabel(label);
} }
@ -50,13 +50,13 @@ public class MethodLocation {
} }
@Nonnull @Nonnull
public LabelMethodItem addNewLabel() { public Label addNewLabel() {
LabelMethodItem label = new LabelMethodItem(this); Label label = new Label(this);
labels.add(label); labels.add(label);
return label; return label;
} }
public void removeLabel(@Nonnull LabelMethodItem label) { public void removeLabel(@Nonnull Label label) {
for (int i=0; i<labels.size(); i++) { for (int i=0; i<labels.size(); i++) {
labels.remove(label); labels.remove(label);
} }

View File

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

View File

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