org.jf.dexlib2.util.ReferenceUtil Java Examples

The following examples show how to use org.jf.dexlib2.util.ReferenceUtil. 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 File: ReferenceFormatter.java    From HeyGirl with 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 #2
Source File: ReferenceFormatter.java    From zjdroid with 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 File: ReferenceFormatter.java    From ZjDroid with 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 #4
Source File: ReferenceFormatter.java    From ZjDroid with 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 #5
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

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

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

        boolean setInStaticConstructor;
        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(fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
 
Example #6
Source File: ClassDefinition.java    From ZjDroid with 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 #7
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef)classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }

    for (Method method: directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
 
Example #8
Source File: ClassDefinition.java    From ZjDroid with 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 #9
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

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

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

        boolean setInStaticConstructor;
        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(fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
 
Example #10
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef)classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }

    for (Method method: directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
 
Example #11
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * 将变动的信息写入到一个文件
 *
 * @param append
 * @param outFile
 */
public void save(BundleDiffResult bundleDiffResult) throws IOException {
        List<BundleDiffResult.ClassDiff>classDiffs = new ArrayList<>();
        for (ClassDiffInfo classDiffInfo:classDiffInfoMap.values()){
            BundleDiffResult.ClassDiff classDiff = new BundleDiffResult.ClassDiff();
            classDiff.setClassName(classDiffInfo.getName());
            classDiff.setDiffType(classDiffInfo.getType());
            classDiffs.add(classDiff);
            if (classDiffInfo.getModifyFields().size() > 0){
                List<BundleDiffResult.FieldDiff>fieldDiffs = new ArrayList<>();
                for (FieldDiffInfo fieldDiffInfo:classDiffInfo.getModifyFields()) {
                    BundleDiffResult.FieldDiff fieldDiff = new BundleDiffResult.FieldDiff();
                    fieldDiffs.add(fieldDiff);
                    fieldDiff.setDiffType(fieldDiffInfo.getType());
                    fieldDiff.setFieldDesc(ReferenceUtil.getFieldDescriptor(fieldDiffInfo.getBackedField()));
                }
                classDiff.setFieldDiffs(fieldDiffs);
            }
            if (classDiffInfo.getModifyMethods().size() > 0){
                List<BundleDiffResult.MethodDiff>methodDiffs = new ArrayList<>();
                for (MethodDiffInfo methodDiffInfo:classDiffInfo.getModifyMethods()) {
                    BundleDiffResult.MethodDiff methodDiff = new BundleDiffResult.MethodDiff();
                    methodDiffs.add(methodDiff);
                    methodDiff.setDiffType(methodDiffInfo.getType());
                    methodDiff.setMethodDesc(ReferenceUtil.getMethodDescriptor(methodDiffInfo.getBackedMethod()));
                }
                classDiff.setMethodDiffs(methodDiffs);
            }
        }

    bundleDiffResult.setClassDiffs(classDiffs);
}
 
Example #12
Source File: ClassDefinition.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

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

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

        boolean setInStaticConstructor;
        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(fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
 
Example #13
Source File: ClassDefinition.java    From HeyGirl with 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 #14
Source File: ClassDefinition.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef)classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }

    for (Method method: directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
 
Example #15
Source File: ClassDefinition.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

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

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

        boolean setInStaticConstructor;
        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(fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
 
Example #16
Source File: ClassDefinition.java    From zjdroid with 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 #17
Source File: ClassDefinition.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
private Set<String> writeDirectMethods(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> directMethods;
    if (classDef instanceof DexBackedClassDef) {
        directMethods = ((DexBackedClassDef)classDef).getDirectMethods(false);
    } else {
        directMethods = classDef.getDirectMethods();
    }

    for (Method method: directMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# direct methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(writer);
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
    return writtenMethods;
}
 
Example #18
Source File: EncodedValueAdaptor.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public static void writeTo(IndentingWriter writer, EncodedValue encodedValue) throws IOException {
    switch (encodedValue.getValueType()) {
        case ValueType.ANNOTATION:
            AnnotationEncodedValueAdaptor.writeTo(writer, (AnnotationEncodedValue)encodedValue);
            return;
        case ValueType.ARRAY:
            ArrayEncodedValueAdaptor.writeTo(writer, (ArrayEncodedValue)encodedValue);
            return;
        case ValueType.BOOLEAN:
            BooleanRenderer.writeTo(writer, ((BooleanEncodedValue)encodedValue).getValue());
            return;
        case ValueType.BYTE:
            ByteRenderer.writeTo(writer, ((ByteEncodedValue)encodedValue).getValue());
            return;
        case ValueType.CHAR:
            CharRenderer.writeTo(writer, ((CharEncodedValue)encodedValue).getValue());
            return;
        case ValueType.DOUBLE:
            DoubleRenderer.writeTo(writer, ((DoubleEncodedValue)encodedValue).getValue());
            return;
        case ValueType.ENUM:
            writer.write(".enum ");
            ReferenceUtil.writeFieldDescriptor(writer, ((EnumEncodedValue)encodedValue).getValue());
            return;
        case ValueType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, ((FieldEncodedValue)encodedValue).getValue());
            return;
        case ValueType.FLOAT:
            FloatRenderer.writeTo(writer, ((FloatEncodedValue)encodedValue).getValue());
            return;
        case ValueType.INT:
            IntegerRenderer.writeTo(writer, ((IntEncodedValue)encodedValue).getValue());
            return;
        case ValueType.LONG:
            LongRenderer.writeTo(writer, ((LongEncodedValue)encodedValue).getValue());
            return;
        case ValueType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, ((MethodEncodedValue)encodedValue).getValue());
            return;
        case ValueType.NULL:
            writer.write("null");
            return;
        case ValueType.SHORT:
            ShortRenderer.writeTo(writer, ((ShortEncodedValue)encodedValue).getValue());
            return;
        case ValueType.STRING:
            ReferenceFormatter.writeStringReference(writer, ((StringEncodedValue)encodedValue).getValue());
            return;
        case ValueType.TYPE:
            writer.write(((TypeEncodedValue)encodedValue).getValue());
    }
}
 
Example #19
Source File: ClassDefinition.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> virtualMethods;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef)classDef).getVirtualMethods(false);
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }

    for (Method method: virtualMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# virtual methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(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");
            System.err.println(String.format("Duplicate direct+virtual method found: %s->%s",
                    classDef.getType(), methodString));
            System.err.println("You will need to rename one of these methods, including all references.");
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
}
 
