org.jf.dexlib2.iface.ClassDef Java Examples

The following examples show how to use org.jf.dexlib2.iface.ClassDef. 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: ImmutableClassDef.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
public static ImmutableClassDef of(ClassDef classDef) {
    if (classDef instanceof ImmutableClassDef) {
        return (ImmutableClassDef)classDef;
    }
    return new ImmutableClassDef(
            classDef.getType(),
            classDef.getAccessFlags(),
            classDef.getSuperclass(),
            classDef.getInterfaces(),
            classDef.getSourceFile(),
            classDef.getAnnotations(),
            classDef.getStaticFields(),
            classDef.getInstanceFields(),
            classDef.getDirectMethods(),
            classDef.getVirtualMethods());
}
 
Example #2
Source File: SmaliCodeUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * 生成解析成smali代码的选项
 *
 * @return
 */
private static BaksmaliOptions createBaksmaliOptions(ClassDef classDef) {
    BaksmaliOptions options = new BaksmaliOptions();
    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = false;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = DEFAULT_API_LEVEL;
    List<ClassDef> classDefs = Lists.newArrayList();
    classDefs.add(classDef);
    options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(),classDefs);
    return options;
}
 
Example #3
Source File: SmaliClassDetailLoader.java    From PATDroid with Apache License 2.0 6 votes vote down vote up
private ClassDetail translateClassDef(ClassInfo ci, ClassDef classDef, IdentityHashMap<MethodInfo, MethodImplementation> collector) {
    ClassDetail.Builder builder = new ClassDetail.Builder();
    if (classDef.getSuperclass() == null) {
        builder.setBaseType(null); // for java.lang.Object
    } else {
        builder.setBaseType(Dalvik.findOrCreateClass(ci.scope, classDef.getSuperclass()));
    }
    builder.setInterfaces(findOrCreateClasses(ci.scope, classDef.getInterfaces()));
    builder.setAccessFlags(translateAccessFlags(classDef.getAccessFlags()));
    builder.setAllMethods(translateMethods(ci, classDef.getMethods(), collector));
    builder.setStaticFields(translateFields(ci.scope, classDef.getStaticFields()));
    HashMap<String, ClassInfo> fields = translateFields(ci.scope, classDef.getInstanceFields());
    // TODO: do we need this?
    if (ci.isInnerClass()) {
        fields.put("this$0", ci.getOuterClass());
    }
    builder.setFields(fields);
    builder.setIsFrameworkClass(isFramework);
    return builder.build();
}
 
Example #4
Source File: HotDexPatchDexTool.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
public Set<ClassDef> createModifyClasses() throws IOException, PatchException {
    dexDiffer.setTpatch(false);
    Set<ClassDef>mClasses =  super.createModifyClasses();
    if (hotClassList == null || hotClassList.size() == 0){
        return mClasses;
    }
    hotClassDefs = new HashSet<>();
    Iterator<ClassDef>iterator = mClasses.iterator();
    while (iterator.hasNext()) {
        ClassDef classDef = iterator.next();
        if (hotClassList.contains(classDef.getType()) || hotClassList.contains(SmaliUtils.getDalvikClassName(classDef.getType()))) {
                hotClassDefs.add(classDef);
                iterator.remove();
            }
    }
    return mClasses;

}
 
Example #5
Source File: PatchDexTool.java    From atlas with Apache License 2.0 6 votes vote down vote up
public PatchDexTool(List<File> baseDexFiles, List<File> newDexFiles, int apiLevel, Map<String,ClassDef> map,boolean mainBundle) {
    this.baseDexFiles = baseDexFiles;
    this.newDexFiles = newDexFiles;
    this.apiLevel = apiLevel;
    this.mainBundle = mainBundle;
    assert (null != baseDexFiles && baseDexFiles.size() > 0);
    assert (null != newDexFiles && newDexFiles.size() > 0);
    this.dexDiffer = new DexDiffer(baseDexFiles, newDexFiles, apiLevel);
    if (map == null) {
        dexDiffer.setLastBundleClassMap(lastBundleClassMap);
    }else {
        dexDiffer.setLastBundleClassMap(map);
    }


}
 
Example #6
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static BaksmaliOptions getBuildOption(Iterable<? extends ClassDef> collection, int apiLevel) {
    BaksmaliOptions options = new BaksmaliOptions();

    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = true;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = apiLevel;
    if (!options.accessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(),collection);
    }

    return options;
}
 
Example #7
Source File: ImmutableClassDef.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
public static ImmutableClassDef of(ClassDef classDef) {
    if (classDef instanceof ImmutableClassDef) {
        return (ImmutableClassDef)classDef;
    }
    return new ImmutableClassDef(
            classDef.getType(),
            classDef.getAccessFlags(),
            classDef.getSuperclass(),
            classDef.getInterfaces(),
            classDef.getSourceFile(),
            classDef.getAnnotations(),
            classDef.getStaticFields(),
            classDef.getInstanceFields(),
            classDef.getDirectMethods(),
            classDef.getVirtualMethods());
}
 
Example #8
Source File: RootBuilder.java    From android-classyshark with Apache License 2.0 6 votes vote down vote up
private void fillFromDex(File file, ClassNode rootNode) {
    try {
        DexFile dxFile = DexlibLoader.loadDexFile(file);

        Set<? extends ClassDef> classSet = dxFile.getClasses();
        for (ClassDef o : classSet) {
            int methodCount = 0;
            for (Method method : o.getMethods()) {
                methodCount++;
            }

            String translatedClassName = o.getType().replaceAll("\\/", "\\.").substring(1, o.getType().length() - 1);
            ClassInfo classInfo = new ClassInfo(translatedClassName, methodCount);
            rootNode.add(classInfo);
        }

    } catch (Exception ex) {
        System.err.println("Error parsing Dexfile: " + file.getName() + ": " + ex.getMessage());
        ex.printStackTrace(System.err);
    }
}
 
Example #9
Source File: ImmutableClassDef.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static ImmutableClassDef of(ClassDef classDef) {
    if (classDef instanceof ImmutableClassDef) {
        return (ImmutableClassDef)classDef;
    }
    return new ImmutableClassDef(
            classDef.getType(),
            classDef.getAccessFlags(),
            classDef.getSuperclass(),
            classDef.getInterfaces(),
            classDef.getSourceFile(),
            classDef.getAnnotations(),
            classDef.getStaticFields(),
            classDef.getInstanceFields(),
            classDef.getDirectMethods(),
            classDef.getVirtualMethods());
}
 
Example #10
Source File: DexPool.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {
    DexPool dexPool = makeDexPool();
    for (ClassDef classDef: input.getClasses()) {
        ((ClassPool)dexPool.classSection).intern(classDef);
    }
    dexPool.writeTo(new FileDataStore(new File(path)));
}
 
Example #11
Source File: ClassProto.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the interfaces directly implemented by this class, or the interfaces they transitively implement.
 *
 * This does not include any interfaces that are only implemented by a superclass
 *
 * @return An iterables of ClassDefs representing the directly or transitively implemented interfaces
 * @throws UnresolvedClassException if interfaces could not be fully resolved
 */
@Nonnull
protected Iterable<ClassDef> getDirectInterfaces() {
    Iterable<ClassDef> directInterfaces =
            FluentIterable.from(getInterfaces().values()).filter(Predicates.notNull());

    if (!interfacesFullyResolved) {
        throw new UnresolvedClassException("Interfaces for class %s not fully resolved", getType());
    }

    return directInterfaces;
}
 
Example #12
Source File: DexPool.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {
    DexPool dexPool = makeDexPool();
    for (ClassDef classDef: input.getClasses()) {
        ((ClassPool)dexPool.classSection).intern(classDef);
    }
    dexPool.writeTo(new FileDataStore(new File(path)));
}
 
Example #13
Source File: ClassProto.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the interfaces directly implemented by this class, or the interfaces they transitively implement.
 *
 * This does not include any interfaces that are only implemented by a superclass
 *
 * @return An iterables of ClassDefs representing the directly or transitively implemented interfaces
 * @throws UnresolvedClassException if interfaces could not be fully resolved
 */
@Nonnull
protected Iterable<ClassDef> getDirectInterfaces() {
    Iterable<ClassDef> directInterfaces =
            FluentIterable.from(getInterfaces().values()).filter(Predicates.notNull());

    if (!interfacesFullyResolved) {
        throw new UnresolvedClassException("Interfaces for class %s not fully resolved", getType());
    }

    return directInterfaces;
}
 
Example #14
Source File: ClassReader.java    From atlas with Apache License 2.0 5 votes vote down vote up
public ClassDef read(String className,String memberName){
    if (classDefs == null){
        return null;
    }
    for (ClassDef classDef:classDefs){
        if (classDef.getType().equals(className)){
            return classDef;
        }
    }
    return null;
}
 
Example #15
Source File: ClassPath.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ClassPath instance that can load classes from the given dex files
 *
 * @param classPath An iterable of DexFile objects. When loading a class, these dex files will be searched in order
 * @param api API level
 */
