Java Code Examples for org.jf.util.IndentingWriter

The following examples show how to use org.jf.util.IndentingWriter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: HeyGirl   Source File: MethodDefinition.java    License: Apache License 2.0 6 votes vote down vote up
public static void writeEmptyMethodTo(IndentingWriter writer, Method method,
                                      baksmaliOptions options) throws IOException {
    writer.write(".method ");
    writeAccessFlags(writer, method.getAccessFlags());
    writer.write(method.getName());
    writer.write("(");
    ImmutableList<MethodParameter> methodParameters = ImmutableList.copyOf(method.getParameters());
    for (MethodParameter parameter: methodParameters) {
        writer.write(parameter.getType());
    }
    writer.write(")");
    writer.write(method.getReturnType());
    writer.write('\n');

    writer.indent(4);
    writeParameters(writer, method, methodParameters, options);
    AnnotationFormatter.writeTo(writer, method.getAnnotations());
    writer.deindent(4);
    writer.write(".end method\n");
}
 
Example 2
Source Project: HeyGirl   Source File: ReferenceFormatter.java    License: Apache License 2.0 6 votes vote down vote up
public static void writeReference(IndentingWriter writer, int referenceType,
                                  Reference reference) throws IOException {
    switch (referenceType) {
        case ReferenceType.STRING:
            writeStringReference(writer, ((StringReference)reference).getString());
            return;
        case ReferenceType.TYPE:
            writer.write(((TypeReference)reference).getType());
            return;
        case ReferenceType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, (MethodReference)reference);
            return;
        case ReferenceType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, (FieldReference)reference);
            return;
        default:
            throw new IllegalStateException("Unknown reference type");
    }
}
 
Example 3
Source Project: ZjDroid   Source File: ArrayEncodedValueAdaptor.java    License: Apache License 2.0 6 votes vote down vote up
public static void writeTo(IndentingWriter writer, ArrayEncodedValue arrayEncodedValue) throws IOException {
    writer.write('{');
    Collection<? extends EncodedValue> values = arrayEncodedValue.getValue();
    if (values.size() == 0) {
        writer.write('}');
        return;
    }

    writer.write('\n');
    writer.indent(4);
    boolean first = true;
    for (EncodedValue encodedValue: values) {
        if (!first) {
            writer.write(",\n");
        }
        first = false;

        EncodedValueAdaptor.writeTo(writer, encodedValue);
    }
    writer.deindent(4);
    writer.write("\n}");
}
 
Example 4
Source Project: ZjDroid   Source File: LocalFormatter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Writes out the given local info
 *
 * The written string will be something like:
 *
 * "localVar":Ljava/lang/String;, "SomeSignature"
 * "localVar":Ljava/lang/String;
 * "localVar":V, "SomeSignature"
 * null:Ljava/lang/String;, "SomeSignature"
 * null:V, "SomeSignature"
 *
 * One of name, type or signature must be non-null
 */
public static void writeLocal(@Nonnull IndentingWriter writer, @Nullable String name, @Nullable String type,
                              @Nullable String signature) throws IOException {
    if (name != null) {
        ReferenceFormatter.writeStringReference(writer, name);
    } else {
        writer.write("null");
    }
    writer.write(':');
    if (type != null) {
        writer.write(type);
    } else {
        writer.write("V");
    }
    if (signature != null) {
        writer.write(", ");
        ReferenceFormatter.writeStringReference(writer, signature);
    }
}
 
Example 5
Source Project: ZjDroid   Source File: MethodDefinition.java    License: Apache License 2.0 6 votes vote down vote up
public static void writeEmptyMethodTo(IndentingWriter writer, Method method,
                                      baksmaliOptions options) throws IOException {
    writer.write(".method ");
    writeAccessFlags(writer, method.getAccessFlags());
    writer.write(method.getName());
    writer.write("(");
    ImmutableList<MethodParameter> methodParameters = ImmutableList.copyOf(method.getParameters());
    for (MethodParameter parameter: methodParameters) {
        writer.write(parameter.getType());
    }
    writer.write(")");
    writer.write(method.getReturnType());
    writer.write('\n');

    writer.indent(4);
    writeParameters(writer, method, methodParameters, options);
    AnnotationFormatter.writeTo(writer, method.getAnnotations());
    writer.deindent(4);
    writer.write(".end method\n");
}
 
Example 6
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    int registerInfo = registerFormatter.options.registerInfo;
    int registerCount = analyzedInstruction.getRegisterCount();
    BitSet registers = new BitSet(registerCount);

    if ((registerInfo & BaksmaliOptions.ALL) != 0) {
        registers.set(0, registerCount);
    } else {
        if ((registerInfo & BaksmaliOptions.ALLPOST) != 0) {
            registers.set(0, registerCount);
        } else if ((registerInfo & BaksmaliOptions.DEST) != 0) {
            addDestRegs(registers, registerCount);
        }
    }

    return writeRegisterInfo(writer, registers);
}
 
