diff options
author | Brian Carlstrom <bdc@google.com> | 2013-04-29 18:26:09 -0700 |
---|---|---|
committer | Brian Carlstrom <bdc@google.com> | 2013-04-29 18:26:09 -0700 |
commit | fd2a1f6f7f56fb9e98bb4c1023ccdc7f7ce6c5d2 (patch) | |
tree | c2cf6896f85eeb983a3ad355d7b384577e53efc7 | |
parent | 7e85b634fbafa4a3b927c80665dea6fac337d886 (diff) | |
download | android_dalvik-fd2a1f6f7f56fb9e98bb4c1023ccdc7f7ce6c5d2.tar.gz android_dalvik-fd2a1f6f7f56fb9e98bb4c1023ccdc7f7ce6c5d2.tar.bz2 android_dalvik-fd2a1f6f7f56fb9e98bb4c1023ccdc7f7ce6c5d2.zip |
Change com.android.dx.io.Annotation to use EncodedValue
(cherry picked from commit 4ec022b0b6c84f994ab2fc78934110ad96d0bdec)
Change-Id: Ia420b7661f372c59af8fe62bdab7982a1d2b008f
-rw-r--r-- | dx/src/com/android/dx/io/Annotation.java | 73 | ||||
-rw-r--r-- | dx/src/com/android/dx/io/DexBuffer.java | 17 | ||||
-rw-r--r-- | dx/src/com/android/dx/io/EncodedValueReader.java | 9 | ||||
-rw-r--r-- | dx/src/com/android/dx/merge/IndexMap.java | 30 |
4 files changed, 47 insertions, 82 deletions
diff --git a/dx/src/com/android/dx/io/Annotation.java b/dx/src/com/android/dx/io/Annotation.java index d3be59271..ed2446ee9 100644 --- a/dx/src/com/android/dx/io/Annotation.java +++ b/dx/src/com/android/dx/io/Annotation.java @@ -16,7 +16,7 @@ package com.android.dx.io; -import com.android.dx.util.Unsigned; +import static com.android.dx.io.EncodedValueReader.ENCODED_ANNOTATION; /** * An annotation. @@ -24,81 +24,40 @@ import com.android.dx.util.Unsigned; public final class Annotation implements Comparable<Annotation> { private final DexBuffer buffer; private final byte visibility; - private final int typeIndex; - private final int[] names; - private final EncodedValue[] values; + private final EncodedValue encodedAnnotation; - public Annotation(DexBuffer buffer, byte visibility, int typeIndex, int[] names, - EncodedValue[] values) { - this.buffer = buffer; + public Annotation(DexBuffer dexBuffer, byte visibility, EncodedValue encodedAnnotation) { + this.buffer = dexBuffer; this.visibility = visibility; - this.typeIndex = typeIndex; - this.names = names; - this.values = values; + this.encodedAnnotation = encodedAnnotation; } public byte getVisibility() { return visibility; } - public int getTypeIndex() { - return typeIndex; - } - - public int[] getNames() { - return names; + public EncodedValueReader getReader() { + return new EncodedValueReader(encodedAnnotation, ENCODED_ANNOTATION); } - public EncodedValue[] getValues() { - return values; + public int getTypeIndex() { + EncodedValueReader reader = getReader(); + reader.readAnnotation(); + return reader.getAnnotationType(); } public void writeTo(DexBuffer.Section out) { out.writeByte(visibility); - out.writeUleb128(typeIndex); - out.writeUleb128(names.length); - for (int i = 0; i < names.length; i++) { - out.writeUleb128(names[i]); - values[i].writeTo(out); - } + encodedAnnotation.writeTo(out); } @Override public int compareTo(Annotation other) { - if (typeIndex != other.typeIndex) { - return Unsigned.compare(typeIndex, other.typeIndex); - } - int size = Math.min(names.length, other.names.length); - for (int i = 0; i < size; i++) { - if (names[i] != other.names[i]) { - return Unsigned.compare(names[i], other.names[i]); - } - int compare = values[i].compareTo(other.values[i]); - if (compare != 0) { - return compare; - } - } - return names.length - other.names.length; + return encodedAnnotation.compareTo(other.encodedAnnotation); } @Override public String toString() { - if (buffer == null) { - return visibility + " " + typeIndex; - } - - StringBuilder result = new StringBuilder(); - result.append(visibility); - result.append(" "); - result.append(buffer.typeNames().get(typeIndex)); - result.append("["); - for (int i = 0; i < names.length; i++) { - if (i > 0) { - result.append(", "); - } - result.append(buffer.strings().get(names[i])); - result.append("="); - result.append(values[i]); - } - result.append("]"); - return result.toString(); + return buffer == null + ? visibility + " " + getTypeIndex() + : visibility + " " + buffer.typeNames().get(getTypeIndex()); } } diff --git a/dx/src/com/android/dx/io/DexBuffer.java b/dx/src/com/android/dx/io/DexBuffer.java index c338ef43e..2ea597efb 100644 --- a/dx/src/com/android/dx/io/DexBuffer.java +++ b/dx/src/com/android/dx/io/DexBuffer.java @@ -552,22 +552,11 @@ public final class DexBuffer { public Annotation readAnnotation() { byte visibility = readByte(); - int typeIndex = readUleb128(); - int size = readUleb128(); - int[] names = new int[size]; - EncodedValue[] values = new EncodedValue[size]; - for (int i = 0; i < size; i++) { - names[i] = readUleb128(); - values[i] = readEncodedValue(); - } - return new Annotation(DexBuffer.this, visibility, typeIndex, names, values); - } - - public EncodedValue readEncodedValue() { int start = position; - new EncodedValueReader(this).skipValue(); + new EncodedValueReader(this, EncodedValueReader.ENCODED_ANNOTATION).skipValue(); int end = position; - return new EncodedValue(Arrays.copyOfRange(data, start, end)); + return new Annotation(DexBuffer.this, visibility, + new EncodedValue(Arrays.copyOfRange(data, start, end))); } public EncodedValue readEncodedArray() { diff --git a/dx/src/com/android/dx/io/EncodedValueReader.java b/dx/src/com/android/dx/io/EncodedValueReader.java index c53e5b892..fb928cc25 100644 --- a/dx/src/com/android/dx/io/EncodedValueReader.java +++ b/dx/src/com/android/dx/io/EncodedValueReader.java @@ -60,14 +60,19 @@ public final class EncodedValueReader { /** * Creates a new encoded value reader whose only value is the specified - * known type. This is useful for class_def_item, which references an - * encoded array that doesn't contain a type+arg prefix. + * known type. This is useful for encoded values without a type prefix, + * such as class_def_item's encoded_array or annotation_item's + * encoded_annotation. */ public EncodedValueReader(ByteInput in, int knownType) { this.in = in; this.type = knownType; } + public EncodedValueReader(EncodedValue in, int knownType) { + this(in.asByteInput(), knownType); + } + /** * Returns the type of the next value to read. */ diff --git a/dx/src/com/android/dx/merge/IndexMap.java b/dx/src/com/android/dx/merge/IndexMap.java index 0a870cfae..9d6ef7abb 100644 --- a/dx/src/com/android/dx/merge/IndexMap.java +++ b/dx/src/com/android/dx/merge/IndexMap.java @@ -22,7 +22,22 @@ import com.android.dx.io.ClassDef; import com.android.dx.io.DexBuffer; import com.android.dx.io.EncodedValue; import com.android.dx.io.EncodedValueReader; -import static com.android.dx.io.EncodedValueReader.*; +import static com.android.dx.io.EncodedValueReader.ENCODED_ANNOTATION; +import static com.android.dx.io.EncodedValueReader.ENCODED_ARRAY; +import static com.android.dx.io.EncodedValueReader.ENCODED_BOOLEAN; +import static com.android.dx.io.EncodedValueReader.ENCODED_BYTE; +import static com.android.dx.io.EncodedValueReader.ENCODED_CHAR; +import static com.android.dx.io.EncodedValueReader.ENCODED_DOUBLE; +import static com.android.dx.io.EncodedValueReader.ENCODED_ENUM; +import static com.android.dx.io.EncodedValueReader.ENCODED_FIELD; +import static com.android.dx.io.EncodedValueReader.ENCODED_FLOAT; +import static com.android.dx.io.EncodedValueReader.ENCODED_INT; +import static com.android.dx.io.EncodedValueReader.ENCODED_LONG; +import static com.android.dx.io.EncodedValueReader.ENCODED_METHOD; +import static com.android.dx.io.EncodedValueReader.ENCODED_NULL; +import static com.android.dx.io.EncodedValueReader.ENCODED_SHORT; +import static com.android.dx.io.EncodedValueReader.ENCODED_STRING; +import static com.android.dx.io.EncodedValueReader.ENCODED_TYPE; import com.android.dx.io.FieldId; import com.android.dx.io.MethodId; import com.android.dx.io.ProtoId; @@ -216,19 +231,16 @@ public final class IndexMap { public EncodedValue adjustEncodedArray(EncodedValue encodedArray) { ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(32); new EncodedValueTransformer(out).transformArray( - new EncodedValueReader(encodedArray.asByteInput(), ENCODED_ARRAY)); + new EncodedValueReader(encodedArray, ENCODED_ARRAY)); return new EncodedValue(out.toByteArray()); } public Annotation adjust(Annotation annotation) { - int[] names = annotation.getNames().clone(); - EncodedValue[] values = annotation.getValues().clone(); - for (int i = 0; i < names.length; i++) { - names[i] = adjustString(names[i]); - values[i] = adjustEncodedValue(values[i]); - } + ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(32); + new EncodedValueTransformer(out).transformAnnotation( + annotation.getReader()); return new Annotation(target, annotation.getVisibility(), - adjustType(annotation.getTypeIndex()), names, values); + new EncodedValue(out.toByteArray())); } /** |