Switch out IndentingWriter for BaksmaliWriter throughout baksmali

This refactors everything using an IndentingWriter to use a BaksmaliWriter
instead, but the usages of ReferenceUtil and EncodedValue aren't refactored
yet.
This commit is contained in:
Ben Gruver 2021-02-26 14:11:25 -08:00
parent 6efebc1543
commit 9ce00aae9c
47 changed files with 253 additions and 384 deletions

View File

@ -29,9 +29,9 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.Adaptors.EncodedValue.AnnotationEncodedValueAdaptor; import org.jf.baksmali.Adaptors.EncodedValue.AnnotationEncodedValueAdaptor;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.AnnotationVisibility; import org.jf.dexlib2.AnnotationVisibility;
import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.Annotation;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -40,7 +40,7 @@ import java.util.Collection;
public class AnnotationFormatter { public class AnnotationFormatter {
public static void writeTo(@Nonnull IndentingWriter writer, public static void writeTo(@Nonnull BaksmaliWriter writer,
@Nonnull Collection<? extends Annotation> annotations, @Nonnull Collection<? extends Annotation> annotations,
@Nullable String containingClass) throws IOException { @Nullable String containingClass) throws IOException {
boolean first = true; boolean first = true;
@ -54,7 +54,7 @@ public class AnnotationFormatter {
} }
} }
public static void writeTo(@Nonnull IndentingWriter writer, @Nonnull Annotation annotation, public static void writeTo(@Nonnull BaksmaliWriter writer, @Nonnull Annotation annotation,
@Nullable String containingClass) throws IOException { @Nullable String containingClass) throws IOException {
writer.write(".annotation "); writer.write(".annotation ");
writer.write(AnnotationVisibility.getVisibility(annotation.getVisibility())); writer.write(AnnotationVisibility.getVisibility(annotation.getVisibility()));

View File

@ -28,7 +28,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
//a "spacer" between instructions //a "spacer" between instructions
public class BlankMethodItem extends MethodItem { public class BlankMethodItem extends MethodItem {
@ -40,7 +40,7 @@ public class BlankMethodItem extends MethodItem {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
public boolean writeTo(IndentingWriter writer) { public boolean writeTo(BaksmaliWriter writer) {
//we didn't technically print something, but returning true indicates that a newline should be printed //we didn't technically print something, but returning true indicates that a newline should be printed
//after this method item, which is the intended functionality //after this method item, which is the intended functionality
return true; return true;

View File

@ -29,7 +29,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -79,7 +79,7 @@ public class CatchMethodItem extends MethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
if (exceptionType == null) { if (exceptionType == null) {
writer.write(".catchall"); writer.write(".catchall");
} else { } else {

View File

@ -29,6 +29,8 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.formatter.BaksmaliFormatter;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.dexbacked.DexBackedClassDef; import org.jf.dexlib2.dexbacked.DexBackedClassDef;
import org.jf.dexlib2.iface.*; import org.jf.dexlib2.iface.*;
@ -37,7 +39,6 @@ import org.jf.dexlib2.iface.instruction.formats.Instruction21c;
import org.jf.dexlib2.iface.reference.FieldReference; import org.jf.dexlib2.iface.reference.FieldReference;
import org.jf.dexlib2.iface.reference.Reference; import org.jf.dexlib2.iface.reference.Reference;
import org.jf.dexlib2.util.ReferenceUtil; import org.jf.dexlib2.util.ReferenceUtil;
import org.jf.util.IndentingWriter;
import org.jf.util.StringUtils; import org.jf.util.StringUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -51,12 +52,14 @@ public class ClassDefinition {
@Nonnull public final BaksmaliOptions options; @Nonnull public final BaksmaliOptions options;
@Nonnull public final ClassDef classDef; @Nonnull public final ClassDef classDef;
@Nonnull private final HashSet<String> fieldsSetInStaticConstructor; @Nonnull private final HashSet<String> fieldsSetInStaticConstructor;
@Nonnull private final BaksmaliFormatter formatter;
protected boolean validationErrors; protected boolean validationErrors;
public ClassDefinition(@Nonnull BaksmaliOptions options, @Nonnull ClassDef classDef) { public ClassDefinition(@Nonnull BaksmaliOptions options, @Nonnull ClassDef classDef) {
this.options = options; this.options = options;
this.classDef = classDef; this.classDef = classDef;
formatter = new BaksmaliFormatter(options.implicitReferences ? classDef.getType() : null);
fieldsSetInStaticConstructor = findFieldsSetInStaticConstructor(classDef); fieldsSetInStaticConstructor = findFieldsSetInStaticConstructor(classDef);
} }
@ -101,7 +104,7 @@ public class ClassDefinition {
return fieldsSetInStaticConstructor; return fieldsSetInStaticConstructor;
} }
public void writeTo(IndentingWriter writer) throws IOException { public void writeTo(BaksmaliWriter writer) throws IOException {
writeClass(writer); writeClass(writer);
writeSuper(writer); writeSuper(writer);
writeSourceFile(writer); writeSourceFile(writer);
@ -113,21 +116,21 @@ public class ClassDefinition {
writeVirtualMethods(writer, directMethods); writeVirtualMethods(writer, directMethods);
} }
private void writeClass(IndentingWriter writer) throws IOException { private void writeClass(BaksmaliWriter writer) throws IOException {
writer.write(".class "); writer.write(".class ");
writeAccessFlags(writer); writeAccessFlags(writer);
writer.write(classDef.getType()); writer.write(classDef.getType());
writer.write('\n'); writer.write('\n');
} }
private void writeAccessFlags(IndentingWriter writer) throws IOException { private void writeAccessFlags(BaksmaliWriter writer) throws IOException {
for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDef.getAccessFlags())) { for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDef.getAccessFlags())) {
writer.write(accessFlag.toString()); writer.write(accessFlag.toString());
writer.write(' '); writer.write(' ');
} }
} }
private void writeSuper(IndentingWriter writer) throws IOException { private void writeSuper(BaksmaliWriter writer) throws IOException {
String superClass = classDef.getSuperclass(); String superClass = classDef.getSuperclass();
if (superClass != null) { if (superClass != null) {
writer.write(".super "); writer.write(".super ");
@ -136,7 +139,7 @@ public class ClassDefinition {
} }
} }
private void writeSourceFile(IndentingWriter writer) throws IOException { private void writeSourceFile(BaksmaliWriter writer) throws IOException {
String sourceFile = classDef.getSourceFile(); String sourceFile = classDef.getSourceFile();
if (sourceFile != null) { if (sourceFile != null) {
writer.write(".source \""); writer.write(".source \"");
@ -145,7 +148,7 @@ public class ClassDefinition {
} }
} }
private void writeInterfaces(IndentingWriter writer) throws IOException { private void writeInterfaces(BaksmaliWriter writer) throws IOException {
List<String> interfaces = classDef.getInterfaces(); List<String> interfaces = classDef.getInterfaces();
if (interfaces.size() != 0) { if (interfaces.size() != 0) {
@ -159,7 +162,7 @@ public class ClassDefinition {
} }
} }
private void writeAnnotations(IndentingWriter writer) throws IOException { private void writeAnnotations(BaksmaliWriter writer) throws IOException {
Collection<? extends Annotation> classAnnotations = classDef.getAnnotations(); Collection<? extends Annotation> classAnnotations = classDef.getAnnotations();
if (classAnnotations.size() != 0) { if (classAnnotations.size() != 0) {
writer.write("\n\n"); writer.write("\n\n");
@ -174,7 +177,7 @@ public class ClassDefinition {
} }
} }
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException { private Set<String> writeStaticFields(BaksmaliWriter writer) throws IOException {
boolean wroteHeader = false; boolean wroteHeader = false;
Set<String> writtenFields = new HashSet<String>(); Set<String> writtenFields = new HashSet<String>();
@ -194,11 +197,11 @@ public class ClassDefinition {
writer.write('\n'); writer.write('\n');
boolean setInStaticConstructor; boolean setInStaticConstructor;
IndentingWriter fieldWriter = writer; BaksmaliWriter fieldWriter = writer;
String fieldString = ReferenceUtil.getShortFieldDescriptor(field); String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
if (!writtenFields.add(fieldString)) { if (!writtenFields.add(fieldString)) {
writer.write("# duplicate field ignored\n"); writer.write("# duplicate field ignored\n");
fieldWriter = new CommentingIndentingWriter(writer); fieldWriter = getCommentingWriter(writer);
System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString)); System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
setInStaticConstructor = false; setInStaticConstructor = false;
} else { } else {
@ -209,7 +212,7 @@ public class ClassDefinition {
return writtenFields; return writtenFields;
} }
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException { private void writeInstanceFields(BaksmaliWriter writer, Set<String> staticFields) throws IOException {
boolean wroteHeader = false; boolean wroteHeader = false;
Set<String> writtenFields = new HashSet<String>(); Set<String> writtenFields = new HashSet<String>();
@ -228,11 +231,11 @@ public class ClassDefinition {
} }
writer.write('\n'); writer.write('\n');
IndentingWriter fieldWriter = writer; BaksmaliWriter fieldWriter = writer;
String fieldString = ReferenceUtil.getShortFieldDescriptor(field); String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
if (!writtenFields.add(fieldString)) { if (!writtenFields.add(fieldString)) {
writer.write("# duplicate field ignored\n"); writer.write("# duplicate field ignored\n");
fieldWriter = new CommentingIndentingWriter(writer); fieldWriter = getCommentingWriter(writer);
System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString)); System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
} else if (staticFields.contains(fieldString)) { } else if (staticFields.contains(fieldString)) {
System.err.println(String.format("Duplicate static+instance field found: %s->%s", System.err.println(String.format("Duplicate static+instance field found: %s->%s",
@ -246,7 +249,7 @@ public class ClassDefinition {
} }
} }
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException { private Set<String> writeDirectMethods(BaksmaliWriter writer) throws IOException {
boolean wroteHeader = false; boolean wroteHeader = false;
Set<String> writtenMethods = new HashSet<String>(); Set<String> writtenMethods = new HashSet<String>();
@ -268,10 +271,10 @@ public class ClassDefinition {
// TODO: check for method validation errors // TODO: check for method validation errors
String methodString = ReferenceUtil.getMethodDescriptor(method, true); String methodString = ReferenceUtil.getMethodDescriptor(method, true);
IndentingWriter methodWriter = writer; BaksmaliWriter methodWriter = writer;
if (!writtenMethods.add(methodString)) { if (!writtenMethods.add(methodString)) {
writer.write("# duplicate method ignored\n"); writer.write("# duplicate method ignored\n");
methodWriter = new CommentingIndentingWriter(writer); methodWriter = getCommentingWriter(writer);
} }
MethodImplementation methodImpl = method.getImplementation(); MethodImplementation methodImpl = method.getImplementation();
@ -285,7 +288,7 @@ public class ClassDefinition {
return writtenMethods; return writtenMethods;
} }
private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException { private void writeVirtualMethods(BaksmaliWriter writer, Set<String> directMethods) throws IOException {
boolean wroteHeader = false; boolean wroteHeader = false;
Set<String> writtenMethods = new HashSet<String>(); Set<String> writtenMethods = new HashSet<String>();
@ -307,10 +310,10 @@ public class ClassDefinition {
// TODO: check for method validation errors // TODO: check for method validation errors
String methodString = ReferenceUtil.getMethodDescriptor(method, true); String methodString = ReferenceUtil.getMethodDescriptor(method, true);
IndentingWriter methodWriter = writer; BaksmaliWriter methodWriter = writer;
if (!writtenMethods.add(methodString)) { if (!writtenMethods.add(methodString)) {
writer.write("# duplicate method ignored\n"); writer.write("# duplicate method ignored\n");
methodWriter = new CommentingIndentingWriter(writer); methodWriter = getCommentingWriter(writer);
} else if (directMethods.contains(methodString)) { } else if (directMethods.contains(methodString)) {
writer.write("# There is both a direct and virtual method with this signature.\n" + writer.write("# There is both a direct and virtual method with this signature.\n" +
"# You will need to rename one of these methods, including all references.\n"); "# You will need to rename one of these methods, including all references.\n");
@ -328,4 +331,12 @@ public class ClassDefinition {
} }
} }
} }
public BaksmaliWriter getCommentingWriter(BaksmaliWriter writer) {
return formatter.getWriter(new CommentingIndentingWriter(writer.indentingWriter()));
}
public BaksmaliFormatter getFormatter() {
return formatter;
}
} }

View File

@ -28,7 +28,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
@ -47,7 +47,7 @@ public class CommentMethodItem extends MethodItem {
return sortOrder; return sortOrder;
} }
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write('#'); writer.write('#');
writer.write(comment); writer.write(comment);
return true; return true;

View File

@ -28,7 +28,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
@ -44,7 +44,7 @@ public class CommentedOutMethodItem extends MethodItem {
return commentedOutMethodItem.getSortOrder() + .001; return commentedOutMethodItem.getSortOrder() + .001;
} }
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write('#'); writer.write('#');
commentedOutMethodItem.writeTo(writer); commentedOutMethodItem.writeTo(writer);
return true; return true;

View File

@ -31,7 +31,7 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
@ -41,7 +41,7 @@ public class BeginEpilogueMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".prologue"); writer.write(".prologue");
return true; return true;
} }

View File

@ -32,8 +32,8 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.baksmali.Adaptors.RegisterFormatter; import org.jf.baksmali.Adaptors.RegisterFormatter;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.debug.EndLocal; import org.jf.dexlib2.iface.debug.EndLocal;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -50,7 +50,7 @@ public class EndLocalMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".end local "); writer.write(".end local ");
registerFormatter.writeTo(writer, endLocal.getRegister()); registerFormatter.writeTo(writer, endLocal.getRegister());

View File

@ -31,7 +31,7 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
@ -41,7 +41,7 @@ public class EndPrologueMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".prologue"); writer.write(".prologue");
return true; return true;
} }

View File

@ -31,8 +31,8 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.debug.LineNumber; import org.jf.dexlib2.iface.debug.LineNumber;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -46,9 +46,9 @@ public class LineNumberMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".line "); writer.write(".line ");
writer.printUnsignedIntAsDec(lineNumber); writer.writeUnsignedIntAsDec(lineNumber);
return true; return true;
} }
} }

View File

@ -32,7 +32,7 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.baksmali.Adaptors.ReferenceFormatter; import org.jf.baksmali.Adaptors.ReferenceFormatter;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -52,7 +52,7 @@ public class LocalFormatter {
* *
* One of name, type or signature must be non-null * One of name, type or signature must be non-null
*/ */
public static void writeLocal(@Nonnull IndentingWriter writer, @Nullable String name, @Nullable String type, public static void writeLocal(@Nonnull BaksmaliWriter writer, @Nullable String name, @Nullable String type,
@Nullable String signature) throws IOException { @Nullable String signature) throws IOException {
if (name != null) { if (name != null) {
ReferenceFormatter.writeStringReference(writer, name); ReferenceFormatter.writeStringReference(writer, name);

View File

@ -32,8 +32,8 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.baksmali.Adaptors.RegisterFormatter; import org.jf.baksmali.Adaptors.RegisterFormatter;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.debug.RestartLocal; import org.jf.dexlib2.iface.debug.RestartLocal;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -50,7 +50,7 @@ public class RestartLocalMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".restart local "); writer.write(".restart local ");
registerFormatter.writeTo(writer, restartLocal.getRegister()); registerFormatter.writeTo(writer, restartLocal.getRegister());

View File

@ -31,8 +31,8 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.debug.SetSourceFile; import org.jf.dexlib2.iface.debug.SetSourceFile;
import org.jf.util.IndentingWriter;
import org.jf.util.StringUtils; import org.jf.util.StringUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -48,7 +48,7 @@ public class SetSourceFileMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".source"); writer.write(".source");
if (sourceFile != null) { if (sourceFile != null) {

View File

@ -32,8 +32,8 @@
package org.jf.baksmali.Adaptors.Debug; package org.jf.baksmali.Adaptors.Debug;
import org.jf.baksmali.Adaptors.RegisterFormatter; import org.jf.baksmali.Adaptors.RegisterFormatter;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.debug.StartLocal; import org.jf.dexlib2.iface.debug.StartLocal;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -50,7 +50,7 @@ public class StartLocalMethodItem extends DebugMethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(".local "); writer.write(".local ");
registerFormatter.writeTo(writer, startLocal.getRegister()); registerFormatter.writeTo(writer, startLocal.getRegister());

View File

@ -28,9 +28,9 @@
package org.jf.baksmali.Adaptors.EncodedValue; package org.jf.baksmali.Adaptors.EncodedValue;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.AnnotationElement; import org.jf.dexlib2.iface.AnnotationElement;
import org.jf.dexlib2.iface.value.AnnotationEncodedValue; import org.jf.dexlib2.iface.value.AnnotationEncodedValue;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -39,7 +39,7 @@ import java.util.Collection;
public abstract class AnnotationEncodedValueAdaptor { public abstract class AnnotationEncodedValueAdaptor {
public static void writeTo(@Nonnull IndentingWriter writer, public static void writeTo(@Nonnull BaksmaliWriter writer,
@Nonnull AnnotationEncodedValue annotationEncodedValue, @Nonnull AnnotationEncodedValue annotationEncodedValue,
@Nullable String containingClass) throws IOException { @Nullable String containingClass) throws IOException {
writer.write(".subannotation "); writer.write(".subannotation ");
@ -50,7 +50,7 @@ public abstract class AnnotationEncodedValueAdaptor {
writer.write(".end subannotation"); writer.write(".end subannotation");
} }
public static void writeElementsTo(@Nonnull IndentingWriter writer, public static void writeElementsTo(@Nonnull BaksmaliWriter writer,
@Nonnull Collection<? extends AnnotationElement> annotationElements, @Nonnull Collection<? extends AnnotationElement> annotationElements,
@Nullable String containingClass) throws IOException { @Nullable String containingClass) throws IOException {
writer.indent(4); writer.indent(4);

View File

@ -28,9 +28,9 @@
package org.jf.baksmali.Adaptors.EncodedValue; package org.jf.baksmali.Adaptors.EncodedValue;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.value.ArrayEncodedValue; import org.jf.dexlib2.iface.value.ArrayEncodedValue;
import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.EncodedValue;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -38,7 +38,7 @@ import java.io.IOException;
import java.util.Collection; import java.util.Collection;
public class ArrayEncodedValueAdaptor { public class ArrayEncodedValueAdaptor {
public static void writeTo(@Nonnull IndentingWriter writer, public static void writeTo(@Nonnull BaksmaliWriter writer,
@Nonnull ArrayEncodedValue arrayEncodedValue, @Nonnull ArrayEncodedValue arrayEncodedValue,
@Nullable String containingClass) throws IOException { @Nullable String containingClass) throws IOException {
writer.write('{'); writer.write('{');

View File

@ -30,18 +30,18 @@ package org.jf.baksmali.Adaptors.EncodedValue;
import org.jf.baksmali.Adaptors.ReferenceFormatter; import org.jf.baksmali.Adaptors.ReferenceFormatter;
import org.jf.baksmali.Renderers.*; import org.jf.baksmali.Renderers.*;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.ReferenceType;
import org.jf.dexlib2.ValueType; import org.jf.dexlib2.ValueType;
import org.jf.dexlib2.iface.value.*; import org.jf.dexlib2.iface.value.*;
import org.jf.dexlib2.util.ReferenceUtil; import org.jf.dexlib2.util.ReferenceUtil;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.IOException; import java.io.IOException;
public abstract class EncodedValueAdaptor { public abstract class EncodedValueAdaptor {
public static void writeTo(@Nonnull IndentingWriter writer, @Nonnull EncodedValue encodedValue, public static void writeTo(@Nonnull BaksmaliWriter writer, @Nonnull EncodedValue encodedValue,
@Nullable String containingClass) @Nullable String containingClass)
throws IOException { throws IOException {
switch (encodedValue.getValueType()) { switch (encodedValue.getValueType()) {

View File

@ -30,20 +30,20 @@ package org.jf.baksmali.Adaptors;
import org.jf.baksmali.Adaptors.EncodedValue.EncodedValueAdaptor; import org.jf.baksmali.Adaptors.EncodedValue.EncodedValueAdaptor;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.AccessFlags;
import org.jf.dexlib2.HiddenApiRestriction; import org.jf.dexlib2.HiddenApiRestriction;
import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.Annotation;
import org.jf.dexlib2.iface.Field; import org.jf.dexlib2.iface.Field;
import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.EncodedValue;
import org.jf.dexlib2.util.EncodedValueUtils; import org.jf.dexlib2.util.EncodedValueUtils;
import org.jf.util.IndentingWriter;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
public class FieldDefinition { public class FieldDefinition {
public static void writeTo(BaksmaliOptions options, IndentingWriter writer, Field field, public static void writeTo(BaksmaliOptions options, BaksmaliWriter writer, Field field,
boolean setInStaticConstructor) throws IOException { boolean setInStaticConstructor) throws IOException {
EncodedValue initialValue = field.getInitialValue(); EncodedValue initialValue = field.getInitialValue();
int accessFlags = field.getAccessFlags(); int accessFlags = field.getAccessFlags();
@ -95,7 +95,7 @@ public class FieldDefinition {
} }
private static void writeAccessFlagsAndRestrictions( private static void writeAccessFlagsAndRestrictions(
IndentingWriter writer, int accessFlags, Set<HiddenApiRestriction> hiddenApiRestrictions) BaksmaliWriter writer, int accessFlags, Set<HiddenApiRestriction> hiddenApiRestrictions)
throws IOException { throws IOException {
for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForField(accessFlags)) { for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForField(accessFlags)) {
writer.write(accessFlag.toString()); writer.write(accessFlag.toString());

View File

@ -30,8 +30,8 @@ package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.baksmali.Renderers.LongRenderer; import org.jf.baksmali.Renderers.LongRenderer;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.instruction.formats.ArrayPayload; import org.jf.dexlib2.iface.instruction.formats.ArrayPayload;
import org.jf.util.IndentingWriter;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -41,11 +41,11 @@ public class ArrayDataMethodItem extends InstructionMethodItem<ArrayPayload> {
super(methodDef, codeAddress, instruction); super(methodDef, codeAddress, instruction);
} }
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
int elementWidth = instruction.getElementWidth(); int elementWidth = instruction.getElementWidth();
writer.write(".array-data "); writer.write(".array-data ");
writer.printSignedIntAsDec(instruction.getElementWidth()); writer.writeSignedIntAsDec(instruction.getElementWidth());
writer.write('\n'); writer.write('\n');
writer.indent(4); writer.indent(4);

View File

@ -34,6 +34,7 @@ import org.jf.baksmali.Adaptors.MethodItem;
import org.jf.baksmali.Adaptors.ReferenceFormatter; import org.jf.baksmali.Adaptors.ReferenceFormatter;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.Renderers.LongRenderer; import org.jf.baksmali.Renderers.LongRenderer;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.Opcode; import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.VerificationError; import org.jf.dexlib2.VerificationError;
import org.jf.dexlib2.iface.instruction.*; import org.jf.dexlib2.iface.instruction.*;
@ -44,7 +45,6 @@ import org.jf.dexlib2.iface.reference.CallSiteReference;
import org.jf.dexlib2.iface.reference.Reference; import org.jf.dexlib2.iface.reference.Reference;
import org.jf.dexlib2.util.ReferenceUtil; import org.jf.dexlib2.util.ReferenceUtil;
import org.jf.util.ExceptionWithContext; import org.jf.util.ExceptionWithContext;
import org.jf.util.IndentingWriter;
import org.jf.util.NumberUtils; import org.jf.util.NumberUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -80,11 +80,11 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
} }
private interface Writable { private interface Writable {
void writeTo(IndentingWriter writer) throws IOException; void writeTo(BaksmaliWriter writer) throws IOException;
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
Opcode opcode = instruction.getOpcode(); Opcode opcode = instruction.getOpcode();
String verificationErrorName = null; String verificationErrorName = null;
Writable referenceWritable = null; Writable referenceWritable = null;
@ -97,7 +97,7 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
verificationErrorName = VerificationError.getVerificationErrorName(verificationError); verificationErrorName = VerificationError.getVerificationErrorName(verificationError);
if (verificationErrorName == null) { if (verificationErrorName == null) {
writer.write("#was invalid verification error type: "); writer.write("#was invalid verification error type: ");
writer.printSignedIntAsDec(verificationError); writer.writeSignedIntAsDec(verificationError);
writer.write("\n"); writer.write("\n");
verificationErrorName = "generic-error"; verificationErrorName = "generic-error";
} }
@ -120,14 +120,14 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
if (reference instanceof CallSiteReference) { if (reference instanceof CallSiteReference) {
referenceWritable = new Writable() { referenceWritable = new Writable() {
@Override @Override
public void writeTo(IndentingWriter indentingWriter) throws IOException { public void writeTo(BaksmaliWriter indentingWriter) throws IOException {
ReferenceFormatter.writeCallSiteReference(indentingWriter, (CallSiteReference)reference); ReferenceFormatter.writeCallSiteReference(indentingWriter, (CallSiteReference)reference);
} }
}; };
} else { } else {
referenceWritable = new Writable() { referenceWritable = new Writable() {
@Override @Override
public void writeTo(IndentingWriter indentingWriter) throws IOException { public void writeTo(BaksmaliWriter indentingWriter) throws IOException {
indentingWriter.write(ReferenceUtil.getReferenceString(reference, classContext)); indentingWriter.write(ReferenceUtil.getReferenceString(reference, classContext));
} }
}; };
@ -220,7 +220,7 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
case Format10x: case Format10x:
if (instruction instanceof UnknownInstruction) { if (instruction instanceof UnknownInstruction) {
writer.write("#unknown opcode: 0x"); writer.write("#unknown opcode: 0x");
writer.printUnsignedLongAsHex(((UnknownInstruction)instruction).getOriginalOpcode()); writer.writeUnsignedLongAsHex(((UnknownInstruction)instruction).getOriginalOpcode());
writer.write('\n'); writer.write('\n');
} }
writeOpcode(writer); writeOpcode(writer);
@ -416,33 +416,33 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
return true; return true;
} }
protected void writeOpcode(IndentingWriter writer) throws IOException { protected void writeOpcode(BaksmaliWriter writer) throws IOException {
writer.write(instruction.getOpcode().name); writer.write(instruction.getOpcode().name);
} }
protected void writeTargetLabel(IndentingWriter writer) throws IOException { protected void writeTargetLabel(BaksmaliWriter writer) throws IOException {
//this method is overridden by OffsetInstructionMethodItem, and should only be called for the formats that //this method is overridden by OffsetInstructionMethodItem, and should only be called for the formats that
//have a target //have a target
throw new RuntimeException(); throw new RuntimeException();
} }
protected void writeRegister(IndentingWriter writer, int registerNumber) throws IOException { protected void writeRegister(BaksmaliWriter writer, int registerNumber) throws IOException {
methodDef.registerFormatter.writeTo(writer, registerNumber); methodDef.registerFormatter.writeTo(writer, registerNumber);
} }
protected void writeFirstRegister(IndentingWriter writer) throws IOException { protected void writeFirstRegister(BaksmaliWriter writer) throws IOException {
writeRegister(writer, ((OneRegisterInstruction)instruction).getRegisterA()); writeRegister(writer, ((OneRegisterInstruction)instruction).getRegisterA());
} }
protected void writeSecondRegister(IndentingWriter writer) throws IOException { protected void writeSecondRegister(BaksmaliWriter writer) throws IOException {
writeRegister(writer, ((TwoRegisterInstruction)instruction).getRegisterB()); writeRegister(writer, ((TwoRegisterInstruction)instruction).getRegisterB());
} }
protected void writeThirdRegister(IndentingWriter writer) throws IOException { protected void writeThirdRegister(BaksmaliWriter writer) throws IOException {
writeRegister(writer, ((ThreeRegisterInstruction) instruction).getRegisterC()); writeRegister(writer, ((ThreeRegisterInstruction) instruction).getRegisterC());
} }
protected void writeInvokeRegisters(IndentingWriter writer) throws IOException { protected void writeInvokeRegisters(BaksmaliWriter writer) throws IOException {
FiveRegisterInstruction instruction = (FiveRegisterInstruction)this.instruction; FiveRegisterInstruction instruction = (FiveRegisterInstruction)this.instruction;
final int regCount = instruction.getRegisterCount(); final int regCount = instruction.getRegisterCount();
@ -487,7 +487,7 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
writer.write('}'); writer.write('}');
} }
protected void writeInvokeRangeRegisters(IndentingWriter writer) throws IOException { protected void writeInvokeRangeRegisters(BaksmaliWriter writer) throws IOException {
RegisterRangeInstruction instruction = (RegisterRangeInstruction)this.instruction; RegisterRangeInstruction instruction = (RegisterRangeInstruction)this.instruction;
int regCount = instruction.getRegisterCount(); int regCount = instruction.getRegisterCount();
@ -499,15 +499,15 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
} }
} }
protected void writeLiteral(IndentingWriter writer) throws IOException { protected void writeLiteral(BaksmaliWriter writer) throws IOException {
LongRenderer.writeSignedIntOrLongTo(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); LongRenderer.writeSignedIntOrLongTo(writer, ((WideLiteralInstruction)instruction).getWideLiteral());
} }
protected void writeCommentIfLikelyFloat(IndentingWriter writer) throws IOException { protected void writeCommentIfLikelyFloat(BaksmaliWriter writer) throws IOException {
writeCommentIfLikelyFloat(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); writeCommentIfLikelyFloat(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral());
} }
protected void writeCommentIfLikelyFloat(IndentingWriter writer, int val) throws IOException { protected void writeCommentIfLikelyFloat(BaksmaliWriter writer, int val) throws IOException {
if (NumberUtils.isLikelyFloat(val)) { if (NumberUtils.isLikelyFloat(val)) {
writer.write(" # "); writer.write(" # ");
float fval = Float.intBitsToFloat(val); float fval = Float.intBitsToFloat(val);
@ -530,11 +530,11 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
} }
} }
protected void writeCommentIfLikelyDouble(IndentingWriter writer) throws IOException { protected void writeCommentIfLikelyDouble(BaksmaliWriter writer) throws IOException {
writeCommentIfLikelyDouble(writer, ((WideLiteralInstruction)instruction).getWideLiteral()); writeCommentIfLikelyDouble(writer, ((WideLiteralInstruction)instruction).getWideLiteral());
} }
protected void writeCommentIfLikelyDouble(IndentingWriter writer, long val) throws IOException { protected void writeCommentIfLikelyDouble(BaksmaliWriter writer, long val) throws IOException {
if (NumberUtils.isLikelyDouble(val)) { if (NumberUtils.isLikelyDouble(val)) {
writer.write(" # "); writer.write(" # ");
double dval = Double.longBitsToDouble(val); double dval = Double.longBitsToDouble(val);
@ -555,11 +555,11 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
} }
} }
protected boolean writeCommentIfResourceId(IndentingWriter writer) throws IOException { protected boolean writeCommentIfResourceId(BaksmaliWriter writer) throws IOException {
return writeCommentIfResourceId(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral()); return writeCommentIfResourceId(writer, ((NarrowLiteralInstruction)instruction).getNarrowLiteral());
} }
protected boolean writeCommentIfResourceId(IndentingWriter writer, int val) throws IOException { protected boolean writeCommentIfResourceId(BaksmaliWriter writer, int val) throws IOException {
Map<Integer,String> resourceIds = methodDef.classDef.options.resourceIds; Map<Integer,String> resourceIds = methodDef.classDef.options.resourceIds;
String resource = resourceIds.get(Integer.valueOf(val)); String resource = resourceIds.get(Integer.valueOf(val));
if (resource != null) { if (resource != null) {
@ -570,18 +570,18 @@ public class InstructionMethodItem<T extends Instruction> extends MethodItem {
return false; return false;
} }
protected void writeFieldOffset(IndentingWriter writer) throws IOException { protected void writeFieldOffset(BaksmaliWriter writer) throws IOException {
writer.write("field@0x"); writer.write("field@0x");
writer.printUnsignedLongAsHex(((FieldOffsetInstruction)instruction).getFieldOffset()); writer.writeUnsignedLongAsHex(((FieldOffsetInstruction)instruction).getFieldOffset());
} }
protected void writeInlineIndex(IndentingWriter writer) throws IOException { protected void writeInlineIndex(BaksmaliWriter writer) throws IOException {
writer.write("inline@"); writer.write("inline@");
writer.printSignedIntAsDec(((InlineIndexInstruction)instruction).getInlineIndex()); writer.writeSignedIntAsDec(((InlineIndexInstruction)instruction).getInlineIndex());
} }
protected void writeVtableIndex(IndentingWriter writer) throws IOException { protected void writeVtableIndex(BaksmaliWriter writer) throws IOException {
writer.write("vtable@"); writer.write("vtable@");
writer.printSignedIntAsDec(((VtableIndexInstruction)instruction).getVtableIndex()); writer.writeSignedIntAsDec(((VtableIndexInstruction)instruction).getVtableIndex());
} }
} }

View File

@ -31,9 +31,9 @@ package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.LabelMethodItem; import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.Opcode; import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.iface.instruction.OffsetInstruction; import org.jf.dexlib2.iface.instruction.OffsetInstruction;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -50,7 +50,7 @@ public class OffsetInstructionFormatMethodItem extends InstructionMethodItem<Off
} }
@Override @Override
protected void writeTargetLabel(IndentingWriter writer) throws IOException { protected void writeTargetLabel(BaksmaliWriter writer) throws IOException {
label.writeTo(writer); label.writeTo(writer);
} }

View File

@ -28,13 +28,12 @@
package org.jf.baksmali.Adaptors.Format; package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
import org.jf.baksmali.Adaptors.LabelMethodItem; import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.baksmali.Renderers.IntegerRenderer;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.instruction.SwitchElement; import org.jf.dexlib2.iface.instruction.SwitchElement;
import org.jf.dexlib2.iface.instruction.formats.PackedSwitchPayload; import org.jf.dexlib2.iface.instruction.formats.PackedSwitchPayload;
import org.jf.util.IndentingWriter;
import org.jf.baksmali.Renderers.IntegerRenderer;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -81,9 +80,9 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
if (commentedOut) { if (commentedOut) {
writer = new CommentingIndentingWriter(writer); writer = methodDef.classDef.getCommentingWriter(writer);
} }
writer.write(".packed-switch "); writer.write(".packed-switch ");
IntegerRenderer.writeTo(writer, firstKey); IntegerRenderer.writeTo(writer, firstKey);
@ -102,7 +101,7 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
} }
private static abstract class PackedSwitchTarget { private static abstract class PackedSwitchTarget {
public abstract void writeTargetTo(IndentingWriter writer) throws IOException; public abstract void writeTargetTo(BaksmaliWriter writer) throws IOException;
} }
private static class PackedSwitchLabelTarget extends PackedSwitchTarget { private static class PackedSwitchLabelTarget extends PackedSwitchTarget {
@ -110,7 +109,7 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
public PackedSwitchLabelTarget(LabelMethodItem target) { public PackedSwitchLabelTarget(LabelMethodItem target) {
this.target = target; this.target = target;
} }
public void writeTargetTo(IndentingWriter writer) throws IOException { public void writeTargetTo(BaksmaliWriter writer) throws IOException {
target.writeTo(writer); target.writeTo(writer);
} }
} }
@ -120,11 +119,11 @@ public class PackedSwitchMethodItem extends InstructionMethodItem<PackedSwitchPa
public PackedSwitchOffsetTarget(int target) { public PackedSwitchOffsetTarget(int target) {
this.target = target; this.target = target;
} }
public void writeTargetTo(IndentingWriter writer) throws IOException { public void writeTargetTo(BaksmaliWriter writer) throws IOException {
if (target >= 0) { if (target >= 0) {
writer.write('+'); writer.write('+');
} }
writer.printSignedIntAsDec(target); writer.writeSignedIntAsDec(target);
} }
} }
} }

View File

@ -28,13 +28,12 @@
package org.jf.baksmali.Adaptors.Format; package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.CommentingIndentingWriter;
import org.jf.baksmali.Adaptors.LabelMethodItem; import org.jf.baksmali.Adaptors.LabelMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.baksmali.Renderers.IntegerRenderer;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.instruction.SwitchElement; import org.jf.dexlib2.iface.instruction.SwitchElement;
import org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload; import org.jf.dexlib2.iface.instruction.formats.SparseSwitchPayload;
import org.jf.util.IndentingWriter;
import org.jf.baksmali.Renderers.IntegerRenderer;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -69,9 +68,9 @@ public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPa
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
if (commentedOut) { if (commentedOut) {
writer = new CommentingIndentingWriter(writer); writer = methodDef.classDef.getCommentingWriter(writer);
} }
writer.write(".sparse-switch\n"); writer.write(".sparse-switch\n");
@ -94,7 +93,7 @@ public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPa
this.key = key; this.key = key;
} }
public int getKey() { return key; } public int getKey() { return key; }
public abstract void writeTargetTo(IndentingWriter writer) throws IOException; public abstract void writeTargetTo(BaksmaliWriter writer) throws IOException;
} }
private static class SparseSwitchLabelTarget extends SparseSwitchTarget { private static class SparseSwitchLabelTarget extends SparseSwitchTarget {
@ -104,7 +103,7 @@ public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPa
this.target = target; this.target = target;
} }
public void writeTargetTo(IndentingWriter writer) throws IOException { public void writeTargetTo(BaksmaliWriter writer) throws IOException {
target.writeTo(writer); target.writeTo(writer);
} }
} }
@ -116,11 +115,11 @@ public class SparseSwitchMethodItem extends InstructionMethodItem<SparseSwitchPa
this.target = target; this.target = target;
} }
public void writeTargetTo(IndentingWriter writer) throws IOException { public void writeTargetTo(BaksmaliWriter writer) throws IOException {
if (target >= 0) { if (target >= 0) {
writer.write('+'); writer.write('+');
} }
writer.printSignedIntAsDec(target); writer.writeSignedIntAsDec(target);
} }
} }
} }

View File

@ -29,8 +29,8 @@
package org.jf.baksmali.Adaptors.Format; package org.jf.baksmali.Adaptors.Format;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.analysis.UnresolvedOdexInstruction; import org.jf.dexlib2.analysis.UnresolvedOdexInstruction;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -41,12 +41,12 @@ public class UnresolvedOdexInstructionMethodItem extends InstructionMethodItem<U
super(methodDef, codeAddress, instruction); super(methodDef, codeAddress, instruction);
} }
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writeThrowTo(writer); writeThrowTo(writer);
return true; return true;
} }
private void writeThrowTo(IndentingWriter writer) throws IOException { private void writeThrowTo(BaksmaliWriter writer) throws IOException {
writer.write("#Replaced unresolvable odex instruction with a throw\n"); writer.write("#Replaced unresolvable odex instruction with a throw\n");
writer.write("throw "); writer.write("throw ");
writeRegister(writer, instruction.objectRegisterNum); writeRegister(writer, instruction.objectRegisterNum);

View File

@ -29,7 +29,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -73,13 +73,13 @@ public class LabelMethodItem extends MethodItem {
} }
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write(':'); writer.write(':');
writer.write(labelPrefix); writer.write(labelPrefix);
if (options.sequentialLabels) { if (options.sequentialLabels) {
writer.printUnsignedLongAsHex(labelSequence); writer.writeUnsignedLongAsHex(labelSequence);
} else { } else {
writer.printUnsignedLongAsHex(this.getLabelAddress()); writer.writeUnsignedLongAsHex(this.getLabelAddress());
} }
return true; return true;
} }