Example 7
private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers) throws IOException {
    int registerNum = registers.nextSetBit(0);
    if (registerNum < 0) {
        return false;
    }

    writer.write('#');
    for (; registerNum >= 0; registerNum = registers.nextSetBit(registerNum + 1)) {
        RegisterType registerType = analyzedInstruction.getPostInstructionRegisterType(registerNum);

        registerFormatter.writeTo(writer, registerNum);
        writer.write('=');
        registerType.writeTo(writer);
        writer.write(';');
    }
    return true;
}
 
Example 8
Source Project: ZjDroid   Source File: ReferenceFormatter.java    License: Apache License 2.0 6 votes vote down vote up
public static void writeReference(IndentingWriter writer, int referenceType,
                                  Reference reference) throws IOException {
    switch (referenceType) {
        case ReferenceType.STRING:
            writeStringReference(writer, ((StringReference)reference).getString());
            return;
        case ReferenceType.TYPE:
            writer.write(((TypeReference)reference).getType());
            return;
        case ReferenceType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, (MethodReference)reference);
            return;
        case ReferenceType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, (FieldReference)reference);
            return;
        default:
            throw new IllegalStateException("Unknown reference type");
    }
}
 
Example 9
Source Project: HeyGirl   Source File: RegisterFormatter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Write out the register range value used by Format3rc. If baksmali.noParameterRegisters is true, it will always
 * 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
 * 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 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 {
    if (!options.noParameterRegisters) {
        assert startRegister <= lastRegister;

        if (startRegister >= registerCount - parameterRegisterCount) {
            writer.write("{p");
            writer.printSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount));
            writer.write(" .. p");
            writer.printSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount));
            writer.write('}');
            return;
        }
    }
    writer.write("{v");
    writer.printSignedIntAsDec(startRegister);
    writer.write(" .. v");
    writer.printSignedIntAsDec(lastRegister);
    writer.write('}');
}
 
Example 10
private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers) throws IOException {
    int registerNum = registers.nextSetBit(0);
    if (registerNum < 0) {
        return false;
    }

    writer.write('#');
    for (; registerNum >= 0; registerNum = registers.nextSetBit(registerNum + 1)) {
        RegisterType registerType = analyzedInstruction.getPostInstructionRegisterType(registerNum);

        registerFormatter.writeTo(writer, registerNum);
        writer.write('=');
        registerType.writeTo(writer);
        writer.write(';');
    }
    return true;
}
 
Example 11
Source Project: ZjDroid   Source File: LocalFormatter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Writes out the given local info
 *
 * The written string will be something like:
 *
 * "localVar":Ljava/lang/String;, "SomeSignature"
 * "localVar":Ljava/lang/String;
 * "localVar":V, "SomeSignature"
 * null:Ljava/lang/String;, "SomeSignature"
 * null:V, "SomeSignature"
 *
 * One of name, type or signature must be non-null
 */
public static void writeLocal(@Nonnull IndentingWriter writer, @Nullable String name, @Nullable String type,
                              @Nullable String signature) throws IOException {
    if (name != null) {
        ReferenceFormatter.writeStringReference(writer, name);
    } else {
        writer.write("null");
    }
    writer.write(':');
    if (type != null) {
        writer.write(type);
    } else {
        writer.write("V");
    }
    if (signature != null) {
        writer.write(", ");
        ReferenceFormatter.writeStringReference(writer, signature);
    }
}
 
Example 12
Source Project: HeyGirl   Source File: AnnotationEncodedValueAdaptor.java    License: Apache License 2.0 5 votes vote down vote up
public static void writeTo(IndentingWriter writer, AnnotationEncodedValue annotationEncodedValue)
                           throws IOException {
    writer.write(".subannotation ");
    writer.write(annotationEncodedValue.getType());
    writer.write('\n');

    writeElementsTo(writer, annotationEncodedValue.getElements());
    writer.write(".end subannotation");
}
 
Example 13
Source Project: ZjDroid   Source File: BooleanRenderer.java    License: Apache License 2.0 5 votes vote down vote up
public static void writeTo(IndentingWriter writer, boolean val) throws IOException {
    if (val) {
        writer.write("true");
    } else {
        writer.write("false");
    }
}
 
