diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java index 4eecad08..b24cc3e8 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java @@ -41,7 +41,6 @@ import javax.annotation.Nonnull; import java.io.IOException; import java.util.Collection; import java.util.HashSet; -import java.util.List; public class ClassDefinition { @Nonnull public final ClassDef classDef; @@ -77,7 +76,7 @@ public class ClassDefinition { case SPUT_WIDE: { Instruction21c ins = (Instruction21c)instruction; FieldReference fieldRef = (FieldReference)ins.getReference(); - if (fieldRef.getContainingClass().equals((classDef.getType()))) { + if (fieldRef.getDefiningClass().equals((classDef.getType()))) { fieldsSetInStaticConstructor.add(ReferenceUtil.getShortFieldDescriptor(fieldRef)); } break; @@ -135,7 +134,7 @@ public class ClassDefinition { } private void writeInterfaces(IndentingWriter writer) throws IOException { - List interfaces = classDef.getInterfaces(); + Collection interfaces = classDef.getInterfaces(); if (interfaces.size() != 0) { writer.write('\n'); writer.write("# interfaces\n"); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotation.java index fdcc2be4..2c098830 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotation.java @@ -31,37 +31,44 @@ package org.jf.dexlib2.base; +import com.google.common.primitives.Ints; import org.jf.dexlib2.iface.Annotation; -import org.jf.dexlib2.iface.AnnotationElement; +import org.jf.util.CollectionUtils; -import javax.annotation.Nonnull; +import java.util.Comparator; public abstract class BaseAnnotation implements Annotation { @Override public int hashCode() { - return hashCode(this); + int hashCode = getVisibility(); + hashCode = hashCode*31 + getType().hashCode(); + return hashCode*31 + getElements().hashCode(); } @Override public boolean equals(Object o) { - if (o != null && o instanceof Annotation) { - return equals(this, (Annotation)o); + if (o instanceof Annotation) { + Annotation other = (Annotation)o; + return (getVisibility() == other.getVisibility()) && + getType().equals(other.getType()) && + getElements().equals(other.getElements()); } return false; } - public static int hashCode(@Nonnull Annotation annotation) { - int hashCode = annotation.getVisibility(); - hashCode = hashCode*31 + annotation.getType().hashCode(); - for (AnnotationElement element: annotation.getElements()) { - hashCode = hashCode*31 + element.hashCode(); - } - return hashCode; + @Override + public int compareTo(Annotation o) { + int res = Ints.compare(getVisibility(), o.getVisibility()); + if (res != 0) return res; + res = getType().compareTo(o.getType()); + if (res != 0) return res; + return CollectionUtils.compareAsSet(getElements(), o.getElements()); } - public static boolean equals(@Nonnull Annotation annotation1, @Nonnull Annotation annotation2) { - return (annotation1.getVisibility() == annotation2.getVisibility()) && - annotation1.getType().equals(annotation2.getType()) && - annotation1.getElements().equals(annotation2.getElements()); - } + public static final Comparator COMPARE_BY_TYPE = new Comparator() { + @Override + public int compare(Annotation annotation1, Annotation annotation2) { + return annotation1.getType().compareTo(annotation2.getType()); + } + }; } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotationElement.java b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotationElement.java index d81d5f43..edfd451b 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotationElement.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseAnnotationElement.java @@ -33,6 +33,9 @@ package org.jf.dexlib2.base; import org.jf.dexlib2.iface.AnnotationElement; +import javax.annotation.Nonnull; +import java.util.Comparator; + public abstract class BaseAnnotationElement implements AnnotationElement { @Override public int hashCode() { @@ -49,4 +52,18 @@ public abstract class BaseAnnotationElement implements AnnotationElement { } return false; } + + @Override + public int compareTo(AnnotationElement o) { + int res = getName().compareTo(o.getName()); + if (res != 0) return res; + return getValue().compareTo(o.getValue()); + } + + public static final Comparator COMPARE_BY_NAME = new Comparator() { + @Override + public int compare(@Nonnull AnnotationElement element1, @Nonnull AnnotationElement element2) { + return element1.getName().compareTo(element2.getName()); + } + }; } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseExceptionHandler.java similarity index 55% rename from dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableArrayEncodedValue.java rename to dexlib2/src/main/java/org/jf/dexlib2/base/BaseExceptionHandler.java index 177e74e1..591e8c32 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableArrayEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseExceptionHandler.java @@ -29,35 +29,45 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.immutable.sorted.value; +package org.jf.dexlib2.base; -import com.google.common.collect.ImmutableList; -import org.jf.dexlib2.base.value.BaseArrayEncodedValue; -import org.jf.dexlib2.iface.sorted.value.SortedArrayEncodedValue; -import org.jf.dexlib2.iface.value.ArrayEncodedValue; -import org.jf.dexlib2.iface.value.EncodedValue; +import com.google.common.base.Objects; +import com.google.common.primitives.Ints; +import org.jf.dexlib2.iface.ExceptionHandler; import javax.annotation.Nonnull; -import java.util.Collection; +import javax.annotation.Nullable; -public class SortedImmutableArrayEncodedValue extends BaseArrayEncodedValue - implements SortedImmutableEncodedValue, SortedArrayEncodedValue { - @Nonnull public final ImmutableList value; - - public SortedImmutableArrayEncodedValue(@Nonnull Collection value) { - this.value = SortedImmutableEncodedValueFactory.immutableListOf(value); +public abstract class BaseExceptionHandler implements ExceptionHandler { + @Override + public int hashCode() { + String exceptionType = getExceptionType(); + int hashCode = exceptionType==null?0:exceptionType.hashCode(); + return hashCode*31 + getHandlerCodeAddress(); } - public SortedImmutableArrayEncodedValue(@Nonnull ImmutableList value) { - this.value = value; - } - - public static SortedImmutableArrayEncodedValue of(@Nonnull ArrayEncodedValue arrayEncodedValue) { - if (arrayEncodedValue instanceof SortedImmutableArrayEncodedValue) { - return (SortedImmutableArrayEncodedValue)arrayEncodedValue; + @Override + public boolean equals(@Nullable Object o) { + if (o instanceof ExceptionHandler) { + ExceptionHandler other = (ExceptionHandler)o; + return Objects.equal(getExceptionType(), other.getExceptionType()) && + (getHandlerCodeAddress() == other.getHandlerCodeAddress()); } - return new SortedImmutableArrayEncodedValue(arrayEncodedValue.getValue()); + return false; } - @Nonnull public ImmutableList getValue() { return value; } + @Override + public int compareTo(@Nonnull ExceptionHandler o) { + int res; + String exceptionType = getExceptionType(); + if (exceptionType == null) { + if (o.getExceptionType() != null) { + return 1; + } + } else { + res = exceptionType.compareTo(o.getExceptionType()); + if (res != 0) return res; + } + return Ints.compare(getHandlerCodeAddress(), o.getHandlerCodeAddress()); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/BaseMethodParameter.java b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseMethodParameter.java index 7da07868..ecac3c96 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/BaseMethodParameter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/BaseMethodParameter.java @@ -31,8 +31,54 @@ package org.jf.dexlib2.base; +import org.jf.dexlib2.ValueType; import org.jf.dexlib2.base.reference.BaseTypeReference; +import org.jf.dexlib2.iface.Annotation; +import org.jf.dexlib2.iface.AnnotationElement; import org.jf.dexlib2.iface.MethodParameter; +import org.jf.dexlib2.iface.value.ArrayEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.dexlib2.iface.value.StringEncodedValue; + +import javax.annotation.Nullable; public abstract class BaseMethodParameter extends BaseTypeReference implements MethodParameter { + @Nullable + @Override + public String getSignature() { + Annotation signatureAnnotation = null; + for (Annotation annotation: getAnnotations()) { + if (annotation.getType().equals("Ldalvik/annotation/Signature;")) { + signatureAnnotation = annotation; + break; + } + } + if (signatureAnnotation == null) { + return null; + } + + ArrayEncodedValue signatureValues = null; + for (AnnotationElement annotationElement: signatureAnnotation.getElements()) { + if (annotationElement.getName().equals("value")) { + EncodedValue encodedValue = annotationElement.getValue(); + if (encodedValue.getValueType() != ValueType.ARRAY) { + return null; + } + signatureValues = (ArrayEncodedValue)encodedValue; + break; + } + } + if (signatureValues == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + for (EncodedValue signatureValue: signatureValues.getValue()) { + if (signatureValue.getValueType() != ValueType.STRING) { + return null; + } + sb.append(((StringEncodedValue)signatureValue).getValue()); + } + return sb.toString(); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseFieldReference.java b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseFieldReference.java index 77a0b15c..f056f245 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseFieldReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseFieldReference.java @@ -34,30 +34,34 @@ package org.jf.dexlib2.base.reference; import org.jf.dexlib2.iface.reference.FieldReference; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseFieldReference implements FieldReference { @Override public int hashCode() { - return hashCode(this); + int hashCode = getDefiningClass().hashCode(); + hashCode = hashCode*31 + getName().hashCode(); + return hashCode*31 + getType().hashCode(); } @Override - public boolean equals(Object o) { - if (o != null && o instanceof FieldReference) { - return equals(this, (FieldReference)o); + public boolean equals(@Nullable Object o) { + if (o instanceof FieldReference) { + FieldReference other = (FieldReference)o; + return getDefiningClass().equals(other.getDefiningClass()) && + getName().equals(other.getName()) && + getType().equals(other.getType()); + } return false; } - public static int hashCode(@Nonnull FieldReference fieldRef) { - int hashCode = fieldRef.getContainingClass().hashCode(); - hashCode = hashCode*31 + fieldRef.getName().hashCode(); - return hashCode*31 + fieldRef.getType().hashCode(); - } - - public static boolean equals(@Nonnull FieldReference fieldRef1, @Nonnull FieldReference fieldRef2) { - return fieldRef1.getContainingClass().equals(fieldRef2.getContainingClass()) && - fieldRef1.getName().equals(fieldRef2.getName()) && - fieldRef1.getType().equals(fieldRef2.getType()); + @Override + public int compareTo(@Nonnull FieldReference o) { + int res = getDefiningClass().compareTo(o.getDefiningClass()); + if (res != 0) return res; + res = getName().compareTo(o.getName()); + if (res != 0) return res; + return getType().compareTo(o.getType()); } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseMethodReference.java b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseMethodReference.java index 7fb73c33..3d797bc5 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseMethodReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseMethodReference.java @@ -32,38 +32,40 @@ package org.jf.dexlib2.base.reference; import org.jf.dexlib2.iface.reference.MethodReference; -import org.jf.dexlib2.iface.reference.TypeReference; +import org.jf.util.CollectionUtils; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseMethodReference implements MethodReference { @Override public int hashCode() { - return hashCode(this); + int hashCode = getDefiningClass().hashCode(); + hashCode = hashCode*31 + getName().hashCode(); + hashCode = hashCode*31 + getReturnType().hashCode(); + return hashCode*31 + getParameters().hashCode(); } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { if (o != null && o instanceof MethodReference) { - return equals(this, (MethodReference)o); + MethodReference other = (MethodReference)o; + return getDefiningClass().equals(other.getDefiningClass()) && + getName().equals(other.getName()) && + getReturnType().equals(other.getReturnType()) && + getParameters().equals(other.getParameters()); } return false; } - public static int hashCode(@Nonnull MethodReference methodRef) { - int hashCode = methodRef.getContainingClass().hashCode(); - hashCode = hashCode*31 + methodRef.getName().hashCode(); - hashCode = hashCode*31 + methodRef.getReturnType().hashCode(); - for (TypeReference param: methodRef.getParameters()) { - hashCode = hashCode*31 + param.hashCode(); - } - return hashCode; - } - - public static boolean equals(@Nonnull MethodReference methodRef1, @Nonnull MethodReference methodRef2) { - return methodRef1.getContainingClass().equals(methodRef2.getContainingClass()) && - methodRef1.getName().equals(methodRef2.getName()) && - methodRef1.getReturnType().equals(methodRef2.getReturnType()) && - methodRef1.getParameters().equals(methodRef2.getParameters()); + @Override + public int compareTo(@Nonnull MethodReference o) { + int res = getDefiningClass().compareTo(o.getDefiningClass()); + if (res != 0) return res; + res = getName().compareTo(o.getName()); + if (res != 0) return res; + res = getReturnType().compareTo(o.getReturnType()); + if (res != 0) return res; + return CollectionUtils.compareAsList(getParameters(), o.getParameters()); } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseStringReference.java b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseStringReference.java index 225bec86..c6daa91e 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseStringReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseStringReference.java @@ -34,27 +34,25 @@ package org.jf.dexlib2.base.reference; import org.jf.dexlib2.iface.reference.StringReference; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseStringReference implements StringReference { @Override public int hashCode() { - return hashCode(this); + return getString().hashCode(); } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { if (o != null && o instanceof StringReference) { - return equals(this, (StringReference)o); + return getString().equals(((StringReference)o).getString()); } return false; } - public static int hashCode(@Nonnull StringReference stringRef) { - return stringRef.getString().hashCode(); - } - - public static boolean equals(@Nonnull StringReference stringRef1, @Nonnull StringReference stringRef2) { - return stringRef1.getString().equals(stringRef2.getString()); + @Override + public int compareTo(@Nonnull CharSequence o) { + return getString().compareTo(o.toString()); } @Override public int length() { return getString().length(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseTypeReference.java b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseTypeReference.java index f813279d..1d5d6f56 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseTypeReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/reference/BaseTypeReference.java @@ -38,23 +38,20 @@ import javax.annotation.Nonnull; public abstract class BaseTypeReference implements TypeReference { @Override public int hashCode() { - return hashCode(this); + return getType().hashCode(); } @Override public boolean equals(Object o) { if (o != null && o instanceof TypeReference) { - return equals(this, (TypeReference)o); + return getType().equals(((TypeReference)o).getType()); } return false; } - public static int hashCode(@Nonnull TypeReference typeRef) { - return typeRef.getType().hashCode(); - } - - public static boolean equals(@Nonnull TypeReference typeRef1, @Nonnull TypeReference typeRef2) { - return typeRef1.getType().equals(typeRef2.getType()); + @Override + public int compareTo(@Nonnull CharSequence o) { + return getType().compareTo(o.toString()); } @Override public int length() { return getType().length(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseAnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseAnnotationEncodedValue.java index 8b660759..828c1eca 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseAnnotationEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseAnnotationEncodedValue.java @@ -31,8 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.AnnotationEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.util.CollectionUtils; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseAnnotationEncodedValue implements AnnotationEncodedValue { @Override @@ -42,13 +48,26 @@ public abstract class BaseAnnotationEncodedValue implements AnnotationEncodedVal } @Override - public boolean equals(Object o) { - if (o != null && o instanceof AnnotationEncodedValue) { - return getType().equals(((AnnotationEncodedValue) o).getType()) && - getElements().equals(((AnnotationEncodedValue) o).getElements()); + public boolean equals(@Nullable Object o) { + if (o instanceof AnnotationEncodedValue) { + AnnotationEncodedValue other = (AnnotationEncodedValue)o; + return getType().equals(other.getType()) && + getElements().equals(other.getElements()); } return false; } - public int getValueType() { return ValueType.ANNOTATION; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + AnnotationEncodedValue other = (AnnotationEncodedValue)o; + res = getType().compareTo(other.getType()); + if (res != 0) return res; + return CollectionUtils.compareAsSet(getElements(), other.getElements()); + } + + public int getValueType() { + return ValueType.ANNOTATION; + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseArrayEncodedValue.java index 3c1ae71e..7c6efd3e 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseArrayEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseArrayEncodedValue.java @@ -31,8 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.ArrayEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; +import org.jf.util.CollectionUtils; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseArrayEncodedValue implements ArrayEncodedValue { @Override @@ -41,12 +47,18 @@ public abstract class BaseArrayEncodedValue implements ArrayEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof ArrayEncodedValue) { - return getValue().equals(((ArrayEncodedValue) o).getValue()); + public boolean equals(@Nullable Object o) { + if (o instanceof ArrayEncodedValue) { + return getValue().equals(((ArrayEncodedValue)o).getValue()); } return false; } + @Override public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return CollectionUtils.compareAsList(getValue(), ((ArrayEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.ARRAY; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseBooleanEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseBooleanEncodedValue.java index 2070f5bd..aeedc2c0 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseBooleanEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseBooleanEncodedValue.java @@ -31,8 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Booleans; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.BooleanEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseBooleanEncodedValue implements BooleanEncodedValue { @Override @@ -41,12 +47,19 @@ public abstract class BaseBooleanEncodedValue implements BooleanEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof BooleanEncodedValue) { - return getValue() == ((BooleanEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof BooleanEncodedValue) { + return getValue() == ((BooleanEncodedValue)o).getValue(); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Booleans.compare(getValue(), ((BooleanEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.BOOLEAN; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseByteEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseByteEncodedValue.java index 7d27b54a..5ec16241 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseByteEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseByteEncodedValue.java @@ -31,8 +31,13 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.ByteEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseByteEncodedValue implements ByteEncodedValue { @Override @@ -41,12 +46,19 @@ public abstract class BaseByteEncodedValue implements ByteEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof ByteEncodedValue) { - return getValue() == ((ByteEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof ByteEncodedValue) { + return getValue() == ((ByteEncodedValue)o).getValue(); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Ints.compare(getValue(), ((ByteEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.BYTE; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseCharEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseCharEncodedValue.java index ceb23dd1..9f076e28 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseCharEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseCharEncodedValue.java @@ -31,8 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Chars; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.CharEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseCharEncodedValue implements CharEncodedValue { @Override @@ -41,12 +47,19 @@ public abstract class BaseCharEncodedValue implements CharEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof CharEncodedValue) { - return getValue() == ((CharEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof CharEncodedValue) { + return getValue() == ((CharEncodedValue)o).getValue(); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Chars.compare(getValue(), ((CharEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.CHAR; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseDoubleEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseDoubleEncodedValue.java index 73bcb85a..d8b08c36 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseDoubleEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseDoubleEncodedValue.java @@ -31,24 +31,36 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.DoubleEncodedValue; +import org.jf.dexlib2.iface.value.EncodedValue; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; public abstract class BaseDoubleEncodedValue implements DoubleEncodedValue { @Override public int hashCode() { - long value = Double.doubleToRawLongBits(getValue()); - int hashCode = (int)value; - return hashCode*31 + (int)(value>>>32); + long v = Double.doubleToRawLongBits(getValue()); + return (int)(v^(v>>>32)); } @Override - public boolean equals(Object o) { - if (o != null && o instanceof DoubleEncodedValue) { - return getValue() == ((DoubleEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof DoubleEncodedValue) { + return Double.doubleToRawLongBits(getValue()) == + Double.doubleToRawLongBits(((DoubleEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Double.compare(getValue(), ((DoubleEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.DOUBLE; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseEnumEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseEnumEncodedValue.java index 88628101..ea2ec121 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseEnumEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseEnumEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.EnumEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseEnumEncodedValue implements EnumEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseEnumEncodedValue implements EnumEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof EnumEncodedValue) { - return getValue().equals(((EnumEncodedValue) o).getValue()); + public boolean equals(@Nullable Object o) { + if (o instanceof EnumEncodedValue) { + return getValue().equals(((EnumEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return getValue().compareTo(((EnumEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.ENUM; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFieldEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFieldEncodedValue.java index bcbf6490..8d4f3d91 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFieldEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFieldEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.FieldEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseFieldEncodedValue implements FieldEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseFieldEncodedValue implements FieldEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof FieldEncodedValue) { - return getValue().equals(((FieldEncodedValue) o).getValue()); + public boolean equals(@Nullable Object o) { + if (o instanceof FieldEncodedValue) { + return getValue().equals(((FieldEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return getValue().compareTo(((FieldEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.FIELD; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFloatEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFloatEncodedValue.java index ab2955fa..50925679 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFloatEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseFloatEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.FloatEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseFloatEncodedValue implements FloatEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseFloatEncodedValue implements FloatEncodedValue { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { if (o != null && o instanceof FloatEncodedValue) { - return getValue() == ((FloatEncodedValue) o).getValue(); + return Float.floatToRawIntBits(getValue()) == Float.floatToRawIntBits(((FloatEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Float.compare(getValue(), ((FloatEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.FLOAT; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseIntEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseIntEncodedValue.java index 6ebc475b..94f7e146 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseIntEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseIntEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.IntEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseIntEncodedValue implements IntEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseIntEncodedValue implements IntEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof IntEncodedValue) { - return getValue() == ((IntEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof IntEncodedValue) { + return getValue() == ((IntEncodedValue)o).getValue(); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Ints.compare(getValue(), ((IntEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.INT; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseLongEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseLongEncodedValue.java index e0f34099..8808dda6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseLongEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseLongEncodedValue.java @@ -31,9 +31,15 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; +import com.google.common.primitives.Longs; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.LongEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseLongEncodedValue implements LongEncodedValue { @Override public int hashCode() { @@ -43,12 +49,19 @@ public abstract class BaseLongEncodedValue implements LongEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof LongEncodedValue) { - return getValue() == ((LongEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof LongEncodedValue) { + return getValue() == ((LongEncodedValue)o).getValue(); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Longs.compare(getValue(), ((LongEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.LONG; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseMethodEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseMethodEncodedValue.java index a7a5c7f7..71492147 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseMethodEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseMethodEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.MethodEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseMethodEncodedValue implements MethodEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseMethodEncodedValue implements MethodEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof MethodEncodedValue) { - return getValue().equals(((MethodEncodedValue) o).getValue()); + public boolean equals(@Nullable Object o) { + if (o instanceof MethodEncodedValue) { + return getValue().equals(((MethodEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return getValue().compareTo(((MethodEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.METHOD; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseNullEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseNullEncodedValue.java index 526de226..923619bd 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseNullEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseNullEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.NullEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseNullEncodedValue implements NullEncodedValue { @Override public int hashCode() { @@ -41,8 +46,13 @@ public abstract class BaseNullEncodedValue implements NullEncodedValue { } @Override - public boolean equals(Object o) { - return o != null && o instanceof NullEncodedValue; + public boolean equals(@Nullable Object o) { + return o instanceof NullEncodedValue; + } + + @Override + public int compareTo(@Nonnull EncodedValue o) { + return Ints.compare(getValueType(), o.getValueType()); } public int getValueType() { return ValueType.NULL; } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseShortEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseShortEncodedValue.java index e5849b8c..88aabaa8 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseShortEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseShortEncodedValue.java @@ -31,9 +31,15 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; +import com.google.common.primitives.Shorts; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.ShortEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseShortEncodedValue implements ShortEncodedValue { @Override public int hashCode() { @@ -41,12 +47,19 @@ public abstract class BaseShortEncodedValue implements ShortEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof ShortEncodedValue) { - return getValue() == ((ShortEncodedValue) o).getValue(); + public boolean equals(@Nullable Object o) { + if (o instanceof ShortEncodedValue) { + return getValue() == ((ShortEncodedValue)o).getValue(); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return Shorts.compare(getValue(), ((ShortEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.SHORT; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseStringEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseStringEncodedValue.java index 9cfdf338..a9bd6cdd 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseStringEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseStringEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.StringEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseStringEncodedValue implements StringEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseStringEncodedValue implements StringEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof StringEncodedValue) { - return getValue().equals(((StringEncodedValue) o).getValue()); + public boolean equals(@Nullable Object o) { + if (o instanceof StringEncodedValue) { + return getValue().equals(((StringEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return getValue().compareTo(((StringEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.STRING; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseTypeEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseTypeEncodedValue.java index 88590620..80f7399d 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseTypeEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/base/value/BaseTypeEncodedValue.java @@ -31,9 +31,14 @@ package org.jf.dexlib2.base.value; +import com.google.common.primitives.Ints; import org.jf.dexlib2.ValueType; +import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.TypeEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + public abstract class BaseTypeEncodedValue implements TypeEncodedValue { @Override public int hashCode() { @@ -41,12 +46,19 @@ public abstract class BaseTypeEncodedValue implements TypeEncodedValue { } @Override - public boolean equals(Object o) { - if (o != null && o instanceof TypeEncodedValue) { - return getValue().equals(((TypeEncodedValue) o).getValue()); + public boolean equals(@Nullable Object o) { + if (o instanceof TypeEncodedValue) { + return getValue().equals(((TypeEncodedValue)o).getValue()); } return false; } + @Override + public int compareTo(@Nonnull EncodedValue o) { + int res = Ints.compare(getValueType(), o.getValueType()); + if (res != 0) return res; + return getValue().compareTo(((TypeEncodedValue)o).getValue()); + } + public int getValueType() { return ValueType.TYPE; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedAnnotation.java index 20e413ed..804d7032 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedAnnotation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedAnnotation.java @@ -32,10 +32,10 @@ package org.jf.dexlib2.dexbacked; import org.jf.dexlib2.base.BaseAnnotation; -import org.jf.dexlib2.dexbacked.util.VariableSizeCollection; +import org.jf.dexlib2.dexbacked.util.VariableSizeSet; import javax.annotation.Nonnull; -import java.util.Collection; +import java.util.Set; public class DexBackedAnnotation extends BaseAnnotation { @Nonnull public final DexBuffer dexBuf; @@ -59,11 +59,11 @@ public class DexBackedAnnotation extends BaseAnnotation { @Nonnull @Override - public Collection getElements() { + public Set getElements() { DexReader reader = dexBuf.readerAt(elementsOffset); final int size = reader.readSmallUleb128(); - return new VariableSizeCollection(dexBuf, reader.getOffset(), size) { + return new VariableSizeSet(dexBuf, reader.getOffset(), size) { @Nonnull @Override protected DexBackedAnnotationElement readNextItem(@Nonnull DexReader reader, int index) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedCatchAllExceptionHandler.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedCatchAllExceptionHandler.java index e2ef3c4a..9b63bbda 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedCatchAllExceptionHandler.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedCatchAllExceptionHandler.java @@ -31,12 +31,13 @@ package org.jf.dexlib2.dexbacked; +import org.jf.dexlib2.base.BaseExceptionHandler; import org.jf.dexlib2.iface.ExceptionHandler; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class DexBackedCatchAllExceptionHandler implements ExceptionHandler { +public class DexBackedCatchAllExceptionHandler extends BaseExceptionHandler implements ExceptionHandler { private final int handlerCodeAddress; public DexBackedCatchAllExceptionHandler(@Nonnull DexReader reader) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedClassDef.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedClassDef.java index f69fa5a4..f788b0d4 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedClassDef.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedClassDef.java @@ -31,17 +31,19 @@ package org.jf.dexlib2.dexbacked; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.reference.BaseTypeReference; -import org.jf.dexlib2.dexbacked.util.*; +import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory; +import org.jf.dexlib2.dexbacked.util.FixedSizeSet; +import org.jf.dexlib2.dexbacked.util.StaticInitialValueIterator; +import org.jf.dexlib2.dexbacked.util.VariableSizeIterator; import org.jf.dexlib2.iface.ClassDef; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.AbstractCollection; -import java.util.Collection; +import java.util.AbstractSet; import java.util.Iterator; -import java.util.List; +import java.util.Set; public class DexBackedClassDef extends BaseTypeReference implements ClassDef { @Nonnull public final DexBuffer dexBuf; @@ -92,11 +94,11 @@ public class DexBackedClassDef extends BaseTypeReference implements ClassDef { @Nonnull @Override - public List getInterfaces() { + public Set getInterfaces() { final int interfacesOffset = dexBuf.readSmallUint(classDefOffset + INTERFACES_OFFSET); if (interfacesOffset > 0) { final int size = dexBuf.readSmallUint(interfacesOffset); - return new FixedSizeList() { + return new FixedSizeSet() { @Nonnull @Override public String readItem(int index) { @@ -106,18 +108,18 @@ public class DexBackedClassDef extends BaseTypeReference implements ClassDef { @Override public int size() { return size; } }; } - return ImmutableList.of(); + return ImmutableSet.of(); } @Nonnull @Override - public List getAnnotations() { + public Set getAnnotations() { return getAnnotationsDirectory().getClassAnnotations(); } @Nonnull @Override - public Collection getFields() { + public Set getFields() { int classDataOffset = getClassDataOffset(); if (getClassDataOffset() != 0) { DexReader reader = dexBuf.readerAt(classDataOffset); @@ -133,7 +135,7 @@ public class DexBackedClassDef extends BaseTypeReference implements ClassDef { dexBuf.readSmallUint(classDefOffset + STATIC_INITIAL_VALUES_OFFSET); final int fieldsStartOffset = reader.getOffset(); - return new AbstractCollection() { + return new AbstractSet() { @Nonnull @Override public Iterator iterator() { @@ -165,12 +167,12 @@ public class DexBackedClassDef extends BaseTypeReference implements ClassDef { }; } } - return ImmutableList.of(); + return ImmutableSet.of(); } @Nonnull @Override - public Collection getMethods() { + public Set getMethods() { int classDataOffset = getClassDataOffset(); if (classDataOffset > 0) { DexReader reader = dexBuf.readerAt(classDataOffset); @@ -185,7 +187,7 @@ public class DexBackedClassDef extends BaseTypeReference implements ClassDef { final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory(); final int methodsStartOffset = reader.getOffset(); - return new AbstractCollection() { + return new AbstractSet() { @Nonnull @Override public Iterator iterator() { @@ -218,7 +220,7 @@ public class DexBackedClassDef extends BaseTypeReference implements ClassDef { }; } } - return ImmutableList.of(); + return ImmutableSet.of(); } private int getClassDataOffset() { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java index 64055255..4b1b66fd 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java @@ -31,11 +31,11 @@ package org.jf.dexlib2.dexbacked; -import org.jf.dexlib2.dexbacked.util.FixedSizeList; +import org.jf.dexlib2.dexbacked.util.FixedSizeSet; import org.jf.dexlib2.iface.DexFile; import javax.annotation.Nonnull; -import java.util.List; +import java.util.Set; public class DexBackedDexFile implements DexFile { @Nonnull public final DexBuffer dexBuf; @@ -46,10 +46,10 @@ public class DexBackedDexFile implements DexFile { @Nonnull @Override - public List getClasses() { + public Set getClasses() { final int classCount = dexBuf.getClassCount(); - return new FixedSizeList() { + return new FixedSizeSet() { @Nonnull @Override public DexBackedClassDef readItem(int index) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedExceptionHandler.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedExceptionHandler.java index a407a196..ed11dbda 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedExceptionHandler.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedExceptionHandler.java @@ -31,11 +31,12 @@ package org.jf.dexlib2.dexbacked; +import org.jf.dexlib2.base.BaseExceptionHandler; import org.jf.dexlib2.iface.ExceptionHandler; import javax.annotation.Nonnull; -public class DexBackedExceptionHandler implements ExceptionHandler { +public class DexBackedExceptionHandler extends BaseExceptionHandler implements ExceptionHandler { @Nonnull private final DexBuffer dexBuf; private final int typeId; private final int handlerCodeAddress; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedField.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedField.java index 3ca56435..d40571b4 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedField.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedField.java @@ -40,7 +40,7 @@ import org.jf.dexlib2.iface.value.EncodedValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.List; +import java.util.Set; public class DexBackedField extends BaseFieldReference implements Field { @Nonnull public final DexBuffer dexBuf; @@ -86,13 +86,13 @@ public class DexBackedField extends BaseFieldReference implements Field { return dexBuf.getType(dexBuf.readUshort(getFieldIdItemOffset() + TYPE_OFFSET)); } - @Nonnull @Override public String getContainingClass() { return classDef.getType(); } + @Nonnull @Override public String getDefiningClass() { return classDef.getType(); } @Override public int getAccessFlags() { return accessFlags; } @Nullable @Override public EncodedValue getInitialValue() { return initialValue; } @Nonnull @Override - public List getAnnotations() { + public Set getAnnotations() { return AnnotationsDirectory.getAnnotations(dexBuf, annotationSetOffset); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java index c6a7d62a..50d4745a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java @@ -32,6 +32,7 @@ package org.jf.dexlib2.dexbacked; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.BaseMethodParameter; import org.jf.dexlib2.base.reference.BaseMethodReference; import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory; @@ -42,8 +43,8 @@ import org.jf.dexlib2.iface.MethodParameter; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; import java.util.List; +import java.util.Set; public class DexBackedMethod extends BaseMethodReference implements Method { @Nonnull public final DexBuffer dexBuf; @@ -101,7 +102,7 @@ public class DexBackedMethod extends BaseMethodReference implements Method { this.parameterAnnotationSetListOffset = paramaterAnnotationIterator.seekTo(methodIndex); } - @Nonnull @Override public String getContainingClass() { return classDef.getType(); } + @Nonnull @Override public String getDefiningClass() { return classDef.getType(); } @Override public int getAccessFlags() { return accessFlags; } @Nonnull @@ -118,7 +119,7 @@ public class DexBackedMethod extends BaseMethodReference implements Method { @Nonnull @Override - public Collection getParameters() { + public List getParameters() { if (getParametersOffset() > 0) { DexBackedMethodImplementation methodImpl = getImplementation(); if (methodImpl != null) { @@ -134,7 +135,7 @@ public class DexBackedMethod extends BaseMethodReference implements Method { final int parametersOffset = getParametersOffset(); if (parametersOffset > 0) { final int size = dexBuf.readSmallUint(parametersOffset); - final List> parameterAnnotations = + final List> parameterAnnotations = AnnotationsDirectory.getParameterAnnotations(dexBuf, parameterAnnotationSetListOffset); return new FixedSizeList() { @@ -151,11 +152,11 @@ public class DexBackedMethod extends BaseMethodReference implements Method { @Nonnull @Override - public List getAnnotations() { + public Set getAnnotations() { if (index < parameterAnnotations.size()) { return parameterAnnotations.get(index); } - return ImmutableList.of(); + return ImmutableSet.of(); } @Nullable @Override public String getName() { return null; } @@ -173,7 +174,7 @@ public class DexBackedMethod extends BaseMethodReference implements Method { @Nonnull @Override - public List getAnnotations() { + public Set getAnnotations() { return AnnotationsDirectory.getAnnotations(dexBuf, methodAnnotationSetOffset); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethodImplementation.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethodImplementation.java index 49c9bff1..d909e4e4 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethodImplementation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethodImplementation.java @@ -44,7 +44,6 @@ import org.jf.dexlib2.iface.instruction.Instruction; import org.jf.util.AlignmentUtils; import javax.annotation.Nonnull; -import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -96,6 +95,7 @@ public class DexBackedMethodImplementation implements MethodImplementation { @Nonnull @Override public List getTryBlocks() { + // TODO: provide utility to put try blocks into a "canonical", easy to use format, which more closely matches java's try blocks final int triesSize = dexBuf.readUshort(codeOffset + TRIES_SIZE_OFFSET); if (triesSize > 0) { int instructionsSize = dexBuf.readSmallUint(codeOffset + INSTRUCTIONS_SIZE_OFFSET); @@ -132,7 +132,7 @@ public class DexBackedMethodImplementation implements MethodImplementation { } @Nonnull - public Collection getParametersWithNames() { + public List getParametersWithNames() { return getDebugInfo().getParametersWithNames(); } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedTryBlock.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedTryBlock.java index 0cbc36f2..cd827602 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedTryBlock.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedTryBlock.java @@ -31,12 +31,12 @@ package org.jf.dexlib2.dexbacked; -import org.jf.dexlib2.dexbacked.util.VariableSizeCollection; +import org.jf.dexlib2.dexbacked.util.VariableSizeSet; import org.jf.dexlib2.iface.ExceptionHandler; import org.jf.dexlib2.iface.TryBlock; import javax.annotation.Nonnull; -import java.util.Collection; +import java.util.Set; public class DexBackedTryBlock implements TryBlock { @Nonnull public final DexBuffer dexBuf; @@ -60,14 +60,14 @@ public class DexBackedTryBlock implements TryBlock { @Nonnull @Override - public Collection getExceptionHandlers() { + public Set getExceptionHandlers() { DexReader reader = dexBuf.readerAt(handlersStartOffset + dexBuf.readUshort(tryItemOffset + HANDLER_OFFSET_OFFSET)); final int encodedSize = reader.readSleb128(); if (encodedSize > 0) { //no catch-all - return new VariableSizeCollection(dexBuf, reader.getOffset(), encodedSize) { + return new VariableSizeSet(dexBuf, reader.getOffset(), encodedSize) { @Nonnull @Override protected DexBackedExceptionHandler readNextItem(@Nonnull DexReader reader, int index) { @@ -77,7 +77,7 @@ public class DexBackedTryBlock implements TryBlock { } else { //with catch-all final int sizeWithCatchAll = (-1 * encodedSize) + 1; - return new VariableSizeCollection(dexBuf, reader.getOffset(), sizeWithCatchAll) { + return new VariableSizeSet(dexBuf, reader.getOffset(), sizeWithCatchAll) { @Nonnull @Override protected ExceptionHandler readNextItem(@Nonnull DexReader dexReader, int index) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedFieldReference.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedFieldReference.java index cff32fc2..3b70ba20 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedFieldReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedFieldReference.java @@ -47,7 +47,7 @@ public class DexBackedFieldReference extends BaseFieldReference { @Nonnull @Override - public String getContainingClass() { + public String getDefiningClass() { return dexBuf.getType(dexBuf.readUshort(fieldIdItemOffset + DexBuffer.FIELD_CLASS_IDX_OFFSET)); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedMethodReference.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedMethodReference.java index cfd42743..6795c0ca 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedMethodReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/reference/DexBackedMethodReference.java @@ -53,7 +53,7 @@ public class DexBackedMethodReference extends BaseMethodReference { @Nonnull @Override - public String getContainingClass() { + public String getDefiningClass() { return dexBuf.getType(dexBuf.readUshort(methodIdItemOffset + DexBuffer.METHOD_CLASS_IDX_OFFSET)); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/AnnotationsDirectory.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/AnnotationsDirectory.java index 11010528..21ee5c4f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/AnnotationsDirectory.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/AnnotationsDirectory.java @@ -32,23 +32,25 @@ package org.jf.dexlib2.dexbacked.util; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.dexbacked.DexBackedAnnotation; import org.jf.dexlib2.dexbacked.DexBuffer; import javax.annotation.Nonnull; import java.util.List; +import java.util.Set; public abstract class AnnotationsDirectory { public static final AnnotationsDirectory EMPTY = new AnnotationsDirectory() { @Override public int getFieldAnnotationCount() { return 0; } - @Nonnull @Override public List getClassAnnotations() {return ImmutableList.of();} + @Nonnull @Override public Set getClassAnnotations() { return ImmutableSet.of(); } @Nonnull @Override public AnnotationIterator getFieldAnnotationIterator() { return AnnotationIterator.EMPTY; } @Nonnull @Override public AnnotationIterator getMethodAnnotationIterator() { return AnnotationIterator.EMPTY; } @Nonnull @Override public AnnotationIterator getParameterAnnotationIterator() {return AnnotationIterator.EMPTY;} }; public abstract int getFieldAnnotationCount(); - @Nonnull public abstract List getClassAnnotations(); + @Nonnull public abstract Set getClassAnnotations(); @Nonnull public abstract AnnotationIterator getFieldAnnotationIterator(); @Nonnull public abstract AnnotationIterator getMethodAnnotationIterator(); @Nonnull public abstract AnnotationIterator getParameterAnnotationIterator(); @@ -74,11 +76,11 @@ public abstract class AnnotationsDirectory { } @Nonnull - public static List getAnnotations(@Nonnull final DexBuffer dexBuf, + public static Set getAnnotations(@Nonnull final DexBuffer dexBuf, final int annotationSetOffset) { if (annotationSetOffset != 0) { final int size = dexBuf.readSmallUint(annotationSetOffset); - return new FixedSizeList() { + return new FixedSizeSet() { @Nonnull @Override public DexBackedAnnotation readItem(int index) { @@ -90,19 +92,19 @@ public abstract class AnnotationsDirectory { }; } - return ImmutableList.of(); + return ImmutableSet.of(); } @Nonnull - public static List> getParameterAnnotations(@Nonnull final DexBuffer dexBuf, + public static List> getParameterAnnotations(@Nonnull final DexBuffer dexBuf, final int annotationSetListOffset) { if (annotationSetListOffset > 0) { final int size = dexBuf.readSmallUint(annotationSetListOffset); - return new FixedSizeList>() { + return new FixedSizeList>() { @Nonnull @Override - public List readItem(int index) { + public Set readItem(int index) { int annotationSetOffset = dexBuf.readSmallUint(annotationSetListOffset + 4 + index * 4); return getAnnotations(dexBuf, annotationSetOffset); } @@ -146,7 +148,7 @@ public abstract class AnnotationsDirectory { } @Nonnull - public List getClassAnnotations() { + public Set getClassAnnotations() { return getAnnotations(dexBuf, dexBuf.readSmallUint(directoryOffset)); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/DebugInfo.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/DebugInfo.java index 8f7de67a..c91bd039 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/DebugInfo.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/DebugInfo.java @@ -48,13 +48,10 @@ import org.jf.dexlib2.immutable.debug.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; +import java.util.*; public abstract class DebugInfo implements Iterable { - @Nonnull public abstract Collection getParametersWithNames(); + @Nonnull public abstract List getParametersWithNames(); public static DebugInfo newOrEmpty(@Nonnull DexBuffer dexBuf, int debugInfoOffset, @Nonnull DexBackedMethodImplementation methodImpl) { @@ -104,8 +101,8 @@ public abstract class DebugInfo implements Iterable { final LocalInfo[] locals = new LocalInfo[registerCount]; Arrays.fill(locals, EMPTY_LOCAL_INFO); - final VariableSizeIterator parameterIterator = - getParametersWithNames().iterator(); + final VariableSizeListIterator parameterIterator = + getParametersWithNames().listIterator(); // first, we grab all the parameters and temporarily store them at the beginning of locals, // disregarding any wide types @@ -234,7 +231,7 @@ public abstract class DebugInfo implements Iterable { @Nonnull @Override - public VariableSizeCollection getParametersWithNames() { + public VariableSizeList getParametersWithNames() { DexReader reader = dexBuf.readerAt(debugInfoOffset); reader.skipUleb128(); final int parameterNameCount = reader.readSmallUleb128(); @@ -242,7 +239,7 @@ public abstract class DebugInfo implements Iterable { methodImpl.method.getParametersWithoutNames(); //TODO: make sure dalvik doesn't allow more parameter names than we have parameters - return new VariableSizeCollection(dexBuf, reader.getOffset(), + return new VariableSizeList(dexBuf, reader.getOffset(), methodParametersWithoutNames.size()) { @Nonnull @Override @@ -258,7 +255,7 @@ public abstract class DebugInfo implements Iterable { @Nonnull @Override public String getType() { return methodParameter.getType(); } @Nullable @Override public String getName() { return name; } @Nullable @Override public String getSignature() { return methodParameter.getSignature();} - @Nonnull @Override public Collection getAnnotations() { + @Nonnull @Override public Set getAnnotations() { return methodParameter.getAnnotations(); } }; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/FixedSizeSet.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/FixedSizeSet.java new file mode 100644 index 00000000..c586c4d7 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/FixedSizeSet.java @@ -0,0 +1,68 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.dexbacked.util; + +import javax.annotation.Nonnull; +import java.util.AbstractSet; +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * This provides a wrapper around AbstractSet to allow easy implementation when backed by a list that can be randomly + * accessed. + */ +public abstract class FixedSizeSet extends AbstractSet { + @Override + public Iterator iterator() { + return new Iterator() { + int index = 0; + + @Override public boolean hasNext() { return index < size(); } + @Override public void remove() { throw new UnsupportedOperationException(); } + @Override + public T next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return readItem(index++); + } + }; + } + + /** + * Reads the item at {@code index} + * @param index The index of the item. This is guaranteed to be in [0, size) + * @return The item at the given index + */ + @Nonnull + public abstract T readItem(int index); +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeList.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeList.java new file mode 100644 index 00000000..03eb56a9 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeList.java @@ -0,0 +1,74 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.dexbacked.util; + +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; + +import javax.annotation.Nonnull; +import java.util.AbstractSequentialList; + +public abstract class VariableSizeList extends AbstractSequentialList { + @Nonnull private final DexBuffer dexBuf; + private final int offset; + private final int size; + + public VariableSizeList(@Nonnull DexBuffer dexBuf, int offset, int size) { + this.dexBuf = dexBuf; + this.offset = offset; + this.size = size; + } + + @Nonnull protected abstract T readNextItem(@Nonnull DexReader reader, int index); + + @Override + public VariableSizeListIterator listIterator() { + return listIterator(0); + } + + @Override public int size() { return size; } + + @Override + public VariableSizeListIterator listIterator(int index) { + VariableSizeListIterator iterator = new VariableSizeListIterator(dexBuf, offset, size) { + @Nonnull + @Override + protected T readNextItem(@Nonnull DexReader reader, int index) { + return VariableSizeList.this.readNextItem(reader, index); + } + }; + while (index++ > 0) { + iterator.next(); + } + return iterator; + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeListIterator.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeListIterator.java new file mode 100644 index 00000000..c1c66bb2 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeListIterator.java @@ -0,0 +1,110 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.dexbacked.util; + +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; + +import javax.annotation.Nonnull; +import java.util.ListIterator; +import java.util.NoSuchElementException; + +public abstract class VariableSizeListIterator implements ListIterator { + @Nonnull private DexReader reader; + protected final int size; + private final int startOffset; + + private int index; + + protected VariableSizeListIterator(@Nonnull DexBuffer dexBuf, int offset, int size) { + this.reader = dexBuf.readerAt(offset); + this.startOffset = offset; + this.size = size; + } + + /** + * Reads the next item from reader. + * + * @param reader The {@code DexReader} to read the next item from + * @param index The index of the item being read. This is guaranteed to be less than {@code size} + * @return The item that was read + */ + @Nonnull protected abstract T readNextItem(@Nonnull DexReader reader, int index); + + public int getReaderOffset() { + return reader.getOffset(); + } + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + @Nonnull + public T next() { + if (index >= size) { + throw new NoSuchElementException(); + } + return readNextItem(reader, index++); + } + + @Override + public boolean hasPrevious() { + return index > 0; + } + + @Override + public T previous() { + int targetIndex = index-1; + reader.setOffset(startOffset); + index = 0; + while (index < targetIndex) { + readNextItem(reader, index++); + } + return readNextItem(reader, index++); + } + + @Override + public int nextIndex() { + return index; + } + + @Override + public int previousIndex() { + return index - 1; + } + + @Override public void remove() { throw new UnsupportedOperationException(); } + @Override public void set(T t) { throw new UnsupportedOperationException(); } + @Override public void add(T t) { throw new UnsupportedOperationException(); } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeSet.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeSet.java new file mode 100644 index 00000000..b7e02852 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/util/VariableSizeSet.java @@ -0,0 +1,65 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.dexlib2.dexbacked.util; + +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; + +import javax.annotation.Nonnull; +import java.util.AbstractSet; + +public abstract class VariableSizeSet extends AbstractSet { + @Nonnull private final DexBuffer dexBuf; + private final int offset; + private final int size; + + public VariableSizeSet(@Nonnull DexBuffer dexBuf, int offset, int size) { + this.dexBuf = dexBuf; + this.offset = offset; + this.size = size; + } + + @Nonnull protected abstract T readNextItem(@Nonnull DexReader reader, int index); + + @Override + public VariableSizeIterator iterator() { + return new VariableSizeIterator(dexBuf, offset, size) { + @Nonnull + @Override + protected T readNextItem(@Nonnull DexReader reader, int index) { + return VariableSizeSet.this.readNextItem(reader, index); + } + }; + } + + @Override public int size() { return size; } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedAnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedAnnotationEncodedValue.java index 4e418d30..03a5e594 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedAnnotationEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedAnnotationEncodedValue.java @@ -35,11 +35,11 @@ import org.jf.dexlib2.base.value.BaseAnnotationEncodedValue; import org.jf.dexlib2.dexbacked.DexBackedAnnotationElement; import org.jf.dexlib2.dexbacked.DexBuffer; import org.jf.dexlib2.dexbacked.DexReader; -import org.jf.dexlib2.dexbacked.util.VariableSizeCollection; +import org.jf.dexlib2.dexbacked.util.VariableSizeSet; import org.jf.dexlib2.iface.value.AnnotationEncodedValue; import javax.annotation.Nonnull; -import java.util.Collection; +import java.util.Set; public class DexBackedAnnotationEncodedValue extends BaseAnnotationEncodedValue implements AnnotationEncodedValue { @Nonnull public final DexBuffer dexBuf; @@ -72,8 +72,8 @@ public class DexBackedAnnotationEncodedValue extends BaseAnnotationEncodedValue @Nonnull @Override - public Collection getElements() { - return new VariableSizeCollection(dexBuf, elementsOffset, elementCount) { + public Set getElements() { + return new VariableSizeSet(dexBuf, elementsOffset, elementCount) { @Nonnull @Override protected DexBackedAnnotationElement readNextItem(@Nonnull DexReader dexReader, int index) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedArrayEncodedValue.java index a27decf1..0b84e1ac 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedArrayEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedArrayEncodedValue.java @@ -34,12 +34,12 @@ package org.jf.dexlib2.dexbacked.value; import org.jf.dexlib2.base.value.BaseArrayEncodedValue; import org.jf.dexlib2.dexbacked.DexBuffer; import org.jf.dexlib2.dexbacked.DexReader; -import org.jf.dexlib2.dexbacked.util.VariableSizeCollection; +import org.jf.dexlib2.dexbacked.util.VariableSizeList; import org.jf.dexlib2.iface.value.ArrayEncodedValue; import org.jf.dexlib2.iface.value.EncodedValue; import javax.annotation.Nonnull; -import java.util.Collection; +import java.util.List; public class DexBackedArrayEncodedValue extends BaseArrayEncodedValue implements ArrayEncodedValue { @Nonnull public final DexBuffer dexBuf; @@ -66,8 +66,8 @@ public class DexBackedArrayEncodedValue extends BaseArrayEncodedValue implements @Nonnull @Override - public Collection getValue() { - return new VariableSizeCollection(dexBuf, encodedArrayOffset, elementCount) { + public List getValue() { + return new VariableSizeList(dexBuf, encodedArrayOffset, elementCount) { @Nonnull @Override protected EncodedValue readNextItem(@Nonnull DexReader dexReader, int index) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEncodedValue.java index fdb10b78..0df2c8dc 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEncodedValue.java @@ -33,8 +33,6 @@ package org.jf.dexlib2.dexbacked.value; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.dexbacked.DexReader; -import org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference; -import org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference; import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.immutable.value.*; import org.jf.dexlib2.util.Preconditions; @@ -78,22 +76,19 @@ public abstract class DexBackedEncodedValue { reader.readSizedRightExtendedLong(valueArg + 1))); case ValueType.STRING: Preconditions.checkValueArg(valueArg, 3); - return new ImmutableStringEncodedValue(reader.getString(reader.readSizedSmallUint(valueArg + 1))); + return new DexBackedStringEncodedValue(reader, valueArg); case ValueType.TYPE: Preconditions.checkValueArg(valueArg, 3); return new ImmutableTypeEncodedValue(reader.getType(reader.readSizedSmallUint(valueArg + 1))); case ValueType.FIELD: Preconditions.checkValueArg(valueArg, 3); - return new ImmutableFieldEncodedValue(new DexBackedFieldReference(reader.getDexBuffer(), - reader.readSizedSmallUint(valueArg + 1))); + return new DexBackedFieldEncodedValue(reader, valueArg); case ValueType.METHOD: Preconditions.checkValueArg(valueArg, 3); - return new ImmutableMethodEncodedValue(new DexBackedMethodReference(reader.getDexBuffer(), - reader.readSizedSmallUint(valueArg + 1))); + return new DexBackedMethodEncodedValue(reader, valueArg); case ValueType.ENUM: Preconditions.checkValueArg(valueArg, 3); - return new ImmutableEnumEncodedValue(new DexBackedFieldReference(reader.getDexBuffer(), - reader.readSizedSmallUint(valueArg + 1))); + return new DexBackedEnumEncodedValue(reader, valueArg); case ValueType.ARRAY: Preconditions.checkValueArg(valueArg, 0); return new DexBackedArrayEncodedValue(reader); @@ -101,6 +96,7 @@ public abstract class DexBackedEncodedValue { Preconditions.checkValueArg(valueArg, 0); return new DexBackedAnnotationEncodedValue(reader); case ValueType.NULL: + Preconditions.checkValueArg(valueArg, 0); return ImmutableNullEncodedValue.INSTANCE; case ValueType.BOOLEAN: Preconditions.checkValueArg(valueArg, 1); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedClassDef.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEnumEncodedValue.java similarity index 67% rename from dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedClassDef.java rename to dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEnumEncodedValue.java index d54a1937..86f3d9aa 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedClassDef.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedEnumEncodedValue.java @@ -29,15 +29,26 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.iface.sorted; +package org.jf.dexlib2.dexbacked.value; -import org.jf.dexlib2.iface.ClassDef; +import org.jf.dexlib2.base.value.BaseEnumEncodedValue; +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; +import org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference; +import org.jf.dexlib2.iface.reference.FieldReference; import javax.annotation.Nonnull; -import java.util.SortedSet; -public interface SortedClassDef extends ClassDef { - @Nonnull SortedSet getAnnotations(); - @Nonnull SortedSet getFields(); - @Nonnull SortedSet getMethods(); +public class DexBackedEnumEncodedValue extends BaseEnumEncodedValue { + @Nonnull public final DexBuffer dexBuf; + private final int fieldIndex; + + public DexBackedEnumEncodedValue(@Nonnull DexReader reader, int valueArg) { + this.dexBuf = reader.getDexBuffer(); + fieldIndex = reader.readSizedSmallUint(valueArg + 1); + } + + @Override public FieldReference getValue() { + return new DexBackedFieldReference(dexBuf, fieldIndex); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedField.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedFieldEncodedValue.java similarity index 67% rename from dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedField.java rename to dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedFieldEncodedValue.java index 8a478088..8d58c5d9 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedField.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedFieldEncodedValue.java @@ -29,16 +29,26 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.iface.sorted; +package org.jf.dexlib2.dexbacked.value; -import org.jf.dexlib2.iface.Field; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import org.jf.dexlib2.base.value.BaseFieldEncodedValue; +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; +import org.jf.dexlib2.dexbacked.reference.DexBackedFieldReference; +import org.jf.dexlib2.iface.reference.FieldReference; import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.SortedSet; -public interface SortedField extends Field { - @Nullable SortedEncodedValue getInitialValue(); - @Nonnull SortedSet getAnnotations(); +public class DexBackedFieldEncodedValue extends BaseFieldEncodedValue { + @Nonnull public final DexBuffer dexBuf; + private final int fieldIndex; + + public DexBackedFieldEncodedValue(@Nonnull DexReader reader, int valueArg) { + this.dexBuf = reader.getDexBuffer(); + fieldIndex = reader.readSizedSmallUint(valueArg + 1); + } + + @Override public FieldReference getValue() { + return new DexBackedFieldReference(dexBuf, fieldIndex); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedMethodEncodedValue.java similarity index 67% rename from dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedArrayEncodedValue.java rename to dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedMethodEncodedValue.java index 260c12f3..1eb13271 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedArrayEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedMethodEncodedValue.java @@ -29,13 +29,26 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.iface.sorted.value; +package org.jf.dexlib2.dexbacked.value; -import org.jf.dexlib2.iface.value.ArrayEncodedValue; +import org.jf.dexlib2.base.value.BaseMethodEncodedValue; +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; +import org.jf.dexlib2.dexbacked.reference.DexBackedMethodReference; +import org.jf.dexlib2.iface.reference.MethodReference; import javax.annotation.Nonnull; -import java.util.Collection; -public interface SortedArrayEncodedValue extends SortedEncodedValue, ArrayEncodedValue{ - @Nonnull Collection getValue(); +public class DexBackedMethodEncodedValue extends BaseMethodEncodedValue { + @Nonnull public final DexBuffer dexBuf; + private final int MethodIndex; + + public DexBackedMethodEncodedValue(@Nonnull DexReader reader, int valueArg) { + this.dexBuf = reader.getDexBuffer(); + MethodIndex = reader.readSizedSmallUint(valueArg + 1); + } + + @Override public MethodReference getValue() { + return new DexBackedMethodReference(dexBuf, MethodIndex); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedAnnotationElement.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedStringEncodedValue.java similarity index 71% rename from dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedAnnotationElement.java rename to dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedStringEncodedValue.java index d1fedd52..6d1458ad 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedAnnotationElement.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedStringEncodedValue.java @@ -29,13 +29,24 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.iface.sorted; +package org.jf.dexlib2.dexbacked.value; -import org.jf.dexlib2.iface.AnnotationElement; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import org.jf.dexlib2.base.value.BaseStringEncodedValue; +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; import javax.annotation.Nonnull; -public interface SortedAnnotationElement extends AnnotationElement { - @Nonnull SortedEncodedValue getValue(); +public class DexBackedStringEncodedValue extends BaseStringEncodedValue { + @Nonnull public final DexBuffer dexBuf; + private final int stringIndex; + + public DexBackedStringEncodedValue(@Nonnull DexReader reader, int valueArg) { + this.dexBuf = reader.getDexBuffer(); + stringIndex = reader.readSizedSmallUint(valueArg + 1); + } + + @Override public String getValue() { + return dexBuf.getString(stringIndex); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedMethodParameter.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedTypeEncodedValue.java similarity index 72% rename from dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedMethodParameter.java rename to dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedTypeEncodedValue.java index e4de29c0..a100174d 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedMethodParameter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/value/DexBackedTypeEncodedValue.java @@ -29,13 +29,24 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.iface.sorted; +package org.jf.dexlib2.dexbacked.value; -import org.jf.dexlib2.iface.MethodParameter; +import org.jf.dexlib2.base.value.BaseTypeEncodedValue; +import org.jf.dexlib2.dexbacked.DexBuffer; +import org.jf.dexlib2.dexbacked.DexReader; import javax.annotation.Nonnull; -import java.util.SortedSet; -public interface SortedMethodParameter extends MethodParameter { - @Nonnull SortedSet getAnnotations(); +public class DexBackedTypeEncodedValue extends BaseTypeEncodedValue { + @Nonnull public final DexBuffer dexBuf; + private final int typeIndex; + + public DexBackedTypeEncodedValue(@Nonnull DexReader reader, int valueArg) { + this.dexBuf = reader.getDexBuffer(); + typeIndex = reader.readSizedSmallUint(valueArg + 1); + } + + @Override public String getValue() { + return dexBuf.getType(typeIndex); + } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/Annotation.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/Annotation.java index 88183899..8e29c5dc 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/Annotation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/Annotation.java @@ -32,16 +32,78 @@ package org.jf.dexlib2.iface; import javax.annotation.Nonnull; -import java.util.Collection; -import java.util.List; - -public interface Annotation { - // The possible annotation visibility values - public static final int BUILD = 0; - public static final int RUNTIME = 1; - public static final int SYSTEM = 2; +import javax.annotation.Nullable; +import java.util.Set; +/** + * This class represents a specific instance of an annotation applied to a class/field/method/parameter + */ +public interface Annotation extends Comparable { + /** + * Gets the visibility of this annotation. + * + * This will be one of the AnnotationVisibility.* constants. + * + * @return The visibility of this annotation + */ int getVisibility(); + + /** + * Gets the type of this annotation. + * + * This will be the type descriptor of the class that defines this annotation. + * + * @return The type of this annotation + */ @Nonnull String getType(); - @Nonnull Collection getElements(); + + /** + * Gets a set of the name/value elements associated with this annotation. + * + * The elements in the returned set will be unique with respect to the element name. + * + * @return A set of AnnotationElements + */ + @Nonnull Set getElements(); + + /** + * Returns a hashcode for this Annotation. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getVisibility();
+     * hashCode = hashCode*31 + getType().hashCode();
+     * hashCode = hashCode*31 + getElements().hashCode();
+     * }
+ * + * @return The hash code value for this Annotation + */ + @Override int hashCode(); + + /** + * Compares this Annotation to another Annotation for equality. + * + * This Annotation is equal to another Annotation if all of it's "fields" are equal. That is, if the return values + * of getVisibility(), getType(), and getElements() are all equal. + * + * TODO: when an Annotation is used in a set, need to specify that the uniqueness in the set is based on annotation type + * + * @param o The object to be compared for equality with this Annotation + * @return true if the specified object is equal to this Annotation + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compares this Annotation to another Annotation. + * + * The comparison is based on the value of getVisibility(), getType() and getElements(), in that order. When + * comparing the set of elements, the comparison is done with the semantics of + * org.jf.util.CollectionUtils.compareAsSet(), using the natural ordering of AnnotationElement. + * + * @param o The Annotation to compare with this Annotation + * @return An integer representing the result of the comparison + */ + @Override int compareTo(Annotation o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/AnnotationElement.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/AnnotationElement.java index 3e53dae6..0ac61cc3 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/AnnotationElement.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/AnnotationElement.java @@ -34,8 +34,59 @@ package org.jf.dexlib2.iface; import org.jf.dexlib2.iface.value.EncodedValue; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface AnnotationElement { +/** + * This class represents an individual name/value element in an annotation + */ +public interface AnnotationElement extends Comparable { + /** + * Gets the name of the element. + * + * @return The name of the element. + */ @Nonnull String getName(); + + /** + * Gets the value of the element. + * + * @return The value of the element + */ @Nonnull EncodedValue getValue(); + + /** + * Returns a hashcode for this AnnotationElement. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getName().hashCode();
+     * hashCode = hashCode*31 + getValue().hashCode();
+     * }
+ * + * @return The hash code value for this AnnotationElement + */ + @Override int hashCode(); + + /** + * Compares this AnnotationElement to another AnnotationElement for equality. + * + * This AnnotationElement is equal to another AnnotationElement if all of it's "fields" are equal. That is, if + * the return values of getName() and getValue() are both equal. + * + * @param o The object to be compared for equality with this AnnotationElement + * @return true if the specified object is equal to this AnnotationElement + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compares this AnnotationElement to another AnnotationElement. + * + * The comparison is based on the value of getName() and getValue(), in that order. + * + * @param o The AnnotationElement to compare with this AnnotationElement + * @return An integer representing the result of the comparison + */ + @Override int compareTo(AnnotationElement o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/ClassDef.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/ClassDef.java index 1ab1ede2..9f40f771 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/ClassDef.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/ClassDef.java @@ -35,16 +35,83 @@ import org.jf.dexlib2.iface.reference.TypeReference; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; -import java.util.List; +import java.util.Set; +/** + * This class represents a class definition. + * + * It also acts as a TypeReference to itself. Any equality/comparison is based on its identity as a TypeReference, + * and shouldn't take into account anything other than the type of this class. + */ public interface ClassDef extends TypeReference { - @Nonnull String getType(); + /** + * Gets the class type. + * + * This will be a type descriptor per the dex file specification. + * + * @return The class type + */ + @Override @Nonnull String getType(); + + /** + * Gets the access flags for this class. + * + * This will be a combination of the AccessFlags.* flags that are marked as compatible for use with a class. + * + * @return The access flags for this class + */ int getAccessFlags(); + + /** + * Gets the superclass of this class. + * + * This will only be null if this is the base java.lang.Object class. + * + * @return The superclass of this class + */ @Nullable String getSuperclass(); - @Nonnull List getInterfaces(); + + /** + * Gets a set of the interfaces that this class implements. + * + * @return A set of the interfaces that this class implements + */ + @Nonnull Set getInterfaces(); + + /** + * Gets the name of the primary source file that this class is defined in, if available. + * + * This will be the default source file associated with all methods defined in this class. This can be overridden + * for sections of an individual method with the SetSourceFile debug item. + * + * @return The name of the primary source file for this class, or null if not available + */ @Nullable String getSourceFile(); - @Nonnull Collection getAnnotations(); - @Nonnull Collection getFields(); - @Nonnull Collection getMethods(); + + /** + * Gets a set of the annotations that are applied to this class. + * + * The annotations in the returned set are guaranteed to have unique types. + * + * @return A set of the annotations that are applied to this class + */ + @Nonnull Set getAnnotations(); + + /** + * Gets a set of the fields that are defined by this class. + * + * TODO: uniqueness? + * + * @return A set of the fields that are defined by this class + */ + @Nonnull Set getFields(); + + /** + * Gets a set of the methods that are defined by this class. + * + * TODO: uniqueness? + * + * @return A set of the methods that are defined by this class. + */ + @Nonnull Set getMethods(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/DexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/DexFile.java index fdcbb823..45707f03 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/DexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/DexFile.java @@ -32,8 +32,18 @@ package org.jf.dexlib2.iface; import javax.annotation.Nonnull; -import java.util.List; +import java.util.Set; +/** + * This class is a high level representation of a dex file - essentially a set of class definitions. + */ public interface DexFile { - @Nonnull List getClasses(); + /** + * Get a set of the classes defined in this dex file. + * + * The classes in the returned set will all have unique types. + * + * @return A set of the classes defined in this dex file + */ + @Nonnull Set getClasses(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/ExceptionHandler.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/ExceptionHandler.java index f299694b..07defc78 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/ExceptionHandler.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/ExceptionHandler.java @@ -31,9 +31,63 @@ package org.jf.dexlib2.iface; +import javax.annotation.Nonnull; import javax.annotation.Nullable; -public interface ExceptionHandler { +/** + * This class represents an individual exception handler entry, in a try block. + */ +public interface ExceptionHandler extends Comparable { + /** + * Gets the type of exception that is handled by this handler. + * + * @return The type of exception that is handled by this handler, or null if this is a catch-all handler. + */ @Nullable String getExceptionType(); + + /** + * Gets the code offset of the handler. + * + * @return The offset of the handler from the the beginning of the bytecode for the method. The offset will be in + * terms of 16-bit code units + */ int getHandlerCodeAddress(); + + /** + * Returns a hashcode for this ExceptionHandler. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * String exceptionType = getExceptionType();
+     * int hashCode = exceptionType==null?0:exceptionType.hashCode();
+     * return hashCode*31 + getHandlerCodeAddress();
+     * }
+ * + * @return The hash code value for this ExceptionHandler + */ + @Override int hashCode(); + + /** + * Compares this ExceptionHandler to another ExceptionHandler for equality. + * + * This ExceptionHandler is equal to another ExceptionHandler if all of it's "fields" are equal. That is, if + * the return values of getExceptionType() and getHandlerCodeAddress() are both equal. + * + * @param o The object to be compared for equality with this ExceptionHandler + * @return true if the specified object is equal to this ExceptionHandler + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this ExceptionHandler to another ExceptionHandler. + * + * The comparison is based on the comparison of the return values of getExceptionType() and + * getHandlerCodeAddress() in that order. A null value for getExceptionType() compares after a non-null value. + * + * @param o The ExceptionHandler to compare with this ExceptionHandler + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull ExceptionHandler o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/Field.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/Field.java index 9a5c13b9..7b4aad38 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/Field.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/Field.java @@ -36,13 +36,61 @@ import org.jf.dexlib2.iface.value.EncodedValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; +import java.util.Set; +/** + * This class represents a specific field definition in a class. + * + * It also acts as a FieldReference to itself. Any equality/comparison is based on its identity as a FieldReference, + * and shouldn't take into account any non-FieldReference specifics of this field. + */ public interface Field extends FieldReference { - @Nonnull String getContainingClass(); + /** + * Gets the type of the class that defines this field. + * + * @return The type of the class that defines this field + */ + @Nonnull String getDefiningClass(); + + /** + * Gets the name of this field. + * + * @return The name of this field + */ @Nonnull String getName(); + + /** + * Gets the type of this field. + * + * @return The type of this field + */ @Nonnull String getType(); + + /** + * Gets the access flags for this field. + * + * This will be a combination of the AccessFlags.* flags that are marked as compatible for use with a field. + * + * @return The access flags for this field + */ int getAccessFlags(); + + /** + * Gets the initial value for this field, if available. + * + * Only static field may have an initial value set, but are not required to have an initial value. + * + * @return The initial value for this field, or null if this field is not a static field, or if this static field + * does not have an initial value. + */ @Nullable EncodedValue getInitialValue(); - @Nonnull Collection getAnnotations(); + + /** + * Gets a set of the annotations that are applied to this field. + * + * The annotations in the returned set are guaranteed to have unique types. + * + * @return A set of the annotations that are applied to this field + */ + @Nonnull Set getAnnotations(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/Method.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/Method.java index fb958d3a..3f9c502c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/Method.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/Method.java @@ -35,14 +35,76 @@ import org.jf.dexlib2.iface.reference.MethodReference; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; +import java.util.List; +import java.util.Set; +/** + * This class represents a specific method definition in a class. + * + * It also acts as a MethodReference to itself. Any equality/comparison is based on its identity as a MethodReference, + * and shouldn't take into account any non-MethodReference specifics of this method. + */ public interface Method extends MethodReference { - @Nonnull String getContainingClass(); - @Nonnull String getName(); - @Nonnull Collection getParameters(); - @Nonnull String getReturnType(); + /** + * Gets the type of the class that defines this method. + * + * @return The type of the class that defines this method + */ + @Override @Nonnull String getDefiningClass(); + + /** + * Gets the name of this method. + * + * @return The name of this method + */ + @Override @Nonnull String getName(); + + /** + * Gets a list of the types of the parameters of this method. + * + * As per the MethodReference interface, the MethodParameter objects contained in the returned list also act + * as a simple reference to the type of the parameter. However, the MethodParameter object can also contain + * additional information about the parameter. + * + * Note: In some implementations, the returned list is likely to *not* provide efficient random access. + * + * @return A list of MethodParameter objects, representing the parameters of this method. + */ + @Override @Nonnull List getParameters(); + + /** + * Gets the return type of this method. + * + * @return The return type of this method. + */ + @Override @Nonnull String getReturnType(); + + /** + * Gets the access flags for this method. + * + * This will be a combination of the AccessFlags.* flags that are marked as compatible for use with a method. + * + * @return The access flags for this method + */ int getAccessFlags(); - @Nonnull Collection getAnnotations(); + + /** + * Gets a set of the annotations that are applied to this method. + * + * The annotations in the returned set are guaranteed to have unique types. + * + * @return A set of the annotations that are applied to this method + */ + @Nonnull Set getAnnotations(); + + /** + * Gets a MethodImplementation object that defines the implementation of the method. + * + * If this is an abstract method in an abstract class, or an interface method in an interface definition, then the + * method has no implementation, and this will return null. + * + * @return A MethodImplementation object defining the implementation of this method, or null if the method has no + * implementation + */ @Nullable MethodImplementation getImplementation(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodImplementation.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodImplementation.java index 2953cfe9..e559d56c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodImplementation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodImplementation.java @@ -37,9 +37,48 @@ import org.jf.dexlib2.iface.instruction.Instruction; import javax.annotation.Nonnull; import java.util.List; +/** + * This class represents the implementation details of a method. + */ public interface MethodImplementation { + /** + * Gets the number of registers in this method. + * + * @return The number of register in this method. + */ int getRegisterCount(); + + /** + * Gets the instructions in this method. + * + * @return An Iterable of the instructions in this method + */ @Nonnull Iterable getInstructions(); + + /** + * Gets a list of the try blocks defined for this method. + * + * Try blocks may overlap freely, and do not need to be strictly nested, as in java. This is a more relaxed + * requirement than specified by the dex format, where try blocks may not overlap, and must be specified in + * ascending order. When writing to a dex file, the try blocks will be massaged into the appropriate format. + * + * In any region where there are overlapping try blocks, set of exception handlers for the overlapping region will + * consist of the union of all handlers in any try block that covers that region. + * + * If multiple overlapping try blocks define a handler for the same exception type, or define a catch-all + * handler, then those duplicate handlers must use the same handler offset. + * + * @return A list of the TryBlock items + */ @Nonnull List getTryBlocks(); + + /** + * Get a list of debug items for this method. + * + * This generally matches the semantics of the debug_info_item in the dex specification, although in an easier to + * digest form. + * + * @return A list of DebugInfo items + */ @Nonnull Iterable getDebugItems(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodParameter.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodParameter.java index 36114f89..d982fe2d 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodParameter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/MethodParameter.java @@ -36,11 +36,50 @@ import org.jf.dexlib2.iface.reference.TypeReference; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; +import java.util.Set; +/** + * This class represents a method parameter. + * + * It also acts as a TypeReference to the type of this parameter. Any equality/comparison is based on its identity as a + * TypeReference, and should not take into account any details other than the parameter type. + * + * It also acts as a LocalInfo object, and conceptually defines the debug information for any parameter register at the + * beginning of the method. + */ public interface MethodParameter extends TypeReference, LocalInfo { + /** + * The type of this method parameter. + * + * This may be any type, including primitive or array types, other than the void (V) type. + * + * @return The type of this method parameter + */ @Nonnull String getType(); - @Nonnull Collection getAnnotations(); + + /** + * Gets a set of the annotations that are applied to this parameter. + * + * The annotations in the returned set are guaranteed to have unique types. + * + * @return A set of the annotations that are applied to this parameter + */ + @Nonnull Set getAnnotations(); + + /** + * Gets the name of this parameter, if available. + * + * @return The name of this parameter, or null if the name is not available. + */ @Nullable String getName(); + + /** + * Gets the signature of this parameter, if available. + * + * The signature of a parameter is defined to be the concatenated version of the dalvik.annotation.Signature + * annotation applied to this parameter, or null if there is no dalvik.annotation.Signature annotation. + * + * @return The signature of this parameter, or null if not available + */ @Nullable String getSignature(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/TryBlock.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/TryBlock.java index 057bce34..72c3704e 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/TryBlock.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/TryBlock.java @@ -32,10 +32,39 @@ package org.jf.dexlib2.iface; import javax.annotation.Nonnull; -import java.util.Collection; +import java.util.Set; +/** + * This class represents an individual try block and associated set of handlers. + */ public interface TryBlock { + /** + * Gets the code offset of the start of this try block. + * + * The starting location must not occur in the middle of an instruction. + * + * @return The offset of the start of the try block from the the beginning of the bytecode for the method. The + * offset will be in terms of 16-bit code units. + */ int getStartCodeAddress(); + + /** + * Gets the number of code units covered by this try block. + * + * The end of the try block is typically coincident with the end of an instruction, but does not strictly need to + * be. If the last instruction is only partly covered by this try block, it is considered to be covered. + * + * @return The number of code units covered by this try block. + */ int getCodeUnitCount(); - @Nonnull Collection getExceptionHandlers(); + + /** + * A set of the exception handlers associated with this try block. + * + * The exception handlers in the returned set will all have a unique type, including at most 1 with no type, which + * is the catch-all handler. + * + * @return A set of ExceptionHandler objects + */ + @Nonnull Set getExceptionHandlers(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/debug/DebugItem.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/debug/DebugItem.java index fbe9e747..ea3f5ffb 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/debug/DebugItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/debug/DebugItem.java @@ -31,7 +31,25 @@ package org.jf.dexlib2.iface.debug; +/** + * This class represents a generic debug item. + * + * The specific sub-types of this class correspond to the various debug opcodes specified in the debug_info_item type + * in the dex file specification. + */ public interface DebugItem { + /** + * The type of this debug item. + * + * The returned integer will be one of the DebugItemType.* constants. + * + * @return The type of this debug item. + */ int getDebugItemType(); + + /** + * The code address + * @return + */ int getCodeAddress(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/instruction/Instruction.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/instruction/Instruction.java index c06b073c..db85e61a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/instruction/Instruction.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/instruction/Instruction.java @@ -33,7 +33,26 @@ package org.jf.dexlib2.iface.instruction; import org.jf.dexlib2.Opcode; +/** + * This class represents a generic instruction. + * + * There are two categories of sub-interfaces of this interface. The dexlib2.iface.instruction.* interfaces are set of + * generic categories of instructions, while the dexlib2.iface.instruction.formats.* interfaces each represent a + * specific instruction format, and are typically built up as a composite of generic instruction interfaces. + */ public interface Instruction { + /** + * Gets the opcode of this instruction. + * + * @return The Opcode of this instruction. + */ Opcode getOpcode(); + + /** + * Gets the size of this instruction. + * + * @return The size of this instruction, as a count of the number of 16-bit code units that make up this + * instruction. + */ int getCodeUnits(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/FieldReference.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/FieldReference.java index 4966922a..8d1a1043 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/FieldReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/FieldReference.java @@ -32,9 +32,68 @@ package org.jf.dexlib2.iface.reference; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface FieldReference extends Reference { - @Nonnull String getContainingClass(); +/** + * This class represents a reference to a field. + */ +public interface FieldReference extends Reference, Comparable { + /** + * Gets the type of the class that defines the referenced field. + * + * @return The type of the class that defines the referenced field + */ + @Nonnull String getDefiningClass(); + + /** + * Gets the name of the referenced field. + * + * @return The name of the referenced field + */ @Nonnull String getName(); + + /** + * Gets the type of the referenced field. + * + * @return The type of the referenced field + */ @Nonnull String getType(); + + /** + * Returns a hashcode for this FieldReference. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode =  getDefiningClass().hashCode();
+     * hashCode = hashCode*31 + getName().hashCode();
+     * hashCode = hashCode*31 + getType().hashCode();
+     * }
+ * + * @return The hash code value for this FieldReference + */ + @Override int hashCode(); + + /** + * Compares this FieldReference to another FieldReference for equality. + * + * This FieldReference is equal to another FieldReference if all of it's "fields" are equal. That is, if + * the return values of getDefiningClass(), getName() and getType() are all equal. + * + * @param o The object to be compared for equality with this FieldReference + * @return true if the specified object is equal to this FieldReference + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this FieldReference to another FieldReference. + * + * The comparison is based on the comparison of the return values of getDefiningClass(), getName() and + * getType(), in that order. + * + * @param o The FieldReference to compare with this FieldReference + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull FieldReference o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/MethodReference.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/MethodReference.java index 9f85a3df..c2987c48 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/MethodReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/MethodReference.java @@ -32,11 +32,80 @@ package org.jf.dexlib2.iface.reference; import javax.annotation.Nonnull; -import java.util.Collection; +import javax.annotation.Nullable; +import java.util.List; -public interface MethodReference extends Reference { - @Nonnull String getContainingClass(); +/** + * This class represents a reference to a method. + */ +public interface MethodReference extends Reference, Comparable { + /** + * Gets the type of the class that defines the referenced method. + * + * @return The type of the class that defines the referenced method + */ + @Nonnull String getDefiningClass(); + + /** + * Gets the name of the referenced method. + * + * @return The name of the referenced method + */ @Nonnull String getName(); - @Nonnull Collection getParameters(); + + /** + * Gets a list of the types of the parameters of the referenced method. + * + * Note: In some implementations, the returned list is likely to *not* provide efficient random access. + * + * @return A list of the types of the parameters of the referenced method + */ + @Nonnull List getParameters(); + + /** + * Gets the return type of the referenced method. + * + * @return The return type of the referenced method. + */ @Nonnull String getReturnType(); + + /** + * Returns a hashcode for this MethodReference. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode =  getDefiningClass().hashCode();
+     * hashCode = hashCode*31 + getName().hashCode();
+     * hashCode = hashCode*31 + getReturnType().hashCode();
+     * hashCode = hashCode*31 + getParameters().hashCode();
+     * }
+ * + * @return The hash code value for this MethodReference + */ + @Override int hashCode(); + + /** + * Compares this MethodReference to another MethodReference for equality. + * + * This MethodReference is equal to another MethodReference if all of it's "fields" are equal. That is, if + * the return values of getDefiningClass(), getName(), getReturnType() and getParameters() are all equal. + * + * @param o The object to be compared for equality with this MethodReference + * @return true if the specified object is equal to this MethodReference + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this MethodReference to another MethodReference. + * + * The comparison is based on the comparison of the return values of getDefiningClass(), getName(), + * getReturnType() and getParameters(), in that order. getParameters() should be compared using the semantics + * of org.jf.util.CollectionUtils.compareAsList() + * + * @param o The MethodReference to compare with this MethodReference + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull MethodReference o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/Reference.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/Reference.java index be0c5ba4..1fa93a03 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/Reference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/Reference.java @@ -31,5 +31,9 @@ package org.jf.dexlib2.iface.reference; +/** + * This class is the base interface for field/method/string/type references in a dex file. It has no functionality or + * contract itself. + */ public interface Reference { } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/StringReference.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/StringReference.java index 97fcf2e2..5b13ef15 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/StringReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/StringReference.java @@ -32,7 +32,55 @@ package org.jf.dexlib2.iface.reference; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface StringReference extends Reference, CharSequence { +/** + * This class represents a reference to an arbitrary string. + * + * When possible, elsewhere in the interface, a string is represented directly by its value. A StringReference is only + * used in those cases where a generic Reference is needed + * + * This type also acts as a CharSequence wrapper around the referenced string value. As per the CharSequence contract, + * calling toString() on a StringReference yields the referenced string value. This is the same value returned by + * getString(). + */ +public interface StringReference extends Reference, CharSequence, Comparable { + /** + * Gets the referenced string. + * + * @return the referenced string + */ @Nonnull String getString(); + + /** + * Returns a hashcode for this StringReference. + * + * This is defined to be getString().hashCode(). + * + * @return The hash code value for this StringReference + */ + @Override int hashCode(); + + /** + * Compares this StringReference to another CharSequence for equality. + * + * String StringReference is equal to a CharSequence iff this.getString().equals(other.toString()). + * + * Equivalently, This StringReference is equal to another StringReference iff + * this.getString().equals(other.getString()). + * + * @param o The object to be compared for equality with this TypeReference + * @return true if the specified object is equal to this TypeReference + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this StringReference to another StringReference, or more generally to another CharSequence. + * + * The comparison is defined to be this.getString().compareTo(other.toString()). + * + * @param o The CharSequence to compare with this StringReference + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull CharSequence o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/TypeReference.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/TypeReference.java index 649377a3..4c2249e4 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/TypeReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/reference/TypeReference.java @@ -32,7 +32,59 @@ package org.jf.dexlib2.iface.reference; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface TypeReference extends Reference, CharSequence { +/** + * This class represents a reference to a type. + * + * When possible, elsewhere in the interface, a type is referenced directly as a String. A TypeReference is only used + * in those cases where a generic Reference is needed + * + * The type being referenced is represented as a String in the format of a TypeDescriptor, as defined by the dex file + * specification. + * + * This type also acts as a CharSequence wrapper around the TypeDescriptor string. As per the CharSequence contract, + * calling toString() on a TypeReference yields the type descriptor as a String. This is the same value returned by + * getType() + */ +public interface TypeReference extends Reference, CharSequence, Comparable { + /** + * Gets the string representation of the referenced type. + * + * The returned string will be a TypeDescriptor, as defined in the dex file specification + * + * @return The string representation of the referenced type. + */ @Nonnull String getType(); + + /** + * Returns a hashcode for this TypeReference. + * + * This is defined to be getType().hashCode() + * + * @return The hash code value for this TypeReference + */ + @Override int hashCode(); + + /** + * Compares this TypeReference to another TypeReference, or more generally to another CharSequence for equality. + * + * This TypeReference is equal to a CharSequence iff this.getType().equals(other.getString()). + * + * Equivalently, This TypeReference is equal to another TypeReference iff this.getType().equals(other.getType()). + * + * @param o The object to be compared for equality with this TypeReference + * @return true if the specified object is equal to this TypeReference + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this TypeReference to another TypeReference, or more generally to another CharSequence. + * + * The comparison is defined to be this.getType().compareTo(other.toString()) + * + * @param o The CharSequence to compare with this TypeReference + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull CharSequence o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedAnnotation.java deleted file mode 100644 index bf6184a4..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedAnnotation.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.iface.sorted; - -import org.jf.dexlib2.iface.Annotation; - -import javax.annotation.Nonnull; -import java.util.SortedSet; - -public interface SortedAnnotation extends Annotation { - @Nonnull SortedSet getElements(); -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedMethod.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedMethod.java deleted file mode 100644 index 76bd3539..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/SortedMethod.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.iface.sorted; - -import org.jf.dexlib2.iface.Method; - -import javax.annotation.Nonnull; -import java.util.Collection; -import java.util.SortedSet; - -public interface SortedMethod extends Method { - @Nonnull Collection getParameters(); - @Nonnull SortedSet getAnnotations(); -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedAnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedAnnotationEncodedValue.java deleted file mode 100644 index efc33fa5..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedAnnotationEncodedValue.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.iface.sorted.value; - -import org.jf.dexlib2.iface.sorted.SortedAnnotationElement; -import org.jf.dexlib2.iface.value.AnnotationEncodedValue; - -import javax.annotation.Nonnull; -import java.util.SortedSet; - -public interface SortedAnnotationEncodedValue extends SortedEncodedValue, AnnotationEncodedValue { - @Nonnull SortedSet getElements(); -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedEncodedValue.java deleted file mode 100644 index 31ec7bd8..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/sorted/value/SortedEncodedValue.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.iface.sorted.value; - -import org.jf.dexlib2.iface.value.EncodedValue; - -public interface SortedEncodedValue extends EncodedValue { -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/AnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/AnnotationEncodedValue.java index 9721f27a..bda7583e 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/AnnotationEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/AnnotationEncodedValue.java @@ -34,10 +34,66 @@ package org.jf.dexlib2.iface.value; import org.jf.dexlib2.iface.AnnotationElement; import javax.annotation.Nonnull; -import java.util.Collection; -import java.util.List; +import javax.annotation.Nullable; +import java.util.Set; +/** + * This class represents an encoded annotation value. + */ public interface AnnotationEncodedValue extends EncodedValue { + /** + * Gets the type of this annotation. + * + * This will be the type descriptor of the class that defines this annotation. + * + * @return The type of this annotation + */ @Nonnull String getType(); - @Nonnull Collection getElements(); + + /** + * Gets a set of the name/value elements associated with this annotation. + * + * The elements in the returned set will be unique by name. + * + * @return A set of AnnotationElements + */ + @Nonnull Set getElements(); + + /** + * Returns a hashcode for this AnnotationEncodedValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getType().hashCode();
+     * hashCode = hashCode*31 + getElements().hashCode();
+     * }
+ * + * @return The hash code value for this AnnotationEncodedValue + */ + @Override int hashCode(); + + /** + * Compares this AnnotationEncodedValue to another AnnotationEncodedValue for equality. + * + * This AnnotationEncodedValue is equal to another AnnotationEncodedValue if all of it's "fields" are equal. That + * is, if the return values getType() and getElements() are both equal. + * + * @param o The object to be compared for equality with this AnnotationEncodedValue + * @return true if the specified object is equal to this AnnotationEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this AnnotationEncodedValue to another EncodedValue. + * + * The comparison is based on the value of getType() and getElements(), in that order. When + * comparing the set of elements, the comparison is done with the semantics of + * org.jf.util.CollectionUtils.compareAsSet(), using the natural ordering of AnnotationElement. + * + * @param o The EncodedValue to compare with this AnnotationEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ArrayEncodedValue.java index 6dcb590a..f0e2d906 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ArrayEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ArrayEncodedValue.java @@ -32,9 +32,53 @@ package org.jf.dexlib2.iface.value; import javax.annotation.Nonnull; -import java.util.Collection; +import javax.annotation.Nullable; import java.util.List; +/** + * This class represents an encoded array value. + */ public interface ArrayEncodedValue extends EncodedValue { - @Nonnull Collection getValue(); + /** + * Gets the list of the values contained in this ArrayEncodedValue + * + * @return A list of EncodedValue instances + */ + @Nonnull List getValue(); + + /** + * Returns a hashcode for this EncodedArrayValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue().hashCode();
+     * }
+ * + * @return The hash code value for this EncodedArrayValue + */ + @Override int hashCode(); + + /** + * Compares this ArrayEncodedValue to another ArrayEncodedValue for equality. + * + * This ArrayEncodedValue is equal to another ArrayEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this ArrayEncodedValue + * @return true if the specified object is equal to this ArrayEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this ArrayEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * ArrayEncodedValue, the lists returned by getValue() are compared, based on the semantics of + * org.jf.util.ComparisonUtils.compareAsList(), using the natural ordering for EncodedValue. + * + * @param o The EncodedValue to compare with this ArrayEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/BooleanEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/BooleanEncodedValue.java index 21bd260f..c8229559 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/BooleanEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/BooleanEncodedValue.java @@ -31,8 +31,53 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface BooleanEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded boolean value. + */ +public interface BooleanEncodedValue extends EncodedValue { + /** + * Gets the boolean value. + * + * @return the boolean value + */ boolean getValue(); + + /** + * Returns a hashcode for this EncodedBooleanValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue()?1:0;
+     * }
+ * + * @return The hash code value for this EncodedBooleanValue + */ + @Override int hashCode(); + + /** + * Compares this BooleanEncodedValue to another BooleanEncodedValue for equality. + * + * This BooleanEncodedValue is equal to another BooleanEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this BooleanEncodedValue + * @return true if the specified object is equal to this BooleanEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this BooleanEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * BooleanEncodedValue, the return values of getValue() are compared, based on the semantics of + * Boolean.compareTo(). + * + * @param o The EncodedValue to compare with this BooleanEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ByteEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ByteEncodedValue.java index 761ece58..27360f61 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ByteEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ByteEncodedValue.java @@ -31,8 +31,52 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface ByteEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded byte value. + */ +public interface ByteEncodedValue extends EncodedValue { + /** + * Gets the byte value. + * + * @return the byte value + */ byte getValue(); + + /** + * Returns a hashcode for this EncodedByteValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue();
+     * }
+ * + * @return The hash code value for this EncodedByteValue + */ + @Override int hashCode(); + + /** + * Compares this ByteEncodedValue to another ByteEncodedValue for equality. + * + * This ByteEncodedValue is equal to another ByteEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this ByteEncodedValue + * @return true if the specified object is equal to this ByteEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this ByteEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * ByteEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this ByteEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/CharEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/CharEncodedValue.java index 5e70b1c2..d208e343 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/CharEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/CharEncodedValue.java @@ -31,8 +31,52 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface CharEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded char value. + */ +public interface CharEncodedValue extends EncodedValue { + /** + * Gets the char value. + * + * @return the char value + */ char getValue(); + + /** + * Returns a hashcode for this EncodedCharValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue();
+     * }
+ * + * @return The hash code value for this EncodedCharValue + */ + @Override int hashCode(); + + /** + * Compares this CharEncodedValue to another CharEncodedValue for equality. + * + * This CharEncodedValue is equal to another CharEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this CharEncodedValue + * @return true if the specified object is equal to this CharEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this CharEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * CharEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this CharEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/DoubleEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/DoubleEncodedValue.java index ef7dbe56..c60fe5d0 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/DoubleEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/DoubleEncodedValue.java @@ -31,8 +31,59 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface DoubleEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded double value. + */ +public interface DoubleEncodedValue extends EncodedValue { + /** + * Gets the double value. + * + * @return the double value + */ double getValue(); + + /** + * Returns a hashcode for this EncodedDoubleValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * long v = Double.doubleToRawLongBits(getValue());
+     * int hashCode = (int)(v^(v>>>32));
+     * }
+ * + * Note: This is slightly different than the definition of Double.hashCode(). This uses doubleToRawLongBits() + * instead of doubleToLongBits(), in order to preserve as much information as possible. + * + * @return The hash code value for this EncodedDoubleValue + */ + @Override int hashCode(); + + /** + * Compares this DoubleEncodedValue to another DoubleEncodedValue for equality. + * + * This DoubleEncodedValue is equal to another DoubleEncodedValue if the values returned by + * getValue().doubleToRawLongBits() are equal. + * + * Note: this isn't quite the same as getValue() == getValue(), due to various NaN issues and signed zero issues. + * + * @param o The object to be compared for equality with this DoubleEncodedValue + * @return true if the specified object is equal to this DoubleEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this DoubleEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(), and then if the other value is another + * DoubleEncodedValue, the return values of getValue() are compared, using the semantics of Double.compare() + * + * @param o The EncodedValue to compare with this DoubleEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EncodedValue.java index 52ffa089..82382868 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EncodedValue.java @@ -31,6 +31,18 @@ package org.jf.dexlib2.iface.value; -public interface EncodedValue { +/** + * This class represents a generic encoded value. + * + * It acts as the base interface for each specific type of encoded value. + */ +public interface EncodedValue extends Comparable { + /** + * Returns the type of this encoded value. + * + * The returned integer will be one of the ValueType.* constants. + * + * @return The type of this encoded value + */ int getValueType(); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EnumEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EnumEncodedValue.java index 2c1e720b..004b6c67 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EnumEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/EnumEncodedValue.java @@ -32,10 +32,55 @@ package org.jf.dexlib2.iface.value; import org.jf.dexlib2.iface.reference.FieldReference; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface EnumEncodedValue extends EncodedValue, SortedEncodedValue { - @Nonnull FieldReference getValue(); +/** + * This class represents an encoded enum value. + * + * The enum value is represented as a FieldReference to the field on an enum class that holds the enum value. + */ +public interface EnumEncodedValue extends EncodedValue { + /** + * Gets the enum value. + * + * @return a reference to the field on the enum class that holds the enum value, as a FieldReference + */ + FieldReference getValue(); + + /** + * Returns a hashcode for this EncodedEnumValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue.hashCode();
+     * }
+ * + * @return The hash code value for this EncodedEnumValue + */ + @Override int hashCode(); + + /** + * Compares this EnumEncodedValue to another EnumEncodedValue for equality. + * + * This EnumEncodedValue is equal to another EnumEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this EnumEncodedValue + * @return true if the specified object is equal to this EnumEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this EnumEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * EnumEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this EnumEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FieldEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FieldEncodedValue.java index fff087ad..864b2da0 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FieldEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FieldEncodedValue.java @@ -32,10 +32,56 @@ package org.jf.dexlib2.iface.value; import org.jf.dexlib2.iface.reference.FieldReference; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface FieldEncodedValue extends EncodedValue, SortedEncodedValue { - @Nonnull FieldReference getValue(); +/** + * This class represents an encoded field value. + */ +public interface FieldEncodedValue extends EncodedValue { + /** + * Gets the field value. + * + * @return the field value as a FieldReference + */ + FieldReference getValue(); + + /** + * Returns a hashcode for this EncodedFieldValue. + *

+ * This hashCode is defined to be the following: + *

+ *

+     * {@code
+     * int hashCode = getValue.hashCode();
+     * }
+ * + * @return The hash code value for this EncodedFieldValue + */ + @Override + int hashCode(); + + /** + * Compares this FieldEncodedValue to another FieldEncodedValue for equality. + *

+ * This FieldEncodedValue is equal to another FieldEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this FieldEncodedValue + * @return true if the specified object is equal to this FieldEncodedValue + */ + @Override + boolean equals(@Nullable Object o); + + /** + * Compare this FieldEncodedValue to another EncodedValue. + *

+ * The comparison is first done on the return values of getValueType(). If the other value is another + * FieldEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this FieldEncodedValue + * @return An integer representing the result of the comparison + */ + @Override + int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FloatEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FloatEncodedValue.java index 83d34116..275ca6bc 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FloatEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/FloatEncodedValue.java @@ -31,8 +31,58 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface FloatEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded float value. + */ +public interface FloatEncodedValue extends EncodedValue { + /** + * Gets the float value. + * + * @return the float value + */ float getValue(); + + /** + * Returns a hashcode for this EncodedFloatValue. + * + * This hashCode is defined to be the following: + * + *

+     * {@code
+     * int hashCode = Float.floatToRawIntBits(getValue());
+     * }
+ * + * Note: This is slightly different than the definition of Float.hashCode(). This uses floatToRawIntBits() + * instead of floatToIntBits(), in order to preserve as much information as possible. + * + * @return The hash code value for this EncodedFloatValue + */ + @Override int hashCode(); + + /** + * Compares this FloatEncodedValue to another FloatEncodedValue for equality. + * + * This FloatEncodedValue is equal to another FloatEncodedValue if the values returned by + * getValue().floatToRawIntBits() are equal. + * + * Note: this isn't quite the same as getValue() == getValue(), due to various NaN issues and signed zero issues. + * + * @param o The object to be compared for equality with this FloatEncodedValue + * @return true if the specified object is equal to this FloatEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this FloatEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * FloatEncodedValue, the return values of getValue() are compared, using the semantics of Float.compare() + * + * @param o The EncodedValue to compare with this FloatEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/IntEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/IntEncodedValue.java index a39e6b09..00909dbc 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/IntEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/IntEncodedValue.java @@ -31,8 +31,52 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface IntEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded integer value. + */ +public interface IntEncodedValue extends EncodedValue { + /** + * Gets the integer value. + * + * @return the int value + */ int getValue(); + + /** + * Returns a hashcode for this EncodedIntValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue();
+     * }
+ * + * @return The hash code value for this EncodedIntValue + */ + @Override int hashCode(); + + /** + * Compares this IntEncodedValue to another IntEncodedValue for equality. + * + * This IntEncodedValue is equal to another IntEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this IntEncodedValue + * @return true if the specified object is equal to this IntEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this IntEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * IntEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this IntEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/LongEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/LongEncodedValue.java index 8d2bcb4f..52fb3f3c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/LongEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/LongEncodedValue.java @@ -31,8 +31,53 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface LongEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded long value. + */ +public interface LongEncodedValue extends EncodedValue { + /** + * Gets the long value. + * + * @return the long value + */ long getValue(); + + /** + * Returns a hashcode for this EncodedLongValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * long v = getValue();
+     * int hashCode = (int)(v^(v>>>32));
+     * }
+ * + * @return The hash code value for this EncodedLongValue + */ + @Override int hashCode(); + + /** + * Compares this LongEncodedValue to another LongEncodedValue for equality. + * + * This LongEncodedValue is equal to another LongEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this LongEncodedValue + * @return true if the specified object is equal to this LongEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this LongEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * LongEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this LongEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/MethodEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/MethodEncodedValue.java index fdd903b2..3e3a2f85 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/MethodEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/MethodEncodedValue.java @@ -32,10 +32,53 @@ package org.jf.dexlib2.iface.value; import org.jf.dexlib2.iface.reference.MethodReference; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface MethodEncodedValue extends EncodedValue, SortedEncodedValue { - @Nonnull MethodReference getValue(); +/** + * This class represents an encoded method value. + */ +public interface MethodEncodedValue extends EncodedValue { + /** + * Gets the method value. + * + * @return the method value as a MethodReference + */ + MethodReference getValue(); + + /** + * Returns a hashcode for this EncodedMethodValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue.hashCode();
+     * }
+ * + * @return The hash code value for this EncodedMethodValue + */ + @Override int hashCode(); + + /** + * Compares this MethodEncodedValue to another MethodEncodedValue for equality. + * + * This MethodEncodedValue is equal to another MethodEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this MethodEncodedValue + * @return true if the specified object is equal to this MethodEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this MethodEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * MethodEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this MethodEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/NullEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/NullEncodedValue.java index 73bbd8f9..3c6988d5 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/NullEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/NullEncodedValue.java @@ -31,8 +31,45 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface NullEncodedValue extends EncodedValue, SortedEncodedValue { - //no value +/** + * This class represents an encoded null value. + */ +public interface NullEncodedValue extends EncodedValue { + /** + * Returns a hashcode for this EncodedNullValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = 0;
+     * }
+ * + * @return The hash code value for this EncodedNullValue + */ + @Override int hashCode(); + + /** + * Compares this NullEncodedValue to another NullEncodedValue for equality. + * + * This NullEncodedValue is always equal to another other NullEncodedValue + * + * @param o The object to be compared for equality with this NullEncodedValue + * @return true if the specified object is equal to this NullEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this NullEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * NullEncodedValue, then 0 is returned. + * + * @param o The EncodedValue to compare with this NullEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ShortEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ShortEncodedValue.java index f50eeaed..f5cb5154 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ShortEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/ShortEncodedValue.java @@ -31,8 +31,52 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface ShortEncodedValue extends EncodedValue, SortedEncodedValue { +/** + * This class represents an encoded short value. + */ +public interface ShortEncodedValue extends EncodedValue { + /** + * Gets the short value. + * + * @return the short value + */ short getValue(); + + /** + * Returns a hashcode for this EncodedShortValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue();
+     * }
+ * + * @return The hash code value for this EncodedShortValue + */ + @Override int hashCode(); + + /** + * Compares this ShortEncodedValue to another ShortEncodedValue for equality. + * + * This ShortEncodedValue is equal to another ShortEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this ShortEncodedValue + * @return true if the specified object is equal to this ShortEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this ShortEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * ShortEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this ShortEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/StringEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/StringEncodedValue.java index 98b3f6bb..c9cd4f36 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/StringEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/StringEncodedValue.java @@ -31,10 +31,52 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; - import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface StringEncodedValue extends EncodedValue, SortedEncodedValue { - @Nonnull String getValue(); +/** + * This class represents an encoded string value. + */ +public interface StringEncodedValue extends EncodedValue { + /** + * Gets the string value. + * + * @return the string value + */ + String getValue(); + + /** + * Returns a hashcode for this EncodedStringValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue().hashCode();
+     * }
+ * + * @return The hash code value for this EncodedStringValue + */ + @Override int hashCode(); + + /** + * Compares this StringEncodedValue to another StringEncodedValue for equality. + * + * This StringEncodedValue is equal to another StringEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this StringEncodedValue + * @return true if the specified object is equal to this StringEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this StringEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * StringEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this StringEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/TypeEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/TypeEncodedValue.java index 20c6acbd..95d9e9c0 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/iface/value/TypeEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/iface/value/TypeEncodedValue.java @@ -31,10 +31,52 @@ package org.jf.dexlib2.iface.value; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; - import javax.annotation.Nonnull; +import javax.annotation.Nullable; -public interface TypeEncodedValue extends EncodedValue, SortedEncodedValue { - @Nonnull String getValue(); +/** + * This class represents an encoded type value. + */ +public interface TypeEncodedValue extends EncodedValue { + /** + * Gets the type value. + * + * @return the type value + */ + String getValue(); + + /** + * Returns a hashcode for this EncodedTypeValue. + * + * This hashCode is defined to be the following: + * + *
+     * {@code
+     * int hashCode = getValue().hashCode();
+     * }
+ * + * @return The hash code value for this EncodedTypeValue + */ + @Override int hashCode(); + + /** + * Compares this TypeEncodedValue to another TypeEncodedValue for equality. + * + * This TypeEncodedValue is equal to another TypeEncodedValue if the values returned by getValue() are equal. + * + * @param o The object to be compared for equality with this TypeEncodedValue + * @return true if the specified object is equal to this TypeEncodedValue + */ + @Override boolean equals(@Nullable Object o); + + /** + * Compare this TypeEncodedValue to another EncodedValue. + * + * The comparison is first done on the return values of getValueType(). If the other value is another + * TypeEncodedValue, the return values of getValue() are compared. + * + * @param o The EncodedValue to compare with this TypeEncodedValue + * @return An integer representing the result of the comparison + */ + @Override int compareTo(@Nonnull EncodedValue o); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java index c9dfafab..bb68e5be 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotation.java @@ -31,11 +31,11 @@ package org.jf.dexlib2.immutable; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.BaseAnnotation; import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.AnnotationElement; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; @@ -43,24 +43,24 @@ import javax.annotation.Nullable; import java.util.Collection; public class ImmutableAnnotation extends BaseAnnotation { - public final int visibility; - @Nonnull public final String type; - @Nonnull public final ImmutableList elements; + protected final int visibility; + @Nonnull protected final String type; + @Nonnull protected final ImmutableSet elements; public ImmutableAnnotation(int visibility, @Nonnull String type, @Nullable Collection elements) { this.visibility = visibility; this.type = type; - this.elements = ImmutableAnnotationElement.immutableListOf(elements); + this.elements = ImmutableAnnotationElement.immutableSetOf(elements); } public ImmutableAnnotation(int visibility, @Nonnull String type, - @Nullable ImmutableList elements) { + @Nullable ImmutableSet elements) { this.visibility = visibility; this.type = type; - this.elements = ImmutableUtils.nullToEmptyList(elements); + this.elements = ImmutableUtils.nullToEmptySet(elements); } public static ImmutableAnnotation of(Annotation annotation) { @@ -75,15 +75,15 @@ public class ImmutableAnnotation extends BaseAnnotation { @Override public int getVisibility() { return visibility; } @Nonnull @Override public String getType() { return type; } - @Nonnull @Override public ImmutableList getElements() { return elements; } + @Nonnull @Override public ImmutableSet getElements() { return elements; } @Nonnull - public static ImmutableList immutableListOf(@Nullable Iterable list) { - return CONVERTER.convert(list); + public static ImmutableSet immutableSetOf(@Nullable Iterable list) { + return CONVERTER.toSet(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull Annotation item) { return item instanceof ImmutableAnnotation; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java index 354b2d9c..82556584 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableAnnotationElement.java @@ -31,20 +31,20 @@ package org.jf.dexlib2.immutable; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.BaseAnnotationElement; import org.jf.dexlib2.iface.AnnotationElement; import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.immutable.value.ImmutableEncodedValue; import org.jf.dexlib2.immutable.value.ImmutableEncodedValueFactory; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class ImmutableAnnotationElement extends BaseAnnotationElement { - @Nonnull public final String name; - @Nonnull public final ImmutableEncodedValue value; + @Nonnull protected final String name; + @Nonnull protected final ImmutableEncodedValue value; public ImmutableAnnotationElement(@Nonnull String name, @Nonnull EncodedValue value) { @@ -71,13 +71,13 @@ public class ImmutableAnnotationElement extends BaseAnnotationElement { @Nonnull @Override public EncodedValue getValue() { return value; } @Nonnull - public static ImmutableList immutableListOf( + public static ImmutableSet immutableSetOf( @Nullable Iterable list) { - return CONVERTER.convert(list); + return CONVERTER.toSet(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull AnnotationElement item) { return item instanceof ImmutableAnnotationElement; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java index fc26fe18..5970ca02 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableClassDef.java @@ -31,34 +31,33 @@ package org.jf.dexlib2.immutable; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.reference.BaseTypeReference; import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.Field; import org.jf.dexlib2.iface.Method; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Collection; -import java.util.List; public class ImmutableClassDef extends BaseTypeReference implements ClassDef { - @Nonnull public final String type; - public final int accessFlags; - @Nullable public final String superclass; - @Nonnull public final ImmutableList interfaces; - @Nullable public final String sourceFile; - @Nonnull public final ImmutableList annotations; - @Nonnull public final ImmutableList fields; - @Nonnull public final ImmutableList methods; + @Nonnull protected final String type; + protected final int accessFlags; + @Nullable protected final String superclass; + @Nonnull protected final ImmutableSet interfaces; + @Nullable protected final String sourceFile; + @Nonnull protected final ImmutableSet annotations; + @Nonnull protected final ImmutableSet fields; + @Nonnull protected final ImmutableSet methods; public ImmutableClassDef(@Nonnull String type, int accessFlags, @Nullable String superclass, - @Nullable List interfaces, + @Nullable Collection interfaces, @Nullable String sourceFile, @Nullable Collection annotations, @Nullable Collection fields, @@ -66,29 +65,29 @@ public class ImmutableClassDef extends BaseTypeReference implements ClassDef { this.type = type; this.accessFlags = accessFlags; this.superclass = superclass; - this.interfaces = interfaces==null ? ImmutableList.of() : ImmutableList.copyOf(interfaces); + this.interfaces = interfaces==null ? ImmutableSet.of() : ImmutableSet.copyOf(interfaces); this.sourceFile = sourceFile; - this.annotations = ImmutableAnnotation.immutableListOf(annotations); - this.fields = ImmutableField.immutableListOf(fields); - this.methods = ImmutableMethod.immutableListOf(methods); + this.annotations = ImmutableAnnotation.immutableSetOf(annotations); + this.fields = ImmutableField.immutableSetOf(fields); + this.methods = ImmutableMethod.immutableSetOf(methods); } public ImmutableClassDef(@Nonnull String type, int accessFlags, @Nullable String superclass, - @Nullable ImmutableList interfaces, + @Nullable ImmutableSet interfaces, @Nullable String sourceFile, - @Nullable ImmutableList annotations, - @Nullable ImmutableList fields, - @Nullable ImmutableList methods) { + @Nullable ImmutableSet annotations, + @Nullable ImmutableSet fields, + @Nullable ImmutableSet methods) { this.type = type; this.accessFlags = accessFlags; this.superclass = superclass; - this.interfaces = ImmutableUtils.nullToEmptyList(interfaces); + this.interfaces = ImmutableUtils.nullToEmptySet(interfaces); this.sourceFile = sourceFile; - this.annotations = ImmutableUtils.nullToEmptyList(annotations); - this.fields = ImmutableUtils.nullToEmptyList(fields); - this.methods = ImmutableUtils.nullToEmptyList(methods); + this.annotations = ImmutableUtils.nullToEmptySet(annotations); + this.fields = ImmutableUtils.nullToEmptySet(fields); + this.methods = ImmutableUtils.nullToEmptySet(methods); } public static ImmutableClassDef of(ClassDef classDef) { @@ -109,19 +108,19 @@ public class ImmutableClassDef extends BaseTypeReference implements ClassDef { @Nonnull @Override public String getType() { return type; } @Override public int getAccessFlags() { return accessFlags; } @Nullable @Override public String getSuperclass() { return superclass; } - @Nonnull @Override public ImmutableList getInterfaces() { return interfaces; } + @Nonnull @Override public ImmutableSet getInterfaces() { return interfaces; } @Nullable @Override public String getSourceFile() { return sourceFile; } - @Nonnull @Override public ImmutableList getAnnotations() { return annotations; } - @Nonnull @Override public ImmutableList getFields() { return fields; } - @Nonnull @Override public ImmutableList getMethods() { return methods; } + @Nonnull @Override public ImmutableSet getAnnotations() { return annotations; } + @Nonnull @Override public ImmutableSet getFields() { return fields; } + @Nonnull @Override public ImmutableSet getMethods() { return methods; } @Nonnull - public static ImmutableList immutableListOf(@Nullable List list) { - return CONVERTER.convert(list); + public static ImmutableSet immutableSetOf(@Nullable Iterable iterable) { + return CONVERTER.toSet(iterable); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull ClassDef item) { return item instanceof ImmutableClassDef; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableDexFile.java index e76f692f..e911eb38 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableDexFile.java @@ -31,24 +31,24 @@ package org.jf.dexlib2.immutable; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.DexFile; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.List; +import java.util.Collection; public class ImmutableDexFile implements DexFile { - @Nonnull public final ImmutableList classes; + @Nonnull protected final ImmutableSet classes; - public ImmutableDexFile(@Nullable List classes) { - this.classes = ImmutableClassDef.immutableListOf(classes); + public ImmutableDexFile(@Nullable Collection classes) { + this.classes = ImmutableClassDef.immutableSetOf(classes); } - public ImmutableDexFile(@Nullable ImmutableList classes) { - this.classes = ImmutableUtils.nullToEmptyList(classes); + public ImmutableDexFile(@Nullable ImmutableSet classes) { + this.classes = ImmutableUtils.nullToEmptySet(classes); } public static ImmutableDexFile of(DexFile dexFile) { @@ -58,5 +58,5 @@ public class ImmutableDexFile implements DexFile { return new ImmutableDexFile(dexFile.getClasses()); } - @Nonnull @Override public List getClasses() { return classes; } + @Nonnull @Override public ImmutableSet getClasses() { return classes; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java index 611f3b8e..ceb34ef7 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableExceptionHandler.java @@ -31,16 +31,17 @@ package org.jf.dexlib2.immutable; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import org.jf.dexlib2.base.BaseExceptionHandler; import org.jf.dexlib2.iface.ExceptionHandler; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class ImmutableExceptionHandler implements ExceptionHandler { - @Nullable public final String exceptionType; - public final int handlerCodeAddress; +public class ImmutableExceptionHandler extends BaseExceptionHandler implements ExceptionHandler { + @Nullable protected final String exceptionType; + protected final int handlerCodeAddress; public ImmutableExceptionHandler(@Nullable String exceptionType, int handlerCodeAddress) { @@ -61,13 +62,13 @@ public class ImmutableExceptionHandler implements ExceptionHandler { @Override public int getHandlerCodeAddress() { return handlerCodeAddress; } @Nonnull - public static ImmutableList immutableListOf( + public static ImmutableSet immutableSetOf( @Nullable Iterable list) { - return CONVERTER.convert(list); + return CONVERTER.toSet(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull ExceptionHandler item) { return item instanceof ImmutableExceptionHandler; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java index 58d5cf2c..dc7c6ece 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableField.java @@ -31,14 +31,14 @@ package org.jf.dexlib2.immutable; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.reference.BaseFieldReference; import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.Field; import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.immutable.value.ImmutableEncodedValue; import org.jf.dexlib2.immutable.value.ImmutableEncodedValueFactory; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; @@ -46,39 +46,39 @@ import javax.annotation.Nullable; import java.util.Collection; public class ImmutableField extends BaseFieldReference implements Field { - @Nonnull public final String containingClass; - @Nonnull public final String name; - @Nonnull public final String type; - public final int accessFlags; - @Nullable public final ImmutableEncodedValue initialValue; - @Nonnull public final ImmutableList annotations; + @Nonnull protected final String definingClass; + @Nonnull protected final String name; + @Nonnull protected final String type; + protected final int accessFlags; + @Nullable protected final ImmutableEncodedValue initialValue; + @Nonnull protected final ImmutableSet annotations; - public ImmutableField(@Nonnull String containingClass, + public ImmutableField(@Nonnull String definingClass, @Nonnull String name, @Nonnull String type, int accessFlags, @Nullable EncodedValue initialValue, @Nullable Collection annotations) { - this.containingClass = containingClass; + this.definingClass = definingClass; this.name = name; this.type = type; this.accessFlags = accessFlags; this.initialValue = ImmutableEncodedValueFactory.ofNullable(initialValue); - this.annotations = ImmutableAnnotation.immutableListOf(annotations); + this.annotations = ImmutableAnnotation.immutableSetOf(annotations); } - public ImmutableField(@Nonnull String containingClass, + public ImmutableField(@Nonnull String definingClass, @Nonnull String name, @Nonnull String type, int accessFlags, @Nullable ImmutableEncodedValue initialValue, - @Nullable ImmutableList annotations) { - this.containingClass = containingClass; + @Nullable ImmutableSet annotations) { + this.definingClass = definingClass; this.name = name; this.type = type; this.accessFlags = accessFlags; this.initialValue = initialValue; - this.annotations = ImmutableUtils.nullToEmptyList(annotations); + this.annotations = ImmutableUtils.nullToEmptySet(annotations); } public static ImmutableField of(Field field) { @@ -86,7 +86,7 @@ public class ImmutableField extends BaseFieldReference implements Field { return (ImmutableField)field; } return new ImmutableField( - field.getContainingClass(), + field.getDefiningClass(), field.getName(), field.getType(), field.getAccessFlags(), @@ -94,20 +94,20 @@ public class ImmutableField extends BaseFieldReference implements Field { field.getAnnotations()); } - @Nonnull @Override public String getContainingClass() { return containingClass; } + @Nonnull @Override public String getDefiningClass() { return definingClass; } @Nonnull @Override public String getName() { return name; } @Nonnull @Override public String getType() { return type; } @Override public int getAccessFlags() { return accessFlags; } @Override public EncodedValue getInitialValue() { return initialValue;} - @Nonnull @Override public ImmutableList getAnnotations() { return annotations; } + @Nonnull @Override public ImmutableSet getAnnotations() { return annotations; } @Nonnull - public static ImmutableList immutableListOf(@Nullable Iterable list) { - return CONVERTER.convert(list); + public static ImmutableSet immutableSetOf(@Nullable Iterable list) { + return CONVERTER.toSet(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull Field item) { return item instanceof ImmutableField; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java index 42b468e5..4cb8841c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethod.java @@ -32,56 +32,60 @@ package org.jf.dexlib2.immutable; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Ordering; import org.jf.dexlib2.base.reference.BaseMethodReference; import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.Method; import org.jf.dexlib2.iface.MethodImplementation; import org.jf.dexlib2.iface.MethodParameter; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; +import java.util.List; +import java.util.Set; public class ImmutableMethod extends BaseMethodReference implements Method { - @Nonnull public final String containingClass; - @Nonnull public final String name; - @Nonnull public final ImmutableList parameters; - @Nonnull public final String returnType; - public final int accessFlags; - @Nonnull public final ImmutableList annotations; - @Nullable public final ImmutableMethodImplementation methodImplementation; + @Nonnull protected final String definingClass; + @Nonnull protected final String name; + @Nonnull protected final ImmutableList parameters; + @Nonnull protected final String returnType; + protected final int accessFlags; + @Nonnull protected final ImmutableSet annotations; + @Nullable protected final ImmutableMethodImplementation methodImplementation; - public ImmutableMethod(@Nonnull String containingClass, + public ImmutableMethod(@Nonnull String definingClass, @Nonnull String name, - @Nullable Collection parameters, + @Nullable List parameters, @Nonnull String returnType, int accessFlags, - @Nullable Collection annotations, + @Nullable Set annotations, @Nullable MethodImplementation methodImplementation) { - this.containingClass = containingClass; + this.definingClass = definingClass; this.name = name; this.parameters = ImmutableMethodParameter.immutableListOf(parameters); this.returnType = returnType; this.accessFlags = accessFlags; - this.annotations = ImmutableAnnotation.immutableListOf(annotations); + this.annotations = ImmutableAnnotation.immutableSetOf(annotations); this.methodImplementation = ImmutableMethodImplementation.of(methodImplementation); } - public ImmutableMethod(@Nonnull String containingClass, + public ImmutableMethod(@Nonnull String definingClass, @Nonnull String name, @Nullable ImmutableList parameters, @Nonnull String returnType, int accessFlags, - @Nullable ImmutableList annotations, + @Nullable ImmutableSet annotations, @Nullable ImmutableMethodImplementation methodImplementation) { - this.containingClass = containingClass; + this.definingClass = definingClass; this.name = name; this.parameters = ImmutableUtils.nullToEmptyList(parameters); this.returnType = returnType; this.accessFlags = accessFlags; - this.annotations = ImmutableUtils.nullToEmptyList(annotations); + this.annotations = ImmutableUtils.nullToEmptySet(annotations); this.methodImplementation = methodImplementation; } @@ -90,7 +94,7 @@ public class ImmutableMethod extends BaseMethodReference implements Method { return (ImmutableMethod)method; } return new ImmutableMethod( - method.getContainingClass(), + method.getDefiningClass(), method.getName(), method.getParameters(), method.getReturnType(), @@ -99,21 +103,21 @@ public class ImmutableMethod extends BaseMethodReference implements Method { method.getImplementation()); } - @Nonnull public String getContainingClass() { return containingClass; } + @Nonnull public String getDefiningClass() { return definingClass; } @Nonnull public String getName() { return name; } @Nonnull public ImmutableList getParameters() { return parameters; } @Nonnull public String getReturnType() { return returnType; } public int getAccessFlags() { return accessFlags; } - @Nonnull public ImmutableList getAnnotations() { return annotations; } + @Nonnull public ImmutableSet getAnnotations() { return annotations; } @Nullable public ImmutableMethodImplementation getImplementation() { return methodImplementation; } @Nonnull - public static ImmutableList immutableListOf(@Nullable Iterable list) { - return CONVERTER.convert(list); + public static ImmutableSortedSet immutableSetOf(@Nullable Iterable list) { + return CONVERTER.toSortedSet(Ordering.natural(), list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull Method item) { return item instanceof ImmutableMethod; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java index 9ab1939d..99d4f194 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodImplementation.java @@ -45,10 +45,10 @@ import javax.annotation.Nullable; import java.util.List; public class ImmutableMethodImplementation implements MethodImplementation { - public final int registerCount; - @Nonnull public final ImmutableList instructions; - @Nonnull public final ImmutableList tryBlocks; - @Nonnull public final ImmutableList debugItems; + protected final int registerCount; + @Nonnull protected final ImmutableList instructions; + @Nonnull protected final ImmutableList tryBlocks; + @Nonnull protected final ImmutableList debugItems; public ImmutableMethodImplementation(int registerCount, @Nullable Iterable instructions, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java index 18ffaba4..05124ff7 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableMethodParameter.java @@ -32,35 +32,35 @@ package org.jf.dexlib2.immutable; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.BaseMethodParameter; import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.MethodParameter; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; -import java.util.List; +import java.util.Set; public class ImmutableMethodParameter extends BaseMethodParameter { - @Nonnull public final String type; - @Nonnull public final ImmutableList annotations; - @Nullable public final String name; + @Nonnull protected final String type; + @Nonnull protected final ImmutableSet annotations; + @Nullable protected final String name; public ImmutableMethodParameter(@Nonnull String type, - @Nullable Collection annotations, + @Nullable Set annotations, @Nullable String name) { this.type = type; - this.annotations = ImmutableAnnotation.immutableListOf(annotations); + this.annotations = ImmutableAnnotation.immutableSetOf(annotations); this.name = name; } public ImmutableMethodParameter(@Nonnull String type, - @Nullable ImmutableList annotations, + @Nullable ImmutableSet annotations, @Nullable String name) { this.type = type; - this.annotations = ImmutableUtils.nullToEmptyList(annotations); + this.annotations = ImmutableUtils.nullToEmptySet(annotations); this.name = name; } @@ -75,7 +75,7 @@ public class ImmutableMethodParameter extends BaseMethodParameter { } @Nonnull @Override public String getType() { return type; } - @Nonnull @Override public List getAnnotations() { return annotations; } + @Nonnull @Override public Set getAnnotations() { return annotations; } @Nullable @Override public String getName() { return name; } //TODO: iterate over the annotations to get the signature @@ -84,11 +84,11 @@ public class ImmutableMethodParameter extends BaseMethodParameter { @Nonnull public static ImmutableList immutableListOf( @Nullable Iterable list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull MethodParameter item) { return item instanceof ImmutableMethodParameter; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java index 642c210c..e59b43d2 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/ImmutableTryBlock.java @@ -32,35 +32,36 @@ package org.jf.dexlib2.immutable; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.iface.ExceptionHandler; import org.jf.dexlib2.iface.TryBlock; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import org.jf.util.ImmutableUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collection; import java.util.List; +import java.util.Set; public class ImmutableTryBlock implements TryBlock { - public final int startCodeAddress; - public final int codeUnitCount; - @Nonnull public final ImmutableList exceptionHandlers; + protected final int startCodeAddress; + protected final int codeUnitCount; + @Nonnull protected final ImmutableSet exceptionHandlers; public ImmutableTryBlock(int startCodeAddress, int codeUnitCount, - @Nullable Collection exceptionHandlers) { + @Nullable Set exceptionHandlers) { this.startCodeAddress = startCodeAddress; this.codeUnitCount = codeUnitCount; - this.exceptionHandlers = ImmutableExceptionHandler.immutableListOf(exceptionHandlers); + this.exceptionHandlers = ImmutableExceptionHandler.immutableSetOf(exceptionHandlers); } public ImmutableTryBlock(int startCodeAddress, int codeUnitCount, - @Nullable ImmutableList exceptionHandlers) { + @Nullable ImmutableSet exceptionHandlers) { this.startCodeAddress = startCodeAddress; this.codeUnitCount = codeUnitCount; - this.exceptionHandlers = ImmutableUtils.nullToEmptyList(exceptionHandlers); + this.exceptionHandlers = ImmutableUtils.nullToEmptySet(exceptionHandlers); } public static ImmutableTryBlock of(TryBlock tryBlock) { @@ -76,17 +77,17 @@ public class ImmutableTryBlock implements TryBlock { @Override public int getStartCodeAddress() { return startCodeAddress; } @Override public int getCodeUnitCount() { return codeUnitCount; } - @Nonnull @Override public ImmutableList getExceptionHandlers() { + @Nonnull @Override public ImmutableSet getExceptionHandlers() { return exceptionHandlers; } @Nonnull public static ImmutableList immutableListOf(@Nullable List list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull TryBlock item) { return item instanceof ImmutableTryBlock; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableDebugItem.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableDebugItem.java index 2d699b2f..7b016ef6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableDebugItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableDebugItem.java @@ -35,13 +35,13 @@ import com.google.common.collect.ImmutableList; import org.jf.dexlib2.DebugItemType; import org.jf.dexlib2.iface.debug.*; import org.jf.util.ExceptionWithContext; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; import javax.annotation.Nullable; public abstract class ImmutableDebugItem implements DebugItem { - public final int codeAddress; + protected final int codeAddress; public ImmutableDebugItem(int codeAddress) { this.codeAddress = codeAddress; @@ -76,11 +76,11 @@ public abstract class ImmutableDebugItem implements DebugItem { @Nonnull public static ImmutableList immutableListOf(@Nullable Iterable list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull DebugItem item) { return item instanceof ImmutableDebugItem; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableEndLocal.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableEndLocal.java index 48c9dd5a..4cc0cacd 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableEndLocal.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableEndLocal.java @@ -38,10 +38,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; public class ImmutableEndLocal extends ImmutableDebugItem implements EndLocal { - public final int register; - @Nullable public final String name; - @Nullable public final String type; - @Nullable public final String signature; + protected final int register; + @Nullable protected final String name; + @Nullable protected final String type; + @Nullable protected final String signature; public ImmutableEndLocal(int codeAddress, int register) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableLineNumber.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableLineNumber.java index 1c85b956..fcfafe5f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableLineNumber.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableLineNumber.java @@ -37,7 +37,7 @@ import org.jf.dexlib2.iface.debug.LineNumber; import javax.annotation.Nonnull; public class ImmutableLineNumber extends ImmutableDebugItem implements LineNumber { - public final int lineNumber; + protected final int lineNumber; public ImmutableLineNumber(int codeAddress, int lineNumber) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableRestartLocal.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableRestartLocal.java index b487dfb0..c639e7d3 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableRestartLocal.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableRestartLocal.java @@ -38,10 +38,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; public class ImmutableRestartLocal extends ImmutableDebugItem implements RestartLocal { - public final int register; - @Nullable public final String name; - @Nullable public final String type; - @Nullable public final String signature; + protected final int register; + @Nullable protected final String name; + @Nullable protected final String type; + @Nullable protected final String signature; public ImmutableRestartLocal(int codeAddress, int register) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableSetSourceFile.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableSetSourceFile.java index ee4db48e..54d7e29c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableSetSourceFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableSetSourceFile.java @@ -38,7 +38,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; public class ImmutableSetSourceFile extends ImmutableDebugItem implements SetSourceFile { - @Nullable public final String sourceFile; + @Nullable protected final String sourceFile; public ImmutableSetSourceFile(int codeAddress, @Nullable String sourceFile) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableStartLocal.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableStartLocal.java index 4d3daadc..24eb7c83 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableStartLocal.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/debug/ImmutableStartLocal.java @@ -38,10 +38,10 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; public class ImmutableStartLocal extends ImmutableDebugItem implements StartLocal { - public final int register; - @Nullable public final String name; - @Nullable public final String type; - @Nullable public final String signature; + protected final int register; + @Nullable protected final String name; + @Nullable protected final String type; + @Nullable protected final String signature; public ImmutableStartLocal(int codeAddress, int register, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableArrayPayload.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableArrayPayload.java index 9eb1ccbb..50c6f54f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableArrayPayload.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableArrayPayload.java @@ -44,8 +44,8 @@ import java.util.List; public class ImmutableArrayPayload extends ImmutableInstruction implements ArrayPayload { public static final Opcode OPCODE = Opcode.ARRAY_PAYLOAD; - public final int elementWidth; - @Nonnull public final ImmutableList arrayElements; + protected final int elementWidth; + @Nonnull protected final ImmutableList arrayElements; public ImmutableArrayPayload(int elementWidth, @Nullable List arrayElements) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java index 792a3746..0086a91e 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction.java @@ -37,12 +37,12 @@ import org.jf.dexlib2.Opcode; import org.jf.dexlib2.iface.instruction.Instruction; import org.jf.dexlib2.iface.instruction.formats.*; import org.jf.dexlib2.util.Preconditions; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; public abstract class ImmutableInstruction implements Instruction { - @Nonnull public final Opcode opcode; + @Nonnull protected final Opcode opcode; protected ImmutableInstruction(@Nonnull Opcode opcode) { this.opcode = opcode; @@ -131,11 +131,11 @@ public abstract class ImmutableInstruction implements Instruction { @Nonnull public static ImmutableList immutableListOf(Iterable list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull Instruction item) { return item instanceof ImmutableInstruction; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java index 19ef36ea..be773ee7 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction10t.java @@ -41,7 +41,7 @@ import javax.annotation.Nonnull; public class ImmutableInstruction10t extends ImmutableInstruction implements Instruction10t { public static final Format FORMAT = Format.Format10t; - public final int codeOffset; + protected final int codeOffset; public ImmutableInstruction10t(@Nonnull Opcode opcode, int codeOffset) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java index fec1adfc..e7f66c7c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11n.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction11n extends ImmutableInstruction implements Instruction11n { public static final Format FORMAT = Format.Format11n; - public final int registerA; - public final int literal; + protected final int registerA; + protected final int literal; public ImmutableInstruction11n(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java index e0957b6a..2abbf4e6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction11x.java @@ -41,7 +41,7 @@ import javax.annotation.Nonnull; public class ImmutableInstruction11x extends ImmutableInstruction implements Instruction11x { public static final Format FORMAT = Format.Format11x; - public final int registerA; + protected final int registerA; public ImmutableInstruction11x(@Nonnull Opcode opcode, int registerA) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java index d53bd7aa..d9062540 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction12x.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction12x extends ImmutableInstruction implements Instruction12x { public static final Format FORMAT = Format.Format12x; - public final int registerA; - public final int registerB; + protected final int registerA; + protected final int registerB; public ImmutableInstruction12x(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java index bf3d2309..19a6d9f1 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction20t.java @@ -41,7 +41,7 @@ import javax.annotation.Nonnull; public class ImmutableInstruction20t extends ImmutableInstruction implements Instruction20t { public static final Format FORMAT = Format.Format20t; - public final int codeOffset; + protected final int codeOffset; public ImmutableInstruction20t(@Nonnull Opcode opcode, int codeOffset) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java index 52681120..c0d2b179 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21c.java @@ -44,8 +44,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction21c extends ImmutableInstruction implements Instruction21c { public static final Format FORMAT = Format.Format21c; - public final int registerA; - @Nonnull public final ImmutableReference reference; + protected final int registerA; + @Nonnull protected final ImmutableReference reference; public ImmutableInstruction21c(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java index 05dc40c8..b27f7ed5 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21ih.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction21ih extends ImmutableInstruction implements Instruction21ih { public static final Format FORMAT = Format.Format21ih; - public final int registerA; - public final int literal; + protected final int registerA; + protected final int literal; public ImmutableInstruction21ih(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java index 42d1efae..dff19984 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21lh.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction21lh extends ImmutableInstruction implements Instruction21lh { public static final Format FORMAT = Format.Format21lh; - public final int registerA; - public final long literal; + protected final int registerA; + protected final long literal; public ImmutableInstruction21lh(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java index 5e57391a..9f5315e1 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21s.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction21s extends ImmutableInstruction implements Instruction21s { public static final Format FORMAT = Format.Format21s; - public final int registerA; - public final int literal; + protected final int registerA; + protected final int literal; public ImmutableInstruction21s(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java index 20d1ab18..9fdb3a09 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction21t.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction21t extends ImmutableInstruction implements Instruction21t { public static final Format FORMAT = Format.Format21t; - public final int registerA; - public final int codeOffset; + protected final int registerA; + protected final int codeOffset; public ImmutableInstruction21t(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java index 7b076305..d9b2dc97 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22b.java @@ -41,9 +41,9 @@ import javax.annotation.Nonnull; public class ImmutableInstruction22b extends ImmutableInstruction implements Instruction22b { public static final Format FORMAT = Format.Format22b; - public final int registerA; - public final int registerB; - public final int literal; + protected final int registerA; + protected final int registerB; + protected final int literal; public ImmutableInstruction22b(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java index a632ef7b..99e32366 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22c.java @@ -44,9 +44,9 @@ import javax.annotation.Nonnull; public class ImmutableInstruction22c extends ImmutableInstruction implements Instruction22c { public static final Format FORMAT = Format.Format22c; - public final int registerA; - public final int registerB; - @Nonnull public final ImmutableReference reference; + protected final int registerA; + protected final int registerB; + @Nonnull protected final ImmutableReference reference; public ImmutableInstruction22c(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java index 6eb5b6c0..f0c25376 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22s.java @@ -41,9 +41,9 @@ import javax.annotation.Nonnull; public class ImmutableInstruction22s extends ImmutableInstruction implements Instruction22s { public static final Format FORMAT = Format.Format22s; - public final int registerA; - public final int registerB; - public final int literal; + protected final int registerA; + protected final int registerB; + protected final int literal; public ImmutableInstruction22s(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java index f4e5de67..c4662e1a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22t.java @@ -41,9 +41,9 @@ import javax.annotation.Nonnull; public class ImmutableInstruction22t extends ImmutableInstruction implements Instruction22t { public static final Format FORMAT = Format.Format22t; - public final int registerA; - public final int registerB; - public final int codeOffset; + protected final int registerA; + protected final int registerB; + protected final int codeOffset; public ImmutableInstruction22t(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java index 95e6bdaa..7fd74558 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction22x.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction22x extends ImmutableInstruction implements Instruction22x { public static final Format FORMAT = Format.Format22x; - public final int registerA; - public final int registerB; + protected final int registerA; + protected final int registerB; public ImmutableInstruction22x(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java index 60f12285..fb2f2a30 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction23x.java @@ -41,9 +41,9 @@ import javax.annotation.Nonnull; public class ImmutableInstruction23x extends ImmutableInstruction implements Instruction23x { public static final Format FORMAT = Format.Format23x; - public final int registerA; - public final int registerB; - public final int registerC; + protected final int registerA; + protected final int registerB; + protected final int registerC; public ImmutableInstruction23x(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java index d1538813..9e73cc46 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction30t.java @@ -41,7 +41,7 @@ import javax.annotation.Nonnull; public class ImmutableInstruction30t extends ImmutableInstruction implements Instruction30t { public static final Format FORMAT = Format.Format30t; - public final int codeOffset; + protected final int codeOffset; public ImmutableInstruction30t(@Nonnull Opcode opcode, int codeOffset) { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java index a3624437..9cfa95b6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31c.java @@ -44,8 +44,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction31c extends ImmutableInstruction implements Instruction31c { public static final Format FORMAT = Format.Format31c; - public final int registerA; - @Nonnull public final ImmutableReference reference; + protected final int registerA; + @Nonnull protected final ImmutableReference reference; public ImmutableInstruction31c(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java index cefc3ce8..ffcde117 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31i.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction31i extends ImmutableInstruction implements Instruction31i { public static final Format FORMAT = Format.Format31i; - public final int registerA; - public final int literal; + protected final int registerA; + protected final int literal; public ImmutableInstruction31i(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java index 3531fda0..3fc3da21 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction31t.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction31t extends ImmutableInstruction implements Instruction31t { public static final Format FORMAT = Format.Format31t; - public final int registerA; - public final int codeOffset; + protected final int registerA; + protected final int codeOffset; public ImmutableInstruction31t(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java index e2197a21..aa46b23c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction32x.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction32x extends ImmutableInstruction implements Instruction32x { public static final Format FORMAT = Format.Format32x; - public final int registerA; - public final int registerB; + protected final int registerA; + protected final int registerB; public ImmutableInstruction32x(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java index e31f8fe6..a0b8fe39 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction35c.java @@ -44,13 +44,13 @@ import javax.annotation.Nonnull; public class ImmutableInstruction35c extends ImmutableInstruction implements Instruction35c { public static final Format FORMAT = Format.Format35c; - public final int registerCount; - public final int registerC; - public final int registerD; - public final int registerE; - public final int registerF; - public final int registerG; - @Nonnull public final ImmutableReference reference; + protected final int registerCount; + protected final int registerC; + protected final int registerD; + protected final int registerE; + protected final int registerF; + protected final int registerG; + @Nonnull protected final ImmutableReference reference; public ImmutableInstruction35c(@Nonnull Opcode opcode, int registerCount, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java index 0446d2ff..22992a7d 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction3rc.java @@ -44,10 +44,10 @@ import javax.annotation.Nonnull; public class ImmutableInstruction3rc extends ImmutableInstruction implements Instruction3rc { public static final Format FORMAT = Format.Format3rc; - public final int startRegister; - public final int registerCount; + protected final int startRegister; + protected final int registerCount; - @Nonnull public final ImmutableReference reference; + @Nonnull protected final ImmutableReference reference; public ImmutableInstruction3rc(@Nonnull Opcode opcode, int startRegister, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java index 0857dc4d..a092ea57 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableInstruction51l.java @@ -41,8 +41,8 @@ import javax.annotation.Nonnull; public class ImmutableInstruction51l extends ImmutableInstruction implements Instruction51l { public static final Format FORMAT = Format.Format51l; - public final int registerA; - public final long literal; + protected final int registerA; + protected final long literal; public ImmutableInstruction51l(@Nonnull Opcode opcode, int registerA, diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutablePackedSwitchPayload.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutablePackedSwitchPayload.java index e46944d9..bee51a16 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutablePackedSwitchPayload.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutablePackedSwitchPayload.java @@ -45,7 +45,7 @@ import java.util.List; public class ImmutablePackedSwitchPayload extends ImmutableInstruction implements PackedSwitchPayload { public static final Opcode OPCODE = Opcode.PACKED_SWITCH_PAYLOAD; - @Nonnull public final ImmutableList switchElements; + @Nonnull protected final ImmutableList switchElements; public ImmutablePackedSwitchPayload(@Nullable List switchElements) { super(OPCODE); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSparseSwitchPayload.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSparseSwitchPayload.java index f8c0522a..eb4e90e6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSparseSwitchPayload.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSparseSwitchPayload.java @@ -45,7 +45,7 @@ import java.util.List; public class ImmutableSparseSwitchPayload extends ImmutableInstruction implements SparseSwitchPayload { public static final Opcode OPCODE = Opcode.SPARSE_SWITCH_PAYLOAD; - @Nonnull public final ImmutableList switchElements; + @Nonnull protected final ImmutableList switchElements; public ImmutableSparseSwitchPayload(@Nullable List switchElements) { super(OPCODE); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSwitchElement.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSwitchElement.java index 3dc13003..bc26ed9e 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSwitchElement.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/instruction/ImmutableSwitchElement.java @@ -33,15 +33,15 @@ package org.jf.dexlib2.immutable.instruction; import com.google.common.collect.ImmutableList; import org.jf.dexlib2.iface.instruction.SwitchElement; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.List; public class ImmutableSwitchElement implements SwitchElement { - public final int key; - public final int offset; + protected final int key; + protected final int offset; public ImmutableSwitchElement(int key, int offset) { @@ -64,11 +64,11 @@ public class ImmutableSwitchElement implements SwitchElement { @Nonnull public static ImmutableList immutableListOf(@Nullable List list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull SwitchElement item) { return item instanceof ImmutableSwitchElement; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableFieldReference.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableFieldReference.java index 5c8e3b66..e9fdf3e0 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableFieldReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableFieldReference.java @@ -37,14 +37,14 @@ import org.jf.dexlib2.iface.reference.FieldReference; import javax.annotation.Nonnull; public class ImmutableFieldReference extends BaseFieldReference implements ImmutableReference { - @Nonnull public final String containingClass; - @Nonnull public final String name; - @Nonnull public final String type; + @Nonnull protected final String definingClass; + @Nonnull protected final String name; + @Nonnull protected final String type; - public ImmutableFieldReference(@Nonnull String containingClass, + public ImmutableFieldReference(@Nonnull String definingClass, @Nonnull String name, @Nonnull String type) { - this.containingClass = containingClass; + this.definingClass = definingClass; this.name = name; this.type = type; } @@ -55,12 +55,12 @@ public class ImmutableFieldReference extends BaseFieldReference implements Immut return (ImmutableFieldReference)fieldReference; } return new ImmutableFieldReference( - fieldReference.getContainingClass(), + fieldReference.getDefiningClass(), fieldReference.getName(), fieldReference.getType()); } - @Nonnull public String getContainingClass() { return containingClass; } + @Nonnull public String getDefiningClass() { return definingClass; } @Nonnull public String getName() { return name; } @Nonnull public String getType() { return type; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableMethodReference.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableMethodReference.java index 53871aa6..1f601d16 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableMethodReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableMethodReference.java @@ -42,26 +42,26 @@ import javax.annotation.Nullable; import java.util.List; public class ImmutableMethodReference extends BaseMethodReference implements ImmutableReference { - @Nonnull public final String containingClass; - @Nonnull public final String name; - @Nonnull public final ImmutableList parameters; - @Nonnull public final String returnType; + @Nonnull protected final String definingClass; + @Nonnull protected final String name; + @Nonnull protected final ImmutableList parameters; + @Nonnull protected final String returnType; - public ImmutableMethodReference(@Nonnull String containingClass, + public ImmutableMethodReference(@Nonnull String definingClass, @Nonnull String name, @Nullable List parameters, @Nonnull String returnType) { - this.containingClass = containingClass; + this.definingClass = definingClass; this.name = name; this.parameters = ImmutableTypeReference.immutableListOf(parameters); this.returnType = returnType; } - public ImmutableMethodReference(@Nonnull String containingClass, + public ImmutableMethodReference(@Nonnull String definingClass, @Nonnull String name, @Nullable ImmutableList parameters, @Nonnull String returnType) { - this.containingClass = containingClass; + this.definingClass = definingClass; this.name = name; this.parameters = ImmutableUtils.nullToEmptyList(parameters); this.returnType = returnType; @@ -73,13 +73,13 @@ public class ImmutableMethodReference extends BaseMethodReference implements Imm return (ImmutableMethodReference)methodReference; } return new ImmutableMethodReference( - methodReference.getContainingClass(), + methodReference.getDefiningClass(), methodReference.getName(), ImmutableList.copyOf(methodReference.getParameters()), methodReference.getReturnType()); } - @Nonnull @Override public String getContainingClass() { return containingClass; } + @Nonnull @Override public String getDefiningClass() { return definingClass; } @Nonnull @Override public String getName() { return name; } @Nonnull @Override public List getParameters() { return parameters; } @Nonnull @Override public String getReturnType() { return returnType; } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableStringReference.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableStringReference.java index 5f1daf7c..292c8412 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableStringReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableStringReference.java @@ -37,7 +37,7 @@ import org.jf.dexlib2.iface.reference.StringReference; import javax.annotation.Nonnull; public class ImmutableStringReference extends BaseStringReference implements ImmutableReference { - @Nonnull public final String str; + @Nonnull protected final String str; public ImmutableStringReference(String str) { this.str = str; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableTypeReference.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableTypeReference.java index 2873bccd..128fbc4c 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableTypeReference.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/reference/ImmutableTypeReference.java @@ -34,14 +34,14 @@ package org.jf.dexlib2.immutable.reference; import com.google.common.collect.ImmutableList; import org.jf.dexlib2.base.reference.BaseTypeReference; import org.jf.dexlib2.iface.reference.TypeReference; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.List; public class ImmutableTypeReference extends BaseTypeReference implements ImmutableReference { - @Nonnull public final String type; + @Nonnull protected final String type; public ImmutableTypeReference(String type) { this.type = type; @@ -59,11 +59,11 @@ public class ImmutableTypeReference extends BaseTypeReference implements Immutab @Nonnull public static ImmutableList immutableListOf(@Nullable List list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull TypeReference item) { return item instanceof ImmutableTypeReference; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableAnnotation.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableAnnotation.java deleted file mode 100644 index 6c57ee51..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableAnnotation.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted; - -import com.google.common.collect.ImmutableSortedMultiset; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Multiset; -import org.jf.dexlib2.base.BaseAnnotation; -import org.jf.dexlib2.iface.Annotation; -import org.jf.dexlib2.iface.AnnotationElement; -import org.jf.dexlib2.iface.sorted.SortedAnnotation; -import org.jf.util.ImmutableSortedSetConverter; -import org.jf.util.ImmutableUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.Comparator; - -public class SortedImmutableAnnotation extends BaseAnnotation implements SortedAnnotation { - public final int visibility; - @Nonnull public final String type; - @Nonnull public final ImmutableSortedSet elements; - - public SortedImmutableAnnotation(int visibility, - @Nonnull String type, - @Nullable Collection elements) { - this.visibility = visibility; - this.type = type; - this.elements = SortedImmutableAnnotationElement.immutableSortedSetOf(elements); - } - - public SortedImmutableAnnotation( - int visibility, - @Nonnull String type, - @Nullable ImmutableSortedSet elements) { - this.visibility = visibility; - this.type = type; - this.elements = ImmutableUtils.nullToEmptySortedSet(elements); - } - - public static SortedImmutableAnnotation of(Annotation annotation) { - if (annotation instanceof SortedImmutableAnnotation) { - return (SortedImmutableAnnotation)annotation; - } - return new SortedImmutableAnnotation( - annotation.getVisibility(), - annotation.getType(), - annotation.getElements()); - } - - @Override public int getVisibility() { return visibility; } - @Nonnull @Override public String getType() { return type; } - @Nonnull @Override public ImmutableSortedSet getElements() { - return elements; - } - - public static final Comparator COMPARE_BY_TYPE = new Comparator() { - @Override - public int compare(Annotation annotation1, Annotation annotation2) { - return annotation1.getType().compareTo(annotation2.getType()); - } - }; - - @Nonnull - public static ImmutableSortedSet immutableSortedSetOf( - @Nullable Collection list) { - ImmutableSortedSet set = CONVERTER.convert(COMPARE_BY_TYPE, list); - if (list != null && set.size() < list.size()) { - // There were duplicate annotations. Let's find them and print a warning. - ImmutableSortedMultiset multiset = ImmutableSortedMultiset.copyOf(COMPARE_BY_TYPE, list); - for (Multiset.Entry entry: multiset.entrySet()) { - Annotation annotation = entry.getElement(); - // TODO: need to provide better context - System.err.println(String.format("Ignoring duplicate annotation definition for annotation type: %s", - annotation.getType())); - } - } - return set; - } - - private static final ImmutableSortedSetConverter CONVERTER = - new ImmutableSortedSetConverter() { - @Override - protected boolean isImmutable(@Nonnull Annotation item) { - return item instanceof SortedImmutableAnnotation; - } - - @Nonnull - @Override - protected SortedImmutableAnnotation makeImmutable(@Nonnull Annotation item) { - return SortedImmutableAnnotation.of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableAnnotationElement.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableAnnotationElement.java deleted file mode 100644 index a13b8d35..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableAnnotationElement.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted; - -import com.google.common.collect.ImmutableSortedMultiset; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Multiset; -import org.jf.dexlib2.base.BaseAnnotationElement; -import org.jf.dexlib2.iface.AnnotationElement; -import org.jf.dexlib2.iface.sorted.SortedAnnotationElement; -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; -import org.jf.dexlib2.iface.value.EncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValueFactory; -import org.jf.util.ImmutableSortedSetConverter; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.Comparator; - -public class SortedImmutableAnnotationElement extends BaseAnnotationElement implements SortedAnnotationElement { - @Nonnull public final String name; - @Nonnull public final SortedImmutableEncodedValue value; - - public SortedImmutableAnnotationElement(@Nonnull String name, - @Nonnull EncodedValue value) { - this.name = name; - this.value = SortedImmutableEncodedValueFactory.of(value); - } - - public SortedImmutableAnnotationElement(@Nonnull String name, - @Nonnull SortedImmutableEncodedValue value) { - this.name = name; - this.value = value; - } - - public static SortedImmutableAnnotationElement of(AnnotationElement annotationElement) { - if (annotationElement instanceof SortedImmutableAnnotationElement) { - return (SortedImmutableAnnotationElement)annotationElement; - } - return new SortedImmutableAnnotationElement( - annotationElement.getName(), - annotationElement.getValue()); - } - - @Nonnull @Override public String getName() { return name; } - @Nonnull @Override public SortedEncodedValue getValue() { return value; } - - public static final Comparator COMPARE_BY_NAME = new Comparator() { - @Override - public int compare(@Nonnull AnnotationElement element1, @Nonnull AnnotationElement element2) { - return element1.getName().compareTo(element2.getName()); - } - }; - - @Nonnull - public static ImmutableSortedSet immutableSortedSetOf( - @Nullable Collection list) { - ImmutableSortedSet set = CONVERTER.convert(COMPARE_BY_NAME, list); - if (list != null && set.size() < list.size()) { - // There were duplicate annotations. Let's find them and print a warning. - ImmutableSortedMultiset multiset = ImmutableSortedMultiset.copyOf(COMPARE_BY_NAME, list); - for (Multiset.Entry entry: multiset.entrySet()) { - AnnotationElement annotationElement = entry.getElement(); - // TODO: need to provide better context - System.err.println(String.format("Ignoring duplicate annotation value for name: %s", - annotationElement.getName())); - } - } - return set; - } - - private static final ImmutableSortedSetConverter CONVERTER = - new ImmutableSortedSetConverter() { - @Override - protected boolean isImmutable(@Nonnull AnnotationElement item) { - return item instanceof SortedImmutableAnnotationElement; - } - - @Nonnull - @Override - protected SortedImmutableAnnotationElement makeImmutable(@Nonnull AnnotationElement item) { - return SortedImmutableAnnotationElement.of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableClassDef.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableClassDef.java deleted file mode 100644 index 587610a3..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableClassDef.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSortedSet; -import org.jf.dexlib2.base.reference.BaseTypeReference; -import org.jf.dexlib2.iface.Annotation; -import org.jf.dexlib2.iface.ClassDef; -import org.jf.dexlib2.iface.Field; -import org.jf.dexlib2.iface.Method; -import org.jf.dexlib2.iface.sorted.SortedClassDef; -import org.jf.util.ImmutableListConverter; -import org.jf.util.ImmutableUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.List; - -public class SortedImmutableClassDef extends BaseTypeReference implements SortedClassDef { - @Nonnull public final String type; - public final int accessFlags; - @Nullable public final String superclass; - @Nonnull public final ImmutableList interfaces; - @Nullable public final String sourceFile; - @Nonnull public final ImmutableSortedSet annotations; - @Nonnull public final ImmutableSortedSet fields; - @Nonnull public final ImmutableSortedSet methods; - - public SortedImmutableClassDef(@Nonnull String type, - int accessFlags, - @Nullable String superclass, - @Nullable List interfaces, - @Nullable String sourceFile, - @Nullable Collection annotations, - @Nullable Collection fields, - @Nullable Collection methods) { - this.type = type; - this.accessFlags = accessFlags; - this.superclass = superclass; - this.interfaces = interfaces==null ? ImmutableList.of() : ImmutableList.copyOf(interfaces); - this.sourceFile = sourceFile; - this.annotations = SortedImmutableAnnotation.immutableSortedSetOf(annotations); - this.fields = SortedImmutableField.immutableSortedSetOf(fields); - this.methods = SortedImmutableMethod.immutableSortedSetOf(methods); - } - - public SortedImmutableClassDef(@Nonnull String type, - int accessFlags, - @Nullable String superclass, - @Nullable ImmutableList interfaces, - @Nullable String sourceFile, - @Nullable ImmutableSortedSet annotations, - @Nullable ImmutableSortedSet fields, - @Nullable ImmutableSortedSet methods) { - this.type = type; - this.accessFlags = accessFlags; - this.superclass = superclass; - this.interfaces = ImmutableUtils.nullToEmptyList(interfaces); - this.sourceFile = sourceFile; - this.annotations = ImmutableUtils.nullToEmptySortedSet(annotations); - this.fields = ImmutableUtils.nullToEmptySortedSet(fields); - this.methods = ImmutableUtils.nullToEmptySortedSet(methods); - } - - public static SortedImmutableClassDef of(ClassDef classDef) { - if (classDef instanceof SortedImmutableClassDef) { - return (SortedImmutableClassDef)classDef; - } - return new SortedImmutableClassDef( - classDef.getType(), - classDef.getAccessFlags(), - classDef.getSuperclass(), - classDef.getInterfaces(), - classDef.getSourceFile(), - classDef.getAnnotations(), - classDef.getFields(), - classDef.getMethods()); - } - - @Nonnull @Override public String getType() { return type; } - @Override public int getAccessFlags() { return accessFlags; } - @Nullable @Override public String getSuperclass() { return superclass; } - @Nonnull @Override public ImmutableList getInterfaces() { return interfaces; } - @Nullable @Override public String getSourceFile() { return sourceFile; } - @Nonnull @Override public ImmutableSortedSet getFields() { return fields; } - @Nonnull @Override public ImmutableSortedSet getMethods() { return methods; } - @Nonnull @Override public ImmutableSortedSet getAnnotations() { - return annotations; - } - - @Nonnull - public static ImmutableList immutableListOf(@Nullable List list) { - return CONVERTER.convert(list); - } - - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { - @Override - protected boolean isImmutable(@Nonnull ClassDef item) { - return item instanceof SortedImmutableClassDef; - } - - @Nonnull - @Override - protected SortedImmutableClassDef makeImmutable(@Nonnull ClassDef item) { - return SortedImmutableClassDef.of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableField.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableField.java deleted file mode 100644 index 8c730c9f..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableField.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted; - -import com.google.common.collect.ImmutableSortedMultiset; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Multiset; -import org.jf.dexlib2.AccessFlags; -import org.jf.dexlib2.base.reference.BaseFieldReference; -import org.jf.dexlib2.iface.Annotation; -import org.jf.dexlib2.iface.Field; -import org.jf.dexlib2.iface.reference.FieldReference; -import org.jf.dexlib2.iface.sorted.SortedField; -import org.jf.dexlib2.iface.value.EncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValueFactory; -import org.jf.util.ImmutableSortedSetConverter; -import org.jf.util.ImmutableUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.Comparator; - -public class SortedImmutableField extends BaseFieldReference implements SortedField { - @Nonnull public final String containingClass; - @Nonnull public final String name; - @Nonnull public final String type; - public final int accessFlags; - @Nullable public final SortedImmutableEncodedValue initialValue; - @Nonnull public final ImmutableSortedSet annotations; - - public SortedImmutableField(@Nonnull String containingClass, - @Nonnull String name, - @Nonnull String type, - int accessFlags, - @Nullable EncodedValue initialValue, - @Nullable Collection annotations) { - this.containingClass = containingClass; - this.name = name; - this.type = type; - this.accessFlags = accessFlags; - this.initialValue = SortedImmutableEncodedValueFactory.ofNullable(initialValue); - this.annotations = SortedImmutableAnnotation.immutableSortedSetOf(annotations); - } - - public SortedImmutableField(@Nonnull String containingClass, - @Nonnull String name, - @Nonnull String type, - int accessFlags, - @Nullable SortedImmutableEncodedValue initialValue, - @Nullable ImmutableSortedSet annotations) { - this.containingClass = containingClass; - this.name = name; - this.type = type; - this.accessFlags = accessFlags; - this.initialValue = initialValue; - this.annotations = ImmutableUtils.nullToEmptySortedSet(annotations); - } - - public static SortedImmutableField of(Field field) { - if (field instanceof SortedImmutableField) { - return (SortedImmutableField)field; - } - return new SortedImmutableField( - field.getContainingClass(), - field.getName(), - field.getType(), - field.getAccessFlags(), - field.getInitialValue(), - field.getAnnotations()); - } - - @Nonnull @Override public String getContainingClass() { return containingClass; } - @Nonnull @Override public String getName() { return name; } - @Nonnull @Override public String getType() { return type; } - @Override public int getAccessFlags() { return accessFlags; } - @Nullable @Override public SortedImmutableEncodedValue getInitialValue() { return initialValue;} - @Nonnull @Override public ImmutableSortedSet getAnnotations() { - return annotations; - } - - public static final Comparator COMPARE_BY_SIGNATURE = new Comparator() { - @Override - public int compare(FieldReference field1, FieldReference field2) { - int res = field1.getContainingClass().compareTo(field2.getContainingClass()); - if (res != 0) { - return res; - } - res = field1.getName().compareTo(field2.getName()); - if (res != 0) { - return res; - } - return field1.getType().compareTo(field2.getType()); - } - }; - - @Nonnull - public static ImmutableSortedSet immutableSortedSetOf( - @Nullable Collection list) { - ImmutableSortedSet set = CONVERTER.convert(COMPARE_BY_SIGNATURE, list); - if (list != null && set.size() < list.size()) { - // There were duplicate fields. Let's find them and print a warning. - ImmutableSortedMultiset multiset = ImmutableSortedMultiset.copyOf(COMPARE_BY_SIGNATURE, list); - for (Multiset.Entry entry: multiset.entrySet()) { - Field field = entry.getElement(); - String fieldType = AccessFlags.STATIC.isSet(field.getAccessFlags())?"static":"instance"; - // TODO: need to provide better context - System.err.println(String.format("Ignoring duplicate %s field definition for field: %s:%s", fieldType, - field.getName(), field.getType())); - } - } - return set; - } - - private static final ImmutableSortedSetConverter CONVERTER = - new ImmutableSortedSetConverter() { - @Override - protected boolean isImmutable(@Nonnull Field item) { - return item instanceof SortedImmutableField; - } - - @Nonnull - @Override - protected SortedImmutableField makeImmutable(@Nonnull Field item) { - return SortedImmutableField.of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableMethod.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableMethod.java deleted file mode 100644 index 9d555182..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableMethod.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSortedMultiset; -import com.google.common.collect.ImmutableSortedSet; -import com.google.common.collect.Multiset; -import org.jf.dexlib2.base.reference.BaseMethodReference; -import org.jf.dexlib2.iface.Annotation; -import org.jf.dexlib2.iface.Method; -import org.jf.dexlib2.iface.MethodImplementation; -import org.jf.dexlib2.iface.MethodParameter; -import org.jf.dexlib2.iface.reference.MethodReference; -import org.jf.dexlib2.iface.reference.TypeReference; -import org.jf.dexlib2.iface.sorted.SortedMethod; -import org.jf.dexlib2.iface.sorted.SortedMethodParameter; -import org.jf.dexlib2.immutable.ImmutableMethodImplementation; -import org.jf.dexlib2.util.MethodUtil; -import org.jf.dexlib2.util.ReferenceUtil; -import org.jf.util.ImmutableSortedSetConverter; -import org.jf.util.ImmutableUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; -import java.util.Comparator; -import java.util.Iterator; - -public class SortedImmutableMethod extends BaseMethodReference implements SortedMethod { - @Nonnull public final String containingClass; - @Nonnull public final String name; - @Nonnull public final ImmutableList parameters; - @Nonnull public final String returnType; - public final int accessFlags; - @Nonnull public final ImmutableSortedSet annotations; - @Nullable public final ImmutableMethodImplementation methodImplementation; - - public SortedImmutableMethod(@Nonnull String containingClass, - @Nonnull String name, - @Nullable Collection parameters, - @Nonnull String returnType, - int accessFlags, - @Nullable Collection annotations, - @Nullable MethodImplementation methodImplementation) { - this.containingClass = containingClass; - this.name = name; - this.parameters = SortedImmutableMethodParameter.immutableListOf(parameters); - this.returnType = returnType; - this.accessFlags = accessFlags; - this.annotations = SortedImmutableAnnotation.immutableSortedSetOf(annotations); - this.methodImplementation = ImmutableMethodImplementation.of(methodImplementation); - } - - public SortedImmutableMethod(@Nonnull String containingClass, - @Nonnull String name, - @Nullable ImmutableList parameters, - @Nonnull String returnType, - int accessFlags, - @Nullable ImmutableSortedSet annotations, - @Nullable ImmutableMethodImplementation methodImplementation) { - this.containingClass = containingClass; - this.name = name; - this.parameters = ImmutableUtils.nullToEmptyList(parameters); - this.returnType = returnType; - this.accessFlags = accessFlags; - this.annotations = ImmutableUtils.nullToEmptySortedSet(annotations); - this.methodImplementation = methodImplementation; - } - - public static SortedImmutableMethod of(Method method) { - if (method instanceof SortedImmutableMethod) { - return (SortedImmutableMethod)method; - } - return new SortedImmutableMethod( - method.getContainingClass(), - method.getName(), - method.getParameters(), - method.getReturnType(), - method.getAccessFlags(), - method.getAnnotations(), - method.getImplementation()); - } - - @Nonnull public String getContainingClass() { return containingClass; } - @Nonnull public String getName() { return name; } - @Nonnull public ImmutableList getParameters() { return parameters; } - @Nonnull public String getReturnType() { return returnType; } - public int getAccessFlags() { return accessFlags; } - @Nonnull public ImmutableSortedSet getAnnotations() { return annotations; } - @Nullable public ImmutableMethodImplementation getImplementation() { return methodImplementation; } - - public static final Comparator COMPARE_BY_SIGNATURE = new Comparator() { - @Override - public int compare(MethodReference method1, MethodReference method2) { - int res = method1.getContainingClass().compareTo(method2.getContainingClass()); - if (res != 0) { - return res; - } - res = method1.getName().compareTo(method2.getName()); - if (res != 0) { - return res; - } - res = method1.getReturnType().compareTo(method2.getReturnType()); - if (res != 0) { - return res; - } - Collection params1 = method1.getParameters(); - Collection params2 = method2.getParameters(); - int params1Size = params1.size(); - int params2Size = params2.size(); - - int minSize = Math.min(params1Size, params2Size); - - Iterator paramIter1 = params1.iterator(); - Iterator paramIter2 = params2.iterator(); - - for (int i=0; i params2Size) { - return 1; - } else { - return 0; - } - } - }; - - @Nonnull - public static ImmutableSortedSet immutableSortedSetOf( - @Nullable Collection list) { - ImmutableSortedSet set = CONVERTER.convert(COMPARE_BY_SIGNATURE, list); - if (list != null && set.size() < list.size()) { - // There were duplicate methods. Let's find them and print a warning. - ImmutableSortedMultiset multiset = ImmutableSortedMultiset.copyOf(COMPARE_BY_SIGNATURE, list); - for (Multiset.Entry entry: multiset.entrySet()) { - Method method = entry.getElement(); - String methodType = MethodUtil.isDirect(method)?"direct":"virtual"; - // TODO: need to provide better context - System.err.println(String.format("Ignoring duplicate %s method definition for method: %s", methodType, - ReferenceUtil.getMethodDescriptor(method))); - } - } - return set; - } - - private static final ImmutableSortedSetConverter CONVERTER = - new ImmutableSortedSetConverter() { - @Override - protected boolean isImmutable(@Nonnull Method item) { - return item instanceof SortedImmutableMethod; - } - - @Nonnull - @Override - protected SortedImmutableMethod makeImmutable(@Nonnull Method item) { - return SortedImmutableMethod.of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableMethodParameter.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableMethodParameter.java deleted file mode 100644 index 8b029c3d..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/SortedImmutableMethodParameter.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSortedSet; -import org.jf.dexlib2.base.reference.BaseTypeReference; -import org.jf.dexlib2.iface.Annotation; -import org.jf.dexlib2.iface.MethodParameter; -import org.jf.dexlib2.iface.sorted.SortedMethodParameter; -import org.jf.util.ImmutableListConverter; -import org.jf.util.ImmutableUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; - -public class SortedImmutableMethodParameter extends BaseTypeReference implements SortedMethodParameter { - @Nonnull public final String type; - @Nonnull public final ImmutableSortedSet annotations; - @Nullable public final String name; - - public SortedImmutableMethodParameter(@Nonnull String type, - @Nullable Collection annotations, - @Nullable String name) { - this.type = type; - this.annotations = SortedImmutableAnnotation.immutableSortedSetOf(annotations); - this.name = name; - } - - public SortedImmutableMethodParameter(@Nonnull String type, - @Nullable ImmutableSortedSet annotations, - @Nullable String name) { - this.type = type; - this.annotations = ImmutableUtils.nullToEmptySortedSet(annotations); - this.name = name; - } - - public static SortedImmutableMethodParameter of(MethodParameter methodParameter) { - if (methodParameter instanceof SortedImmutableMethodParameter) { - return (SortedImmutableMethodParameter)methodParameter; - } - return new SortedImmutableMethodParameter( - methodParameter.getType(), - methodParameter.getAnnotations(), - methodParameter.getName()); - } - - @Nonnull @Override public String getType() { return type; } - @Nullable @Override public String getName() { return name; } - @Nonnull @Override public ImmutableSortedSet getAnnotations() { - return annotations; - } - - //TODO: iterate over the annotations to get the signature - @Nullable @Override public String getSignature() { return null; } - - @Nonnull - public static ImmutableList immutableListOf( - @Nullable Iterable list) { - return CONVERTER.convert(list); - } - - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { - @Override - protected boolean isImmutable(@Nonnull MethodParameter item) { - return item instanceof SortedImmutableMethodParameter; - } - - @Nonnull - @Override - protected SortedImmutableMethodParameter makeImmutable(@Nonnull MethodParameter item) { - return SortedImmutableMethodParameter.of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableAnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableAnnotationEncodedValue.java deleted file mode 100644 index 61757c61..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableAnnotationEncodedValue.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted.value; - -import com.google.common.collect.ImmutableSortedSet; -import org.jf.dexlib2.base.value.BaseAnnotationEncodedValue; -import org.jf.dexlib2.iface.AnnotationElement; -import org.jf.dexlib2.iface.sorted.SortedAnnotationElement; -import org.jf.dexlib2.iface.sorted.value.SortedAnnotationEncodedValue; -import org.jf.dexlib2.iface.value.AnnotationEncodedValue; -import org.jf.dexlib2.immutable.sorted.SortedImmutableAnnotationElement; -import org.jf.util.ImmutableUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Collection; - -public class SortedImmutableAnnotationEncodedValue extends BaseAnnotationEncodedValue - implements SortedImmutableEncodedValue, SortedAnnotationEncodedValue { - @Nonnull public final String type; - @Nonnull public final ImmutableSortedSet elements; - - public SortedImmutableAnnotationEncodedValue(@Nonnull String type, - @Nullable Collection elements) { - this.type = type; - this.elements = SortedImmutableAnnotationElement.immutableSortedSetOf(elements); - } - - public SortedImmutableAnnotationEncodedValue( - @Nonnull String type, - @Nullable ImmutableSortedSet elements) { - this.type = type; - this.elements = ImmutableUtils.nullToEmptySortedSet(elements); - } - - public static SortedImmutableAnnotationEncodedValue of(AnnotationEncodedValue annotationEncodedValue) { - if (annotationEncodedValue instanceof SortedImmutableAnnotationEncodedValue) { - return (SortedImmutableAnnotationEncodedValue)annotationEncodedValue; - } - return new SortedImmutableAnnotationEncodedValue( - annotationEncodedValue.getType(), - annotationEncodedValue.getElements()); - } - - @Nonnull @Override public String getType() { return type; } - @Nonnull @Override public ImmutableSortedSet getElements() { - return elements; - } -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableEncodedValue.java deleted file mode 100644 index 6775a9f6..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableEncodedValue.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted.value; - -import org.jf.dexlib2.iface.sorted.value.SortedEncodedValue; -import org.jf.dexlib2.immutable.value.ImmutableEncodedValue; - -public interface SortedImmutableEncodedValue extends SortedEncodedValue, ImmutableEncodedValue { -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableEncodedValueFactory.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableEncodedValueFactory.java deleted file mode 100644 index 48994513..00000000 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/sorted/value/SortedImmutableEncodedValueFactory.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.dexlib2.immutable.sorted.value; - -import com.google.common.collect.ImmutableList; -import org.jf.dexlib2.ValueType; -import org.jf.dexlib2.iface.value.AnnotationEncodedValue; -import org.jf.dexlib2.iface.value.ArrayEncodedValue; -import org.jf.dexlib2.iface.value.EncodedValue; -import org.jf.dexlib2.immutable.value.ImmutableEncodedValueFactory; -import org.jf.util.ImmutableListConverter; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -public class SortedImmutableEncodedValueFactory { - @Nonnull - public static SortedImmutableEncodedValue of(@Nonnull EncodedValue encodedValue) { - switch (encodedValue.getValueType()) { - case ValueType.ARRAY: - return SortedImmutableArrayEncodedValue.of((ArrayEncodedValue)encodedValue); - case ValueType.ANNOTATION: - return SortedImmutableAnnotationEncodedValue.of((AnnotationEncodedValue)encodedValue); - default: - return (SortedImmutableEncodedValue)ImmutableEncodedValueFactory.of(encodedValue); - } - } - - @Nullable - public static SortedImmutableEncodedValue ofNullable(@Nullable EncodedValue encodedValue) { - if (encodedValue == null) { - return null; - } - return of(encodedValue); - } - - @Nonnull - public static ImmutableList immutableListOf( - @Nullable Iterable list) { - return CONVERTER.convert(list); - } - - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { - @Override - protected boolean isImmutable(@Nonnull EncodedValue item) { - return item instanceof SortedImmutableEncodedValue; - } - - @Nonnull - @Override - protected SortedImmutableEncodedValue makeImmutable(@Nonnull EncodedValue item) { - return of(item); - } - }; -} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java index 8185d551..98585e75 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableAnnotationEncodedValue.java @@ -31,7 +31,7 @@ package org.jf.dexlib2.immutable.value; -import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import org.jf.dexlib2.base.value.BaseAnnotationEncodedValue; import org.jf.dexlib2.iface.AnnotationElement; import org.jf.dexlib2.iface.value.AnnotationEncodedValue; @@ -42,21 +42,20 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Collection; -public class ImmutableAnnotationEncodedValue extends BaseAnnotationEncodedValue - implements ImmutableEncodedValue, AnnotationEncodedValue { - @Nonnull public final String type; - @Nonnull public final ImmutableList elements; +public class ImmutableAnnotationEncodedValue extends BaseAnnotationEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final String type; + @Nonnull protected final ImmutableSet elements; public ImmutableAnnotationEncodedValue(@Nonnull String type, @Nullable Collection elements) { this.type = type; - this.elements = ImmutableAnnotationElement.immutableListOf(elements); + this.elements = ImmutableAnnotationElement.immutableSetOf(elements); } public ImmutableAnnotationEncodedValue(@Nonnull String type, - @Nullable ImmutableList elements) { + @Nullable ImmutableSet elements) { this.type = type; - this.elements = ImmutableUtils.nullToEmptyList(elements); + this.elements = ImmutableUtils.nullToEmptySet(elements); } public static ImmutableAnnotationEncodedValue of(AnnotationEncodedValue annotationEncodedValue) { @@ -69,5 +68,5 @@ public class ImmutableAnnotationEncodedValue extends BaseAnnotationEncodedValue } @Nonnull @Override public String getType() { return type; } - @Nonnull @Override public ImmutableList getElements() { return elements; } + @Nonnull @Override public ImmutableSet getElements() { return elements; } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java index 5ff091df..75440491 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableArrayEncodedValue.java @@ -39,9 +39,8 @@ import org.jf.dexlib2.iface.value.EncodedValue; import javax.annotation.Nonnull; import java.util.Collection; -public class ImmutableArrayEncodedValue extends BaseArrayEncodedValue - implements ImmutableEncodedValue, ArrayEncodedValue { - @Nonnull public final ImmutableList value; +public class ImmutableArrayEncodedValue extends BaseArrayEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final ImmutableList value; public ImmutableArrayEncodedValue(@Nonnull Collection value) { this.value = ImmutableEncodedValueFactory.immutableListOf(value); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java index 30f8b156..4cd33881 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableBooleanEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseBooleanEncodedValue; import org.jf.dexlib2.iface.value.BooleanEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableBooleanEncodedValue extends BaseBooleanEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, BooleanEncodedValue { - public final boolean value; +public class ImmutableBooleanEncodedValue extends BaseBooleanEncodedValue implements ImmutableEncodedValue { + protected final boolean value; public ImmutableBooleanEncodedValue(boolean value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java index d5f87087..f5e7220f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableByteEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseByteEncodedValue; import org.jf.dexlib2.iface.value.ByteEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableByteEncodedValue extends BaseByteEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, ByteEncodedValue { - public final byte value; +public class ImmutableByteEncodedValue extends BaseByteEncodedValue implements ImmutableEncodedValue { + protected final byte value; public ImmutableByteEncodedValue(byte value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java index 4e806b65..ee064e4d 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableCharEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseCharEncodedValue; import org.jf.dexlib2.iface.value.CharEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableCharEncodedValue extends BaseCharEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, CharEncodedValue { - public final char value; +public class ImmutableCharEncodedValue extends BaseCharEncodedValue implements ImmutableEncodedValue { + protected final char value; public ImmutableCharEncodedValue(char value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java index e9b85900..d22d0f7a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableDoubleEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseDoubleEncodedValue; import org.jf.dexlib2.iface.value.DoubleEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableDoubleEncodedValue extends BaseDoubleEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, DoubleEncodedValue { - public final double value; +public class ImmutableDoubleEncodedValue extends BaseDoubleEncodedValue implements ImmutableEncodedValue { + protected final double value; public ImmutableDoubleEncodedValue(double value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValueFactory.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValueFactory.java index 46d596a0..e0cfd5a2 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValueFactory.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEncodedValueFactory.java @@ -35,7 +35,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.*; -import org.jf.util.ImmutableListConverter; +import org.jf.util.ImmutableConverter; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -93,11 +93,11 @@ public class ImmutableEncodedValueFactory { @Nonnull public static ImmutableList immutableListOf (@Nullable Iterable list) { - return CONVERTER.convert(list); + return CONVERTER.toList(list); } - private static final ImmutableListConverter CONVERTER = - new ImmutableListConverter() { + private static final ImmutableConverter CONVERTER = + new ImmutableConverter() { @Override protected boolean isImmutable(@Nonnull EncodedValue item) { return item instanceof ImmutableEncodedValue; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java index 1704cc14..2607f8a0 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableEnumEncodedValue.java @@ -34,13 +34,11 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseEnumEncodedValue; import org.jf.dexlib2.iface.reference.FieldReference; import org.jf.dexlib2.iface.value.EnumEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; import javax.annotation.Nonnull; -public class ImmutableEnumEncodedValue extends BaseEnumEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, EnumEncodedValue { - @Nonnull public final FieldReference value; +public class ImmutableEnumEncodedValue extends BaseEnumEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final FieldReference value; public ImmutableEnumEncodedValue(@Nonnull FieldReference value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java index 0ec6617a..691b6cfe 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFieldEncodedValue.java @@ -34,13 +34,11 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseFieldEncodedValue; import org.jf.dexlib2.iface.reference.FieldReference; import org.jf.dexlib2.iface.value.FieldEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; import javax.annotation.Nonnull; -public class ImmutableFieldEncodedValue extends BaseFieldEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, FieldEncodedValue { - @Nonnull public final FieldReference value; +public class ImmutableFieldEncodedValue extends BaseFieldEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final FieldReference value; public ImmutableFieldEncodedValue(@Nonnull FieldReference value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java index f1b9d4c6..e2078712 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableFloatEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseFloatEncodedValue; import org.jf.dexlib2.iface.value.FloatEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableFloatEncodedValue extends BaseFloatEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, FloatEncodedValue { - public final float value; +public class ImmutableFloatEncodedValue extends BaseFloatEncodedValue implements ImmutableEncodedValue { + protected final float value; public ImmutableFloatEncodedValue(float value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java index cf28f6a9..6b2014ba 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableIntEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseIntEncodedValue; import org.jf.dexlib2.iface.value.IntEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableIntEncodedValue extends BaseIntEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, IntEncodedValue { - public final int value; +public class ImmutableIntEncodedValue extends BaseIntEncodedValue implements ImmutableEncodedValue { + protected final int value; public ImmutableIntEncodedValue(int value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java index d0174d3f..097ec721 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableLongEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseLongEncodedValue; import org.jf.dexlib2.iface.value.LongEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableLongEncodedValue extends BaseLongEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, LongEncodedValue { - public final long value; +public class ImmutableLongEncodedValue extends BaseLongEncodedValue implements ImmutableEncodedValue { + protected final long value; public ImmutableLongEncodedValue(long value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java index 7502c73a..05b45809 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableMethodEncodedValue.java @@ -34,13 +34,11 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseMethodEncodedValue; import org.jf.dexlib2.iface.reference.MethodReference; import org.jf.dexlib2.iface.value.MethodEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; import javax.annotation.Nonnull; -public class ImmutableMethodEncodedValue extends BaseMethodEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, MethodEncodedValue { - @Nonnull public final MethodReference value; +public class ImmutableMethodEncodedValue extends BaseMethodEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final MethodReference value; public ImmutableMethodEncodedValue(@Nonnull MethodReference value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java index 85daf1c0..89f414e9 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableNullEncodedValue.java @@ -32,11 +32,8 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseNullEncodedValue; -import org.jf.dexlib2.iface.value.NullEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableNullEncodedValue extends BaseNullEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, NullEncodedValue { +public class ImmutableNullEncodedValue extends BaseNullEncodedValue implements ImmutableEncodedValue { public static final ImmutableNullEncodedValue INSTANCE = new ImmutableNullEncodedValue(); private ImmutableNullEncodedValue() {} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java index aa1dd95d..ceea56f6 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableShortEncodedValue.java @@ -33,11 +33,9 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseShortEncodedValue; import org.jf.dexlib2.iface.value.ShortEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; -public class ImmutableShortEncodedValue extends BaseShortEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, ShortEncodedValue { - public final short value; +public class ImmutableShortEncodedValue extends BaseShortEncodedValue implements ImmutableEncodedValue { + protected final short value; public ImmutableShortEncodedValue(short value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java index 85b28a7e..0795695b 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableStringEncodedValue.java @@ -33,13 +33,11 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseStringEncodedValue; import org.jf.dexlib2.iface.value.StringEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; import javax.annotation.Nonnull; -public class ImmutableStringEncodedValue extends BaseStringEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, StringEncodedValue { - @Nonnull public final String value; +public class ImmutableStringEncodedValue extends BaseStringEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final String value; public ImmutableStringEncodedValue(@Nonnull String value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java index 7541e236..df71978f 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/immutable/value/ImmutableTypeEncodedValue.java @@ -33,13 +33,11 @@ package org.jf.dexlib2.immutable.value; import org.jf.dexlib2.base.value.BaseTypeEncodedValue; import org.jf.dexlib2.iface.value.TypeEncodedValue; -import org.jf.dexlib2.immutable.sorted.value.SortedImmutableEncodedValue; import javax.annotation.Nonnull; -public class ImmutableTypeEncodedValue extends BaseTypeEncodedValue - implements ImmutableEncodedValue, SortedImmutableEncodedValue, TypeEncodedValue { - @Nonnull public final String value; +public class ImmutableTypeEncodedValue extends BaseTypeEncodedValue implements ImmutableEncodedValue { + @Nonnull protected final String value; public ImmutableTypeEncodedValue(@Nonnull String value) { this.value = value; diff --git a/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java b/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java index 2ceba0f9..e14e6896 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/util/Preconditions.java @@ -140,6 +140,11 @@ public class Preconditions { public static void checkValueArg(int valueArg, int maxValue) { if (valueArg > maxValue) { + if (maxValue == 0) { + throw new IllegalArgumentException( + String.format("Invalid value_arg value %d for an encoded_value. Expecting 0", + valueArg)); + } throw new IllegalArgumentException( String.format("Invalid value_arg value %d for an encoded_value. Expecting 0..%d, inclusive", valueArg, maxValue)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/util/ReferenceUtil.java b/dexlib2/src/main/java/org/jf/dexlib2/util/ReferenceUtil.java index 813d8c03..69a6ffd4 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/util/ReferenceUtil.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/util/ReferenceUtil.java @@ -42,7 +42,7 @@ public final class ReferenceUtil { public static String getMethodDescriptor(MethodReference methodReference) { // TODO: try using a thread local StringBuilder StringBuilder sb = new StringBuilder(); - sb.append(methodReference.getContainingClass()); + sb.append(methodReference.getDefiningClass()); sb.append("->"); sb.append(methodReference.getName()); sb.append('('); @@ -55,7 +55,7 @@ public final class ReferenceUtil { } public static void writeMethodDescriptor(Writer writer, MethodReference methodReference) throws IOException { - writer.write(methodReference.getContainingClass()); + writer.write(methodReference.getDefiningClass()); writer.write("->"); writer.write(methodReference.getName()); writer.write('('); @@ -69,7 +69,7 @@ public final class ReferenceUtil { public static String getFieldDescriptor(FieldReference fieldReference) { // TODO: try using a thread local StringBuilder StringBuilder sb = new StringBuilder(); - sb.append(fieldReference.getContainingClass()); + sb.append(fieldReference.getDefiningClass()); sb.append("->"); sb.append(fieldReference.getName()); sb.append(':'); @@ -87,7 +87,7 @@ public final class ReferenceUtil { } public static void writeFieldDescriptor(Writer writer, FieldReference fieldReference) throws IOException { - writer.write(fieldReference.getContainingClass()); + writer.write(fieldReference.getDefiningClass()); writer.write("->"); writer.write(fieldReference.getName()); writer.write(':'); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/util/SyntheticAccessorResolver.java b/dexlib2/src/main/java/org/jf/dexlib2/util/SyntheticAccessorResolver.java index bb6d8145..8a98e177 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/util/SyntheticAccessorResolver.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/util/SyntheticAccessorResolver.java @@ -68,7 +68,7 @@ public class SyntheticAccessorResolver { private final Map classDefMap; private final HashMap resolvedAccessors = new HashMap(); - public SyntheticAccessorResolver(List classDefs) { + public SyntheticAccessorResolver(Iterable classDefs) { ImmutableMap.Builder builder = ImmutableMap.builder(); for (ClassDef classDef: classDefs) { @@ -91,7 +91,7 @@ public class SyntheticAccessorResolver { return accessedMember; } - String type = methodReference.getContainingClass(); + String type = methodReference.getDefiningClass(); ClassDef classDef = classDefMap.get(type); if (classDef == null) { return null; diff --git a/util/src/main/java/org/jf/util/CollectionUtils.java b/util/src/main/java/org/jf/util/CollectionUtils.java new file mode 100644 index 00000000..6ecd6454 --- /dev/null +++ b/util/src/main/java/org/jf/util/CollectionUtils.java @@ -0,0 +1,137 @@ +/* + * Copyright 2012, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.util; + +import com.google.common.collect.ImmutableSortedSet; +import com.google.common.collect.Ordering; +import com.google.common.primitives.Ints; + +import javax.annotation.Nonnull; +import java.util.Collection; +import java.util.Comparator; +import java.util.Iterator; +import java.util.SortedSet; + +public class CollectionUtils { + public static > int compareAsList(@Nonnull Collection list1, + @Nonnull Collection list2) { + int res = Ints.compare(list1.size(), list2.size()); + if (res != 0) return res; + Iterator elements2 = list2.iterator(); + for (T element1: list1) { + res = element1.compareTo(elements2.next()); + if (res != 0) return res; + } + return 0; + } + + public static int compareAsList(@Nonnull Comparator elementComparator, + @Nonnull Collection list1, + @Nonnull Collection list2) { + int res = Ints.compare(list1.size(), list2.size()); + if (res != 0) return res; + Iterator elements2 = list2.iterator(); + for (T element1: list1) { + res = elementComparator.compare(element1, elements2.next()); + if (res != 0) return res; + } + return 0; + } + + public static Comparator> listComparator( + @Nonnull final Comparator elementComparator) { + return new Comparator>() { + @Override + public int compare(Collection list1, Collection list2) { + return compareAsList(elementComparator, list1, list2); + } + }; + } + + @Nonnull + private static SortedSet toNaturalSortedSet(@Nonnull Collection collection) { + if (collection instanceof SortedSet) { + SortedSet sortedSet = (SortedSet)collection; + Comparator comparator = sortedSet.comparator(); + if (comparator == null || comparator.equals(Ordering.natural())) { + return sortedSet; + } + } + return ImmutableSortedSet.copyOf(collection); + } + + @Nonnull + private static SortedSet toSortedSet(@Nonnull Comparator elementComparator, + @Nonnull Collection collection) { + if (collection instanceof SortedSet) { + SortedSet sortedSet = (SortedSet)collection; + Comparator comparator = sortedSet.comparator(); + if (comparator != null && comparator.equals(elementComparator)) { + return sortedSet; + } + } + return ImmutableSortedSet.copyOf(elementComparator, collection); + } + + public static > int compareAsSet(@Nonnull Collection set1, + @Nonnull Collection set2) { + int res = Ints.compare(set1.size(), set2.size()); + if (res != 0) return res; + + SortedSet sortedSet1 = toNaturalSortedSet(set1); + SortedSet sortedSet2 = toNaturalSortedSet(set2); + + Iterator elements2 = set2.iterator(); + for (T element1: set1) { + res = element1.compareTo(elements2.next()); + if (res != 0) return res; + } + return 0; + } + + public static int compareAsSet(@Nonnull Comparator elementComparator, + @Nonnull Collection list1, + @Nonnull Collection list2) { + int res = Ints.compare(list1.size(), list2.size()); + if (res != 0) return res; + + SortedSet set1 = toSortedSet(elementComparator, list1); + SortedSet set2 = toSortedSet(elementComparator, list2); + + Iterator elements2 = set2.iterator(); + for (T element1: set1) { + res = elementComparator.compare(element1, elements2.next()); + if (res != 0) return res; + } + return 0; + } +} diff --git a/util/src/main/java/org/jf/util/ImmutableSortedSetConverter.java b/util/src/main/java/org/jf/util/ImmutableConverter.java similarity index 56% rename from util/src/main/java/org/jf/util/ImmutableSortedSetConverter.java rename to util/src/main/java/org/jf/util/ImmutableConverter.java index 8d1f56c8..d8d88002 100644 --- a/util/src/main/java/org/jf/util/ImmutableSortedSetConverter.java +++ b/util/src/main/java/org/jf/util/ImmutableConverter.java @@ -31,6 +31,8 @@ package org.jf.util; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import javax.annotation.Nonnull; @@ -39,11 +41,75 @@ import java.util.Comparator; import java.util.Iterator; import java.util.SortedSet; -public abstract class ImmutableSortedSetConverter { +public abstract class ImmutableConverter { + protected abstract boolean isImmutable(@Nonnull Item item); + @Nonnull protected abstract ImmutableItem makeImmutable(@Nonnull Item item); @Nonnull - public ImmutableSortedSet convert(@Nonnull Comparator comparator, - @Nullable final Iterable iterable) { + public ImmutableList toList(@Nullable final Iterable iterable) { + if (iterable == null) { + return ImmutableList.of(); + } + + boolean needsCopy = false; + if (iterable instanceof ImmutableList) { + for (Item element: iterable) { + if (!isImmutable(element)) { + needsCopy = true; + break; + } + } + } else { + needsCopy = true; + } + + if (!needsCopy) { + return (ImmutableList)iterable; + } + + final Iterator iter = iterable.iterator(); + + return ImmutableList.copyOf(new Iterator() { + @Override public boolean hasNext() { return iter.hasNext(); } + @Override public ImmutableItem next() { return makeImmutable(iter.next()); } + @Override public void remove() { iter.remove(); } + }); + } + + @Nonnull + public ImmutableSet toSet(@Nullable final Iterable iterable) { + if (iterable == null) { + return ImmutableSet.of(); + } + + boolean needsCopy = false; + if (iterable instanceof ImmutableSet) { + for (Item element: iterable) { + if (!isImmutable(element)) { + needsCopy = true; + break; + } + } + } else { + needsCopy = true; + } + + if (!needsCopy) { + return (ImmutableSet)iterable; + } + + final Iterator iter = iterable.iterator(); + + return ImmutableSet.copyOf(new Iterator() { + @Override public boolean hasNext() { return iter.hasNext(); } + @Override public ImmutableItem next() { return makeImmutable(iter.next()); } + @Override public void remove() { iter.remove(); } + }); + } + + @Nonnull + public ImmutableSortedSet toSortedSet(@Nonnull Comparator comparator, + @Nullable final Iterable iterable) { if (iterable == null) { return ImmutableSortedSet.of(); } @@ -76,8 +142,8 @@ public abstract class ImmutableSortedSetConverter { } @Nonnull - public SortedSet convert(@Nonnull Comparator comparator, - @Nullable final SortedSet sortedSet) { + public SortedSet toSortedSet(@Nonnull Comparator comparator, + @Nullable final SortedSet sortedSet) { if (sortedSet == null || sortedSet.size() == 0) { return ImmutableSortedSet.of(); } @@ -91,7 +157,4 @@ public abstract class ImmutableSortedSetConverter { return ArraySortedSet.of(comparator, newItems); } - - protected abstract boolean isImmutable(@Nonnull Item item); - @Nonnull protected abstract ImmutableItem makeImmutable(@Nonnull Item item); } diff --git a/util/src/main/java/org/jf/util/ImmutableListConverter.java b/util/src/main/java/org/jf/util/ImmutableListConverter.java deleted file mode 100644 index 16a28502..00000000 --- a/util/src/main/java/org/jf/util/ImmutableListConverter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.util; - -import com.google.common.collect.ImmutableList; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Iterator; - -/** - * This class converts a list of items to an immutable list of immutable items - * - * @param The immutable version of the element - * @param The normal version of the element - */ -public abstract class ImmutableListConverter { - /** - * Converts a {@code List} of {@code Item}s to an {@code ImmutableList} of {@code ImmutableItem}s. - * - * If the provided list is already an ImmutableList of ImmutableItems, then the list is not copied and is returned - * as-is. If the list is null, an empty ImmutableList will be returned - * - * @param iterable The iterable of items to convert. - * @return An ImmutableList of ImmutableItem. If list is null, an empty list will be returned. - */ - @Nonnull - public ImmutableList convert(@Nullable final Iterable iterable) { - if (iterable == null) { - return ImmutableList.of(); - } - - boolean needsCopy = false; - if (iterable instanceof ImmutableList) { - for (Item element: iterable) { - if (!isImmutable(element)) { - needsCopy = true; - break; - } - } - } else { - needsCopy = true; - } - - if (!needsCopy) { - return (ImmutableList)iterable; - } - - final Iterator iter = iterable.iterator(); - - return ImmutableList.copyOf(new Iterator() { - @Override public boolean hasNext() { return iter.hasNext(); } - @Override public ImmutableItem next() { return makeImmutable(iter.next()); } - @Override public void remove() { iter.remove(); } - }); - } - - protected abstract boolean isImmutable(@Nonnull Item item); - @Nonnull protected abstract ImmutableItem makeImmutable(@Nonnull Item item); -} diff --git a/util/src/main/java/org/jf/util/ImmutableUtils.java b/util/src/main/java/org/jf/util/ImmutableUtils.java index d2eea7c0..8f4371fa 100644 --- a/util/src/main/java/org/jf/util/ImmutableUtils.java +++ b/util/src/main/java/org/jf/util/ImmutableUtils.java @@ -32,6 +32,7 @@ package org.jf.util; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import javax.annotation.Nonnull; @@ -45,6 +46,13 @@ public class ImmutableUtils { return list; } + @Nonnull public static ImmutableSet nullToEmptySet(@Nullable ImmutableSet set) { + if (set == null) { + return ImmutableSet.of(); + } + return set; + } + @Nonnull public static ImmutableSortedSet nullToEmptySortedSet(@Nullable ImmutableSortedSet set) { if (set == null) { return ImmutableSortedSet.of();