View File

@ -33,6 +33,7 @@ import com.google.common.collect.Lists;
import org.jf.baksmali.Adaptors.Debug.DebugMethodItem; import org.jf.baksmali.Adaptors.Debug.DebugMethodItem;
import org.jf.baksmali.Adaptors.Format.InstructionMethodItemFactory; import org.jf.baksmali.Adaptors.Format.InstructionMethodItemFactory;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.*; import org.jf.dexlib2.*;
import org.jf.dexlib2.analysis.AnalysisException; import org.jf.dexlib2.analysis.AnalysisException;
import org.jf.dexlib2.analysis.AnalyzedInstruction; import org.jf.dexlib2.analysis.AnalyzedInstruction;
@ -53,7 +54,6 @@ import org.jf.dexlib2.util.SyntheticAccessorResolver;
import org.jf.dexlib2.util.SyntheticAccessorResolver.AccessedMember; import org.jf.dexlib2.util.SyntheticAccessorResolver.AccessedMember;
import org.jf.dexlib2.util.TypeUtils; import org.jf.dexlib2.util.TypeUtils;
import org.jf.util.ExceptionWithContext; import org.jf.util.ExceptionWithContext;
import org.jf.util.IndentingWriter;
import org.jf.util.SparseIntArray; import org.jf.util.SparseIntArray;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -159,7 +159,7 @@ public class MethodDefinition {
} }
} }
public static void writeEmptyMethodTo(IndentingWriter writer, Method method, public static void writeEmptyMethodTo(BaksmaliWriter writer, Method method,
BaksmaliOptions options) throws IOException { BaksmaliOptions options) throws IOException {
writer.write(".method "); writer.write(".method ");
writeAccessFlagsAndRestrictions(writer, method.getAccessFlags(), method.getHiddenApiRestrictions()); writeAccessFlagsAndRestrictions(writer, method.getAccessFlags(), method.getHiddenApiRestrictions());
@ -186,7 +186,7 @@ public class MethodDefinition {
writer.write(".end method\n"); writer.write(".end method\n");
} }
public void writeTo(IndentingWriter writer) throws IOException { public void writeTo(BaksmaliWriter writer) throws IOException {
int parameterRegisterCount = 0; int parameterRegisterCount = 0;
if (!AccessFlags.STATIC.isSet(method.getAccessFlags())) { if (!AccessFlags.STATIC.isSet(method.getAccessFlags())) {
parameterRegisterCount++; parameterRegisterCount++;
@ -211,10 +211,10 @@ public class MethodDefinition {
writer.indent(4); writer.indent(4);
if (classDef.options.localsDirective) { if (classDef.options.localsDirective) {
writer.write(".locals "); writer.write(".locals ");
writer.printSignedIntAsDec(methodImpl.getRegisterCount() - parameterRegisterCount); writer.writeSignedIntAsDec(methodImpl.getRegisterCount() - parameterRegisterCount);
} else { } else {
writer.write(".registers "); writer.write(".registers ");
writer.printSignedIntAsDec(methodImpl.getRegisterCount()); writer.writeSignedIntAsDec(methodImpl.getRegisterCount());
} }
writer.write('\n'); writer.write('\n');
writeParameters(writer, method, methodParameters, classDef.options); writeParameters(writer, method, methodParameters, classDef.options);
@ -301,7 +301,7 @@ public class MethodDefinition {
} }
private static void writeAccessFlagsAndRestrictions( private static void writeAccessFlagsAndRestrictions(
IndentingWriter writer, int accessFlags, Set<HiddenApiRestriction> hiddenApiRestrictions) BaksmaliWriter writer, int accessFlags, Set<HiddenApiRestriction> hiddenApiRestrictions)
throws IOException { throws IOException {
for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForMethod(accessFlags)) { for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForMethod(accessFlags)) {
writer.write(accessFlag.toString()); writer.write(accessFlag.toString());
@ -313,7 +313,7 @@ public class MethodDefinition {
} }
} }
private static void writeParameters(IndentingWriter writer, Method method, private static void writeParameters(BaksmaliWriter writer, Method method,
List<? extends MethodParameter> parameters, List<? extends MethodParameter> parameters,
BaksmaliOptions options) throws IOException { BaksmaliOptions options) throws IOException {
boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags()); boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
@ -324,7 +324,7 @@ public class MethodDefinition {
Collection<? extends Annotation> annotations = parameter.getAnnotations(); Collection<? extends Annotation> annotations = parameter.getAnnotations();
if ((options.debugInfo && parameterName != null) || annotations.size() != 0) { if ((options.debugInfo && parameterName != null) || annotations.size() != 0) {
writer.write(".param p"); writer.write(".param p");
writer.printSignedIntAsDec(registerNumber); writer.writeSignedIntAsDec(registerNumber);
if (parameterName != null && options.debugInfo) { if (parameterName != null && options.debugInfo) {
writer.write(", "); writer.write(", ");
@ -426,9 +426,9 @@ public class MethodDefinition {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write("#@"); writer.write("#@");
writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL); writer.writeUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL);
return true; return true;
} }
}); });
@ -505,9 +505,9 @@ public class MethodDefinition {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write("#@"); writer.write("#@");
writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL); writer.writeUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL);
return true; return true;
} }
}); });

