/** * 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 = methodIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (out.annotates()) { out.annotate(4, "method_ids_size: " + Hex.u4(sz)); out.annotate(4, "method_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** {@inheritDoc} */ public void addContents(DexFile file) { MethodIdsSection methodIds = file.getMethodIds(); MixedItemSection wordData = file.getWordData(); methodIds.intern(method); wordData.add(annotationsItem); }
/** * Interns an element into this instance. * * @param method {@code non-null;} the reference to intern * @return {@code non-null;} the interned reference */ public synchronized MethodIdItem intern(CstBaseMethodRef method) { if (method == null) { throw new NullPointerException("method == null"); } throwIfPrepared(); MethodIdItem result = methodIds.get(method); if (result == null) { result = new MethodIdItem(method); methodIds.put(method, result); } return result; }
private int getTargetIndex(DexFile file) { Constant ref = methodHandle.getRef(); if (methodHandle.isAccessor()) { FieldIdsSection fieldIds = file.getFieldIds(); return fieldIds.indexOf((CstFieldRef) ref); } else if (methodHandle.isInvocation()) { if (ref instanceof CstInterfaceMethodRef) { ref = ((CstInterfaceMethodRef)ref).toMethodRef(); } MethodIdsSection methodIds = file.getMethodIds(); return methodIds.indexOf((CstBaseMethodRef) ref); } else { throw new IllegalStateException("Unhandled invocation type"); } } }
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); IndexedItem result = methodIds.get((CstBaseMethodRef) cst); if (result == null) { throw new IllegalArgumentException("not found"); } 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.getProtoIds().writeHeaderPart(out); file.getFieldIds().writeHeaderPart(out); file.getMethodIds().writeHeaderPart(out); file.getClassDefs().writeHeaderPart(out);
methodIds.prepare(); fieldIds.prepare(); protoIds.prepare();
/** {@inheritDoc} */ public void writeTo(DexFile file, AnnotatedOutput out) { int methodIdx = file.getMethodIds().indexOf(method); int annotationsOff = annotationsItem.getAbsoluteOffset(); if (out.annotates()) { out.annotate(0, " " + method.toHuman()); out.annotate(4, " method_idx: " + Hex.u4(methodIdx)); out.annotate(4, " annotations_off: " + Hex.u4(annotationsOff)); } out.writeInt(methodIdx); out.writeInt(annotationsOff); }
/** {@inheritDoc} */ @Override public IndexedItem get(Constant cst) { if (cst == null) { throw new NullPointerException("cst == null"); } throwIfNotPrepared(); IndexedItem result = methodIds.get((CstBaseMethodRef) cst); if (result == null) { throw new IllegalArgumentException("not found"); } 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.getProtoIds().writeHeaderPart(out); file.getFieldIds().writeHeaderPart(out); file.getMethodIds().writeHeaderPart(out); file.getClassDefs().writeHeaderPart(out);
wordData.prepare(); byteData.prepare(); methodIds.prepare(); fieldIds.prepare(); protoIds.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 = methodIds.size(); int offset = (sz == 0) ? 0 : getFileOffset(); if (out.annotates()) { out.annotate(4, "method_ids_size: " + Hex.u4(sz)); out.annotate(4, "method_ids_off: " + Hex.u4(offset)); } out.writeInt(sz); out.writeInt(offset); }
/** {@inheritDoc} */ @Override public void addContents(DexFile file) { MethodIdsSection methodIds = file.getMethodIds(); MixedItemSection wordData = file.getWordData(); methodIds.intern(method); if (code != null) { wordData.add(code); } }
/** {@inheritDoc} */ public void writeTo(DexFile file, AnnotatedOutput out) { int methodIdx = file.getMethodIds().indexOf(method); int annotationsOff = annotations.getAbsoluteOffset(); if (out.annotates()) { out.annotate(0, " " + method.toHuman()); out.annotate(4, " method_idx: " + Hex.u4(methodIdx)); out.annotate(4, " annotations_off: " + Hex.u4(annotationsOff)); } out.writeInt(methodIdx); out.writeInt(annotationsOff); }