From a6e22fc9b70ebe39abd716ce37450bda935c0fb8 Mon Sep 17 00:00:00 2001 From: Ian Rogers Date: Thu, 19 Sep 2013 02:51:22 -0700 Subject: Avoid computing class def indices. Bug: 10244719 Also tidy AnnotationAccess. (cherry-picked from 8b2c0b9abc3f520495f4387ea040132ba85cae69) Change-Id: I6ec8fd4e36b428d7e16e01d98b7bebc143fac8c3 Conflicts: libdvm/src/main/java/java/lang/Class.java --- dex/src/main/java/com/android/dex/Dex.java | 76 +++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'dex') diff --git a/dex/src/main/java/com/android/dex/Dex.java b/dex/src/main/java/com/android/dex/Dex.java index 89edf6e1b..116a33c24 100644 --- a/dex/src/main/java/com/android/dex/Dex.java +++ b/dex/src/main/java/com/android/dex/Dex.java @@ -357,6 +357,19 @@ public final class Dex { return Collections.binarySearch(methodIds, methodId); } + public int findClassDefIndexFromTypeIndex(int typeIndex) { + checkBounds(typeIndex, tableOfContents.typeIds.size); + if (!tableOfContents.classDefs.exists()) { + return -1; + } + for (int i = 0; i < tableOfContents.classDefs.size; i++) { + if (typeIndexFromClassDefIndex(i) == typeIndex) { + return i; + } + } + return -1; + } + /** * Look up a field id type index from a field index. Cheaper than: * {@code fieldIds().get(fieldDexIndex).getTypeIndex();} @@ -368,6 +381,16 @@ public final class Dex { return data.getShort(position) & 0xFFFF; // typeIndex } + /** + * Look up a method id declaring class index from a method index. Cheaper than: + * {@code methodIds().get(methodIndex).getDeclaringClassIndex();} + */ + public int declaringClassIndexFromMethodIndex(int methodIndex) { + checkBounds(methodIndex, tableOfContents.methodIds.size); + int position = tableOfContents.methodIds.off + (SizeOf.MEMBER_ID_ITEM * methodIndex); + return data.getShort(position) & 0xFFFF; // declaringClassIndex + } + /** * Look up a method id name index from a method index. Cheaper than: * {@code methodIds().get(methodIndex).getNameIndex();} @@ -430,12 +453,63 @@ public final class Dex { * Look up a descriptor index from a type index. Cheaper than: * {@code open(tableOfContents.typeIds.off + (index * SizeOf.TYPE_ID_ITEM)).readInt();} */ - private int descriptorIndexFromTypeIndex(int typeIndex) { + public int descriptorIndexFromTypeIndex(int typeIndex) { checkBounds(typeIndex, tableOfContents.typeIds.size); int position = tableOfContents.typeIds.off + (SizeOf.TYPE_ID_ITEM * typeIndex); return data.getInt(position); } + /** + * Look up a type index index from a class def index. + */ + public int typeIndexFromClassDefIndex(int classDefIndex) { + checkBounds(classDefIndex, tableOfContents.classDefs.size); + int position = tableOfContents.classDefs.off + (SizeOf.CLASS_DEF_ITEM * classDefIndex); + return data.getInt(position); + } + + /** + * Look up a type index index from a class def index. + */ + public int annotationDirectoryOffsetFromClassDefIndex(int classDefIndex) { + checkBounds(classDefIndex, tableOfContents.classDefs.size); + int position = tableOfContents.classDefs.off + (SizeOf.CLASS_DEF_ITEM * classDefIndex); + position += SizeOf.UINT; // type + position += SizeOf.UINT; // accessFlags + position += SizeOf.UINT; // superType + position += SizeOf.UINT; // interfacesOffset + position += SizeOf.UINT; // sourceFileIndex + return data.getInt(position); + } + + /** + * Look up interface types indices from a return type index from a method index. Cheaper than: + * {@code ...getClassDef(classDefIndex).getInterfaces();} + */ + public short[] interfaceTypeIndicesFromClassDefIndex(int classDefIndex) { + checkBounds(classDefIndex, tableOfContents.classDefs.size); + int position = tableOfContents.classDefs.off + (SizeOf.CLASS_DEF_ITEM * classDefIndex); + position += SizeOf.UINT; // type + position += SizeOf.UINT; // accessFlags + position += SizeOf.UINT; // superType + int interfacesOffset = data.getInt(position); + if (interfacesOffset == 0) { + return EMPTY_SHORT_ARRAY; + } + position = interfacesOffset; + int size = data.getInt(position); + if (size <= 0) { + throw new AssertionError("Unexpected interfaces list size: " + size); + } + position += SizeOf.UINT; + short[] types = new short[size]; + for (int i = 0; i < size; i++) { + types[i] = data.getShort(position); + position += SizeOf.USHORT; + } + return types; + } + public final class Section implements ByteInput, ByteOutput { private final String name; private final ByteBuffer data; -- cgit v1.2.3