Example #20
Source File: ClassPool.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public void intern(@Nonnull ClassDef classDef) {
    PoolClassDef poolClassDef = new PoolClassDef(classDef);

    PoolClassDef prev = internedItems.put(poolClassDef.getType(), poolClassDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", poolClassDef.getType());
    }

    typePool.intern(poolClassDef.getType());
    typePool.internNullable(poolClassDef.getSuperclass());
    typeListPool.intern(poolClassDef.getInterfaces());
    stringPool.internNullable(poolClassDef.getSourceFile());

    HashSet<String> fields = new HashSet<String>();
    for (Field field: poolClassDef.getFields()) {
        String fieldDescriptor = ReferenceUtil.getShortFieldDescriptor(field);
        if (!fields.add(fieldDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for field %s->%s",
                    poolClassDef.getType(), fieldDescriptor);
        }
        fieldPool.intern(field);

        EncodedValue initialValue = field.getInitialValue();
        if (initialValue != null) {
            DexPool.internEncodedValue(initialValue, stringPool, typePool, fieldPool, methodPool);
        }

        annotationSetPool.intern(field.getAnnotations());
    }

    HashSet<String> methods = new HashSet<String>();
    for (PoolMethod method: poolClassDef.getMethods()) {
        String methodDescriptor = ReferenceUtil.getShortMethodDescriptor(method);
        if (!methods.add(methodDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for method %s->%s",
                    poolClassDef.getType(), methodDescriptor);
        }
        methodPool.intern(method);
        internCode(method);
        internDebug(method);
        annotationSetPool.intern(method.getAnnotations());

        for (MethodParameter parameter: method.getParameters()) {
            annotationSetPool.intern(parameter.getAnnotations());
        }
    }

    annotationSetPool.intern(poolClassDef.getAnnotations());
}
 