Example 14
Source Project: atlas   Source File: ClassDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
        return null;
    }
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

    Iterable<? extends Field> staticFields;
    if (classDef instanceof DexBackedClassDef) {
        staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
    } else {
        staticFields = classDef.getStaticFields();
    }

    for (Field field : staticFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# static fields");
            wroteHeader = true;
        }
        writer.write('\n');

        boolean setInStaticConstructor;
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
            setInStaticConstructor = false;
        } else {
            setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
        }
        FieldDefinition.writeTo(options, fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
 
Example 15
private void writeFullMerge(IndentingWriter writer, int registerNum) throws IOException {
    registerFormatter.writeTo(writer, registerNum);
    writer.write('=');
    analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer);
    writer.write(":merge{");

    boolean first = true;

    for (AnalyzedInstruction predecessor: analyzedInstruction.getPredecessors()) {
        RegisterType predecessorRegisterType = predecessor.getPostInstructionRegisterType(registerNum);

        if (!first) {
            writer.write(',');
        }

        if (predecessor.getInstructionIndex() == -1) {
            //the fake "StartOfMethod" instruction
            writer.write("Start:");
        } else {
            writer.write("0x");
            writer.printUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor));
            writer.write(':');
        }
        predecessorRegisterType.writeTo(writer);

        first = false;
    }
    writer.write('}');
}
 
Example 16
Source Project: HeyGirl   Source File: StartLocalMethodItem.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".local ");
    registerFormatter.writeTo(writer, startLocal.getRegister());

    String name = startLocal.getName();
    String type = startLocal.getType();
    String signature = startLocal.getSignature();

    if (name != null || type != null || signature != null) {
        writer.write(", ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example 17
Source Project: zjdroid   Source File: SparseSwitchMethodItem.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".sparse-switch\n");
    writer.indent(4);
    for (SparseSwitchTarget target: targets) {
        IntegerRenderer.writeTo(writer, target.getKey());
        writer.write(" -> ");
        target.writeTargetTo(writer);
        writeResourceId(writer, target.getKey());
        writer.write('\n');
    }
    writer.deindent(4);
    writer.write(".end sparse-switch");
    return true;
}
 
Example 18
Source Project: ZjDroid   Source File: AnnotationEncodedValueAdaptor.java    License: Apache License 2.0 5 votes vote down vote up
public static void writeTo(IndentingWriter writer, AnnotationEncodedValue annotationEncodedValue)
                           throws IOException {
    writer.write(".subannotation ");
    writer.write(annotationEncodedValue.getType());
    writer.write('\n');

    writeElementsTo(writer, annotationEncodedValue.getElements());
    writer.write(".end subannotation");
}
 
Example 19
Source Project: HeyGirl   Source File: ClassDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private void writeInterfaces(IndentingWriter writer) throws IOException {
    List<String> interfaces = Lists.newArrayList(classDef.getInterfaces());
    Collections.sort(interfaces);

    if (interfaces.size() != 0) {
        writer.write('\n');
        writer.write("# interfaces\n");
        for (String interfaceName: interfaces) {
            writer.write(".implements ");
            writer.write(interfaceName);
            writer.write('\n');
        }
    }
}
 
Example 20
Source Project: HeyGirl   Source File: MethodDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private static void writeParameters(IndentingWriter writer, Method method,
                                    List<? extends MethodParameter> parameters,
                                    baksmaliOptions options) throws IOException {
    boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
    int registerNumber = isStatic?0:1;
    for (MethodParameter parameter: parameters) {
        String parameterType = parameter.getType();
        String parameterName = parameter.getName();
        Collection<? extends Annotation> annotations = parameter.getAnnotations();
        if (parameterName != null || annotations.size() != 0) {
            writer.write(".param p");
            writer.printSignedIntAsDec(registerNumber);

            if (parameterName != null && options.outputDebugInfo) {
                writer.write(", ");
                ReferenceFormatter.writeStringReference(writer, parameterName);
            }
            writer.write("    # ");
            writer.write(parameterType);
            writer.write("\n");
            if (annotations.size() > 0) {
                writer.indent(4);
                AnnotationFormatter.writeTo(writer, annotations);
                writer.deindent(4);
                writer.write(".end param\n");
            }
        }

        registerNumber++;
        if (TypeUtils.isWideType(parameterType)) {
            registerNumber++;
        }
    }
}
 
Example 21
Source Project: zjdroid   Source File: ClassDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private void writeSuper(IndentingWriter writer) throws IOException {
    String superClass = classDef.getSuperclass();
    if (superClass != null) {
        writer.write(".super ");
        writer.write(superClass);
        writer.write('\n');
    }
}
 