View File

@ -28,7 +28,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
@ -55,5 +55,5 @@ public abstract class MethodItem implements Comparable<MethodItem> {
return result; return result;
} }
public abstract boolean writeTo(IndentingWriter writer) throws IOException; public abstract boolean writeTo(BaksmaliWriter writer) throws IOException;
} }

View File

@ -29,9 +29,9 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.analysis.AnalyzedInstruction; import org.jf.dexlib2.analysis.AnalyzedInstruction;
import org.jf.dexlib2.analysis.RegisterType; import org.jf.dexlib2.analysis.RegisterType;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -55,7 +55,7 @@ public class PostInstructionRegisterInfoMethodItem extends MethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
int registerInfo = registerFormatter.options.registerInfo; int registerInfo = registerFormatter.options.registerInfo;
int registerCount = analyzedInstruction.getRegisterCount(); int registerCount = analyzedInstruction.getRegisterCount();
BitSet registers = new BitSet(registerCount); BitSet registers = new BitSet(registerCount);
@ -82,7 +82,7 @@ public class PostInstructionRegisterInfoMethodItem extends MethodItem {
} }
} }
private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers) throws IOException { private boolean writeRegisterInfo(BaksmaliWriter writer, BitSet registers) throws IOException {
int registerNum = registers.nextSetBit(0); int registerNum = registers.nextSetBit(0);
if (registerNum < 0) { if (registerNum < 0) {
return false; return false;

View File

@ -29,11 +29,11 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.analysis.AnalyzedInstruction; import org.jf.dexlib2.analysis.AnalyzedInstruction;
import org.jf.dexlib2.analysis.MethodAnalyzer; import org.jf.dexlib2.analysis.MethodAnalyzer;
import org.jf.dexlib2.analysis.RegisterType; import org.jf.dexlib2.analysis.RegisterType;
import org.jf.dexlib2.iface.instruction.*; import org.jf.dexlib2.iface.instruction.*;
import org.jf.util.IndentingWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -63,7 +63,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem {
} }
@Override @Override
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
int registerCount = analyzedInstruction.getRegisterCount(); int registerCount = analyzedInstruction.getRegisterCount();
BitSet registers = new BitSet(registerCount); BitSet registers = new BitSet(registerCount);
BitSet mergeRegisters = null; BitSet mergeRegisters = null;
@ -171,7 +171,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem {
registers.set(registerCount-parameterRegisterCount, registerCount); registers.set(registerCount-parameterRegisterCount, registerCount);
} }
private void writeFullMerge(IndentingWriter writer, int registerNum) throws IOException { private void writeFullMerge(BaksmaliWriter writer, int registerNum) throws IOException {
registerFormatter.writeTo(writer, registerNum); registerFormatter.writeTo(writer, registerNum);
writer.write('='); writer.write('=');
analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer); analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer);
@ -192,7 +192,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem {
writer.write("Start:"); writer.write("Start:");
} else { } else {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor)); writer.writeUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor));
writer.write(':'); writer.write(':');
} }
predecessorRegisterType.writeTo(writer); predecessorRegisterType.writeTo(writer);
@ -202,7 +202,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem {
writer.write('}'); writer.write('}');
} }
private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers, private boolean writeRegisterInfo(BaksmaliWriter writer, BitSet registers,
BitSet fullMergeRegisters) throws IOException { BitSet fullMergeRegisters) throws IOException {
boolean firstRegister = true; boolean firstRegister = true;
boolean previousWasFullMerge = false; boolean previousWasFullMerge = false;

View File

@ -29,24 +29,24 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.Adaptors.EncodedValue.EncodedValueAdaptor; import org.jf.baksmali.Adaptors.EncodedValue.EncodedValueAdaptor;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.MethodHandleType; import org.jf.dexlib2.MethodHandleType;
import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.ReferenceType;
import org.jf.dexlib2.iface.reference.*; import org.jf.dexlib2.iface.reference.*;
import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.iface.value.EncodedValue;
import org.jf.dexlib2.util.ReferenceUtil; import org.jf.dexlib2.util.ReferenceUtil;
import org.jf.util.IndentingWriter;
import org.jf.util.StringUtils; import org.jf.util.StringUtils;
import java.io.IOException; import java.io.IOException;
public class ReferenceFormatter { public class ReferenceFormatter {
public static void writeStringReference(IndentingWriter writer, String item) throws IOException { public static void writeStringReference(BaksmaliWriter writer, String item) throws IOException {
writer.write('"'); writer.write('"');
StringUtils.writeEscapedString(writer, item); StringUtils.writeEscapedString(writer, item);
writer.write('"'); writer.write('"');
} }
public static void writeCallSiteReference(IndentingWriter writer, CallSiteReference callSite) throws IOException { public static void writeCallSiteReference(BaksmaliWriter writer, CallSiteReference callSite) throws IOException {
writer.write(callSite.getName()); writer.write(callSite.getName());
writer.write('('); writer.write('(');
writer.write('"'); writer.write('"');
@ -66,7 +66,7 @@ public class ReferenceFormatter {
writeReference(writer, ReferenceType.METHOD, callSite.getMethodHandle().getMemberReference()); writeReference(writer, ReferenceType.METHOD, callSite.getMethodHandle().getMemberReference());
} }
public static void writeReference(IndentingWriter writer, int referenceType, public static void writeReference(BaksmaliWriter writer, int referenceType,
Reference reference) throws IOException { Reference reference) throws IOException {
switch (referenceType) { switch (referenceType) {
case ReferenceType.STRING: case ReferenceType.STRING:

View File

@ -29,7 +29,7 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.BaksmaliOptions;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.io.IOException; import java.io.IOException;
@ -53,27 +53,27 @@ public class RegisterFormatter {
* output the registers in the v<n> format. But if false, then it will check if *both* registers are parameter * output the registers in the v<n> format. But if false, then it will check if *both* registers are parameter
* registers, and if so, use the p<n> format for both. If only the last register is a parameter register, it will * registers, and if so, use the p<n> format for both. If only the last register is a parameter register, it will
* use the v<n> format for both, otherwise it would be confusing to have something like {v20 .. p1} * use the v<n> format for both, otherwise it would be confusing to have something like {v20 .. p1}
* @param writer the <code>IndentingWriter</code> to write to * @param writer the <code>BaksmaliWriter</code> to write to
* @param startRegister the first register in the range * @param startRegister the first register in the range
* @param lastRegister the last register in the range * @param lastRegister the last register in the range
*/ */
public void writeRegisterRange(IndentingWriter writer, int startRegister, int lastRegister) throws IOException { public void writeRegisterRange(BaksmaliWriter writer, int startRegister, int lastRegister) throws IOException {
if (options.parameterRegisters) { if (options.parameterRegisters) {
assert startRegister <= lastRegister; assert startRegister <= lastRegister;
if (startRegister >= registerCount - parameterRegisterCount) { if (startRegister >= registerCount - parameterRegisterCount) {
writer.write("{p"); writer.write("{p");
writer.printSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount)); writer.writeSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount));
writer.write(" .. p"); writer.write(" .. p");
writer.printSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount)); writer.writeSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount));
writer.write('}'); writer.write('}');
return; return;
} }
} }
writer.write("{v"); writer.write("{v");
writer.printSignedIntAsDec(startRegister); writer.writeSignedIntAsDec(startRegister);
writer.write(" .. v"); writer.write(" .. v");
writer.printSignedIntAsDec(lastRegister); writer.writeSignedIntAsDec(lastRegister);
writer.write('}'); writer.write('}');
} }
@ -82,18 +82,18 @@ public class RegisterFormatter {
* output a register in the v<n> format. If false, then it determines if the register is a parameter register, * output a register in the v<n> format. If false, then it determines if the register is a parameter register,
* and if so, formats it in the p<n> format instead. * and if so, formats it in the p<n> format instead.
* *
* @param writer the <code>IndentingWriter</code> to write to * @param writer the <code>BaksmaliWriter</code> to write to
* @param register the register number * @param register the register number
*/ */
public void writeTo(IndentingWriter writer, int register) throws IOException { public void writeTo(BaksmaliWriter writer, int register) throws IOException {
if (options.parameterRegisters) { if (options.parameterRegisters) {
if (register >= registerCount - parameterRegisterCount) { if (register >= registerCount - parameterRegisterCount) {
writer.write('p'); writer.write('p');
writer.printSignedIntAsDec((register - (registerCount - parameterRegisterCount))); writer.writeSignedIntAsDec((register - (registerCount - parameterRegisterCount)));
return; return;
} }
} }
writer.write('v'); writer.write('v');
writer.printSignedIntAsDec(register); writer.writeSignedIntAsDec(register);
} }
} }