Example #21
Source File: ClassPool.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
private void internCode(@Nonnull Method method) {
    // this also handles parameter names, which aren't directly tied to the MethodImplementation, even though the debug items are
    boolean hasInstruction = false;

    MethodImplementation methodImpl = method.getImplementation();
    if (methodImpl != null) {
        for (Instruction instruction: methodImpl.getInstructions()) {
            hasInstruction = true;
            if (instruction instanceof ReferenceInstruction) {
                Reference reference = ((ReferenceInstruction)instruction).getReference();
                switch (instruction.getOpcode().referenceType) {
                    case ReferenceType.STRING:
                        stringPool.intern((StringReference)reference);
                        break;
                    case ReferenceType.TYPE:
                        typePool.intern((TypeReference)reference);
                        break;
                    case ReferenceType.FIELD:
                        fieldPool.intern((FieldReference) reference);
                        break;
                    case ReferenceType.METHOD:
                        methodPool.intern((MethodReference)reference);
                        break;
                    default:
                        throw new ExceptionWithContext("Unrecognized reference type: %d",
                                instruction.getOpcode().referenceType);
                }
            }
        }

        List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
        if (!hasInstruction && tryBlocks.size() > 0) {
            throw new ExceptionWithContext("Method %s has no instructions, but has try blocks.",
                    ReferenceUtil.getMethodDescriptor(method));
        }

        for (TryBlock<? extends ExceptionHandler> tryBlock: methodImpl.getTryBlocks()) {
            for (ExceptionHandler handler: tryBlock.getExceptionHandlers()) {
                typePool.internNullable(handler.getExceptionType());
            }
        }
    }
}
 
Example #22
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> virtualMethods;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef)classDef).getVirtualMethods(false);
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }

    for (Method method: virtualMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# virtual methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(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");
            System.err.println(String.format("Duplicate direct+virtual method found: %s->%s",
                    classDef.getType(), methodString));
            System.err.println("You will need to rename one of these methods, including all references.");
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
}
 
Example #23
Source File: ClassPool.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public void intern(@Nonnull ClassDef classDef) {
    PoolClassDef poolClassDef = new PoolClassDef(classDef);

    PoolClassDef prev = internedItems.put(poolClassDef.getType(), poolClassDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", poolClassDef.getType());
    }

    typePool.intern(poolClassDef.getType());
    typePool.internNullable(poolClassDef.getSuperclass());
    typeListPool.intern(poolClassDef.getInterfaces());
    stringPool.internNullable(poolClassDef.getSourceFile());

    HashSet<String> fields = new HashSet<String>();
    for (Field field: poolClassDef.getFields()) {
        String fieldDescriptor = ReferenceUtil.getShortFieldDescriptor(field);
        if (!fields.add(fieldDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for field %s->%s",
                    poolClassDef.getType(), fieldDescriptor);
        }
        fieldPool.intern(field);

        EncodedValue initialValue = field.getInitialValue();
        if (initialValue != null) {
            DexPool.internEncodedValue(initialValue, stringPool, typePool, fieldPool, methodPool);
        }

        annotationSetPool.intern(field.getAnnotations());
    }

    HashSet<String> methods = new HashSet<String>();
    for (PoolMethod method: poolClassDef.getMethods()) {
        String methodDescriptor = ReferenceUtil.getShortMethodDescriptor(method);
        if (!methods.add(methodDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for method %s->%s",
                    poolClassDef.getType(), methodDescriptor);
        }
        methodPool.intern(method);
        internCode(method);
        internDebug(method);
        annotationSetPool.intern(method.getAnnotations());

        for (MethodParameter parameter: method.getParameters()) {
            annotationSetPool.intern(parameter.getAnnotations());
        }
    }

    annotationSetPool.intern(poolClassDef.getAnnotations());
}
 
Example #24
Source File: EncodedValueAdaptor.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
public static void writeTo(IndentingWriter writer, EncodedValue encodedValue) throws IOException {
    switch (encodedValue.getValueType()) {
        case ValueType.ANNOTATION:
            AnnotationEncodedValueAdaptor.writeTo(writer, (AnnotationEncodedValue)encodedValue);
            return;
        case ValueType.ARRAY:
            ArrayEncodedValueAdaptor.writeTo(writer, (ArrayEncodedValue)encodedValue);
            return;
        case ValueType.BOOLEAN:
            BooleanRenderer.writeTo(writer, ((BooleanEncodedValue)encodedValue).getValue());
            return;
        case ValueType.BYTE:
            ByteRenderer.writeTo(writer, ((ByteEncodedValue)encodedValue).getValue());
            return;
        case ValueType.CHAR:
            CharRenderer.writeTo(writer, ((CharEncodedValue)encodedValue).getValue());
            return;
        case ValueType.DOUBLE:
            DoubleRenderer.writeTo(writer, ((DoubleEncodedValue)encodedValue).getValue());
            return;
        case ValueType.ENUM:
            writer.write(".enum ");
            ReferenceUtil.writeFieldDescriptor(writer, ((EnumEncodedValue)encodedValue).getValue());
            return;
        case ValueType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, ((FieldEncodedValue)encodedValue).getValue());
            return;
        case ValueType.FLOAT:
            FloatRenderer.writeTo(writer, ((FloatEncodedValue)encodedValue).getValue());
            return;
        case ValueType.INT:
            IntegerRenderer.writeTo(writer, ((IntEncodedValue)encodedValue).getValue());
            return;
        case ValueType.LONG:
            LongRenderer.writeTo(writer, ((LongEncodedValue)encodedValue).getValue());
            return;
        case ValueType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, ((MethodEncodedValue)encodedValue).getValue());
            return;
        case ValueType.NULL:
            writer.write("null");
            return;
        case ValueType.SHORT:
            ShortRenderer.writeTo(writer, ((ShortEncodedValue)encodedValue).getValue());
            return;
        case ValueType.STRING:
            ReferenceFormatter.writeStringReference(writer, ((StringEncodedValue)encodedValue).getValue());
            return;
        case ValueType.TYPE:
            writer.write(((TypeEncodedValue)encodedValue).getValue());
    }
}
 
Example #25
Source File: ClassPool.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
private void internCode(@Nonnull Method method) {
    // this also handles parameter names, which aren't directly tied to the MethodImplementation, even though the debug items are
    boolean hasInstruction = false;

    MethodImplementation methodImpl = method.getImplementation();
    if (methodImpl != null) {
        for (Instruction instruction: methodImpl.getInstructions()) {
            hasInstruction = true;
            if (instruction instanceof ReferenceInstruction) {
                Reference reference = ((ReferenceInstruction)instruction).getReference();
                switch (instruction.getOpcode().referenceType) {
                    case ReferenceType.STRING:
                        stringPool.intern((StringReference)reference);
                        break;
                    case ReferenceType.TYPE:
                        typePool.intern((TypeReference)reference);
                        break;
                    case ReferenceType.FIELD:
                        fieldPool.intern((FieldReference) reference);
                        break;
                    case ReferenceType.METHOD:
                        methodPool.intern((MethodReference)reference);
                        break;
                    default:
                        throw new ExceptionWithContext("Unrecognized reference type: %d",
                                instruction.getOpcode().referenceType);
                }
            }
        }

        List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
        if (!hasInstruction && tryBlocks.size() > 0) {
            throw new ExceptionWithContext("Method %s has no instructions, but has try blocks.",
                    ReferenceUtil.getMethodDescriptor(method));
        }

        for (TryBlock<? extends ExceptionHandler> tryBlock: methodImpl.getTryBlocks()) {
            for (ExceptionHandler handler: tryBlock.getExceptionHandlers()) {
                typePool.internNullable(handler.getExceptionType());
            }
        }
    }
}
 
Example #26
Source File: ClassPool.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
public void intern(@Nonnull ClassDef classDef) {
    PoolClassDef poolClassDef = new PoolClassDef(classDef);

    PoolClassDef prev = internedItems.put(poolClassDef.getType(), poolClassDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", poolClassDef.getType());
    }

    typePool.intern(poolClassDef.getType());
    typePool.internNullable(poolClassDef.getSuperclass());
    typeListPool.intern(poolClassDef.getInterfaces());
    stringPool.internNullable(poolClassDef.getSourceFile());

    HashSet<String> fields = new HashSet<String>();
    for (Field field: poolClassDef.getFields()) {
        String fieldDescriptor = ReferenceUtil.getShortFieldDescriptor(field);
        if (!fields.add(fieldDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for field %s->%s",
                    poolClassDef.getType(), fieldDescriptor);
        }
        fieldPool.intern(field);

        EncodedValue initialValue = field.getInitialValue();
        if (initialValue != null) {
            DexPool.internEncodedValue(initialValue, stringPool, typePool, fieldPool, methodPool);
        }

        annotationSetPool.intern(field.getAnnotations());
    }

    HashSet<String> methods = new HashSet<String>();
    for (PoolMethod method: poolClassDef.getMethods()) {
        String methodDescriptor = ReferenceUtil.getShortMethodDescriptor(method);
        if (!methods.add(methodDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for method %s->%s",
                    poolClassDef.getType(), methodDescriptor);
        }
        methodPool.intern(method);
        internCode(method);
        internDebug(method);
        annotationSetPool.intern(method.getAnnotations());

        for (MethodParameter parameter: method.getParameters()) {
            annotationSetPool.intern(parameter.getAnnotations());
        }
    }

    annotationSetPool.intern(poolClassDef.getAnnotations());
}
 
Example #27
Source File: ClassDefinition.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
private void writeVirtualMethods(IndentingWriter writer, Set<String> directMethods) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenMethods = new HashSet<String>();

    Iterable<? extends Method> virtualMethods;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef)classDef).getVirtualMethods(false);
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }

    for (Method method: virtualMethods) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# virtual methods");
            wroteHeader = true;
        }
        writer.write('\n');

        // TODO: check for method validation errors
        String methodString = ReferenceUtil.getShortMethodDescriptor(method);

        IndentingWriter methodWriter = writer;
        if (!writtenMethods.add(methodString)) {
            writer.write("# duplicate method ignored\n");
            methodWriter = new CommentingIndentingWriter(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");
            System.err.println(String.format("Duplicate direct+virtual method found: %s->%s",
                    classDef.getType(), methodString));
            System.err.println("You will need to rename one of these methods, including all references.");
        }

        MethodImplementation methodImpl = method.getImplementation();
        if (methodImpl == null) {
            MethodDefinition.writeEmptyMethodTo(methodWriter, method, options);
        } else {
            MethodDefinition methodDefinition = new MethodDefinition(this, method, methodImpl);
            methodDefinition.writeTo(methodWriter);
        }
    }
}
 
Example #28
Source File: EncodedValueAdaptor.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
public static void writeTo(IndentingWriter writer, EncodedValue encodedValue) throws IOException {
    switch (encodedValue.getValueType()) {
        case ValueType.ANNOTATION:
            AnnotationEncodedValueAdaptor.writeTo(writer, (AnnotationEncodedValue)encodedValue);
            return;
        case ValueType.ARRAY:
            ArrayEncodedValueAdaptor.writeTo(writer, (ArrayEncodedValue)encodedValue);
            return;
        case ValueType.BOOLEAN:
            BooleanRenderer.writeTo(writer, ((BooleanEncodedValue)encodedValue).getValue());
            return;
        case ValueType.BYTE:
            ByteRenderer.writeTo(writer, ((ByteEncodedValue)encodedValue).getValue());
            return;
        case ValueType.CHAR:
            CharRenderer.writeTo(writer, ((CharEncodedValue)encodedValue).getValue());
            return;
        case ValueType.DOUBLE:
            DoubleRenderer.writeTo(writer, ((DoubleEncodedValue)encodedValue).getValue());
            return;
        case ValueType.ENUM:
            writer.write(".enum ");
            ReferenceUtil.writeFieldDescriptor(writer, ((EnumEncodedValue)encodedValue).getValue());
            return;
        case ValueType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, ((FieldEncodedValue)encodedValue).getValue());
            return;
        case ValueType.FLOAT:
            FloatRenderer.writeTo(writer, ((FloatEncodedValue)encodedValue).getValue());
            return;
        case ValueType.INT:
            IntegerRenderer.writeTo(writer, ((IntEncodedValue)encodedValue).getValue());
            return;
        case ValueType.LONG:
            LongRenderer.writeTo(writer, ((LongEncodedValue)encodedValue).getValue());
            return;
        case ValueType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, ((MethodEncodedValue)encodedValue).getValue());
            return;
        case ValueType.NULL:
            writer.write("null");
            return;
        case ValueType.SHORT:
            ShortRenderer.writeTo(writer, ((ShortEncodedValue)encodedValue).getValue());
            return;
        case ValueType.STRING:
            ReferenceFormatter.writeStringReference(writer, ((StringEncodedValue)encodedValue).getValue());
            return;
        case ValueType.TYPE:
            writer.write(((TypeEncodedValue)encodedValue).getValue());
    }
}
 
Example #29
Source File: ClassPool.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
private void internCode(@Nonnull Method method) {
    // this also handles parameter names, which aren't directly tied to the MethodImplementation, even though the debug items are
    boolean hasInstruction = false;

    MethodImplementation methodImpl = method.getImplementation();
    if (methodImpl != null) {
        for (Instruction instruction: methodImpl.getInstructions()) {
            hasInstruction = true;
            if (instruction instanceof ReferenceInstruction) {
                Reference reference = ((ReferenceInstruction)instruction).getReference();
                switch (instruction.getOpcode().referenceType) {
                    case ReferenceType.STRING:
                        stringPool.intern((StringReference)reference);
                        break;
                    case ReferenceType.TYPE:
                        typePool.intern((TypeReference)reference);
                        break;
                    case ReferenceType.FIELD:
                        fieldPool.intern((FieldReference) reference);
                        break;
                    case ReferenceType.METHOD:
                        methodPool.intern((MethodReference)reference);
                        break;
                    default:
                        throw new ExceptionWithContext("Unrecognized reference type: %d",
                                instruction.getOpcode().referenceType);
                }
            }
        }

        List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
        if (!hasInstruction && tryBlocks.size() > 0) {
            throw new ExceptionWithContext("Method %s has no instructions, but has try blocks.",
                    ReferenceUtil.getMethodDescriptor(method));
        }

        for (TryBlock<? extends ExceptionHandler> tryBlock: methodImpl.getTryBlocks()) {
            for (ExceptionHandler handler: tryBlock.getExceptionHandlers()) {
                typePool.internNullable(handler.getExceptionType());
            }
        }
    }
}
 
Example #30
Source File: ClassPool.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
public void intern(@Nonnull ClassDef classDef) {
    PoolClassDef poolClassDef = new PoolClassDef(classDef);

    PoolClassDef prev = internedItems.put(poolClassDef.getType(), poolClassDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", poolClassDef.getType());
    }

    typePool.intern(poolClassDef.getType());
    typePool.internNullable(poolClassDef.getSuperclass());
    typeListPool.intern(poolClassDef.getInterfaces());
    stringPool.internNullable(poolClassDef.getSourceFile());

    HashSet<String> fields = new HashSet<String>();
    for (Field field: poolClassDef.getFields()) {
        String fieldDescriptor = ReferenceUtil.getShortFieldDescriptor(field);
        if (!fields.add(fieldDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for field %s->%s",
                    poolClassDef.getType(), fieldDescriptor);
        }
        fieldPool.intern(field);

        EncodedValue initialValue = field.getInitialValue();
        if (initialValue != null) {
            DexPool.internEncodedValue(initialValue, stringPool, typePool, fieldPool, methodPool);
        }

        annotationSetPool.intern(field.getAnnotations());
    }

    HashSet<String> methods = new HashSet<String>();
    for (PoolMethod method: poolClassDef.getMethods()) {
        String methodDescriptor = ReferenceUtil.getShortMethodDescriptor(method);
        if (!methods.add(methodDescriptor)) {
            throw new ExceptionWithContext("Multiple definitions for method %s->%s",
                    poolClassDef.getType(), methodDescriptor);
        }
        methodPool.intern(method);
        internCode(method);
        internDebug(method);
        annotationSetPool.intern(method.getAnnotations());

        for (MethodParameter parameter: method.getParameters()) {
            annotationSetPool.intern(parameter.getAnnotations());
        }
    }

    annotationSetPool.intern(poolClassDef.getAnnotations());
}