diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/AnnotationFormatter.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/AnnotationFormatter.java index 1310f191..35f23017 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/AnnotationFormatter.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/AnnotationFormatter.java @@ -29,9 +29,9 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.Adaptors.EncodedValue.AnnotationEncodedValueAdaptor; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.AnnotationVisibility; import org.jf.dexlib2.iface.Annotation; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -40,7 +40,7 @@ import java.util.Collection; public class AnnotationFormatter { - public static void writeTo(@Nonnull IndentingWriter writer, + public static void writeTo(@Nonnull BaksmaliWriter writer, @Nonnull Collection annotations, @Nullable String containingClass) throws IOException { 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 { writer.write(".annotation "); writer.write(AnnotationVisibility.getVisibility(annotation.getVisibility())); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/BlankMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/BlankMethodItem.java index d0078493..9e73357a 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/BlankMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/BlankMethodItem.java @@ -28,7 +28,7 @@ package org.jf.baksmali.Adaptors; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; //a "spacer" between instructions public class BlankMethodItem extends MethodItem { @@ -40,7 +40,7 @@ public class BlankMethodItem extends MethodItem { 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 //after this method item, which is the intended functionality return true; diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/CatchMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/CatchMethodItem.java index 4b545ee6..b947b263 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/CatchMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/CatchMethodItem.java @@ -29,7 +29,7 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.BaksmaliOptions; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -79,7 +79,7 @@ public class CatchMethodItem extends MethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { if (exceptionType == null) { writer.write(".catchall"); } else { diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java index 73603f19..d9fc7673 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ClassDefinition.java @@ -29,6 +29,8 @@ package org.jf.baksmali.Adaptors; 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.dexbacked.DexBackedClassDef; 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.Reference; import org.jf.dexlib2.util.ReferenceUtil; -import org.jf.util.IndentingWriter; import org.jf.util.StringUtils; import javax.annotation.Nonnull; @@ -51,12 +52,14 @@ public class ClassDefinition { @Nonnull public final BaksmaliOptions options; @Nonnull public final ClassDef classDef; @Nonnull private final HashSet fieldsSetInStaticConstructor; + @Nonnull private final BaksmaliFormatter formatter; protected boolean validationErrors; public ClassDefinition(@Nonnull BaksmaliOptions options, @Nonnull ClassDef classDef) { this.options = options; this.classDef = classDef; + formatter = new BaksmaliFormatter(options.implicitReferences ? classDef.getType() : null); fieldsSetInStaticConstructor = findFieldsSetInStaticConstructor(classDef); } @@ -101,7 +104,7 @@ public class ClassDefinition { return fieldsSetInStaticConstructor; } - public void writeTo(IndentingWriter writer) throws IOException { + public void writeTo(BaksmaliWriter writer) throws IOException { writeClass(writer); writeSuper(writer); writeSourceFile(writer); @@ -113,21 +116,21 @@ public class ClassDefinition { writeVirtualMethods(writer, directMethods); } - private void writeClass(IndentingWriter writer) throws IOException { + private void writeClass(BaksmaliWriter writer) throws IOException { writer.write(".class "); writeAccessFlags(writer); writer.write(classDef.getType()); writer.write('\n'); } - private void writeAccessFlags(IndentingWriter writer) throws IOException { + private void writeAccessFlags(BaksmaliWriter writer) throws IOException { for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForClass(classDef.getAccessFlags())) { writer.write(accessFlag.toString()); writer.write(' '); } } - private void writeSuper(IndentingWriter writer) throws IOException { + private void writeSuper(BaksmaliWriter writer) throws IOException { String superClass = classDef.getSuperclass(); if (superClass != null) { 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(); if (sourceFile != null) { 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 interfaces = classDef.getInterfaces(); 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 classAnnotations = classDef.getAnnotations(); if (classAnnotations.size() != 0) { writer.write("\n\n"); @@ -174,7 +177,7 @@ public class ClassDefinition { } } - private Set writeStaticFields(IndentingWriter writer) throws IOException { + private Set writeStaticFields(BaksmaliWriter writer) throws IOException { boolean wroteHeader = false; Set writtenFields = new HashSet(); @@ -194,11 +197,11 @@ public class ClassDefinition { writer.write('\n'); boolean setInStaticConstructor; - IndentingWriter fieldWriter = writer; + BaksmaliWriter fieldWriter = writer; String fieldString = ReferenceUtil.getShortFieldDescriptor(field); if (!writtenFields.add(fieldString)) { 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)); setInStaticConstructor = false; } else { @@ -209,7 +212,7 @@ public class ClassDefinition { return writtenFields; } - private void writeInstanceFields(IndentingWriter writer, Set staticFields) throws IOException { + private void writeInstanceFields(BaksmaliWriter writer, Set staticFields) throws IOException { boolean wroteHeader = false; Set writtenFields = new HashSet(); @@ -228,11 +231,11 @@ public class ClassDefinition { } writer.write('\n'); - IndentingWriter fieldWriter = writer; + BaksmaliWriter fieldWriter = writer; String fieldString = ReferenceUtil.getShortFieldDescriptor(field); if (!writtenFields.add(fieldString)) { 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)); } else if (staticFields.contains(fieldString)) { System.err.println(String.format("Duplicate static+instance field found: %s->%s", @@ -246,7 +249,7 @@ public class ClassDefinition { } } - private Set writeDirectMethods(IndentingWriter writer) throws IOException { + private Set writeDirectMethods(BaksmaliWriter writer) throws IOException { boolean wroteHeader = false; Set writtenMethods = new HashSet(); @@ -268,10 +271,10 @@ public class ClassDefinition { // TODO: check for method validation errors String methodString = ReferenceUtil.getMethodDescriptor(method, true); - IndentingWriter methodWriter = writer; + BaksmaliWriter methodWriter = writer; if (!writtenMethods.add(methodString)) { writer.write("# duplicate method ignored\n"); - methodWriter = new CommentingIndentingWriter(writer); + methodWriter = getCommentingWriter(writer); } MethodImplementation methodImpl = method.getImplementation(); @@ -285,7 +288,7 @@ public class ClassDefinition { return writtenMethods; } - private void writeVirtualMethods(IndentingWriter writer, Set directMethods) throws IOException { + private void writeVirtualMethods(BaksmaliWriter writer, Set directMethods) throws IOException { boolean wroteHeader = false; Set writtenMethods = new HashSet(); @@ -307,10 +310,10 @@ public class ClassDefinition { // TODO: check for method validation errors String methodString = ReferenceUtil.getMethodDescriptor(method, true); - IndentingWriter methodWriter = writer; + BaksmaliWriter methodWriter = writer; if (!writtenMethods.add(methodString)) { writer.write("# duplicate method ignored\n"); - methodWriter = new CommentingIndentingWriter(writer); + methodWriter = getCommentingWriter(writer); } else if (directMethods.contains(methodString)) { 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"); @@ -328,4 +331,12 @@ public class ClassDefinition { } } } + + public BaksmaliWriter getCommentingWriter(BaksmaliWriter writer) { + return formatter.getWriter(new CommentingIndentingWriter(writer.indentingWriter())); + } + + public BaksmaliFormatter getFormatter() { + return formatter; + } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentMethodItem.java index 8ac43962..bfcdaf5b 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentMethodItem.java @@ -28,7 +28,7 @@ package org.jf.baksmali.Adaptors; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; @@ -47,7 +47,7 @@ public class CommentMethodItem extends MethodItem { return sortOrder; } - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write('#'); writer.write(comment); return true; diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentedOutMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentedOutMethodItem.java index aaeb6a60..7a93d8e8 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentedOutMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/CommentedOutMethodItem.java @@ -28,7 +28,7 @@ package org.jf.baksmali.Adaptors; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; @@ -44,7 +44,7 @@ public class CommentedOutMethodItem extends MethodItem { return commentedOutMethodItem.getSortOrder() + .001; } - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write('#'); commentedOutMethodItem.writeTo(writer); return true; diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/BeginEpilogueMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/BeginEpilogueMethodItem.java index a1294fcb..e0ce9255 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/BeginEpilogueMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/BeginEpilogueMethodItem.java @@ -31,7 +31,7 @@ package org.jf.baksmali.Adaptors.Debug; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; @@ -41,7 +41,7 @@ public class BeginEpilogueMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".prologue"); return true; } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndLocalMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndLocalMethodItem.java index 231e0498..b36d04d9 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndLocalMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndLocalMethodItem.java @@ -32,8 +32,8 @@ package org.jf.baksmali.Adaptors.Debug; import org.jf.baksmali.Adaptors.RegisterFormatter; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.debug.EndLocal; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -50,7 +50,7 @@ public class EndLocalMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".end local "); registerFormatter.writeTo(writer, endLocal.getRegister()); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndPrologueMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndPrologueMethodItem.java index 369c38fd..2e039edd 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndPrologueMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/EndPrologueMethodItem.java @@ -31,7 +31,7 @@ package org.jf.baksmali.Adaptors.Debug; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; @@ -41,7 +41,7 @@ public class EndPrologueMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".prologue"); return true; } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LineNumberMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LineNumberMethodItem.java index 91473bdc..0d7665ef 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LineNumberMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LineNumberMethodItem.java @@ -31,8 +31,8 @@ package org.jf.baksmali.Adaptors.Debug; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.debug.LineNumber; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -46,9 +46,9 @@ public class LineNumberMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".line "); - writer.printUnsignedIntAsDec(lineNumber); + writer.writeUnsignedIntAsDec(lineNumber); return true; } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LocalFormatter.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LocalFormatter.java index 62ed9958..bc24c0ee 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LocalFormatter.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/LocalFormatter.java @@ -32,7 +32,7 @@ package org.jf.baksmali.Adaptors.Debug; import org.jf.baksmali.Adaptors.ReferenceFormatter; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -52,7 +52,7 @@ public class LocalFormatter { * * 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 { if (name != null) { ReferenceFormatter.writeStringReference(writer, name); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/RestartLocalMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/RestartLocalMethodItem.java index 44617193..f88e0997 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/RestartLocalMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/RestartLocalMethodItem.java @@ -32,8 +32,8 @@ package org.jf.baksmali.Adaptors.Debug; import org.jf.baksmali.Adaptors.RegisterFormatter; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.debug.RestartLocal; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -50,7 +50,7 @@ public class RestartLocalMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".restart local "); registerFormatter.writeTo(writer, restartLocal.getRegister()); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/SetSourceFileMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/SetSourceFileMethodItem.java index faccfdf3..303afa12 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/SetSourceFileMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/SetSourceFileMethodItem.java @@ -31,8 +31,8 @@ package org.jf.baksmali.Adaptors.Debug; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.debug.SetSourceFile; -import org.jf.util.IndentingWriter; import org.jf.util.StringUtils; import javax.annotation.Nonnull; @@ -48,7 +48,7 @@ public class SetSourceFileMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".source"); if (sourceFile != null) { diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/StartLocalMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/StartLocalMethodItem.java index 0cd2d2b1..d42ba56a 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/StartLocalMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Debug/StartLocalMethodItem.java @@ -32,8 +32,8 @@ package org.jf.baksmali.Adaptors.Debug; import org.jf.baksmali.Adaptors.RegisterFormatter; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.debug.StartLocal; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -50,7 +50,7 @@ public class StartLocalMethodItem extends DebugMethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write(".local "); registerFormatter.writeTo(writer, startLocal.getRegister()); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/AnnotationEncodedValueAdaptor.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/AnnotationEncodedValueAdaptor.java index e8f12a29..880517ba 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/AnnotationEncodedValueAdaptor.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/AnnotationEncodedValueAdaptor.java @@ -28,9 +28,9 @@ package org.jf.baksmali.Adaptors.EncodedValue; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.AnnotationElement; import org.jf.dexlib2.iface.value.AnnotationEncodedValue; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -39,7 +39,7 @@ import java.util.Collection; public abstract class AnnotationEncodedValueAdaptor { - public static void writeTo(@Nonnull IndentingWriter writer, + public static void writeTo(@Nonnull BaksmaliWriter writer, @Nonnull AnnotationEncodedValue annotationEncodedValue, @Nullable String containingClass) throws IOException { writer.write(".subannotation "); @@ -50,7 +50,7 @@ public abstract class AnnotationEncodedValueAdaptor { writer.write(".end subannotation"); } - public static void writeElementsTo(@Nonnull IndentingWriter writer, + public static void writeElementsTo(@Nonnull BaksmaliWriter writer, @Nonnull Collection annotationElements, @Nullable String containingClass) throws IOException { writer.indent(4); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/ArrayEncodedValueAdaptor.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/ArrayEncodedValueAdaptor.java index eb079b30..c832a7ce 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/ArrayEncodedValueAdaptor.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/ArrayEncodedValueAdaptor.java @@ -28,9 +28,9 @@ 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.EncodedValue; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -38,7 +38,7 @@ import java.io.IOException; import java.util.Collection; public class ArrayEncodedValueAdaptor { - public static void writeTo(@Nonnull IndentingWriter writer, + public static void writeTo(@Nonnull BaksmaliWriter writer, @Nonnull ArrayEncodedValue arrayEncodedValue, @Nullable String containingClass) throws IOException { writer.write('{'); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/EncodedValueAdaptor.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/EncodedValueAdaptor.java index 880c7607..782d4bc5 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/EncodedValueAdaptor.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/EncodedValue/EncodedValueAdaptor.java @@ -30,18 +30,18 @@ package org.jf.baksmali.Adaptors.EncodedValue; import org.jf.baksmali.Adaptors.ReferenceFormatter; import org.jf.baksmali.Renderers.*; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.ValueType; import org.jf.dexlib2.iface.value.*; import org.jf.dexlib2.util.ReferenceUtil; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.IOException; 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) throws IOException { switch (encodedValue.getValueType()) { diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/FieldDefinition.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/FieldDefinition.java index 8b5f9205..b0224cb7 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/FieldDefinition.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/FieldDefinition.java @@ -30,20 +30,20 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.Adaptors.EncodedValue.EncodedValueAdaptor; import org.jf.baksmali.BaksmaliOptions; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.HiddenApiRestriction; import org.jf.dexlib2.iface.Annotation; import org.jf.dexlib2.iface.Field; import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.util.EncodedValueUtils; -import org.jf.util.IndentingWriter; import java.io.IOException; import java.util.Collection; import java.util.Set; 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 { EncodedValue initialValue = field.getInitialValue(); int accessFlags = field.getAccessFlags(); @@ -95,7 +95,7 @@ public class FieldDefinition { } private static void writeAccessFlagsAndRestrictions( - IndentingWriter writer, int accessFlags, Set hiddenApiRestrictions) + BaksmaliWriter writer, int accessFlags, Set hiddenApiRestrictions) throws IOException { for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForField(accessFlags)) { writer.write(accessFlag.toString()); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java index 9c7b658c..a17845e7 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/ArrayDataMethodItem.java @@ -30,8 +30,8 @@ package org.jf.baksmali.Adaptors.Format; import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Renderers.LongRenderer; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.instruction.formats.ArrayPayload; -import org.jf.util.IndentingWriter; import java.io.IOException; import java.util.List; @@ -41,11 +41,11 @@ public class ArrayDataMethodItem extends InstructionMethodItem { super(methodDef, codeAddress, instruction); } - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { int elementWidth = instruction.getElementWidth(); writer.write(".array-data "); - writer.printSignedIntAsDec(instruction.getElementWidth()); + writer.writeSignedIntAsDec(instruction.getElementWidth()); writer.write('\n'); writer.indent(4); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java index e5ef9263..daeea08b 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/InstructionMethodItem.java @@ -34,6 +34,7 @@ import org.jf.baksmali.Adaptors.MethodItem; import org.jf.baksmali.Adaptors.ReferenceFormatter; import org.jf.baksmali.BaksmaliOptions; import org.jf.baksmali.Renderers.LongRenderer; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.Opcode; import org.jf.dexlib2.VerificationError; 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.util.ReferenceUtil; import org.jf.util.ExceptionWithContext; -import org.jf.util.IndentingWriter; import org.jf.util.NumberUtils; import javax.annotation.Nonnull; @@ -80,11 +80,11 @@ public class InstructionMethodItem extends MethodItem { } private interface Writable { - void writeTo(IndentingWriter writer) throws IOException; + void writeTo(BaksmaliWriter writer) throws IOException; } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { Opcode opcode = instruction.getOpcode(); String verificationErrorName = null; Writable referenceWritable = null; @@ -97,7 +97,7 @@ public class InstructionMethodItem extends MethodItem { verificationErrorName = VerificationError.getVerificationErrorName(verificationError); if (verificationErrorName == null) { writer.write("#was invalid verification error type: "); - writer.printSignedIntAsDec(verificationError); + writer.writeSignedIntAsDec(verificationError); writer.write("\n"); verificationErrorName = "generic-error"; } @@ -120,14 +120,14 @@ public class InstructionMethodItem extends MethodItem { if (reference instanceof CallSiteReference) { referenceWritable = new Writable() { @Override - public void writeTo(IndentingWriter indentingWriter) throws IOException { + public void writeTo(BaksmaliWriter indentingWriter) throws IOException { ReferenceFormatter.writeCallSiteReference(indentingWriter, (CallSiteReference)reference); } }; } else { referenceWritable = new Writable() { @Override - public void writeTo(IndentingWriter indentingWriter) throws IOException { + public void writeTo(BaksmaliWriter indentingWriter) throws IOException { indentingWriter.write(ReferenceUtil.getReferenceString(reference, classContext)); } }; @@ -220,7 +220,7 @@ public class InstructionMethodItem extends MethodItem { case Format10x: if (instruction instanceof UnknownInstruction) { writer.write("#unknown opcode: 0x"); - writer.printUnsignedLongAsHex(((UnknownInstruction)instruction).getOriginalOpcode()); + writer.writeUnsignedLongAsHex(((UnknownInstruction)instruction).getOriginalOpcode()); writer.write('\n'); } writeOpcode(writer); @@ -416,33 +416,33 @@ public class InstructionMethodItem extends MethodItem { return true; } - protected void writeOpcode(IndentingWriter writer) throws IOException { + protected void writeOpcode(BaksmaliWriter writer) throws IOException { 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 //have a target 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); } - protected void writeFirstRegister(IndentingWriter writer) throws IOException { + protected void writeFirstRegister(BaksmaliWriter writer) throws IOException { writeRegister(writer, ((OneRegisterInstruction)instruction).getRegisterA()); } - protected void writeSecondRegister(IndentingWriter writer) throws IOException { + protected void writeSecondRegister(BaksmaliWriter writer) throws IOException { writeRegister(writer, ((TwoRegisterInstruction)instruction).getRegisterB()); } - protected void writeThirdRegister(IndentingWriter writer) throws IOException { + protected void writeThirdRegister(BaksmaliWriter writer) throws IOException { writeRegister(writer, ((ThreeRegisterInstruction) instruction).getRegisterC()); } - protected void writeInvokeRegisters(IndentingWriter writer) throws IOException { + protected void writeInvokeRegisters(BaksmaliWriter writer) throws IOException { FiveRegisterInstruction instruction = (FiveRegisterInstruction)this.instruction; final int regCount = instruction.getRegisterCount(); @@ -487,7 +487,7 @@ public class InstructionMethodItem extends MethodItem { writer.write('}'); } - protected void writeInvokeRangeRegisters(IndentingWriter writer) throws IOException { + protected void writeInvokeRangeRegisters(BaksmaliWriter writer) throws IOException { RegisterRangeInstruction instruction = (RegisterRangeInstruction)this.instruction; int regCount = instruction.getRegisterCount(); @@ -499,15 +499,15 @@ public class InstructionMethodItem extends MethodItem { } } - protected void writeLiteral(IndentingWriter writer) throws IOException { + protected void writeLiteral(BaksmaliWriter writer) throws IOException { 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()); } - protected void writeCommentIfLikelyFloat(IndentingWriter writer, int val) throws IOException { + protected void writeCommentIfLikelyFloat(BaksmaliWriter writer, int val) throws IOException { if (NumberUtils.isLikelyFloat(val)) { writer.write(" # "); float fval = Float.intBitsToFloat(val); @@ -530,11 +530,11 @@ public class InstructionMethodItem extends MethodItem { } } - protected void writeCommentIfLikelyDouble(IndentingWriter writer) throws IOException { + protected void writeCommentIfLikelyDouble(BaksmaliWriter writer) throws IOException { 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)) { writer.write(" # "); double dval = Double.longBitsToDouble(val); @@ -555,11 +555,11 @@ public class InstructionMethodItem extends MethodItem { } } - protected boolean writeCommentIfResourceId(IndentingWriter writer) throws IOException { + protected boolean writeCommentIfResourceId(BaksmaliWriter writer) throws IOException { 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 resourceIds = methodDef.classDef.options.resourceIds; String resource = resourceIds.get(Integer.valueOf(val)); if (resource != null) { @@ -570,18 +570,18 @@ public class InstructionMethodItem extends MethodItem { return false; } - protected void writeFieldOffset(IndentingWriter writer) throws IOException { + protected void writeFieldOffset(BaksmaliWriter writer) throws IOException { 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.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.printSignedIntAsDec(((VtableIndexInstruction)instruction).getVtableIndex()); + writer.writeSignedIntAsDec(((VtableIndexInstruction)instruction).getVtableIndex()); } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/OffsetInstructionFormatMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/OffsetInstructionFormatMethodItem.java index be76edfe..aef82e36 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/OffsetInstructionFormatMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/OffsetInstructionFormatMethodItem.java @@ -31,9 +31,9 @@ package org.jf.baksmali.Adaptors.Format; import org.jf.baksmali.Adaptors.LabelMethodItem; import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.BaksmaliOptions; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.Opcode; import org.jf.dexlib2.iface.instruction.OffsetInstruction; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -50,7 +50,7 @@ public class OffsetInstructionFormatMethodItem extends InstructionMethodItem= 0) { writer.write('+'); } - writer.printSignedIntAsDec(target); + writer.writeSignedIntAsDec(target); } } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/SparseSwitchMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/SparseSwitchMethodItem.java index 68d7e92b..a98c1e4e 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/SparseSwitchMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/SparseSwitchMethodItem.java @@ -28,13 +28,12 @@ package org.jf.baksmali.Adaptors.Format; -import org.jf.baksmali.Adaptors.CommentingIndentingWriter; import org.jf.baksmali.Adaptors.LabelMethodItem; 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.formats.SparseSwitchPayload; -import org.jf.util.IndentingWriter; -import org.jf.baksmali.Renderers.IntegerRenderer; import java.io.IOException; import java.util.ArrayList; @@ -69,9 +68,9 @@ public class SparseSwitchMethodItem extends InstructionMethodItem= 0) { writer.write('+'); } - writer.printSignedIntAsDec(target); + writer.writeSignedIntAsDec(target); } } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/UnresolvedOdexInstructionMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/UnresolvedOdexInstructionMethodItem.java index a7768817..734546cb 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/UnresolvedOdexInstructionMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/Format/UnresolvedOdexInstructionMethodItem.java @@ -29,8 +29,8 @@ package org.jf.baksmali.Adaptors.Format; import org.jf.baksmali.Adaptors.MethodDefinition; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.analysis.UnresolvedOdexInstruction; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -41,12 +41,12 @@ public class UnresolvedOdexInstructionMethodItem extends InstructionMethodItem hiddenApiRestrictions) + BaksmaliWriter writer, int accessFlags, Set hiddenApiRestrictions) throws IOException { for (AccessFlags accessFlag: AccessFlags.getAccessFlagsForMethod(accessFlags)) { 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 parameters, BaksmaliOptions options) throws IOException { boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags()); @@ -324,7 +324,7 @@ public class MethodDefinition { Collection annotations = parameter.getAnnotations(); if ((options.debugInfo && parameterName != null) || annotations.size() != 0) { writer.write(".param p"); - writer.printSignedIntAsDec(registerNumber); + writer.writeSignedIntAsDec(registerNumber); if (parameterName != null && options.debugInfo) { writer.write(", "); @@ -426,9 +426,9 @@ public class MethodDefinition { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write("#@"); - writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL); + writer.writeUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL); return true; } }); @@ -505,9 +505,9 @@ public class MethodDefinition { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write("#@"); - writer.printUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL); + writer.writeUnsignedLongAsHex(codeAddress & 0xFFFFFFFFL); return true; } }); diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/MethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/MethodItem.java index 60358c5d..a8bc840d 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/MethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/MethodItem.java @@ -28,7 +28,7 @@ package org.jf.baksmali.Adaptors; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; @@ -55,5 +55,5 @@ public abstract class MethodItem implements Comparable { return result; } - public abstract boolean writeTo(IndentingWriter writer) throws IOException; + public abstract boolean writeTo(BaksmaliWriter writer) throws IOException; } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/PostInstructionRegisterInfoMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/PostInstructionRegisterInfoMethodItem.java index 62826b1e..477cadc4 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/PostInstructionRegisterInfoMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/PostInstructionRegisterInfoMethodItem.java @@ -29,9 +29,9 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.BaksmaliOptions; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.analysis.AnalyzedInstruction; import org.jf.dexlib2.analysis.RegisterType; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -55,7 +55,7 @@ public class PostInstructionRegisterInfoMethodItem extends MethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { int registerInfo = registerFormatter.options.registerInfo; int registerCount = analyzedInstruction.getRegisterCount(); 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); if (registerNum < 0) { return false; diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/PreInstructionRegisterInfoMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/PreInstructionRegisterInfoMethodItem.java index f934eddb..19d6b8b3 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/PreInstructionRegisterInfoMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/PreInstructionRegisterInfoMethodItem.java @@ -29,11 +29,11 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.BaksmaliOptions; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.analysis.AnalyzedInstruction; import org.jf.dexlib2.analysis.MethodAnalyzer; import org.jf.dexlib2.analysis.RegisterType; import org.jf.dexlib2.iface.instruction.*; -import org.jf.util.IndentingWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -63,7 +63,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem { } @Override - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { int registerCount = analyzedInstruction.getRegisterCount(); BitSet registers = new BitSet(registerCount); BitSet mergeRegisters = null; @@ -171,7 +171,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem { 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); writer.write('='); analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer); @@ -192,7 +192,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem { writer.write("Start:"); } else { writer.write("0x"); - writer.printUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor)); + writer.writeUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor)); writer.write(':'); } predecessorRegisterType.writeTo(writer); @@ -202,7 +202,7 @@ public class PreInstructionRegisterInfoMethodItem extends MethodItem { writer.write('}'); } - private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers, + private boolean writeRegisterInfo(BaksmaliWriter writer, BitSet registers, BitSet fullMergeRegisters) throws IOException { boolean firstRegister = true; boolean previousWasFullMerge = false; diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ReferenceFormatter.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ReferenceFormatter.java index bfb71e82..03ce236c 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/ReferenceFormatter.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/ReferenceFormatter.java @@ -29,24 +29,24 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.Adaptors.EncodedValue.EncodedValueAdaptor; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.MethodHandleType; import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.iface.reference.*; import org.jf.dexlib2.iface.value.EncodedValue; import org.jf.dexlib2.util.ReferenceUtil; -import org.jf.util.IndentingWriter; import org.jf.util.StringUtils; import java.io.IOException; 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('"'); StringUtils.writeEscapedString(writer, item); 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('('); writer.write('"'); @@ -66,7 +66,7 @@ public class ReferenceFormatter { 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 { switch (referenceType) { case ReferenceType.STRING: diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/RegisterFormatter.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/RegisterFormatter.java index 3d72f468..6c2c67ff 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/RegisterFormatter.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/RegisterFormatter.java @@ -29,7 +29,7 @@ package org.jf.baksmali.Adaptors; import org.jf.baksmali.BaksmaliOptions; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import javax.annotation.Nonnull; import java.io.IOException; @@ -53,27 +53,27 @@ public class RegisterFormatter { * output the registers in the v format. But if false, then it will check if *both* registers are parameter * registers, and if so, use the p format for both. If only the last register is a parameter register, it will * use the v format for both, otherwise it would be confusing to have something like {v20 .. p1} - * @param writer the IndentingWriter to write to + * @param writer the BaksmaliWriter to write to * @param startRegister the first 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) { assert startRegister <= lastRegister; if (startRegister >= registerCount - parameterRegisterCount) { writer.write("{p"); - writer.printSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount)); + writer.writeSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount)); writer.write(" .. p"); - writer.printSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount)); + writer.writeSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount)); writer.write('}'); return; } } writer.write("{v"); - writer.printSignedIntAsDec(startRegister); + writer.writeSignedIntAsDec(startRegister); writer.write(" .. v"); - writer.printSignedIntAsDec(lastRegister); + writer.writeSignedIntAsDec(lastRegister); writer.write('}'); } @@ -82,18 +82,18 @@ public class RegisterFormatter { * output a register in the v format. If false, then it determines if the register is a parameter register, * and if so, formats it in the p format instead. * - * @param writer the IndentingWriter to write to + * @param writer the BaksmaliWriter to write to * @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 (register >= registerCount - parameterRegisterCount) { writer.write('p'); - writer.printSignedIntAsDec((register - (registerCount - parameterRegisterCount))); + writer.writeSignedIntAsDec((register - (registerCount - parameterRegisterCount))); return; } } writer.write('v'); - writer.printSignedIntAsDec(register); + writer.writeSignedIntAsDec(register); } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Adaptors/SyntheticAccessCommentMethodItem.java b/baksmali/src/main/java/org/jf/baksmali/Adaptors/SyntheticAccessCommentMethodItem.java index ef0abb7a..26486462 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Adaptors/SyntheticAccessCommentMethodItem.java +++ b/baksmali/src/main/java/org/jf/baksmali/Adaptors/SyntheticAccessCommentMethodItem.java @@ -28,10 +28,10 @@ package org.jf.baksmali.Adaptors; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.ReferenceType; import org.jf.dexlib2.util.SyntheticAccessorResolver; import org.jf.util.ExceptionWithContext; -import org.jf.util.IndentingWriter; import java.io.IOException; @@ -48,7 +48,7 @@ public class SyntheticAccessCommentMethodItem extends MethodItem { return 99.8; } - public boolean writeTo(IndentingWriter writer) throws IOException { + public boolean writeTo(BaksmaliWriter writer) throws IOException { writer.write("# "); switch (accessedMember.accessedMemberType) { case SyntheticAccessorResolver.METHOD: diff --git a/baksmali/src/main/java/org/jf/baksmali/Baksmali.java b/baksmali/src/main/java/org/jf/baksmali/Baksmali.java index 4b017bd7..a4aaf006 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Baksmali.java +++ b/baksmali/src/main/java/org/jf/baksmali/Baksmali.java @@ -31,10 +31,10 @@ package org.jf.baksmali; import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import org.jf.baksmali.Adaptors.ClassDefinition; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.DexFile; import org.jf.util.ClassFileNameHandler; -import org.jf.util.IndentingWriter; import javax.annotation.Nullable; import java.io.*; @@ -131,7 +131,7 @@ public class Baksmali { ClassDefinition classDefinition = new ClassDefinition(options, classDef); //write the disassembly - Writer writer = null; + BaksmaliWriter writer = null; try { File smaliParent = smaliFile.getParentFile(); @@ -155,8 +155,10 @@ public class Baksmali { BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(smaliFile), "UTF8")); - writer = new IndentingWriter(bufWriter); - classDefinition.writeTo((IndentingWriter)writer); + writer = new BaksmaliWriter( + bufWriter, + options.implicitReferences ? classDef.getType() : null); + classDefinition.writeTo(writer); } catch (Exception ex) { System.err.println("\n\nError occurred while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); ex.printStackTrace(); diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/BooleanRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/BooleanRenderer.java index f181bb48..f9ad1d52 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/BooleanRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/BooleanRenderer.java @@ -28,12 +28,12 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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) { writer.write("true"); } else { diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/ByteRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/ByteRenderer.java index 9c060fd4..847c9402 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/ByteRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/ByteRenderer.java @@ -28,26 +28,26 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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) { writer.write("-0x"); - writer.printUnsignedLongAsHex(-val); + writer.writeUnsignedLongAsHex(-val); writer.write('t'); } else { writer.write("0x"); - writer.printUnsignedLongAsHex(val); + writer.writeUnsignedLongAsHex(val); 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.printUnsignedLongAsHex(val & 0xFF); + writer.writeUnsignedLongAsHex(val & 0xFF); writer.write('t'); } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/CharRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/CharRenderer.java index daf76340..f216faf9 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/CharRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/CharRenderer.java @@ -28,13 +28,13 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.util.StringUtils; import java.io.IOException; 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('\''); StringUtils.writeEscapedChar(writer, val); writer.write('\''); diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/DoubleRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/DoubleRenderer.java index 03fff640..6c1d4a5f 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/DoubleRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/DoubleRenderer.java @@ -28,12 +28,12 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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)); } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/FloatRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/FloatRenderer.java index a1de2b9d..cff8eadc 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/FloatRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/FloatRenderer.java @@ -28,12 +28,12 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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('f'); } diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/IntegerRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/IntegerRenderer.java index 22beaacb..9144fd21 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/IntegerRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/IntegerRenderer.java @@ -28,23 +28,23 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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) { writer.write("-0x"); - writer.printUnsignedLongAsHex(-((long) val)); + writer.writeUnsignedLongAsHex(-((long) val)); } else { 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.printUnsignedLongAsHex(val & 0xFFFFFFFFL); + writer.writeUnsignedLongAsHex(val & 0xFFFFFFFFL); } } diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/LongRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/LongRenderer.java index 83076e48..db31bc2f 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/LongRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/LongRenderer.java @@ -28,33 +28,33 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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) { writer.write("-0x"); - writer.printUnsignedLongAsHex(-val); + writer.writeUnsignedLongAsHex(-val); writer.write('L'); } else { writer.write("0x"); - writer.printUnsignedLongAsHex(val); + writer.writeUnsignedLongAsHex(val); 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) { writer.write("-0x"); - writer.printUnsignedLongAsHex(-val); + writer.writeUnsignedLongAsHex(-val); if (val < Integer.MIN_VALUE) { writer.write('L'); } } else { writer.write("0x"); - writer.printUnsignedLongAsHex(val); + writer.writeUnsignedLongAsHex(val); if (val > Integer.MAX_VALUE) { writer.write('L'); } diff --git a/baksmali/src/main/java/org/jf/baksmali/Renderers/ShortRenderer.java b/baksmali/src/main/java/org/jf/baksmali/Renderers/ShortRenderer.java index 3385c6b6..f5a1b406 100644 --- a/baksmali/src/main/java/org/jf/baksmali/Renderers/ShortRenderer.java +++ b/baksmali/src/main/java/org/jf/baksmali/Renderers/ShortRenderer.java @@ -28,19 +28,19 @@ package org.jf.baksmali.Renderers; -import org.jf.util.IndentingWriter; +import org.jf.baksmali.formatter.BaksmaliWriter; import java.io.IOException; 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) { writer.write("-0x"); - writer.printUnsignedLongAsHex(-val); + writer.writeUnsignedLongAsHex(-val); writer.write('s'); } else { writer.write("0x"); - writer.printUnsignedLongAsHex(val); + writer.writeUnsignedLongAsHex(val); writer.write('s'); } } diff --git a/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java b/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java index 80a54b70..b957de4f 100644 --- a/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java +++ b/baksmali/src/test/java/org/jf/baksmali/AnalysisTest.java @@ -35,13 +35,13 @@ import com.google.common.base.Charsets; import com.google.common.io.Resources; import junit.framework.Assert; import org.jf.baksmali.Adaptors.ClassDefinition; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.DexFileFactory; import org.jf.dexlib2.Opcodes; import org.jf.dexlib2.analysis.ClassPath; import org.jf.dexlib2.analysis.ClassProvider; import org.jf.dexlib2.iface.ClassDef; import org.jf.dexlib2.iface.DexFile; -import org.jf.util.IndentingWriter; import org.junit.Test; import javax.annotation.Nonnull; @@ -111,7 +111,7 @@ public class AnalysisTest { for (ClassDef classDef: dexFile.getClasses()) { StringWriter stringWriter = new StringWriter(); - IndentingWriter writer = new IndentingWriter(stringWriter); + BaksmaliWriter writer = new BaksmaliWriter(stringWriter); ClassDefinition classDefinition = new ClassDefinition(options, classDef); classDefinition.writeTo(writer); writer.close(); diff --git a/baksmali/src/test/java/org/jf/baksmali/BaksmaliTestUtils.java b/baksmali/src/test/java/org/jf/baksmali/BaksmaliTestUtils.java index e6406fba..f7bdff09 100644 --- a/baksmali/src/test/java/org/jf/baksmali/BaksmaliTestUtils.java +++ b/baksmali/src/test/java/org/jf/baksmali/BaksmaliTestUtils.java @@ -35,9 +35,9 @@ import com.google.common.io.ByteStreams; import junit.framework.Assert; import org.antlr.runtime.RecognitionException; import org.jf.baksmali.Adaptors.ClassDefinition; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.iface.ClassDef; import org.jf.smali.SmaliTestUtils; -import org.jf.util.IndentingWriter; import org.junit.Test; import javax.annotation.Nonnull; @@ -88,7 +88,7 @@ public class BaksmaliTestUtils { boolean stripComments) throws IOException { StringWriter stringWriter = new StringWriter(); - IndentingWriter writer = new IndentingWriter(stringWriter); + BaksmaliWriter writer = new BaksmaliWriter(stringWriter); ClassDefinition classDefinition = new ClassDefinition(options, classDef); classDefinition.writeTo(writer); writer.close(); diff --git a/baksmali/src/test/java/org/jf/baksmali/InstructionMethodItemTest.java b/baksmali/src/test/java/org/jf/baksmali/InstructionMethodItemTest.java index 20a38037..82b34da1 100644 --- a/baksmali/src/test/java/org/jf/baksmali/InstructionMethodItemTest.java +++ b/baksmali/src/test/java/org/jf/baksmali/InstructionMethodItemTest.java @@ -37,6 +37,7 @@ import org.jf.baksmali.Adaptors.ClassDefinition; import org.jf.baksmali.Adaptors.Format.InstructionMethodItem; import org.jf.baksmali.Adaptors.MethodDefinition; import org.jf.baksmali.Adaptors.RegisterFormatter; +import org.jf.baksmali.formatter.BaksmaliWriter; import org.jf.dexlib2.Format; import org.jf.dexlib2.HiddenApiRestriction; 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.formats.Instruction21c; import org.jf.dexlib2.iface.reference.Reference; -import org.jf.util.IndentingWriter; import org.junit.Assert; import org.junit.Test; @@ -141,7 +141,7 @@ public class InstructionMethodItemTest { InstructionMethodItem methodItem = new InstructionMethodItem(methodDefinition, 0, instruction); StringWriter stringWriter = new StringWriter(); - IndentingWriter indentingWriter = new IndentingWriter(stringWriter); + BaksmaliWriter indentingWriter = new BaksmaliWriter(stringWriter); methodItem.writeTo(indentingWriter); Assert.assertEquals("#Invalid reference\n#const-string v0, blahblahblah\nnop", stringWriter.toString()); diff --git a/baksmali/src/test/java/org/jf/baksmali/formatter/BaksmaliWriterTest.java b/baksmali/src/test/java/org/jf/baksmali/formatter/BaksmaliWriterTest.java index e2b3ffe0..87f4e66e 100644 --- a/baksmali/src/test/java/org/jf/baksmali/formatter/BaksmaliWriterTest.java +++ b/baksmali/src/test/java/org/jf/baksmali/formatter/BaksmaliWriterTest.java @@ -38,7 +38,6 @@ import org.jf.dexlib2.iface.reference.MethodHandleReference; import org.jf.dexlib2.immutable.ImmutableAnnotationElement; import org.jf.dexlib2.immutable.reference.*; import org.jf.dexlib2.immutable.value.*; -import org.jf.util.IndentingWriter; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -48,102 +47,92 @@ import java.io.StringWriter; public class BaksmaliWriterTest { - private StringWriter stringWriter; - private IndentingWriter output; + private StringWriter output; @Before public void setup() { - stringWriter = new StringWriter(); - output = new IndentingWriter(stringWriter); + output = new StringWriter(); } @Test public void testWriteMethodDescriptor_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeMethodDescriptor(getMethodReferenceWithSpaces()); Assert.assertEquals( "Ldefining/class/`with spaces`;->`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)" + "Lreturn/type/`with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteShortMethodDescriptor_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeShortMethodDescriptor(getMethodReferenceWithSpaces()); Assert.assertEquals( "`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)" + "Lreturn/type/`with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteMethodProtoDescriptor_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeMethodProtoDescriptor(getMethodProtoReferenceWithSpaces()); Assert.assertEquals( "(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteFieldDescriptor_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeFieldDescriptor(getFieldReferenceWithSpaces()); Assert.assertEquals("Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteShortFieldDescriptor_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeShortFieldDescriptor(getFieldReferenceWithSpaces()); Assert.assertEquals("`fieldName with spaces`:Lfield/`type with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteMethodHandle_fieldAccess_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeMethodHandle(getMethodHandleReferenceForFieldWithSpaces()); Assert.assertEquals("instance-get@Ldefining/class/`with spaces`;->`fieldName with spaces`:" + - "Lfield/`type with spaces`;", stringWriter.toString()); + "Lfield/`type with spaces`;", output.toString()); } @Test public void testWriteMethodHandle_methodAccess_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeMethodHandle(getMethodHandleReferenceForMethodWithSpaces()); 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`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteCallsite_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeCallSite(new ImmutableCallSiteReference( "callsiteName with spaces", @@ -162,13 +151,12 @@ public class BaksmaliWriterTest { "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;)@" + "Ldefining/class/`with spaces`;->`methodName with spaces`(" + "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_annotation_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableAnnotationEncodedValue( "Lannotation/type with spaces;", @@ -185,13 +173,12 @@ public class BaksmaliWriterTest { " `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" + ".end subannotation", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_array_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableArrayEncodedValue(ImmutableList.of( 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`;->`methodName with spaces`(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;\n" + "}", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_field_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableFieldEncodedValue(getFieldReferenceWithSpaces())); Assert.assertEquals( "Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_enum_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableEnumEncodedValue(getFieldReferenceWithSpaces())); Assert.assertEquals( ".enum Ldefining/class/`with spaces`;->`fieldName with spaces`:Lfield/`type with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_method_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableMethodEncodedValue(getMethodReferenceWithSpaces())); Assert.assertEquals( "Ldefining/class/`with spaces`;->`methodName with spaces`(" + "L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_type_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableTypeEncodedValue("Ltest/type with spaces;")); Assert.assertEquals( "Ltest/`type with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_methodType_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue(new ImmutableMethodTypeEncodedValue(getMethodProtoReferenceWithSpaces())); Assert.assertEquals( "(L`param with spaces 1`;L`param with spaces 2`;)Lreturn/type/`with spaces`;", - stringWriter.toString()); + output.toString()); } @Test public void testWriteEncodedValue_methodHandle_withSpaces() throws IOException { - BaksmaliWriter writer = - new BaksmaliWriter(output); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeEncodedValue( new ImmutableMethodHandleEncodedValue(getMethodHandleReferenceForMethodWithSpaces())); @@ -277,7 +258,7 @@ public class BaksmaliWriterTest { 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`;", - stringWriter.toString()); + output.toString()); } @Test @@ -296,13 +277,12 @@ public class BaksmaliWriterTest { } private String performWriteUnsignedLongAsHex(long value) throws IOException { - StringWriter stringWriter = new StringWriter(); - IndentingWriter indentingWriter = new IndentingWriter(stringWriter); - BaksmaliWriter writer = new BaksmaliWriter(indentingWriter); + output = new StringWriter(); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeUnsignedLongAsHex(value); writer.close(); - return stringWriter.toString(); + return output.toString(); } @Test @@ -321,13 +301,12 @@ public class BaksmaliWriterTest { } private String performWriteSignedLongAsDec(long value) throws IOException { - StringWriter stringWriter = new StringWriter(); - IndentingWriter indentingWriter = new IndentingWriter(stringWriter); - BaksmaliWriter writer = new BaksmaliWriter(indentingWriter); + output = new StringWriter(); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeSignedLongAsDec(value); writer.close(); - return stringWriter.toString(); + return output.toString(); } @Test @@ -340,13 +319,12 @@ public class BaksmaliWriterTest { } private String performWriteSignedIntAsDec(int value) throws IOException { - StringWriter stringWriter = new StringWriter(); - IndentingWriter indentingWriter = new IndentingWriter(stringWriter); - BaksmaliWriter writer = new BaksmaliWriter(indentingWriter); + output = new StringWriter(); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeSignedIntAsDec(value); writer.close(); - return stringWriter.toString(); + return output.toString(); } @Test @@ -359,13 +337,12 @@ public class BaksmaliWriterTest { } private String performWriteUnsignedIntAsDec(int value) throws IOException { - StringWriter stringWriter = new StringWriter(); - IndentingWriter indentingWriter = new IndentingWriter(stringWriter); - BaksmaliWriter writer = new BaksmaliWriter(indentingWriter); + output = new StringWriter(); + BaksmaliWriter writer = new BaksmaliWriter(output); writer.writeUnsignedIntAsDec(value); writer.close(); - return stringWriter.toString(); + return output.toString(); } private ImmutableMethodReference getMethodReferenceWithSpaces() { diff --git a/dexlib2/src/main/java/org/jf/util/IndentingWriter.java b/dexlib2/src/main/java/org/jf/util/IndentingWriter.java index 8e4ca628..d8658455 100644 --- a/dexlib2/src/main/java/org/jf/util/IndentingWriter.java +++ b/dexlib2/src/main/java/org/jf/util/IndentingWriter.java @@ -173,72 +173,4 @@ public class IndentingWriter extends Writer { 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); - } - } } diff --git a/dexlib2/src/test/java/org/jf/util/IndentingWriterTest.java b/dexlib2/src/test/java/org/jf/util/IndentingWriterTest.java deleted file mode 100644 index 457262b5..00000000 --- a/dexlib2/src/test/java/org/jf/util/IndentingWriterTest.java +++ /dev/null @@ -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()); - } -}