View File

@ -28,10 +28,10 @@
package org.jf.baksmali.Adaptors; package org.jf.baksmali.Adaptors;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.ReferenceType;
import org.jf.dexlib2.util.SyntheticAccessorResolver; import org.jf.dexlib2.util.SyntheticAccessorResolver;
import org.jf.util.ExceptionWithContext; import org.jf.util.ExceptionWithContext;
import org.jf.util.IndentingWriter;
import java.io.IOException; import java.io.IOException;
@ -48,7 +48,7 @@ public class SyntheticAccessCommentMethodItem extends MethodItem {
return 99.8; return 99.8;
} }
public boolean writeTo(IndentingWriter writer) throws IOException { public boolean writeTo(BaksmaliWriter writer) throws IOException {
writer.write("# "); writer.write("# ");
switch (accessedMember.accessedMemberType) { switch (accessedMember.accessedMemberType) {
case SyntheticAccessorResolver.METHOD: case SyntheticAccessorResolver.METHOD:

View File

@ -31,10 +31,10 @@ package org.jf.baksmali;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import org.jf.baksmali.Adaptors.ClassDefinition; import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.DexFile; import org.jf.dexlib2.iface.DexFile;
import org.jf.util.ClassFileNameHandler; import org.jf.util.ClassFileNameHandler;
import org.jf.util.IndentingWriter;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.*; import java.io.*;
@ -131,7 +131,7 @@ public class Baksmali {
ClassDefinition classDefinition = new ClassDefinition(options, classDef); ClassDefinition classDefinition = new ClassDefinition(options, classDef);
//write the disassembly //write the disassembly
Writer writer = null; BaksmaliWriter writer = null;
try try
{ {
File smaliParent = smaliFile.getParentFile(); File smaliParent = smaliFile.getParentFile();
@ -155,8 +155,10 @@ public class Baksmali {
BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter( BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(smaliFile), "UTF8")); new FileOutputStream(smaliFile), "UTF8"));
writer = new IndentingWriter(bufWriter); writer = new BaksmaliWriter(
classDefinition.writeTo((IndentingWriter)writer); bufWriter,
options.implicitReferences ? classDef.getType() : null);
classDefinition.writeTo(writer);
} catch (Exception ex) { } catch (Exception ex) {
System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class");
ex.printStackTrace(); ex.printStackTrace();

View File

@ -28,12 +28,12 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class BooleanRenderer { public class BooleanRenderer {
public static void writeTo(IndentingWriter writer, boolean val) throws IOException { public static void writeTo(BaksmaliWriter writer, boolean val) throws IOException {
if (val) { if (val) {
writer.write("true"); writer.write("true");
} else { } else {

View File

@ -28,26 +28,26 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class ByteRenderer { public class ByteRenderer {
public static void writeTo(IndentingWriter writer, byte val) throws IOException { public static void writeTo(BaksmaliWriter writer, byte val) throws IOException {
if (val<0) { if (val<0) {
writer.write("-0x"); writer.write("-0x");
writer.printUnsignedLongAsHex(-val); writer.writeUnsignedLongAsHex(-val);
writer.write('t'); writer.write('t');
} else { } else {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val); writer.writeUnsignedLongAsHex(val);
writer.write('t'); writer.write('t');
} }
} }
public static void writeUnsignedTo(IndentingWriter writer, byte val) throws IOException { public static void writeUnsignedTo(BaksmaliWriter writer, byte val) throws IOException {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val & 0xFF); writer.writeUnsignedLongAsHex(val & 0xFF);
writer.write('t'); writer.write('t');
} }
} }

View File

@ -28,13 +28,13 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.util.StringUtils; import org.jf.util.StringUtils;
import java.io.IOException; import java.io.IOException;
public class CharRenderer { public class CharRenderer {
public static void writeTo(IndentingWriter writer, char val) throws IOException { public static void writeTo(BaksmaliWriter writer, char val) throws IOException {
writer.write('\''); writer.write('\'');
StringUtils.writeEscapedChar(writer, val); StringUtils.writeEscapedChar(writer, val);
writer.write('\''); writer.write('\'');

View File

@ -28,12 +28,12 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class DoubleRenderer { public class DoubleRenderer {
public static void writeTo(IndentingWriter writer, double val) throws IOException { public static void writeTo(BaksmaliWriter writer, double val) throws IOException {
writer.write(Double.toString(val)); writer.write(Double.toString(val));
} }
} }

View File

@ -28,12 +28,12 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class FloatRenderer { public class FloatRenderer {
public static void writeTo(IndentingWriter writer, float val) throws IOException { public static void writeTo(BaksmaliWriter writer, float val) throws IOException {
writer.write(Float.toString(val)); writer.write(Float.toString(val));
writer.write('f'); writer.write('f');
} }

View File

@ -28,23 +28,23 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class IntegerRenderer { public class IntegerRenderer {
public static void writeTo(IndentingWriter writer, int val) throws IOException { public static void writeTo(BaksmaliWriter writer, int val) throws IOException {
if (val<0) { if (val<0) {
writer.write("-0x"); writer.write("-0x");
writer.printUnsignedLongAsHex(-((long) val)); writer.writeUnsignedLongAsHex(-((long) val));
} else { } else {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val); writer.writeUnsignedLongAsHex(val);
} }
} }
public static void writeUnsignedTo(IndentingWriter writer, int val) throws IOException { public static void writeUnsignedTo(BaksmaliWriter writer, int val) throws IOException {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val & 0xFFFFFFFFL); writer.writeUnsignedLongAsHex(val & 0xFFFFFFFFL);
} }
} }

View File

@ -28,33 +28,33 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class LongRenderer { public class LongRenderer {
public static void writeTo(IndentingWriter writer, long val) throws IOException { public static void writeTo(BaksmaliWriter writer, long val) throws IOException {
if (val<0) { if (val<0) {
writer.write("-0x"); writer.write("-0x");
writer.printUnsignedLongAsHex(-val); writer.writeUnsignedLongAsHex(-val);
writer.write('L'); writer.write('L');
} else { } else {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val); writer.writeUnsignedLongAsHex(val);
writer.write('L'); writer.write('L');
} }
} }
public static void writeSignedIntOrLongTo(IndentingWriter writer, long val) throws IOException { public static void writeSignedIntOrLongTo(BaksmaliWriter writer, long val) throws IOException {
if (val<0) { if (val<0) {
writer.write("-0x"); writer.write("-0x");
writer.printUnsignedLongAsHex(-val); writer.writeUnsignedLongAsHex(-val);
if (val < Integer.MIN_VALUE) { if (val < Integer.MIN_VALUE) {
writer.write('L'); writer.write('L');
} }
} else { } else {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val); writer.writeUnsignedLongAsHex(val);
if (val > Integer.MAX_VALUE) { if (val > Integer.MAX_VALUE) {
writer.write('L'); writer.write('L');
} }

View File

@ -28,19 +28,19 @@
package org.jf.baksmali.Renderers; package org.jf.baksmali.Renderers;
import org.jf.util.IndentingWriter; import org.jf.baksmali.formatter.BaksmaliWriter;
import java.io.IOException; import java.io.IOException;
public class ShortRenderer { public class ShortRenderer {
public static void writeTo(IndentingWriter writer, short val) throws IOException { public static void writeTo(BaksmaliWriter writer, short val) throws IOException {
if (val < 0) { if (val < 0) {
writer.write("-0x"); writer.write("-0x");
writer.printUnsignedLongAsHex(-val); writer.writeUnsignedLongAsHex(-val);
writer.write('s'); writer.write('s');
} else { } else {
writer.write("0x"); writer.write("0x");
writer.printUnsignedLongAsHex(val); writer.writeUnsignedLongAsHex(val);
writer.write('s'); writer.write('s');
} }
} }

View File

@ -35,13 +35,13 @@ import com.google.common.base.Charsets;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import junit.framework.Assert; import junit.framework.Assert;
import org.jf.baksmali.Adaptors.ClassDefinition; import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.DexFileFactory; import org.jf.dexlib2.DexFileFactory;
import org.jf.dexlib2.Opcodes; import org.jf.dexlib2.Opcodes;
import org.jf.dexlib2.analysis.ClassPath; import org.jf.dexlib2.analysis.ClassPath;
import org.jf.dexlib2.analysis.ClassProvider; import org.jf.dexlib2.analysis.ClassProvider;
import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.ClassDef;
import org.jf.dexlib2.iface.DexFile; import org.jf.dexlib2.iface.DexFile;
import org.jf.util.IndentingWriter;
import org.junit.Test; import org.junit.Test;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -111,7 +111,7 @@ public class AnalysisTest {
for (ClassDef classDef: dexFile.getClasses()) { for (ClassDef classDef: dexFile.getClasses()) {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(stringWriter); BaksmaliWriter writer = new BaksmaliWriter(stringWriter);
ClassDefinition classDefinition = new ClassDefinition(options, classDef); ClassDefinition classDefinition = new ClassDefinition(options, classDef);
classDefinition.writeTo(writer); classDefinition.writeTo(writer);
writer.close(); writer.close();

View File

@ -35,9 +35,9 @@ import com.google.common.io.ByteStreams;
import junit.framework.Assert; import junit.framework.Assert;
import org.antlr.runtime.RecognitionException; import org.antlr.runtime.RecognitionException;
import org.jf.baksmali.Adaptors.ClassDefinition; import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.ClassDef;
import org.jf.smali.SmaliTestUtils; import org.jf.smali.SmaliTestUtils;
import org.jf.util.IndentingWriter;
import org.junit.Test; import org.junit.Test;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -88,7 +88,7 @@ public class BaksmaliTestUtils {
boolean stripComments) boolean stripComments)
throws IOException { throws IOException {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(stringWriter); BaksmaliWriter writer = new BaksmaliWriter(stringWriter);
ClassDefinition classDefinition = new ClassDefinition(options, classDef); ClassDefinition classDefinition = new ClassDefinition(options, classDef);
classDefinition.writeTo(writer); classDefinition.writeTo(writer);
writer.close(); writer.close();

View File

@ -37,6 +37,7 @@ import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.baksmali.Adaptors.Format.InstructionMethodItem; import org.jf.baksmali.Adaptors.Format.InstructionMethodItem;
import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.MethodDefinition;
import org.jf.baksmali.Adaptors.RegisterFormatter; import org.jf.baksmali.Adaptors.RegisterFormatter;
import org.jf.baksmali.formatter.BaksmaliWriter;
import org.jf.dexlib2.Format; import org.jf.dexlib2.Format;
import org.jf.dexlib2.HiddenApiRestriction; import org.jf.dexlib2.HiddenApiRestriction;
import org.jf.dexlib2.Opcode; import org.jf.dexlib2.Opcode;
@ -49,7 +50,6 @@ import org.jf.dexlib2.iface.debug.DebugItem;
import org.jf.dexlib2.iface.instruction.Instruction; import org.jf.dexlib2.iface.instruction.Instruction;
import org.jf.dexlib2.iface.instruction.formats.Instruction21c; import org.jf.dexlib2.iface.instruction.formats.Instruction21c;
import org.jf.dexlib2.iface.reference.Reference; import org.jf.dexlib2.iface.reference.Reference;
import org.jf.util.IndentingWriter;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -141,7 +141,7 @@ public class InstructionMethodItemTest {
InstructionMethodItem methodItem = new InstructionMethodItem<Instruction21c>(methodDefinition, 0, instruction); InstructionMethodItem methodItem = new InstructionMethodItem<Instruction21c>(methodDefinition, 0, instruction);
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
IndentingWriter indentingWriter = new IndentingWriter(stringWriter); BaksmaliWriter indentingWriter = new BaksmaliWriter(stringWriter);
methodItem.writeTo(indentingWriter); methodItem.writeTo(indentingWriter);
Assert.assertEquals("#Invalid reference\n#const-string v0, blahblahblah\nnop", stringWriter.toString()); Assert.assertEquals("#Invalid reference\n#const-string v0, blahblahblah\nnop", stringWriter.toString());

View File

@ -38,7 +38,6 @@ import org.jf.dexlib2.iface.reference.MethodHandleReference;
import org.jf.dexlib2.immutable.ImmutableAnnotationElement; import org.jf.dexlib2.immutable.ImmutableAnnotationElement;
import org.jf.dexlib2.immutable.reference.*; import org.jf.dexlib2.immutable.reference.*;
import org.jf.dexlib2.immutable.value.*; import org.jf.dexlib2.immutable.value.*;
import org.jf.util.IndentingWriter;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -48,102 +47,92 @@ import java.io.StringWriter;
public class BaksmaliWriterTest { public class BaksmaliWriterTest {
private StringWriter stringWriter; private StringWriter output;
private IndentingWriter output;
@Before @Before
public void setup() { public void setup() {
stringWriter = new StringWriter(); output = new StringWriter();
output = new IndentingWriter(stringWriter);
} }
@Test @Test
public void testWriteMethodDescriptor_withSpaces() throws IOException { public void testWriteMethodDescriptor_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeMethodDescriptor(getMethodReferenceWithSpaces()); writer.writeMethodDescriptor(getMethodReferenceWithSpaces());
Assert.assertEquals( Assert.assertEquals(
"Ldefining/class/`with spaces`;->`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)" + "Ldefining/class/`with spaces`;->`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)" +
"Lreturn/type/`with spaces`;", "Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteShortMethodDescriptor_withSpaces() throws IOException { public void testWriteShortMethodDescriptor_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeShortMethodDescriptor(getMethodReferenceWithSpaces()); writer.writeShortMethodDescriptor(getMethodReferenceWithSpaces());
Assert.assertEquals( Assert.assertEquals(
"`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)" + "`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)" +
"Lreturn/type/`with spaces`;", "Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteMethodProtoDescriptor_withSpaces() throws IOException { public void testWriteMethodProtoDescriptor_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeMethodProtoDescriptor(getMethodProtoReferenceWithSpaces()); writer.writeMethodProtoDescriptor(getMethodProtoReferenceWithSpaces());
Assert.assertEquals( Assert.assertEquals(
"(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", "(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteFieldDescriptor_withSpaces() throws IOException { public void testWriteFieldDescriptor_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeFieldDescriptor(getFieldReferenceWithSpaces()); writer.writeFieldDescriptor(getFieldReferenceWithSpaces());
Assert.assertEquals("Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;", Assert.assertEquals("Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteShortFieldDescriptor_withSpaces() throws IOException { public void testWriteShortFieldDescriptor_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeShortFieldDescriptor(getFieldReferenceWithSpaces()); writer.writeShortFieldDescriptor(getFieldReferenceWithSpaces());
Assert.assertEquals("`fieldName with spaces`:Lfield/`type with spaces`;", Assert.assertEquals("`fieldName with spaces`:Lfield/`type with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteMethodHandle_fieldAccess_withSpaces() throws IOException { public void testWriteMethodHandle_fieldAccess_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeMethodHandle(getMethodHandleReferenceForFieldWithSpaces()); writer.writeMethodHandle(getMethodHandleReferenceForFieldWithSpaces());
Assert.assertEquals("instance-get@Ldefining/class/`with spaces`;->`fieldName with spaces`:" + Assert.assertEquals("instance-get@Ldefining/class/`with spaces`;->`fieldName with spaces`:" +
"Lfield/`type with spaces`;", stringWriter.toString()); "Lfield/`type with spaces`;", output.toString());
} }
@Test @Test
public void testWriteMethodHandle_methodAccess_withSpaces() throws IOException { public void testWriteMethodHandle_methodAccess_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeMethodHandle(getMethodHandleReferenceForMethodWithSpaces()); writer.writeMethodHandle(getMethodHandleReferenceForMethodWithSpaces());
Assert.assertEquals("invoke-instance@Ldefining/class/`with spaces`;->`methodName with spaces`(" + Assert.assertEquals("invoke-instance@Ldefining/class/`with spaces`;->`methodName with spaces`(" +
"L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteCallsite_withSpaces() throws IOException { public void testWriteCallsite_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeCallSite(new ImmutableCallSiteReference( writer.writeCallSite(new ImmutableCallSiteReference(
"callsiteName with spaces", "callsiteName with spaces",
@ -162,13 +151,12 @@ public class BaksmaliWriterTest {
"L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;)@" + "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;)@" +
"Ldefining/class/`with spaces`;->`methodName with spaces`(" + "Ldefining/class/`with spaces`;->`methodName with spaces`(" +
"L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_annotation_withSpaces() throws IOException { public void testWriteEncodedValue_annotation_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableAnnotationEncodedValue( writer.writeEncodedValue(new ImmutableAnnotationEncodedValue(
"Lannotation/type with spaces;", "Lannotation/type with spaces;",
@ -185,13 +173,12 @@ public class BaksmaliWriterTest {
" `element with spaces 2` = Ldefining/class/`with spaces`;->`methodName with spaces`(" + " `element with spaces 2` = Ldefining/class/`with spaces`;->`methodName with spaces`(" +
"L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;\n" + "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;\n" +
".end subannotation", ".end subannotation",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_array_withSpaces() throws IOException { public void testWriteEncodedValue_array_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableArrayEncodedValue(ImmutableList.of( writer.writeEncodedValue(new ImmutableArrayEncodedValue(ImmutableList.of(
new ImmutableFieldEncodedValue(getFieldReferenceWithSpaces()), new ImmutableFieldEncodedValue(getFieldReferenceWithSpaces()),
@ -202,74 +189,68 @@ public class BaksmaliWriterTest {
" Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;,\n" + " Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;,\n" +
" Ldefining/class/`with spaces`;->`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;\n" + " Ldefining/class/`with spaces`;->`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;\n" +
"}", "}",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_field_withSpaces() throws IOException { public void testWriteEncodedValue_field_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableFieldEncodedValue(getFieldReferenceWithSpaces())); writer.writeEncodedValue(new ImmutableFieldEncodedValue(getFieldReferenceWithSpaces()));
Assert.assertEquals( Assert.assertEquals(
"Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;", "Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_enum_withSpaces() throws IOException { public void testWriteEncodedValue_enum_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableEnumEncodedValue(getFieldReferenceWithSpaces())); writer.writeEncodedValue(new ImmutableEnumEncodedValue(getFieldReferenceWithSpaces()));
Assert.assertEquals( Assert.assertEquals(
".enum Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;", ".enum Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_method_withSpaces() throws IOException { public void testWriteEncodedValue_method_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableMethodEncodedValue(getMethodReferenceWithSpaces())); writer.writeEncodedValue(new ImmutableMethodEncodedValue(getMethodReferenceWithSpaces()));
Assert.assertEquals( Assert.assertEquals(
"Ldefining/class/`with spaces`;->`methodName with spaces`(" + "Ldefining/class/`with spaces`;->`methodName with spaces`(" +
"L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_type_withSpaces() throws IOException { public void testWriteEncodedValue_type_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableTypeEncodedValue("Ltest/type with spaces;")); writer.writeEncodedValue(new ImmutableTypeEncodedValue("Ltest/type with spaces;"));
Assert.assertEquals( Assert.assertEquals(
"Ltest/`type with spaces`;", "Ltest/`type with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_methodType_withSpaces() throws IOException { public void testWriteEncodedValue_methodType_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue(new ImmutableMethodTypeEncodedValue(getMethodProtoReferenceWithSpaces())); writer.writeEncodedValue(new ImmutableMethodTypeEncodedValue(getMethodProtoReferenceWithSpaces()));
Assert.assertEquals( Assert.assertEquals(
"(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", "(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
public void testWriteEncodedValue_methodHandle_withSpaces() throws IOException { public void testWriteEncodedValue_methodHandle_withSpaces() throws IOException {
BaksmaliWriter writer = BaksmaliWriter writer = new BaksmaliWriter(output);
new BaksmaliWriter(output);
writer.writeEncodedValue( writer.writeEncodedValue(
new ImmutableMethodHandleEncodedValue(getMethodHandleReferenceForMethodWithSpaces())); new ImmutableMethodHandleEncodedValue(getMethodHandleReferenceForMethodWithSpaces()));
@ -277,7 +258,7 @@ public class BaksmaliWriterTest {
Assert.assertEquals( Assert.assertEquals(
"invoke-instance@Ldefining/class/`with spaces`;->`methodName with spaces`(" + "invoke-instance@Ldefining/class/`with spaces`;->`methodName with spaces`(" +
"L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;",
stringWriter.toString()); output.toString());
} }
@Test @Test
@ -296,13 +277,12 @@ public class BaksmaliWriterTest {
} }
private String performWriteUnsignedLongAsHex(long value) throws IOException { private String performWriteUnsignedLongAsHex(long value) throws IOException {
StringWriter stringWriter = new StringWriter(); output = new StringWriter();
IndentingWriter indentingWriter = new IndentingWriter(stringWriter); BaksmaliWriter writer = new BaksmaliWriter(output);
BaksmaliWriter writer = new BaksmaliWriter(indentingWriter);
writer.writeUnsignedLongAsHex(value); writer.writeUnsignedLongAsHex(value);
writer.close(); writer.close();
return stringWriter.toString(); return output.toString();
} }
@Test @Test
@ -321,13 +301,12 @@ public class BaksmaliWriterTest {
} }
private String performWriteSignedLongAsDec(long value) throws IOException { private String performWriteSignedLongAsDec(long value) throws IOException {
StringWriter stringWriter = new StringWriter(); output = new StringWriter();
IndentingWriter indentingWriter = new IndentingWriter(stringWriter); BaksmaliWriter writer = new BaksmaliWriter(output);
BaksmaliWriter writer = new BaksmaliWriter(indentingWriter);
writer.writeSignedLongAsDec(value); writer.writeSignedLongAsDec(value);
writer.close(); writer.close();
return stringWriter.toString(); return output.toString();
} }
@Test @Test
@ -340,13 +319,12 @@ public class BaksmaliWriterTest {
} }
private String performWriteSignedIntAsDec(int value) throws IOException { private String performWriteSignedIntAsDec(int value) throws IOException {
StringWriter stringWriter = new StringWriter(); output = new StringWriter();
IndentingWriter indentingWriter = new IndentingWriter(stringWriter); BaksmaliWriter writer = new BaksmaliWriter(output);
BaksmaliWriter writer = new BaksmaliWriter(indentingWriter);
writer.writeSignedIntAsDec(value); writer.writeSignedIntAsDec(value);
writer.close(); writer.close();
return stringWriter.toString(); return output.toString();
} }
@Test @Test
@ -359,13 +337,12 @@ public class BaksmaliWriterTest {
} }
private String performWriteUnsignedIntAsDec(int value) throws IOException { private String performWriteUnsignedIntAsDec(int value) throws IOException {
StringWriter stringWriter = new StringWriter(); output = new StringWriter();
IndentingWriter indentingWriter = new IndentingWriter(stringWriter); BaksmaliWriter writer = new BaksmaliWriter(output);
BaksmaliWriter writer = new BaksmaliWriter(indentingWriter);
writer.writeUnsignedIntAsDec(value); writer.writeUnsignedIntAsDec(value);
writer.close(); writer.close();
return stringWriter.toString(); return output.toString();
} }
private ImmutableMethodReference getMethodReferenceWithSpaces() { private ImmutableMethodReference getMethodReferenceWithSpaces() {

View File

@ -173,72 +173,4 @@ public class IndentingWriter extends Writer {
indentLevel = 0; indentLevel = 0;
} }
} }
public void printUnsignedLongAsHex(long value) throws IOException {
int bufferIndex = 23;
do {
int digit = (int)(value & 15);
if (digit < 10) {
buffer[bufferIndex--] = (char)(digit + '0');
} else {
buffer[bufferIndex--] = (char)((digit - 10) + 'a');
}
value >>>= 4;
} while (value != 0);
bufferIndex++;
writeLine(buffer, bufferIndex, 24-bufferIndex);
}
public void printSignedLongAsDec(long value) throws IOException {
int bufferIndex = 23;
if (value < 0) {
value *= -1;
write('-');
}
do {
long digit = value % 10;
buffer[bufferIndex--] = (char)(digit + '0');
value = value / 10;
} while (value != 0);
bufferIndex++;
writeLine(buffer, bufferIndex, 24-bufferIndex);
}
public void printSignedIntAsDec(int value) throws IOException {
int bufferIndex = 15;
if (value < 0) {
value *= -1;
write('-');
}
do {
int digit = value % 10;
buffer[bufferIndex--] = (char)(digit + '0');
value = value / 10;
} while (value != 0);
bufferIndex++;
writeLine(buffer, bufferIndex, 16-bufferIndex);
}
public void printUnsignedIntAsDec(int value) throws IOException {
int bufferIndex = 15;
if (value < 0) {
printSignedLongAsDec(value & 0xFFFFFFFFL);
} else {
printSignedIntAsDec(value);
}
}
} }

View File

@ -1,51 +0,0 @@
/*
* Copyright 2013, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.util;
import junit.framework.Assert;
import org.junit.Test;
import java.io.IOException;
import java.io.StringWriter;
public class IndentingWriterTest {
@Test
public void testPrintSignedLongAsDec() throws IOException {
StringWriter stringWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(stringWriter);
writer.printUnsignedIntAsDec(-1);
writer.close();
Assert.assertEquals("4294967295", stringWriter.toString());
}
}