use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.
the class BaksmaliTestUtils method getNormalizedSmali.
@Nonnull
public static String getNormalizedSmali(@Nonnull ClassDef classDef, @Nonnull BaksmaliOptions options, boolean stripComments) throws IOException {
StringWriter stringWriter = new StringWriter();
BaksmaliWriter writer = new BaksmaliWriter(stringWriter, options.implicitReferences ? classDef.getType() : null);
ClassDefinition classDefinition = new ClassDefinition(options, classDef);
classDefinition.writeTo(writer);
writer.close();
return normalizeSmali(stringWriter.toString(), stripComments);
}
use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.
the class InstructionMethodItemTest method testInvalidReference.
@Test
public void testInvalidReference() throws IOException {
Instruction21c instruction = new Instruction21c() {
@Override
public int getRegisterA() {
return 0;
}
@Nonnull
@Override
public Reference getReference() {
return new BaseStringReference() {
@Override
public void validateReference() throws InvalidReferenceException {
throw new InvalidReferenceException("blahblahblah");
}
@Nonnull
@Override
public String getString() {
throw new RuntimeException("invalid reference");
}
};
}
@Override
public int getReferenceType() {
return ReferenceType.STRING;
}
@Override
public Opcode getOpcode() {
return Opcode.CONST_STRING;
}
@Override
public int getCodeUnits() {
return Format.Format21c.size / 2;
}
};
MethodImplementation methodImplementation = new MethodImplementation() {
@Override
public int getRegisterCount() {
return 1;
}
@Nonnull
@Override
public Iterable<? extends Instruction> getInstructions() {
return ImmutableList.of(instruction);
}
@Nonnull
@Override
public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() {
return ImmutableList.of();
}
@Nonnull
@Override
public Iterable<? extends DebugItem> getDebugItems() {
return ImmutableList.of();
}
};
Method method = new TestMethod(methodImplementation);
ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), new TestClassDef());
MethodDefinition methodDefinition = new MethodDefinition(classDefinition, method, methodImplementation);
methodDefinition.registerFormatter = new RegisterFormatter(new BaksmaliOptions(), 1, 0);
InstructionMethodItem methodItem = new InstructionMethodItem<Instruction21c>(methodDefinition, 0, instruction);
StringWriter stringWriter = new StringWriter();
BaksmaliWriter writer = new BaksmaliWriter(stringWriter);
methodItem.writeTo(writer);
Assert.assertEquals("#Invalid reference\n#const-string v0, blahblahblah\nnop", stringWriter.toString());
}
use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.
the class AnalysisTest method runTest.
public void runTest(String test, boolean registerInfo, boolean isArt) throws IOException, URISyntaxException {
String dexFilePath = String.format("%s%sclasses.dex", test, File.separatorChar);
DexFile dexFile = DexFileFactory.loadDexFile(findResource(dexFilePath), Opcodes.getDefault());
BaksmaliOptions options = new BaksmaliOptions();
if (registerInfo) {
options.registerInfo = BaksmaliOptions.ALL | BaksmaliOptions.FULLMERGE;
if (isArt) {
options.classPath = new ClassPath(new ArrayList<ClassProvider>(), true, 56);
} else {
options.classPath = new ClassPath();
}
}
options.implicitReferences = false;
for (ClassDef classDef : dexFile.getClasses()) {
StringWriter stringWriter = new StringWriter();
BaksmaliWriter writer = new BaksmaliWriter(stringWriter);
ClassDefinition classDefinition = new ClassDefinition(options, classDef);
classDefinition.writeTo(writer);
writer.close();
String className = classDef.getType();
String smaliPath = String.format("%s%s%s.smali", test, File.separatorChar, className.substring(1, className.length() - 1));
String smaliContents = readResource(smaliPath);
Assert.assertEquals(BaksmaliTestUtils.normalizeWhitespace(smaliContents), BaksmaliTestUtils.normalizeWhitespace((stringWriter.toString())));
}
}
use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.
the class ClassDefinition method writeDirectMethods.
private Set<String> writeDirectMethods(BaksmaliWriter writer) throws IOException {
boolean wroteHeader = false;
Set<String> writtenMethods = new HashSet<String>();
Iterable<? extends Method> directMethods;
if (classDef instanceof DexBackedClassDef) {
directMethods = ((DexBackedClassDef) classDef).getDirectMethods(false);
} else {
directMethods = classDef.getDirectMethods();
}
for (Method method : directMethods) {
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# direct methods");
wroteHeader = true;
}
writer.write('\n');
// TODO: check for method validation errors
String methodString = formatter.getShortMethodDescriptor(method);
BaksmaliWriter methodWriter = writer;
if (!writtenMethods.add(methodString)) {
writer.write("# duplicate method ignored\n");
methodWriter = getCommentingWriter(writer);
}
MethodImplementation methodImpl = method.getImplementation();
if (methodImpl == null) {
MethodDefinition.writeEmptyMethodTo(methodWriter, method, this);
} else {
MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
methodDefinition.writeTo(methodWriter);
}
}
return writtenMethods;
}
use of org.jf.baksmali.formatter.BaksmaliWriter in project smali by JesusFreke.
the class ClassDefinition method writeStaticFields.
private Set<String> writeStaticFields(BaksmaliWriter writer) throws IOException {
boolean wroteHeader = false;
Set<String> writtenFields = new HashSet<String>();
Iterable<? extends Field> staticFields;
if (classDef instanceof DexBackedClassDef) {
staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
} else {
staticFields = classDef.getStaticFields();
}
for (Field field : staticFields) {
if (!wroteHeader) {
writer.write("\n\n");
writer.write("# static fields");
wroteHeader = true;
}
writer.write('\n');
boolean setInStaticConstructor;
BaksmaliWriter fieldWriter = writer;
String fieldString = formatter.getShortFieldDescriptor(field);
if (!writtenFields.add(fieldString)) {
writer.write("# duplicate field ignored\n");
fieldWriter = getCommentingWriter(writer);
System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
setInStaticConstructor = false;
} else {
setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
}
FieldDefinition.writeTo(fieldWriter, field, setInStaticConstructor);
}
return writtenFields;
}
Aggregations