/** * 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 = strings.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (out.annotates()) { out.annotate(4, "string_ids_size: " + Hex.u4(sz)); out.annotate(4, "string_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** * Gets the index of the given string, which must have been added * to this instance. * * @param string {@code non-null;} the string to look up * @return {@code >= 0;} the string's index */ public int indexOf(CstString string) { return indexOf(string.getString()); }
/** * Interns an element into this instance. * * @param string {@code non-null;} the string to intern, as a constant * @return {@code non-null;} the interned string */ public StringIdItem intern(CstString string) { return intern(new StringIdItem(string)); }
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); IndexedItem result = strings.get((CstString) cst); if (result == null) { throw new IllegalArgumentException("not found"); } return result; }
/** * Interns an element into this instance. * * @param string {@code non-null;} the string to intern * @return {@code non-null;} the interned string */ public synchronized StringIdItem intern(StringIdItem string) { if (string == null) { throw new NullPointerException("string == null"); } throwIfPrepared(); CstString value = string.getValue(); StringIdItem already = strings.get(value); if (already != null) { return already; } strings.put(value, string); return string; }
/** * 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.getStringIds().writeHeaderPart(out); file.getTypeIds().writeHeaderPart(out); file.getProtoIds().writeHeaderPart(out);
typeLists.prepare(); typeIds.prepare(); stringIds.prepare(); stringData.prepare(); header.prepare();
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); IndexedItem result = strings.get((CstString) cst); if (result == null) { throw new IllegalArgumentException("not found"); } return result; }
/** * Interns an element into this instance. * * @param string {@code non-null;} the string to intern * @return {@code non-null;} the interned string */ public StringIdItem intern(StringIdItem string) { if (string == null) { throw new NullPointerException("string == null"); } throwIfPrepared(); CstString value = string.getValue(); StringIdItem already = strings.get(value); if (already != null) { return already; } strings.put(value, string); return string; }
/** * 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.getStringIds().writeHeaderPart(out); file.getTypeIds().writeHeaderPart(out); file.getProtoIds().writeHeaderPart(out);
typeLists.prepare(); typeIds.prepare(); stringIds.prepare(); stringData.prepare(); header.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 = strings.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (out.annotates()) { out.annotate(4, "string_ids_size: " + Hex.u4(sz)); out.annotate(4, "string_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** * Emits a string index as an unsigned LEB128. The actual value written * is shifted by 1, so that the '0' value is reserved for "null". The * null symbol is used in some cases by the parameter name list * at the beginning of the sequence. * * @param string {@code null-ok;} string to emit * @throws IOException */ private void emitStringIndex(CstString string) throws IOException { if ((string == null) || (file == null)) { output.writeUleb128(0); } else { output.writeUleb128( 1 + file.getStringIds().indexOf(string)); } if (DEBUG) { System.err.printf("Emit string %s\n", string == null ? "<null>" : string.toQuoted()); } }
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); IndexedItem result = strings.get((CstString) cst); if (result == null) { throw new IllegalArgumentException("not found"); } return result; }
/** * Interns an element into this instance. * * @param string {@code non-null;} the string to intern * @return {@code non-null;} the interned string */ public synchronized StringIdItem intern(StringIdItem string) { if (string == null) { throw new NullPointerException("string == null"); } throwIfPrepared(); CstString value = string.getValue(); StringIdItem already = strings.get(value); if (already != null) { return already; } strings.put(value, string); return string; }