Example 22
Source Project: zjdroid   Source File: AnnotationFormatter.java    License: Apache License 2.0 5 votes vote down vote up
public static void writeTo(IndentingWriter writer, Annotation annotation) throws IOException {
    writer.write(".annotation ");
    writer.write(AnnotationVisibility.getVisibility(annotation.getVisibility()));
    writer.write(' ');
    writer.write(annotation.getType());
    writer.write('\n');

    AnnotationEncodedValueAdaptor.writeElementsTo(writer, annotation.getElements());

    writer.write(".end annotation\n");
}
 
Example 23
Source Project: HeyGirl   Source File: ClassDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef)classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }

    for (Field field: instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');

        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(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",
                    classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");

            writer.write("# There is both a static and instance field with this signature.\n" +
                         "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(fieldWriter, field, false);
    }
}
 
Example 24
Source Project: ZjDroid   Source File: StartLocalMethodItem.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".local ");
    registerFormatter.writeTo(writer, startLocal.getRegister());

    String name = startLocal.getName();
    String type = startLocal.getType();
    String signature = startLocal.getSignature();

    if (name != null || type != null || signature != null) {
        writer.write(", ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example 25
Source Project: HeyGirl   Source File: ClassDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private void writeSuper(IndentingWriter writer) throws IOException {
    String superClass = classDef.getSuperclass();
    if (superClass != null) {
        writer.write(".super ");
        writer.write(superClass);
        writer.write('\n');
    }
}
 
Example 26
Source Project: ZjDroid   Source File: ClassDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef)classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }

    for (Field field: instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');

        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(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",
                    classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");

            writer.write("# There is both a static and instance field with this signature.\n" +
                         "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(fieldWriter, field, false);
    }
}
 
Example 27
Source Project: ZjDroid   Source File: ArrayDataMethodItem.java    License: Apache License 2.0 5 votes vote down vote up
public boolean writeTo(IndentingWriter writer) throws IOException {
    int elementWidth = instruction.getElementWidth();

    writer.write(".array-data ");
    writer.printSignedIntAsDec(instruction.getElementWidth());
    writer.write('\n');

    writer.indent(4);

    List<Number> elements = instruction.getArrayElements();

    String suffix = "";
    switch (elementWidth) {
        case 1:
            suffix = "t";
            break;
        case 2:
            suffix = "s";
            break;
    }

    for (Number number: elements) {
        LongRenderer.writeSignedIntOrLongTo(writer, number.longValue());
        writer.write(suffix);
        if (elementWidth == 4)
            writeResourceId(writer, number.intValue());
        writer.write("\n");
    }
    writer.deindent(4);
    writer.write(".end array-data");
    return true;
}
 
Example 28
Source Project: atlas   Source File: MethodDefinition.java    License: Apache License 2.0 5 votes vote down vote up
private static void writeParameters(IndentingWriter writer, Method method,
                                    List<? extends MethodParameter> parameters,
                                    BaksmaliOptions options) throws IOException {
    boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
    int registerNumber = isStatic ? 0 : 1;
    for (MethodParameter parameter : parameters) {
        String parameterType = parameter.getType();
        String parameterName = parameter.getName();
        Collection<? extends Annotation> annotations = parameter.getAnnotations();
        if (parameterName != null || annotations.size() != 0) {
            writer.write(".param p");
            writer.printSignedIntAsDec(registerNumber);

            if (parameterName != null && options.debugInfo) {
                writer.write(", ");
                ReferenceFormatter.writeStringReference(writer, parameterName);
            }
            writer.write("    # ");
            writer.write(parameterType);
            writer.write("\n");
            if (annotations.size() > 0) {
                writer.indent(4);

                String containingClass = null;
                if (options.implicitReferences) {
                    containingClass = method.getDefiningClass();
                }
                AnnotationFormatter.writeTo(writer, annotations, containingClass);
                writer.deindent(4);
                writer.write(".end param\n");
            }
        }

        registerNumber++;
        if (TypeUtils.isWideType(parameterType)) {
            registerNumber++;
        }
    }
}
 
Example 29
Source Project: HeyGirl   Source File: LabelMethodItem.java    License: Apache License 2.0 5 votes vote down vote up
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(':');
    writer.write(labelPrefix);
    if (options.useSequentialLabels) {
        writer.printUnsignedLongAsHex(labelSequence);
    } else {
        writer.printUnsignedLongAsHex(this.getLabelAddress());
    }
    return true;
}
 
Example 30
Source Project: HeyGirl   Source File: InstructionMethodItem.java    License: Apache License 2.0 5 votes vote down vote up
protected void writeResourceId(IndentingWriter writer, int val) throws IOException {
    Map<Integer,String> resourceIds = methodDef.classDef.options.resourceIds;
    String resource = resourceIds.get(Integer.valueOf(val));
    if (resource != null) {
        writer.write("    # ");
        writer.write(resource);
    }
}