/** * Gets the index of the given type, which must have * been added to this instance. * * @param type {@code non-null;} the type to look up * @return {@code >= 0;} the reference's index */ public int indexOf(CstType type) { if (type == null) { throw new NullPointerException("type == null"); } return indexOf(type.getClassType()); }
/** {@inheritDoc} */ @Override public void addContents(DexFile file) { TypeIdsSection typeIds = file.getTypeIds(); typeIds.intern(type); }
/** {@inheritDoc} */ @Override protected void orderItems() { int idx = 0; for (Object i : items()) { ((TypeIdItem) i).setIndex(idx); idx++; } } }
/** * Writes the portion of the file header that refers to this instance. * * @param out {@code non-null;} where to write */ public void writeHeaderPart(AnnotatedOutput out) { throwIfNotPrepared(); int sz = typeIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (sz > 65536) { throw new UnsupportedOperationException("too many type ids"); } if (out.annotates()) { out.annotate(4, "type_ids_size: " + Hex.u4(sz)); out.annotate(4, "type_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** * Writes the portion of the file header that refers to this instance. * * @param out {@code non-null;} where to write */ public void writeHeaderPart(AnnotatedOutput out) { throwIfNotPrepared(); int sz = typeIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (sz > DexFormat.MAX_TYPE_IDX + 1) { throw new DexIndexOverflowException( String.format("Too many type identifiers to fit in one dex file: %1$d; max is %2$d.%n" + "You may try using multi-dex. If multi-dex is enabled then the list of " + "classes for the main dex list is too large.", items().size(), DexFormat.MAX_MEMBER_IDX + 1)); } if (out.annotates()) { out.annotate(4, "type_ids_size: " + Hex.u4(sz)); out.annotate(4, "type_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** * Interns an element into this instance. * * @param type {@code non-null;} the type to intern * @return {@code non-null;} the interned reference */ public synchronized TypeIdItem intern(CstType type) { if (type == null) { throw new NullPointerException("type == null"); } throwIfPrepared(); Type typePerSe = type.getClassType(); TypeIdItem result = typeIds.get(typePerSe); if (result == null) { result = new TypeIdItem(type); typeIds.put(typePerSe, result); } return result; }
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); Type type = ((CstType) cst).getClassType(); IndexedItem result = typeIds.get(type); if (result == null) { throw new IllegalArgumentException("not found: " + cst); } return result; }
/** * Gets the {@link IndexedItem} corresponding to the given constant, * if it is a constant that has such a correspondence, or return * {@code null} if it isn't such a constant. This will throw * an exception if the given constant <i>should</i> have been found * but wasn't. * * @param cst {@code non-null;} the constant to look up * @return {@code null-ok;} its corresponding item, if it has a corresponding * item, or {@code null} if it's not that sort of constant */ /*package*/ IndexedItem findItemOrNull(Constant cst) { IndexedItem item; if (cst instanceof CstString) { return stringIds.get(cst); } else if (cst instanceof CstType) { return typeIds.get(cst); } else if (cst instanceof CstBaseMethodRef) { return methodIds.get(cst); } else if (cst instanceof CstFieldRef) { return fieldIds.get(cst); } else { return null; } }
/** * Constructs an instance. It is initially empty. */ public DexFile(DexOptions dexOptions) { this.dexOptions = dexOptions; header = new HeaderSection(this); typeLists = new MixedItemSection(null, this, 4, SortType.NONE); wordData = new MixedItemSection("word_data", this, 4, SortType.TYPE); stringData = new MixedItemSection("string_data", this, 1, SortType.INSTANCE); classData = new MixedItemSection(null, this, 1, SortType.NONE); byteData = new MixedItemSection("byte_data", this, 1, SortType.TYPE); stringIds = new StringIdsSection(this); typeIds = new TypeIdsSection(this); protoIds = new ProtoIdsSection(this); fieldIds = new FieldIdsSection(this); methodIds = new MethodIdsSection(this); classDefs = new ClassDefsSection(this); map = new MixedItemSection("map", this, 4, SortType.NONE); /* * This is the list of sections in the order they appear in * the final output. */ sections = new Section[] { header, stringIds, typeIds, protoIds, fieldIds, methodIds, classDefs, wordData, typeLists, stringData, byteData, classData, map }; fileSize = -1; dumpWidth = 79; }
file.getTypeIds().writeHeaderPart(out); file.getProtoIds().writeHeaderPart(out); file.getFieldIds().writeHeaderPart(out);
protoIds.prepare(); typeLists.prepare(); typeIds.prepare(); stringIds.prepare(); stringData.prepare();
/** * Writes the portion of the file header that refers to this instance. * * @param out {@code non-null;} where to write */ public void writeHeaderPart(AnnotatedOutput out) { throwIfNotPrepared(); int sz = typeIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (sz > 65536) { throw new UnsupportedOperationException("too many type ids"); } if (out.annotates()) { out.annotate(4, "type_ids_size: " + Hex.u4(sz)); out.annotate(4, "type_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** * Writes the portion of the file header that refers to this instance. * * @param out {@code non-null;} where to write */ public void writeHeaderPart(AnnotatedOutput out) { throwIfNotPrepared(); int sz = typeIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (sz > DexFormat.MAX_TYPE_IDX + 1) { throw new DexIndexOverflowException( String.format("Too many type identifiers to fit in one dex file: %1$d; max is %2$d.%n" + "You may try using multi-dex. If multi-dex is enabled then the list of " + "classes for the main dex list is too large.", items().size(), DexFormat.MAX_MEMBER_IDX + 1)); } if (out.annotates()) { out.annotate(4, "type_ids_size: " + Hex.u4(sz)); out.annotate(4, "type_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** * Interns an element into this instance. * * @param type {@code non-null;} the type to intern * @return {@code non-null;} the interned reference */ public TypeIdItem intern(CstType type) { if (type == null) { throw new NullPointerException("type == null"); } throwIfPrepared(); Type typePerSe = type.getClassType(); TypeIdItem result = typeIds.get(typePerSe); if (result == null) { result = new TypeIdItem(type); typeIds.put(typePerSe, result); } return result; }
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); Type type = ((CstType) cst).getClassType(); IndexedItem result = typeIds.get(type); if (result == null) { throw new IllegalArgumentException("not found: " + cst); } return result; }
/** * Gets the {@link IndexedItem} corresponding to the given constant, * if it is a constant that has such a correspondence, or return * {@code null} if it isn't such a constant. This will throw * an exception if the given constant <i>should</i> have been found * but wasn't. * * @param cst {@code non-null;} the constant to look up * @return {@code null-ok;} its corresponding item, if it has a corresponding * item, or {@code null} if it's not that sort of constant */ /*package*/ IndexedItem findItemOrNull(Constant cst) { IndexedItem item; if (cst instanceof CstString) { return stringIds.get(cst); } else if (cst instanceof CstType) { return typeIds.get(cst); } else if (cst instanceof CstBaseMethodRef) { return methodIds.get(cst); } else if (cst instanceof CstFieldRef) { return fieldIds.get(cst); } else { return null; } }
/** * Constructs an instance. It is initially empty. */ public DexFile(DexOptions dexOptions) { this.dexOptions = dexOptions; header = new HeaderSection(this); typeLists = new MixedItemSection(null, this, 4, SortType.NONE); wordData = new MixedItemSection("word_data", this, 4, SortType.TYPE); stringData = new MixedItemSection("string_data", this, 1, SortType.INSTANCE); classData = new MixedItemSection(null, this, 1, SortType.NONE); byteData = new MixedItemSection("byte_data", this, 1, SortType.TYPE); stringIds = new StringIdsSection(this); typeIds = new TypeIdsSection(this); protoIds = new ProtoIdsSection(this); fieldIds = new FieldIdsSection(this); methodIds = new MethodIdsSection(this); classDefs = new ClassDefsSection(this); map = new MixedItemSection("map", this, 4, SortType.NONE); /* * This is the list of sections in the order they appear in * the final output. */ sections = new Section[] { header, stringIds, typeIds, protoIds, fieldIds, methodIds, classDefs, wordData, typeLists, stringData, byteData, classData, map }; fileSize = -1; dumpWidth = 79; }
file.getTypeIds().writeHeaderPart(out); file.getProtoIds().writeHeaderPart(out); file.getFieldIds().writeHeaderPart(out);
protoIds.prepare(); typeLists.prepare(); typeIds.prepare(); stringIds.prepare(); stringData.prepare();
/** * Writes the portion of the file header that refers to this instance. * * @param out {@code non-null;} where to write */ public void writeHeaderPart(AnnotatedOutput out) { throwIfNotPrepared(); int sz = typeIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (sz > 65536) { throw new UnsupportedOperationException("too many type ids"); } if (out.annotates()) { out.annotate(4, "type_ids_size: " + Hex.u4(sz)); out.annotate(4, "type_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }