org.jf.dexlib2.dexbacked.DexBackedClassDef Java Examples

The following examples show how to use org.jf.dexlib2.dexbacked.DexBackedClassDef. 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: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static Set<DexBackedClassDef> scanClasses(File smaliDir, List<File> newFiles) throws PatchException {

        Set<DexBackedClassDef> classes = Sets.newHashSet();
        try {
            for (File newFile : newFiles) {
                DexBackedDexFile newDexFile = DexFileFactory.loadDexFile(newFile, Opcodes.getDefault());
                Set<? extends DexBackedClassDef> dexClasses = newDexFile.getClasses();
                classes.addAll(dexClasses);
            }

            final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
            final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");

            for (DexBackedClassDef classDef : classes) {
                String className = classDef.getType();
                ApkPatch.currentClassType = null;
                AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classes, 19), true, true);
                File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
            }
        } catch (Exception e) {
            throw new PatchException(e);
        }
        return classes;
    }
 
Example #2
Source File: SmaliUtils.java    From jadx with Apache License 2.0 6 votes vote down vote up
public static boolean getSmaliCode(Path path, int clsDefOffset, StringWriter stringWriter) {
	if (clsDefOffset == 0) {
		return false;
	}
	try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(path))) {
		DexBackedDexFile dexFile = DexBackedDexFile.fromInputStream(null, inputStream);
		DexBackedClassDef dexBackedClassDef = new DexBackedClassDef(dexFile, clsDefOffset, 0);
		getSmaliCode(dexBackedClassDef, stringWriter);
		return true;
	} catch (Exception e) {
		LOG.error("Error generating smali", e);
		stringWriter.append("Error generating smali code: ");
		stringWriter.append(e.getMessage());
		stringWriter.append(System.lineSeparator());
		stringWriter.append(Utils.getStackTrace(e));
		return false;
	}
}
 
Example #3
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static Set<String> buildCode(File smaliDir, File dexFile, DexDiffInfo info) throws IOException,
        RecognitionException {
    Set<String> classes = new HashSet<String>();
    Set<DexBackedClassDef> classDefs = new HashSet<DexBackedClassDef>();
    classDefs.addAll(info.getModifiedClasses());
    classDefs.addAll(info.getAddedClasses());
    final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    File smaliFile;
    String className;
    for (DexBackedClassDef classDef : classDefs) {
        ApkPatch.currentClassType = classDef.getType();
        className = TypeGenUtil.newType(classDef.getType());
        AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classDefs, 19), false, false);
        smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
        classes.add(className.substring(1, className.length() - 1).replace('/', '.'));
        SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true);
    }

    dexBuilder.writeTo(new FileDataStore(dexFile));

    return classes;
}
 
Example #4
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static DexBackedClassDef getModifiedClasses(String clazz) {
    for (DexBackedClassDef classDef : modifiedClasses) {
        if (classDef.getType().equals(clazz)) {
            return classDef;
        }
    }
    return null;
}
 
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 atlas with 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 #7
Source File: PatchFieldTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static void addField(String inFile, String outFile, DexBackedClassDef dexBackedClassDef, ImmutableField immutableField) throws IOException {

        DexFile dexFile = readDexFile(inFile);
        for (ClassDef classDef : dexFile.getClasses()) {
            if (dexBackedClassDef != null && dexBackedClassDef.getType().equals(classDef.getType())) {
                dexFile = addField(dexFile, classDef.getType(), immutableField);
            } else if (dexBackedClassDef == null) {
                dexFile = addField(dexFile, classDef.getType(), immutableField);

            }
        }
        reDexFile(dexFile);

        DexFileFactory.writeDexFile(outFile, new DexFile() {
            @Nonnull
            @Override
            public Set<? extends ClassDef> getClasses() {
                return new AbstractSet<ClassDef>() {
                    @Nonnull
                    @Override
                    public Iterator<ClassDef> iterator() {
                        return classes.iterator();
                    }

                    @Override
                    public int size() {
                        return classes.size();
                    }
                };
            }

            @Nonnull
            @Override
            public Opcodes getOpcodes() {
                return Opcodes.getDefault();
            }
        });
    }
 
Example #8
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 #9
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 #10
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 #11
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static File disassemble(File smaliDir, DexBackedClassDef dexBackedClassDef) throws PatchException {

        Set<DexBackedClassDef> classes = Sets.newHashSet();
        classes.add(dexBackedClassDef);
        final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
        String className = dexBackedClassDef.getType();
        AfBakSmali.disassembleClass(dexBackedClassDef, outFileNameHandler, getBuildOption(classes, 19), true, false);
        File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
        return smaliFile;

    }
 
Example #12
Source File: SmaliUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@NotNull
public static String getSmaliCode(DexNode dex, int clsDefOffset) {
	try {
		Path path = dex.getDexFile().getPath();
		DexBackedDexFile dexFile = DexFileFactory.loadDexFile(path.toFile(), null);
		DexBackedClassDef dexBackedClassDef = new DexBackedClassDef(dexFile, clsDefOffset);
		return getSmaliCode(dexBackedClassDef);
	} catch (Exception e) {
		LOG.error("Error generating smali", e);
		return "Error generating smali code: " + e.getMessage()
				+ '\n' + Utils.getStackTrace(e);
	}
}
 
Example #13
Source File: ReferenceUtil.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static boolean isStaticFiled(DexBackedClassDef classDef, FieldReference reference) {
    for (DexBackedField field : classDef.getStaticFields()) {
        if (field.equals(reference)) {
            return true;
        }
    }
    return false;
}
 
Example #14
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 #15
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 5 votes vote down vote up
public void setOldClasses(String dexNum, Set<? extends DexBackedClassDef> baseClassDefs) {
    baseClassDefs.forEach(new Consumer<DexBackedClassDef>() {
        @Override
        public void accept(DexBackedClassDef dexBackedClassDef) {
            oldClasses.put(dexBackedClassDef,Integer.valueOf(dexNum));
        }
    });

}
 
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> 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 #18
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 #19
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static Set<String> buildCode(File dexFile, DexDiffInfo info) throws IOException,
        RecognitionException {
    Set<String>classes = new HashSet<>();
    Set<DexBackedClassDef> classDefs = new HashSet<DexBackedClassDef>();
    classDefs.addAll(info.getModifiedClasses());
    classDefs.addAll(info.getAddedClasses());
    DexFileFactory.writeDexFile(dexFile.getAbsolutePath(), new DexFile() {
        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<DexBackedClassDef>() {
                @Override
                public Iterator<DexBackedClassDef> iterator() {
                    return classDefs.iterator();
                }

                @Override
                public int size() {
                    return classDefs.size();
                }
            };
        }

        @Nonnull
        @Override
        public Opcodes getOpcodes() {
            return Opcodes.getDefault();
        }
    });

    for (ClassDef classDef:classDefs){
        classes.add(classDef.getType());
    }
    return classes;
}
 
Example #20
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 #21
Source File: DexClass.java    From apkfile with Apache License 2.0 5 votes vote down vote up
DexClass(DexBackedClassDef classDef, boolean shortMethodSignatures, boolean filterSupport) {
    this.classDef = classDef;
    this.shortMethodSignatures = shortMethodSignatures;
    this.filterSupport = filterSupport;

    methodSignatureToMethod = new HashMap<>();
    methodAccessorCounts = new TObjectIntHashMap<>();
    annotationCount = classDef.getAnnotations().size();
    size = classDef.getSize();
    fieldCount = Utils.makeCollection(classDef.getFields()).size();
    accessFlags = classDef.getAccessFlags();

    analyze();
}
 
Example #22
Source File: DexFile.java    From apkfile with Apache License 2.0 5 votes vote down vote up
private synchronized void cacheLocalClasses(DexBackedDexFile dexFile) {
    /*
     * Must collect all local classes before any analysis because an API method is defined as
     * any non-local method. In multi-dex situations, there many be many API calls which are not
     * local to a single DEX.
     */
    for (DexBackedClassDef classDef : dexFile.getClasses()) {
        String classPath = classDef.getType();
        LOCAL_CLASS_PATHS.add(classPath);
    }
}
 
Example #23
Source File: D8DexMergerTest.java    From bundletool with Apache License 2.0 5 votes vote down vote up
private static ImmutableSet<String> listClassesInDexFiles(Collection<Path> dexPaths)
    throws Exception {
  ImmutableSet.Builder<String> classes = ImmutableSet.builder();
  for (Path dexPath : dexPaths) {
    DexBackedDexFile dexFile = DexFileFactory.loadDexFile(dexPath.toFile(), Opcodes.getDefault());
    for (DexBackedClassDef clazz : dexFile.getClasses()) {
      classes.add(clazz.getType());
    }
  }
  return classes.build();
}
 
Example #24
Source File: ClassDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    if (!fullMethod&& !DexDiffInfo.addedClasses.contains(classDef)) {
        return;
    }
    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(options, fieldWriter, field, false);
    }
}
 
Example #25
Source File: SmaliUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@NotNull
public static String getSmaliCode(DexNode dex, int clsDefOffset) {
	try {
		Path path = dex.getDexFile().getPath();
		DexBackedDexFile dexFile = DexFileFactory.loadDexFile(path.toFile(), null);
		DexBackedClassDef dexBackedClassDef = new DexBackedClassDef(dexFile, clsDefOffset);
		return getSmaliCode(dexBackedClassDef);
	} catch (Exception e) {
		LOG.error("Error generating smali", e);
		return "Error generating smali code: " + e.getMessage()
				+ '\n' + Utils.getStackTrace(e);
	}
}
 
Example #26
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 #27
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 #28
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 #29
Source File: ClassDefinition.java    From atlas 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;
    Set<? extends Method> modifieds = null;
    if (classDef instanceof DexBackedClassDef) {
        virtualMethods = ((DexBackedClassDef) classDef).getVirtualMethods(false);
        modifieds = (Set<? extends Method>) DexDiffInfo.modifiedMethods;
    } else {
        virtualMethods = classDef.getVirtualMethods();
    }

    MethodReplaceAnnotation replaceAnnotaion;
    for (Method method : virtualMethods) {

        if (!fullMethod && !DexDiffInfo.addedClasses.contains(classDef)) {
            if (!modifieds.contains(method) && !DexDiffInfo.addedMethods.contains(method)) {
                continue;
            }
        }


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

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

        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 #30
Source File: DexDiffInfo.java    From atlas with Apache License 2.0 4 votes vote down vote up
public Map<DexBackedClassDef,Integer> getOldClasses() {
    return oldClasses;
}