Skip to content

Commit

Permalink
Dep refactor 3 (#41)
Browse files Browse the repository at this point in the history
* Removing guava usage from dexlib2/utils

* Removing guava dependencies from dexlib2 utils, builder and rewriter

* Fixing imports and comments

* Adding some comments

* Minor fix

* Undoing unnecessary changes

* Addressing comments

* Adding ArraySortedSetTest

* Adding license and fixing format of ArraySortedSetTest
  • Loading branch information
melcz authored Apr 11, 2024
1 parent ee40cfa commit 431b328
Show file tree
Hide file tree
Showing 26 changed files with 1,502 additions and 118 deletions.
34 changes: 17 additions & 17 deletions dexlib2/src/main/java/com/android/tools/smali/dexlib2/Opcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

package com.android.tools.smali.dexlib2;

import com.google.common.collect.ImmutableRangeMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import com.android.tools.smali.util.UnmodifiableRangeMap;
import com.android.tools.smali.util.Range;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public enum Opcode
Expand Down Expand Up @@ -348,8 +348,8 @@ private static int maxApi(int api) {

// values and minApis provide a mapping of api -> bytecode value.
// the apis in minApis are guaranteed to be
public final RangeMap<Integer, Short> apiToValueMap;
public final RangeMap<Integer, Short> artVersionToValueMap;
public final UnmodifiableRangeMap<Integer, Short> apiToValueMap;
public final UnmodifiableRangeMap<Integer, Short> artVersionToValueMap;

public final String name;
public final int referenceType;
Expand All @@ -371,8 +371,8 @@ private static int maxApi(int api) {

Opcode(List<VersionConstraint> versionConstraints, String opcodeName, int referenceType, int referenceType2,
Format format, int flags) {
ImmutableRangeMap.Builder<Integer, Short> apiToValueBuilder = ImmutableRangeMap.builder();
ImmutableRangeMap.Builder<Integer, Short> artVersionToValueBuilder = ImmutableRangeMap.builder();
UnmodifiableRangeMap.Builder<Integer, Short> apiToValueBuilder = UnmodifiableRangeMap.builder();
UnmodifiableRangeMap.Builder<Integer, Short> artVersionToValueBuilder = UnmodifiableRangeMap.builder();

for (VersionConstraint versionConstraint : versionConstraints) {
if (!versionConstraint.apiRange.isEmpty()) {
Expand All @@ -393,41 +393,41 @@ private static int maxApi(int api) {
}

private static List<VersionConstraint> firstApi(int opcodeValue, int api) {
return Lists.newArrayList(new VersionConstraint(Range.atLeast(api), Range.openClosed(0, 0), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.atLeast(api), Range.openClosed(0, 0), opcodeValue));
}

private static List<VersionConstraint> lastApi(int opcodeValue, int api) {
return Lists.newArrayList(new VersionConstraint(Range.atMost(api), Range.openClosed(0, 0), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.atMost(api), Range.openClosed(0, 0), opcodeValue));
}

private static List<VersionConstraint> betweenApi(int opcodeValue, int minApi, int maxApi) {
return Lists.newArrayList(new VersionConstraint(Range.closed(minApi, maxApi), Range.openClosed(0, 0),
return Arrays.asList(new VersionConstraint(Range.closed(minApi, maxApi), Range.openClosed(0, 0),
opcodeValue));
}

private static List<VersionConstraint> firstArtVersion(int opcodeValue, int artVersion) {
return Lists.newArrayList(new VersionConstraint(Range.openClosed(0, 0), Range.atLeast(artVersion), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.openClosed(0, 0), Range.atLeast(artVersion), opcodeValue));
}

private static List<VersionConstraint> lastArtVersion(int opcodeValue, int artVersion) {
return Lists.newArrayList(new VersionConstraint(Range.openClosed(0, 0), Range.atMost(artVersion), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.openClosed(0, 0), Range.atMost(artVersion), opcodeValue));
}

private static List<VersionConstraint> allVersions(int opcodeValue) {
return Lists.newArrayList(new VersionConstraint(Range.<Integer>all(), Range.<Integer>all(), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.allValues(), Range.allValues(), opcodeValue));
}

private static List<VersionConstraint> allApis(int opcodeValue) {
return Lists.newArrayList(new VersionConstraint(Range.<Integer>all(), Range.openClosed(0, 0), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.allValues(), Range.openClosed(0, 0), opcodeValue));
}

private static List<VersionConstraint> allArtVersions(int opcodeValue) {
return Lists.newArrayList(new VersionConstraint(Range.openClosed(0, 0), Range.<Integer>all(), opcodeValue));
return Arrays.asList(new VersionConstraint(Range.openClosed(0, 0), Range.allValues(), opcodeValue));
}

@SuppressWarnings("unchecked")
private static List<VersionConstraint> combine(List<VersionConstraint>... versionConstraints) {
List<VersionConstraint> combinedList = Lists.newArrayList();
List<VersionConstraint> combinedList = new ArrayList<>();
for (List<VersionConstraint> versionConstraintList: versionConstraints) {
combinedList.addAll(versionConstraintList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@

package com.android.tools.smali.dexlib2;

import com.google.common.collect.RangeMap;
import static com.android.tools.smali.dexlib2.VersionMap.NO_VERSION;
import static com.android.tools.smali.dexlib2.VersionMap.mapApiToArtVersion;
import static com.android.tools.smali.dexlib2.VersionMap.mapArtVersionToApi;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.EnumMap;
import java.util.HashMap;

import static com.android.tools.smali.dexlib2.VersionMap.NO_VERSION;
import static com.android.tools.smali.dexlib2.VersionMap.mapApiToArtVersion;
import static com.android.tools.smali.dexlib2.VersionMap.mapArtVersionToApi;
import com.android.tools.smali.util.UnmodifiableRangeMap;

public class Opcodes {

Expand Down Expand Up @@ -103,7 +103,7 @@ private Opcodes(int api, int artVersion) {
}

for (Opcode opcode: Opcode.values()) {
RangeMap<Integer, Short> versionToValueMap;
UnmodifiableRangeMap<Integer, Short> versionToValueMap;

if (isArt()) {
versionToValueMap = opcode.artVersionToValueMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@

package com.android.tools.smali.dexlib2.builder;

import com.google.common.collect.ImmutableList;
import com.android.tools.smali.dexlib2.base.BaseTryBlock;
import com.android.tools.smali.dexlib2.iface.reference.TypeReference;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BuilderTryBlock extends BaseTryBlock<BuilderExceptionHandler> {
Expand Down Expand Up @@ -75,6 +76,6 @@ public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label
}

@Nonnull @Override public List<? extends BuilderExceptionHandler> getExceptionHandlers() {
return ImmutableList.of(exceptionHandler);
return Collections.unmodifiableList(Arrays.asList(exceptionHandler));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@

package com.android.tools.smali.dexlib2.builder;

import com.google.common.collect.ImmutableList;

import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
Expand All @@ -50,7 +49,7 @@ public abstract class LocatedItems<T extends ItemWithLocation> {
@Nonnull
private List<T> getItems() {
if (items == null) {
return ImmutableList.of();
return Collections.emptyList();
}
return items;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@
import com.android.tools.smali.dexlib2.iface.instruction.formats.SparseSwitchPayload;
import com.android.tools.smali.dexlib2.iface.reference.TypeReference;
import com.android.tools.smali.util.ExceptionWithContext;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -138,8 +133,9 @@

public class MutableMethodImplementation implements MethodImplementation {
private final int registerCount;
final ArrayList<MethodLocation> instructionList = Lists.newArrayList(new MethodLocation(null, 0, 0));
private final ArrayList<BuilderTryBlock> tryBlocks = Lists.newArrayList();
final ArrayList<MethodLocation> instructionList = new ArrayList<>(
Arrays.asList(new MethodLocation(null, 0, 0)));
private final ArrayList<BuilderTryBlock> tryBlocks = new ArrayList<>();
private boolean fixInstructions = true;

public MutableMethodImplementation(@Nonnull MethodImplementation methodImplementation) {
Expand All @@ -162,7 +158,7 @@ public MutableMethodImplementation(@Nonnull MethodImplementation methodImplement
codeAddressToIndex[instructionList.get(i).codeAddress] = i;
}

List<Task> switchPayloadTasks = Lists.newArrayList();
List<Task> switchPayloadTasks = new ArrayList<>();
index = 0;
for (final Instruction instruction: methodImplementation.getInstructions()) {
final MethodLocation location = instructionList.get(index);
Expand Down Expand Up @@ -256,17 +252,20 @@ public List<BuilderInstruction> getInstructions() {
if (fixInstructions) {
fixInstructions();
}
return Iterables.concat(
Iterables.transform(instructionList, new Function<MethodLocation, Iterable<? extends DebugItem>>() {
@Nullable @Override public Iterable<? extends DebugItem> apply(@Nullable MethodLocation input) {
assert input != null;
if (fixInstructions) {
throw new IllegalStateException("This iterator was invalidated by a change to" +
" this MutableMethodImplementation.");
}
return input.getDebugItems();
}
}));

ArrayList<DebugItem> debugItems = new ArrayList<>();

for (MethodLocation methodLocation: instructionList) {
assert methodLocation != null;

if (fixInstructions) {
throw new IllegalStateException("This iterator was invalidated by a change to" +
" this MutableMethodImplementation.");
}
debugItems.addAll(methodLocation.getDebugItems());
}

return Collections.unmodifiableList(debugItems);
}

public void addCatch(@Nullable TypeReference type, @Nonnull Label from,
Expand Down Expand Up @@ -438,7 +437,7 @@ private BuilderInstruction getFirstNonNop(int startIndex) {
}

private void fixInstructions() {
HashSet<MethodLocation> payloadLocations = Sets.newHashSet();
HashSet<MethodLocation> payloadLocations = new HashSet<>();

for (MethodLocation location: instructionList) {
BuilderInstruction instruction = location.instruction;
Expand Down Expand Up @@ -1104,7 +1103,7 @@ private BuilderPackedSwitchPayload newBuilderPackedSwitchPayload(@Nonnull Method
baseAddress = switchLocation.codeAddress;
}

List<Label> labels = Lists.newArrayList();
List<Label> labels = new ArrayList<>();
for (SwitchElement element: switchElements) {
labels.add(newLabel(codeAddressToIndex, element.getOffset() + baseAddress));
}
Expand All @@ -1129,7 +1128,7 @@ private BuilderSparseSwitchPayload newBuilderSparseSwitchPayload(@Nonnull Method
baseAddress = switchLocation.codeAddress;
}

List<SwitchLabelElement> labelElements = Lists.newArrayList();
List<SwitchLabelElement> labelElements = new ArrayList<>();
for (SwitchElement element: switchElements) {
labelElements.add(new SwitchLabelElement(element.getKey(),
newLabel(codeAddressToIndex, element.getOffset() + baseAddress)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import com.android.tools.smali.dexlib2.Opcode;
import com.android.tools.smali.dexlib2.builder.BuilderInstruction;
import com.android.tools.smali.dexlib2.iface.instruction.formats.ArrayPayload;
import com.google.common.collect.ImmutableList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

public class BuilderArrayPayload extends BuilderInstruction implements ArrayPayload {
Expand All @@ -50,7 +50,7 @@ public BuilderArrayPayload(int elementWidth,
@Nullable List<Number> arrayElements) {
super(OPCODE);
this.elementWidth = elementWidth;
this.arrayElements = arrayElements==null?ImmutableList.<Number>of():arrayElements;
this.arrayElements = arrayElements == null ? Collections.emptyList() : arrayElements;
}

@Override public int getElementWidth() { return elementWidth; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@
import com.android.tools.smali.dexlib2.builder.BuilderSwitchPayload;
import com.android.tools.smali.dexlib2.builder.Label;
import com.android.tools.smali.dexlib2.iface.instruction.formats.PackedSwitchPayload;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BuilderPackedSwitchPayload extends BuilderSwitchPayload implements
Expand All @@ -52,9 +53,9 @@ public BuilderPackedSwitchPayload(final int startKey,
@Nullable List<? extends Label> switchElements) {
super(OPCODE);
if (switchElements == null) {
this.switchElements = ImmutableList.of();
this.switchElements = Collections.emptyList();
} else {
this.switchElements = Lists.newArrayList();
this.switchElements = new ArrayList<>();
int key = startKey;
for (Label target: switchElements) {
this.switchElements.add(new BuilderSwitchElement(this, key++, target));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import com.android.tools.smali.dexlib2.builder.BuilderSwitchPayload;
import com.android.tools.smali.dexlib2.builder.SwitchLabelElement;
import com.android.tools.smali.dexlib2.iface.instruction.formats.SparseSwitchPayload;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class BuilderSparseSwitchPayload extends BuilderSwitchPayload implements
SparseSwitchPayload {
Expand All @@ -52,14 +52,14 @@ public class BuilderSparseSwitchPayload extends BuilderSwitchPayload implements
public BuilderSparseSwitchPayload(@Nullable List<? extends SwitchLabelElement> switchElements) {
super(OPCODE);
if (switchElements == null) {
this.switchElements = ImmutableList.of();
this.switchElements = Collections.emptyList();
} else {
this.switchElements = Lists.transform(switchElements, new Function<SwitchLabelElement, BuilderSwitchElement>() {
this.switchElements = switchElements.stream().map(new Function<SwitchLabelElement, BuilderSwitchElement>() {
@Nullable @Override public BuilderSwitchElement apply(@Nullable SwitchLabelElement element) {
assert element != null;
return new BuilderSwitchElement(BuilderSparseSwitchPayload.this, element.key, element.target);
}
});
}).collect(Collectors.toList());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

package com.android.tools.smali.dexlib2.rewriter;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.android.tools.smali.dexlib2.base.reference.BaseCallSiteReference;
import com.android.tools.smali.dexlib2.iface.reference.CallSiteReference;
import com.android.tools.smali.dexlib2.iface.reference.MethodHandleReference;
Expand All @@ -40,6 +38,8 @@

import javax.annotation.Nonnull;
import java.util.List;
import java.util.stream.Collectors;
import java.util.function.Function;

public class CallSiteReferenceRewriter implements Rewriter<CallSiteReference> {
@Nonnull protected final Rewriters rewriters;
Expand Down Expand Up @@ -79,12 +79,11 @@ public RewrittenCallSiteReference(@Nonnull CallSiteReference callSiteReference)
}

@Override @Nonnull public List<? extends EncodedValue> getExtraArguments() {
return Lists.transform(callSiteReference.getExtraArguments(),
new Function<EncodedValue, EncodedValue>() {
@Nonnull @Override public EncodedValue apply(EncodedValue encodedValue) {
return RewriterUtils.rewriteValue(rewriters, encodedValue);
}
});
return callSiteReference.getExtraArguments().stream().map(new Function<EncodedValue, EncodedValue>() {
@Nonnull @Override public EncodedValue apply(EncodedValue encodedValue) {
return RewriterUtils.rewriteValue(rewriters, encodedValue);
}
}).collect(Collectors.toList());
}
}
}
Loading

0 comments on commit 431b328

Please sign in to comment.