com.android.dex.ClassDef Java Examples
The following examples show how to use
com.android.dex.ClassDef.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DexNode.java From Box with Apache License 2.0 | 6 votes |
private void addDummyClass(ClassDef classDef, Exception exc) { int typeIndex = classDef.getTypeIndex(); String name = null; try { ClassInfo clsInfo = ClassInfo.fromDex(this, typeIndex); if (clsInfo != null) { name = clsInfo.getShortName(); } } catch (Exception e) { LOG.error("Failed to get name for class with type {}", typeIndex, e); } if (name == null || name.isEmpty()) { name = "CLASS_" + typeIndex; } ClassNode clsNode = new ClassNode(this, name, classDef.getAccessFlags()); ErrorsCounter.classError(clsNode, "Load error", exc); addClassNode(clsNode); }
Example #2
Source File: ClassNode.java From Box with Apache License 2.0 | 6 votes |
private void loadStaticValues(ClassDef cls, List<FieldNode> staticFields) throws DecodeException { for (FieldNode f : staticFields) { if (f.getAccessFlags().isFinal()) { f.addAttr(FieldInitAttr.NULL_VALUE); } } int offset = cls.getStaticValuesOffset(); if (offset == 0) { return; } Dex.Section section = dex.openSection(offset); StaticValuesParser parser = new StaticValuesParser(dex, section); parser.processFields(staticFields); // process const fields root().getConstValues().processConstFields(this, staticFields); }
Example #3
Source File: FindUsages.java From buck with Apache License 2.0 | 6 votes |
/** * Returns the set of types that can be assigned to {@code typeIndex}. */ private Set<Integer> findAssignableTypes(Dex dex, int typeIndex) { Set<Integer> assignableTypes = new HashSet<Integer>(); assignableTypes.add(typeIndex); for (ClassDef classDef : dex.classDefs()) { if (assignableTypes.contains(classDef.getSupertypeIndex())) { assignableTypes.add(classDef.getTypeIndex()); continue; } for (int implemented : classDef.getInterfaces()) { if (assignableTypes.contains(implemented)) { assignableTypes.add(classDef.getTypeIndex()); break; } } } return assignableTypes; }
Example #4
Source File: DexNode.java From Box with Apache License 2.0 | 6 votes |
private void addDummyClass(ClassDef classDef, Exception exc) { int typeIndex = classDef.getTypeIndex(); String name = null; try { ClassInfo clsInfo = ClassInfo.fromDex(this, typeIndex); if (clsInfo != null) { name = clsInfo.getShortName(); } } catch (Exception e) { LOG.error("Failed to get name for class with type {}", typeIndex, e); } if (name == null || name.isEmpty()) { name = "CLASS_" + typeIndex; } ClassNode clsNode = new ClassNode(this, name, classDef.getAccessFlags()); ErrorsCounter.classError(clsNode, "Load error", exc); addClassNode(clsNode); }
Example #5
Source File: ClassNode.java From Box with Apache License 2.0 | 6 votes |
private void loadStaticValues(ClassDef cls, List<FieldNode> staticFields) throws DecodeException { for (FieldNode f : staticFields) { if (f.getAccessFlags().isFinal()) { f.addAttr(FieldInitAttr.NULL_VALUE); } } int offset = cls.getStaticValuesOffset(); if (offset == 0) { return; } Dex.Section section = dex.openSection(offset); StaticValuesParser parser = new StaticValuesParser(dex, section); parser.processFields(staticFields); // process const fields root().getConstValues().processConstFields(this, staticFields); }
Example #6
Source File: DexMerger.java From Box with Apache License 2.0 | 6 votes |
/** * Reads a class_def_item beginning at {@code in} and writes the index and * data. */ private void transformClassDef(Dex in, ClassDef classDef, IndexMap indexMap) { idsDefsOut.assertFourByteAligned(); idsDefsOut.writeInt(classDef.getTypeIndex()); idsDefsOut.writeInt(classDef.getAccessFlags()); idsDefsOut.writeInt(classDef.getSupertypeIndex()); idsDefsOut.writeInt(classDef.getInterfacesOffset()); int sourceFileIndex = indexMap.adjustString(classDef.getSourceFileIndex()); idsDefsOut.writeInt(sourceFileIndex); int annotationsOff = classDef.getAnnotationsOffset(); idsDefsOut.writeInt(indexMap.adjustAnnotationDirectory(annotationsOff)); int classDataOff = classDef.getClassDataOffset(); if (classDataOff == 0) { idsDefsOut.writeInt(0); } else { idsDefsOut.writeInt(classDataOut.getPosition()); ClassData classData = in.readClassData(classDef); transformClassData(in, classData, indexMap); } int staticValuesOff = classDef.getStaticValuesOffset(); idsDefsOut.writeInt(indexMap.adjustEncodedArray(staticValuesOff)); }
Example #7
Source File: DexMerger.java From Box with Apache License 2.0 | 6 votes |
/** * Reads a class_def_item beginning at {@code in} and writes the index and * data. */ private void transformClassDef(Dex in, ClassDef classDef, IndexMap indexMap) { idsDefsOut.assertFourByteAligned(); idsDefsOut.writeInt(classDef.getTypeIndex()); idsDefsOut.writeInt(classDef.getAccessFlags()); idsDefsOut.writeInt(classDef.getSupertypeIndex()); idsDefsOut.writeInt(classDef.getInterfacesOffset()); int sourceFileIndex = indexMap.adjustString(classDef.getSourceFileIndex()); idsDefsOut.writeInt(sourceFileIndex); int annotationsOff = classDef.getAnnotationsOffset(); idsDefsOut.writeInt(indexMap.adjustAnnotationDirectory(annotationsOff)); int classDataOff = classDef.getClassDataOffset(); if (classDataOff == 0) { idsDefsOut.writeInt(0); } else { idsDefsOut.writeInt(classDataOut.getPosition()); ClassData classData = in.readClassData(classDef); transformClassData(in, classData, indexMap); } int staticValuesOff = classDef.getStaticValuesOffset(); idsDefsOut.writeInt(indexMap.adjustEncodedArray(staticValuesOff)); }
Example #8
Source File: DexMerger.java From buck with Apache License 2.0 | 6 votes |
/** * Reads a class_def_item beginning at {@code in} and writes the index and * data. */ private void transformClassDef(Dex in, ClassDef classDef, IndexMap indexMap) { idsDefsOut.assertFourByteAligned(); idsDefsOut.writeInt(classDef.getTypeIndex()); idsDefsOut.writeInt(classDef.getAccessFlags()); idsDefsOut.writeInt(classDef.getSupertypeIndex()); idsDefsOut.writeInt(classDef.getInterfacesOffset()); int sourceFileIndex = indexMap.adjustString(classDef.getSourceFileIndex()); idsDefsOut.writeInt(sourceFileIndex); int annotationsOff = classDef.getAnnotationsOffset(); idsDefsOut.writeInt(indexMap.adjustAnnotationDirectory(annotationsOff)); int classDataOff = classDef.getClassDataOffset(); if (classDataOff == 0) { idsDefsOut.writeInt(0); } else { idsDefsOut.writeInt(classDataOut.getPosition()); ClassData classData = in.readClassData(classDef); transformClassData(in, classData, indexMap); } int staticValuesOff = classDef.getStaticValuesOffset(); idsDefsOut.writeInt(indexMap.adjustStaticValues(staticValuesOff)); }
Example #9
Source File: FindUsages.java From Box with Apache License 2.0 | 6 votes |
/** * Returns the set of types that can be assigned to {@code typeIndex}. */ private Set<Integer> findAssignableTypes(Dex dex, int typeIndex) { Set<Integer> assignableTypes = new HashSet<Integer>(); assignableTypes.add(typeIndex); for (ClassDef classDef : dex.classDefs()) { if (assignableTypes.contains(classDef.getSupertypeIndex())) { assignableTypes.add(classDef.getTypeIndex()); continue; } for (int implemented : classDef.getInterfaces()) { if (assignableTypes.contains(implemented)) { assignableTypes.add(classDef.getTypeIndex()); break; } } } return assignableTypes; }
Example #10
Source File: FindUsages.java From Box with Apache License 2.0 | 6 votes |
/** * Returns the set of types that can be assigned to {@code typeIndex}. */ private Set<Integer> findAssignableTypes(Dex dex, int typeIndex) { Set<Integer> assignableTypes = new HashSet<Integer>(); assignableTypes.add(typeIndex); for (ClassDef classDef : dex.classDefs()) { if (assignableTypes.contains(classDef.getSupertypeIndex())) { assignableTypes.add(classDef.getTypeIndex()); continue; } for (int implemented : classDef.getInterfaces()) { if (assignableTypes.contains(implemented)) { assignableTypes.add(classDef.getTypeIndex()); break; } } } return assignableTypes; }
Example #11
Source File: DexMerger.java From buck with Apache License 2.0 | 5 votes |
/** * Reads just enough data on each class so that we can sort it and then find * it later. */ private void readSortableTypes(SortableType[] sortableTypes, Dex buffer, IndexMap indexMap) { for (ClassDef classDef : buffer.classDefs()) { SortableType sortableType = indexMap.adjust( new SortableType(buffer, indexMap, classDef)); int t = sortableType.getTypeIndex(); if (sortableTypes[t] == null) { sortableTypes[t] = sortableType; } else if (collisionPolicy != CollisionPolicy.KEEP_FIRST) { throw new DexException("Multiple dex files define " + buffer.typeNames().get(classDef.getTypeIndex())); } } }
Example #12
Source File: ClassNode.java From Box with Apache License 2.0 | 5 votes |
/** * Restore original access flags from Dalvik annotation if present */ private void initAccessFlags(ClassDef cls) { int accFlagsValue; Annotation a = getAnnotation(Consts.DALVIK_INNER_CLASS); if (a != null) { accFlagsValue = (Integer) a.getValues().get("accessFlags"); } else { accFlagsValue = cls.getAccessFlags(); } this.accessFlags = new AccessInfo(accFlagsValue, AFType.CLASS); }
Example #13
Source File: SortableType.java From Box with Apache License 2.0 | 5 votes |
/** * Assigns this type's depth if the depths of its supertype and implemented * interfaces are known. Returns false if the depth couldn't be computed * yet. */ public boolean tryAssignDepth(SortableType[] types) { int max; if (classDef.getSupertypeIndex() == ClassDef.NO_INDEX) { max = 0; // this is Object.class or an interface } else if (classDef.getSupertypeIndex() == classDef.getTypeIndex()) { // This is an invalid class extending itself. throw new DexException("Class with type index " + classDef.getTypeIndex() + " extends itself"); } else { SortableType sortableSupertype = types[classDef.getSupertypeIndex()]; if (sortableSupertype == null) { max = 1; // unknown, so assume it's a root. } else if (sortableSupertype.depth == -1) { return false; } else { max = sortableSupertype.depth; } } for (short interfaceIndex : classDef.getInterfaces()) { SortableType implemented = types[interfaceIndex]; if (implemented == null) { max = Math.max(max, 1); // unknown, so assume it's a root. } else if (implemented.depth == -1) { return false; } else { max = Math.max(max, implemented.depth); } } depth = max + 1; return true; }
Example #14
Source File: IndexMap.java From Box with Apache License 2.0 | 5 votes |
public ClassDef adjust(ClassDef classDef) { return new ClassDef(target, classDef.getOffset(), adjustType(classDef.getTypeIndex()), classDef.getAccessFlags(), adjustType(classDef.getSupertypeIndex()), adjustTypeListOffset(classDef.getInterfacesOffset()), classDef.getSourceFileIndex(), classDef.getAnnotationsOffset(), classDef.getClassDataOffset(), classDef.getStaticValuesOffset()); }
Example #15
Source File: Grep.java From Box with Apache License 2.0 | 5 votes |
/** * Prints usages to out. Returns the number of matches found. */ public int grep() { for (ClassDef classDef : dex.classDefs()) { currentClass = classDef; currentMethod = null; if (classDef.getClassDataOffset() == 0) { continue; } ClassData classData = dex.readClassData(classDef); // find the strings in encoded constants int staticValuesOffset = classDef.getStaticValuesOffset(); if (staticValuesOffset != 0) { readArray(new EncodedValueReader(dex.open(staticValuesOffset))); } // find the strings in method bodies for (ClassData.Method method : classData.allMethods()) { currentMethod = method; if (method.getCodeOffset() != 0) { codeReader.visitAll(dex.readCode(method).getInstructions()); } } } currentClass = null; currentMethod = null; return count; }
Example #16
Source File: FindUsages.java From Box with Apache License 2.0 | 5 votes |
/** * Prints usages to out. */ public void findUsages() { if (fieldIds == null || methodIds == null) { return; } for (ClassDef classDef : dex.classDefs()) { currentClass = classDef; currentMethod = null; if (classDef.getClassDataOffset() == 0) { continue; } ClassData classData = dex.readClassData(classDef); for (ClassData.Field field : classData.allFields()) { int fieldIndex = field.getFieldIndex(); if (fieldIds.contains(fieldIndex)) { out.println(location() + " field declared " + dex.fieldIds().get(fieldIndex)); } } for (ClassData.Method method : classData.allMethods()) { currentMethod = method; int methodIndex = method.getMethodIndex(); if (methodIds.contains(methodIndex)) { out.println(location() + " method declared " + dex.methodIds().get(methodIndex)); } if (method.getCodeOffset() != 0) { codeReader.visitAll(dex.readCode(method).getInstructions()); } } } currentClass = null; currentMethod = null; }
Example #17
Source File: DexIndexPrinter.java From Box with Apache License 2.0 | 5 votes |
private void printClassDefs() { int index = 0; for (ClassDef classDef : dex.classDefs()) { System.out.println("class def " + index + ": " + classDef); index++; } }
Example #18
Source File: DexMerger.java From Box with Apache License 2.0 | 5 votes |
/** * Reads just enough data on each class so that we can sort it and then find * it later. */ private void readSortableTypes(SortableType[] sortableTypes, Dex buffer, IndexMap indexMap) { for (ClassDef classDef : buffer.classDefs()) { SortableType sortableType = indexMap.adjust( new SortableType(buffer, indexMap, classDef)); int t = sortableType.getTypeIndex(); if (sortableTypes[t] == null) { sortableTypes[t] = sortableType; } else if (collisionPolicy != CollisionPolicy.KEEP_FIRST) { throw new DexException("Multiple dex files define " + buffer.typeNames().get(classDef.getTypeIndex())); } } }
Example #19
Source File: SortableType.java From buck with Apache License 2.0 | 5 votes |
/** * Assigns this type's depth if the depths of its supertype and implemented * interfaces are known. Returns false if the depth couldn't be computed * yet. */ public boolean tryAssignDepth(SortableType[] types) { int max; if (classDef.getSupertypeIndex() == ClassDef.NO_INDEX) { max = 0; // this is Object.class or an interface } else { SortableType sortableSupertype = types[classDef.getSupertypeIndex()]; if (sortableSupertype == null) { max = 1; // unknown, so assume it's a root. } else if (sortableSupertype.depth == -1) { return false; } else { max = sortableSupertype.depth; } } for (short interfaceIndex : classDef.getInterfaces()) { SortableType implemented = types[interfaceIndex]; if (implemented == null) { max = Math.max(max, 1); // unknown, so assume it's a root. } else if (implemented.depth == -1) { return false; } else { max = Math.max(max, implemented.depth); } } depth = max + 1; return true; }
Example #20
Source File: IndexMap.java From buck with Apache License 2.0 | 5 votes |
public ClassDef adjust(ClassDef classDef) { return new ClassDef(target, classDef.getOffset(), adjustType(classDef.getTypeIndex()), classDef.getAccessFlags(), adjustType(classDef.getSupertypeIndex()), adjustTypeListOffset(classDef.getInterfacesOffset()), classDef.getSourceFileIndex(), classDef.getAnnotationsOffset(), classDef.getClassDataOffset(), classDef.getStaticValuesOffset()); }
Example #21
Source File: Grep.java From buck with Apache License 2.0 | 5 votes |
/** * Prints usages to out. Returns the number of matches found. */ public int grep() { for (ClassDef classDef : dex.classDefs()) { currentClass = classDef; currentMethod = null; if (classDef.getClassDataOffset() == 0) { continue; } ClassData classData = dex.readClassData(classDef); // find the strings in encoded constants int staticValuesOffset = classDef.getStaticValuesOffset(); if (staticValuesOffset != 0) { readArray(new EncodedValueReader(dex.open(staticValuesOffset))); } // find the strings in method bodies for (ClassData.Method method : classData.allMethods()) { currentMethod = method; if (method.getCodeOffset() != 0) { codeReader.visitAll(dex.readCode(method).getInstructions()); } } } currentClass = null; currentMethod = null; return count; }
Example #22
Source File: FindUsages.java From buck with Apache License 2.0 | 5 votes |
/** * Prints usages to out. */ public void findUsages() { if (fieldIds == null || methodIds == null) { return; } for (ClassDef classDef : dex.classDefs()) { currentClass = classDef; currentMethod = null; if (classDef.getClassDataOffset() == 0) { continue; } ClassData classData = dex.readClassData(classDef); for (ClassData.Field field : classData.allFields()) { int fieldIndex = field.getFieldIndex(); if (fieldIds.contains(fieldIndex)) { out.println(location() + " field declared " + dex.fieldIds().get(fieldIndex)); } } for (ClassData.Method method : classData.allMethods()) { currentMethod = method; int methodIndex = method.getMethodIndex(); if (methodIds.contains(methodIndex)) { out.println(location() + " method declared " + dex.methodIds().get(methodIndex)); } if (method.getCodeOffset() != 0) { codeReader.visitAll(dex.readCode(method).getInstructions()); } } } currentClass = null; currentMethod = null; }
Example #23
Source File: DexIndexPrinter.java From buck with Apache License 2.0 | 5 votes |
private void printClassDefs() { int index = 0; for (ClassDef classDef : dex.classDefs()) { System.out.println("class def " + index + ": " + classDef); index++; } }
Example #24
Source File: DexNode.java From Box with Apache License 2.0 | 5 votes |
public void loadClasses() { for (ClassDef cls : dexBuf.classDefs()) { try { addClassNode(new ClassNode(this, cls)); } catch (Exception e) { addDummyClass(cls, e); } } // sort classes by name, expect top classes before inner classes.sort(Comparator.comparing(ClassNode::getFullName)); }
Example #25
Source File: ClassNode.java From Box with Apache License 2.0 | 5 votes |
private void loadAnnotations(ClassDef cls) { int offset = cls.getAnnotationsOffset(); if (offset != 0) { try { new AnnotationsParser(this).parse(offset); } catch (Exception e) { LOG.error("Error parsing annotations in {}", this, e); } } }
Example #26
Source File: DexNode.java From Box with Apache License 2.0 | 5 votes |
public void loadClasses() { for (ClassDef cls : dexBuf.classDefs()) { try { addClassNode(new ClassNode(this, cls)); } catch (Exception e) { addDummyClass(cls, e); } } // sort classes by name, expect top classes before inner classes.sort(Comparator.comparing(ClassNode::getFullName)); }
Example #27
Source File: DexMerger.java From Box with Apache License 2.0 | 5 votes |
/** * Reads just enough data on each class so that we can sort it and then find * it later. */ private void readSortableTypes(SortableType[] sortableTypes, Dex buffer, IndexMap indexMap) { for (ClassDef classDef : buffer.classDefs()) { SortableType sortableType = indexMap.adjust( new SortableType(buffer, indexMap, classDef)); int t = sortableType.getTypeIndex(); if (sortableTypes[t] == null) { sortableTypes[t] = sortableType; } else if (collisionPolicy != CollisionPolicy.KEEP_FIRST) { throw new DexException("Multiple dex files define " + buffer.typeNames().get(classDef.getTypeIndex())); } } }
Example #28
Source File: SortableType.java From Box with Apache License 2.0 | 5 votes |
/** * Assigns this type's depth if the depths of its supertype and implemented * interfaces are known. Returns false if the depth couldn't be computed * yet. */ public boolean tryAssignDepth(SortableType[] types) { int max; if (classDef.getSupertypeIndex() == ClassDef.NO_INDEX) { max = 0; // this is Object.class or an interface } else if (classDef.getSupertypeIndex() == classDef.getTypeIndex()) { // This is an invalid class extending itself. throw new DexException("Class with type index " + classDef.getTypeIndex() + " extends itself"); } else { SortableType sortableSupertype = types[classDef.getSupertypeIndex()]; if (sortableSupertype == null) { max = 1; // unknown, so assume it's a root. } else if (sortableSupertype.depth == -1) { return false; } else { max = sortableSupertype.depth; } } for (short interfaceIndex : classDef.getInterfaces()) { SortableType implemented = types[interfaceIndex]; if (implemented == null) { max = Math.max(max, 1); // unknown, so assume it's a root. } else if (implemented.depth == -1) { return false; } else { max = Math.max(max, implemented.depth); } } depth = max + 1; return true; }
Example #29
Source File: IndexMap.java From Box with Apache License 2.0 | 5 votes |
public ClassDef adjust(ClassDef classDef) { return new ClassDef(target, classDef.getOffset(), adjustType(classDef.getTypeIndex()), classDef.getAccessFlags(), adjustType(classDef.getSupertypeIndex()), adjustTypeListOffset(classDef.getInterfacesOffset()), classDef.getSourceFileIndex(), classDef.getAnnotationsOffset(), classDef.getClassDataOffset(), classDef.getStaticValuesOffset()); }
Example #30
Source File: Grep.java From Box with Apache License 2.0 | 5 votes |
/** * Prints usages to out. Returns the number of matches found. */ public int grep() { for (ClassDef classDef : dex.classDefs()) { currentClass = classDef; currentMethod = null; if (classDef.getClassDataOffset() == 0) { continue; } ClassData classData = dex.readClassData(classDef); // find the strings in encoded constants int staticValuesOffset = classDef.getStaticValuesOffset(); if (staticValuesOffset != 0) { readArray(new EncodedValueReader(dex.open(staticValuesOffset))); } // find the strings in method bodies for (ClassData.Method method : classData.allMethods()) { currentMethod = method; if (method.getCodeOffset() != 0) { codeReader.visitAll(dex.readCode(method).getInstructions()); } } } currentClass = null; currentMethod = null; return count; }