public ClassPath(@Nonnull Iterable<DexFile> classPath, int api) {
    // add fallbacks for certain special classes that must be present
    Iterable<DexFile> dexFiles = Iterables.concat(classPath, Lists.newArrayList(getBasicClasses()));

    unknownClass = new UnknownClassProto(this);
    loadedClasses.put(unknownClass.getType(), unknownClass);
    this.api = api;

    loadPrimitiveType("Z");
    loadPrimitiveType("B");
    loadPrimitiveType("S");
    loadPrimitiveType("C");
    loadPrimitiveType("I");
    loadPrimitiveType("J");
    loadPrimitiveType("F");
    loadPrimitiveType("D");
    loadPrimitiveType("L");
    //Logger.log("add the classinfo by classpath");
    for (DexFile dexFile: dexFiles) {
        for (ClassDef classDef: dexFile.getClasses()) {
            ClassDef prev = availableClasses.get(classDef.getType());
            if (prev == null) {
                availableClasses.put(classDef.getType(), classDef);
                //Logger.log("add the calldef "+classDef.getType());
            }
        }
    }
    //Logger.log("end the classinfo by classpath");
}
 
Example #16
Source File: ClassPath.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Nonnull
public ClassDef getClassDef(String type) {
    ClassDef ret = availableClasses.get(type);
    if (ret == null) {
        throw new UnresolvedClassException("Could not resolve class %s", type);
    }
    return ret;
}
 
Example #17
Source File: DexReader.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
public static List<String> readClassNamesFromDex(File binaryArchiveFile) throws Exception {
    DexFile dexFile = DexlibLoader.loadDexFile(binaryArchiveFile);
    List<String> result = new ArrayList<>();

    for (ClassDef classDef : dexFile.getClasses()) {
        result.add(classDef.getType().replaceAll("/", ".").
                substring(1, classDef.getType().length() - 1));
    }

    Collections.sort(result);
    return result;
}
 
Example #18
Source File: PatchMethodTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static boolean methodNeedsModification(@Nonnull ClassDef classDef, @Nonnull Method method, boolean isAndFix) {

        if (isAndFix) {
            return true;
        } else if (method.getName().equals("<init>")) {
            return true;
        }
        return false;

    }
 
Example #19
Source File: PoolClassDef.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
PoolClassDef(@Nonnull ClassDef classDef) {
    this.classDef = classDef;

    interfaces = new TypeListPool.Key<SortedSet<String>>(ImmutableSortedSet.copyOf(classDef.getInterfaces()));
    staticFields = ImmutableSortedSet.copyOf(classDef.getStaticFields());
    instanceFields = ImmutableSortedSet.copyOf(classDef.getInstanceFields());
    directMethods = ImmutableSortedSet.copyOf(
            Iterables.transform(classDef.getDirectMethods(), PoolMethod.TRANSFORM));
    virtualMethods = ImmutableSortedSet.copyOf(
            Iterables.transform(classDef.getVirtualMethods(), PoolMethod.TRANSFORM));
}
 
Example #20
Source File: PoolClassDef.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
PoolClassDef(@Nonnull ClassDef classDef) {
    this.classDef = classDef;

    interfaces = new TypeListPool.Key<SortedSet<String>>(ImmutableSortedSet.copyOf(classDef.getInterfaces()));
    staticFields = ImmutableSortedSet.copyOf(classDef.getStaticFields());
    instanceFields = ImmutableSortedSet.copyOf(classDef.getInstanceFields());
    directMethods = ImmutableSortedSet.copyOf(
            Iterables.transform(classDef.getDirectMethods(), PoolMethod.TRANSFORM));
    virtualMethods = ImmutableSortedSet.copyOf(
            Iterables.transform(classDef.getVirtualMethods(), PoolMethod.TRANSFORM));
}
 
Example #21
Source File: DexPool.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static void writeTo(@Nonnull String path, @Nonnull org.jf.dexlib2.iface.DexFile input) throws IOException {
    DexPool dexPool = makeDexPool();
    for (ClassDef classDef: input.getClasses()) {
        ((ClassPool)dexPool.classSection).intern(classDef);
    }
    dexPool.writeTo(new FileDataStore(new File(path)));
}
 
Example #22
Source File: DexPatchDexTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
public Set<ClassDef> createModifyClasses() throws IOException, PatchException {
    dexDiffer.setTpatch(false);
    dexDiffer.setExludeClasses(excludeClasses);
    dexDiffer.setPatchClasses(patchClasses);
    return super.createModifyClasses();
}
 
Example #23
Source File: PatchDexTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
public DexDiffInfo createPatchDex(File outDexFolder) throws IOException, RecognitionException, PatchException {
     Set<ClassDef>modifyClasses = createModifyClasses();
        if (modifyClasses.size() > 0) {
            writeDex(outDexFolder,modifyClasses);
            writePatchInfo(outDexFolder);

        }
    return dexDiffInfo;
}
 
Example #24
Source File: PatchDexTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
private List<ClassDef> sort(Set<ClassDef> classDefs){
    List<ClassDef>lastDexClasses = new ArrayList<>();
    classDefs.forEach(classDef -> {
        if (classDef.getType().equals("Landroid/taobao/atlas/framework/FrameworkProperties;")||classDef.getType().equals("Landroid/taobao/atlas/bundleInfo/AtlasBundleInfoGenerator;")){
            lastDexClasses.add(classDef);
        }
    });
    classDefs.removeAll(lastDexClasses);

    classDefs.forEach(classDef -> lastDexClasses.add(0,classDef));

    return lastDexClasses;
}
 
Example #25
Source File: MetaObjectDex.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
public MetaObjectDex(ClassDef classDef) {
    super();
    this.classDef = classDef;

    if (this.classDef == null) {
        this.classDef = new EmptyClassDef();
    }
}
 
Example #26
Source File: PoolClassDef.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
PoolClassDef(@Nonnull ClassDef classDef) {
    this.classDef = classDef;

    interfaces = new TypeListPool.Key<SortedSet<String>>(ImmutableSortedSet.copyOf(classDef.getInterfaces()));
    staticFields = ImmutableSortedSet.copyOf(classDef.getStaticFields());
    instanceFields = ImmutableSortedSet.copyOf(classDef.getInstanceFields());
    directMethods = ImmutableSortedSet.copyOf(
            Iterables.transform(classDef.getDirectMethods(), PoolMethod.TRANSFORM));
    virtualMethods = ImmutableSortedSet.copyOf(
            Iterables.transform(classDef.getVirtualMethods(), PoolMethod.TRANSFORM));
}
 
Example #27
Source File: DexlibAdapter.java    From android-classyshark with Apache License 2.0 5 votes vote down vote up
public static ClassDef getClassDefByName(String className, DexFile dexFile)
        throws Exception {
    ClassDef result = null;
    String dexName;

    for (ClassDef currentClassDef : dexFile.getClasses()) {
        dexName = currentClassDef.getType();
        if (isMatchFromDex(className, dexName)) {
            result = currentClassDef;
            break;
        }
    }

    return result;
}
 
Example #28
Source File: ClassDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
public ClassDefinition(BaksmaliOptions options, ClassDef classDef) {
    this.options = options;
    this.classDef = classDef;
    this.isScan = false;
    this.fieldsSetInStaticConstructor = findFieldsSetInStaticConstructor();
    this.fullMethod = true;
}
 
Example #29
Source File: ClassPath.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ClassPath instance that can load classes from the given dex files
 *
 * @param classPath An iterable of DexFile objects. When loading a class, these dex files will be searched in order
 * @param api API level
 */
public ClassPath(@Nonnull Iterable<DexFile> classPath, int api) {
    // add fallbacks for certain special classes that must be present
    Iterable<DexFile> dexFiles = Iterables.concat(classPath, Lists.newArrayList(getBasicClasses()));

    unknownClass = new UnknownClassProto(this);
    loadedClasses.put(unknownClass.getType(), unknownClass);
    this.api = api;

    loadPrimitiveType("Z");
    loadPrimitiveType("B");
    loadPrimitiveType("S");
    loadPrimitiveType("C");
    loadPrimitiveType("I");
    loadPrimitiveType("J");
    loadPrimitiveType("F");
    loadPrimitiveType("D");
    loadPrimitiveType("L");
    //Logger.log("add the classinfo by classpath");
    for (DexFile dexFile: dexFiles) {
        for (ClassDef classDef: dexFile.getClasses()) {
            ClassDef prev = availableClasses.get(classDef.getType());
            if (prev == null) {
                availableClasses.put(classDef.getType(), classDef);
                //Logger.log("add the calldef "+classDef.getType());
            }
        }
    }
    //Logger.log("end the classinfo by classpath");
}
 
Example #30
Source File: PatchUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static Map<String, ClassDef> getBundleClassDef(File file) throws IOException {
    HashMap<String, ClassDef> classDefMap = new HashMap<String, ClassDef>();
    File[] dexFiles = getDexFiles(file);
    HashSet<ClassDef> classDefs = new HashSet<ClassDef>();
    for (File dexFile : dexFiles) {
        classDefs.addAll(DexFileFactory.loadDexFile(dexFile, Opcodes.getDefault()).getClasses());
    }
    for (ClassDef classDef : classDefs) {
        classDefMap.put(classDef.getType(), classDef);
    }

    return classDefMap;
}