use of org.jf.dexlib2.iface.Method in project android-classyshark by google.
the class RootBuilder method fillFromDex.
private void fillFromDex(File file, ClassNode rootNode) {
try {
DexFile dxFile = DexlibLoader.loadDexFile(file);
Set<? extends ClassDef> classSet = dxFile.getClasses();
for (ClassDef o : classSet) {
int methodCount = 0;
for (Method method : o.getMethods()) {
methodCount++;
}
String translatedClassName = o.getType().replaceAll("\\/", "\\.").substring(1, o.getType().length() - 1);
ClassInfo classInfo = new ClassInfo(translatedClassName, methodCount);
rootNode.add(classInfo);
}
} catch (Exception ex) {
System.err.println("Error parsing Dexfile: " + file.getName() + ": " + ex.getMessage());
ex.printStackTrace(System.err);
}
}
use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.
the class DexUtil method verifyOdexHeader.
/**
* Reads in the odex header from the given input stream and verifies that it is valid and a supported version
*
* The inputStream must support mark(), and will be reset to initial position upon exiting the method
*
* @param inputStream An input stream that is positioned at an odex header
* @throws NotAnOdexFile If the file is not an odex file
* @throws UnsupportedFile If the odex header is valid, but is an unsupported version
*/
public static void verifyOdexHeader(@Nonnull InputStream inputStream) throws IOException {
if (!inputStream.markSupported()) {
throw new IllegalArgumentException("InputStream must support mark");
}
inputStream.mark(8);
byte[] partialHeader = new byte[8];
try {
ByteStreams.readFully(inputStream, partialHeader);
} catch (EOFException ex) {
throw new NotAnOdexFile("File is too short");
} finally {
inputStream.reset();
}
verifyOdexHeader(partialHeader, 0);
}
use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.
the class DexUtil method verifyDexHeader.
/**
* Reads in the dex header from the given input stream and verifies that it is valid and a supported version
*
* The inputStream must support mark(), and will be reset to initial position upon exiting the method
*
* @param inputStream An input stream that is positioned at a dex header
* @throws NotADexFile If the file is not a dex file
* @throws InvalidFile If the header appears to be a dex file, but is not valid for some reason
* @throws UnsupportedFile If the dex header is valid, but uses unsupported functionality
*/
public static void verifyDexHeader(@Nonnull InputStream inputStream) throws IOException {
if (!inputStream.markSupported()) {
throw new IllegalArgumentException("InputStream must support mark");
}
inputStream.mark(44);
byte[] partialHeader = new byte[44];
try {
ByteStreams.readFully(inputStream, partialHeader);
} catch (EOFException ex) {
throw new NotADexFile("File is too short");
} finally {
inputStream.reset();
}
verifyDexHeader(partialHeader, 0);
}
use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.
the class SyntheticAccessorResolver method getAccessedMember.
@Nullable
public AccessedMember getAccessedMember(@Nonnull MethodReference methodReference) {
String methodDescriptor = ReferenceUtil.getMethodDescriptor(methodReference);
AccessedMember accessedMember = resolvedAccessors.get(methodDescriptor);
if (accessedMember != null) {
return accessedMember;
}
String type = methodReference.getDefiningClass();
ClassDef classDef = classDefMap.get(type);
if (classDef == null) {
return null;
}
Method matchedMethod = null;
MethodImplementation matchedMethodImpl = null;
for (Method method : classDef.getMethods()) {
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl != null) {
if (methodReferenceEquals(method, methodReference)) {
matchedMethod = method;
matchedMethodImpl = methodImpl;
break;
}
}
}
if (matchedMethod == null) {
return null;
}
//A synthetic accessor will be marked synthetic
if (!AccessFlags.SYNTHETIC.isSet(matchedMethod.getAccessFlags())) {
return null;
}
List<Instruction> instructions = ImmutableList.copyOf(matchedMethodImpl.getInstructions());
int accessType = syntheticAccessorFSM.test(instructions);
if (accessType >= 0) {
AccessedMember member = new AccessedMember(accessType, ((ReferenceInstruction) instructions.get(0)).getReference());
resolvedAccessors.put(methodDescriptor, member);
return member;
}
return null;
}
use of org.jf.dexlib2.iface.Method in project smali by JesusFreke.
the class DexWriter method writeDebugAndCodeItems.
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter, @Nonnull DeferredOutputStream temp) throws IOException {
ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
debugSectionOffset = offsetWriter.getPosition();
DebugWriter<StringKey, TypeKey> debugWriter = new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);
DexDataWriter codeWriter = new DexDataWriter(temp, 0);
List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();
for (ClassKey classKey : classSection.getSortedClasses()) {
Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);
Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);
for (MethodKey methodKey : methods) {
List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = classSection.getTryBlocks(methodKey);
Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);
if (instructions != null && stringSection.hasJumboIndexes()) {
boolean needsFix = false;
for (Instruction instruction : instructions) {
if (instruction.getOpcode() == Opcode.CONST_STRING) {
if (stringSection.getItemIndex((StringRef) ((ReferenceInstruction) instruction).getReference()) >= 65536) {
needsFix = true;
break;
}
}
}
if (needsFix) {
MutableMethodImplementation mutableMethodImplementation = classSection.makeMutableMethodImplementation(methodKey);
fixInstructions(mutableMethodImplementation);
instructions = mutableMethodImplementation.getInstructions();
tryBlocks = mutableMethodImplementation.getTryBlocks();
debugItems = mutableMethodImplementation.getDebugItems();
}
}
int debugItemOffset = writeDebugItem(offsetWriter, debugWriter, classSection.getParameterNames(methodKey), debugItems);
int codeItemOffset;
try {
codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);
} catch (RuntimeException ex) {
throw new ExceptionWithContext(ex, "Exception occurred while writing code_item for method %s", methodSection.getMethodReference(methodKey));
}
if (codeItemOffset != -1) {
codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
}
}
}
offsetWriter.align();
codeSectionOffset = offsetWriter.getPosition();
codeWriter.close();
temp.writeTo(offsetWriter);
temp.close();
for (CodeItemOffset<MethodKey> codeOffset : codeOffsets) {
classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
}
}
Aggregations