use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class DexPool method writeTo.
public static void writeTo(@Nonnull DexDataStore dataStore, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {
DexPool dexPool = new DexPool(input.getOpcodes());
for (ClassDef classDef : input.getClasses()) {
dexPool.internClass(classDef);
}
dexPool.writeTo(dataStore);
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class Baksmali method disassembleClass.
private static boolean disassembleClass(ClassDef classDef, ClassFileNameHandler fileNameHandler, BaksmaliOptions options) {
/**
* The path for the disassembly file is based on the package name
* The class descriptor will look something like:
* Ljava/lang/Object;
* Where the there is leading 'L' and a trailing ';', and the parts of the
* package name are separated by '/'
*/
String classDescriptor = classDef.getType();
//validate that the descriptor is formatted like we expect
if (classDescriptor.charAt(0) != 'L' || classDescriptor.charAt(classDescriptor.length() - 1) != ';') {
System.err.println("Unrecognized class descriptor - " + classDescriptor + " - skipping class");
return false;
}
File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor);
//create and initialize the top level string template
ClassDefinition classDefinition = new ClassDefinition(options, classDef);
//write the disassembly
Writer writer = null;
try {
File smaliParent = smaliFile.getParentFile();
if (!smaliParent.exists()) {
if (!smaliParent.mkdirs()) {
// check again, it's likely it was created in a different thread
if (!smaliParent.exists()) {
System.err.println("Unable to create directory " + smaliParent.toString() + " - skipping class");
return false;
}
}
}
if (!smaliFile.exists()) {
if (!smaliFile.createNewFile()) {
System.err.println("Unable to create file " + smaliFile.toString() + " - skipping class");
return false;
}
}
BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(smaliFile), "UTF8"));
writer = new IndentingWriter(bufWriter);
classDefinition.writeTo((IndentingWriter) writer);
} catch (Exception ex) {
System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
ex.printStackTrace();
// noinspection ResultOfMethodCallIgnored
smaliFile.delete();
return false;
} finally {
if (writer != null) {
try {
writer.close();
} catch (Throwable ex) {
System.err.println("\n\nError occurred while closing file " + smaliFile.toString());
ex.printStackTrace();
}
}
}
return true;
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class BaksmaliTestUtils method assertSmaliCompiledEquals.
public static void assertSmaliCompiledEquals(String source, String expected, BaksmaliOptions options, boolean stripComments) throws IOException, RecognitionException {
ClassDef classDef = SmaliTestUtils.compileSmali(source, options.apiLevel);
// Remove unnecessary whitespace and optionally strip all comments from smali file
String normalizedActual = getNormalizedSmali(classDef, options, stripComments);
String normalizedExpected = normalizeSmali(expected, stripComments);
// Assert that normalized strings are now equal
Assert.assertEquals(normalizedExpected, normalizedActual);
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class DisassemblyTest method runTest.
protected void runTest(@Nonnull String testName, @Nonnull BaksmaliOptions options) {
try {
DexBackedDexFile inputDex = getInputDexFile(testName, options);
Assert.assertEquals(1, inputDex.getClassCount());
ClassDef inputClass = Iterables.getFirst(inputDex.getClasses(), null);
Assert.assertNotNull(inputClass);
String input = BaksmaliTestUtils.getNormalizedSmali(inputClass, options, true);
String output = BaksmaliTestUtils.readResourceFully(getOutputFilename(testName));
output = BaksmaliTestUtils.normalizeSmali(output, true);
// Run smali, baksmali, and then compare strings are equal (minus comments/whitespace)
Assert.assertEquals(output, input);
} catch (IOException ex) {
Assert.fail();
}
}
use of org.jf.dexlib2.iface.ClassDef in project smali by JesusFreke.
the class AnalyzedMethodUtil method canAccess.
public static boolean canAccess(@Nonnull TypeProto type, @Nonnull Method virtualMethod, boolean checkPackagePrivate, boolean checkProtected, boolean checkClass) {
if (checkPackagePrivate && MethodUtil.isPackagePrivate(virtualMethod)) {
String otherPackage = TypeUtils.getPackage(virtualMethod.getDefiningClass());
String thisPackage = TypeUtils.getPackage(type.getType());
if (!otherPackage.equals(thisPackage)) {
return false;
}
}
if (checkProtected && (virtualMethod.getAccessFlags() & AccessFlags.PROTECTED.getValue()) != 0) {
if (!TypeProtoUtils.extendsFrom(type, virtualMethod.getDefiningClass())) {
return false;
}
}
if (checkClass) {
ClassPath classPath = type.getClassPath();
ClassDef methodClassDef = classPath.getClassDef(virtualMethod.getDefiningClass());
if (!TypeUtils.canAccessClass(type.getType(), methodClassDef)) {
return false;
}
}
return true;
}
Aggregations