Search in sources :

Example 16 with AccessFlags

use of org.jf.dexlib2.AccessFlags in project soot by Sable.

the class DexPrinter method toMethods.

private Collection<Method> toMethods(SootClass clazz) {
    if (clazz.getMethods().isEmpty())
        return null;
    String classType = SootToDexUtils.getDexTypeDescriptor(clazz.getType());
    List<Method> methods = new ArrayList<Method>();
    for (SootMethod sm : clazz.getMethods()) {
        if (sm.isPhantom()) {
            // Do not print method bodies for inherited methods
            continue;
        }
        MethodImplementation impl = toMethodImplementation(sm);
        List<String> parameterNames = null;
        if (sm.hasTag("ParamNamesTag"))
            parameterNames = ((ParamNamesTag) sm.getTag("ParamNamesTag")).getNames();
        int paramIdx = 0;
        List<MethodParameter> parameters = null;
        if (sm.getParameterCount() > 0) {
            parameters = new ArrayList<MethodParameter>();
            for (Type tp : sm.getParameterTypes()) {
                String paramType = SootToDexUtils.getDexTypeDescriptor(tp);
                parameters.add(new ImmutableMethodParameter(paramType, buildMethodParameterAnnotations(sm, paramIdx), sm.isConcrete() && parameterNames != null ? parameterNames.get(paramIdx) : null));
                paramIdx++;
            }
        }
        String returnType = SootToDexUtils.getDexTypeDescriptor(sm.getReturnType());
        int accessFlags = SootToDexUtils.getDexAccessFlags(sm);
        ImmutableMethod meth = new ImmutableMethod(classType, sm.getName(), parameters, returnType, accessFlags, buildMethodAnnotations(sm), impl);
        methods.add(meth);
    }
    return methods;
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) ArrayList(java.util.ArrayList) ImmutableMethodParameter(org.jf.dexlib2.immutable.ImmutableMethodParameter) Method(org.jf.dexlib2.iface.Method) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) SootMethod(soot.SootMethod) ParamNamesTag(soot.tagkit.ParamNamesTag) BooleanType(soot.BooleanType) Type(soot.Type) DexType(soot.dexpler.DexType) RefType(soot.RefType) ShortType(soot.ShortType) ByteType(soot.ByteType) IntType(soot.IntType) CharType(soot.CharType) SootMethod(soot.SootMethod) ImmutableMethodParameter(org.jf.dexlib2.immutable.ImmutableMethodParameter) MethodParameter(org.jf.dexlib2.iface.MethodParameter)

Example 17 with AccessFlags

use of org.jf.dexlib2.AccessFlags in project soot by Sable.

the class DexClassLoader method makeSootClass.

public Dependencies makeSootClass(SootClass sc, ClassDef defItem, DexFile dexFile) {
    String superClass = defItem.getSuperclass();
    Dependencies deps = new Dependencies();
    // source file
    String sourceFile = defItem.getSourceFile();
    if (sourceFile != null) {
        sc.addTag(new SourceFileTag(sourceFile));
    }
    // super class for hierarchy level
    if (superClass != null) {
        String superClassName = Util.dottedClassName(superClass);
        SootClass sootSuperClass = SootResolver.v().makeClassRef(superClassName);
        sc.setSuperclass(sootSuperClass);
        deps.typesToHierarchy.add(sootSuperClass.getType());
    }
    // access flags
    int accessFlags = defItem.getAccessFlags();
    sc.setModifiers(accessFlags);
    // Retrieve interface names
    if (defItem.getInterfaces() != null) {
        for (String interfaceName : defItem.getInterfaces()) {
            String interfaceClassName = Util.dottedClassName(interfaceName);
            if (sc.implementsInterface(interfaceClassName))
                continue;
            SootClass interfaceClass = SootResolver.v().makeClassRef(interfaceClassName);
            interfaceClass.setModifiers(interfaceClass.getModifiers() | Modifier.INTERFACE);
            sc.addInterface(interfaceClass);
            deps.typesToHierarchy.add(interfaceClass.getType());
        }
    }
    if (Options.v().oaat() && sc.resolvingLevel() <= SootClass.HIERARCHY) {
        return deps;
    }
    DexAnnotation da = new DexAnnotation(sc, deps);
    // get the fields of the class
    for (Field sf : defItem.getStaticFields()) {
        loadField(sc, da, sf);
    }
    for (Field f : defItem.getInstanceFields()) {
        loadField(sc, da, f);
    }
    // get the methods of the class
    DexMethod dexMethod = createDexMethodFactory(dexFile, sc);
    for (Method method : defItem.getDirectMethods()) {
        loadMethod(method, sc, da, dexMethod);
    }
    for (Method method : defItem.getVirtualMethods()) {
        loadMethod(method, sc, da, dexMethod);
    }
    da.handleClassAnnotation(defItem);
    // In contrast to Java, Dalvik associates the InnerClassAttribute
    // with the inner class, not the outer one. We need to copy the
    // tags over to correspond to the Soot semantics.
    InnerClassAttribute ica = (InnerClassAttribute) sc.getTag("InnerClassAttribute");
    if (ica != null) {
        Iterator<InnerClassTag> innerTagIt = ica.getSpecs().iterator();
        while (innerTagIt.hasNext()) {
            Tag t = innerTagIt.next();
            if (t instanceof InnerClassTag) {
                InnerClassTag ict = (InnerClassTag) t;
                // Get the outer class name
                String outer = DexInnerClassParser.getOuterClassNameFromTag(ict);
                if (outer == null) {
                    // If we don't have any clue what the outer class is, we
                    // just remove
                    // the reference entirely
                    innerTagIt.remove();
                    continue;
                }
                // we leave it as it is
                if (outer.equals(sc.getName()))
                    continue;
                // Check the inner class to make sure that this tag actually
                // refers to the current class as the inner class
                String inner = ict.getInnerClass().replaceAll("/", ".");
                if (!inner.equals(sc.getName())) {
                    innerTagIt.remove();
                    continue;
                }
                SootClass osc = SootResolver.v().makeClassRef(outer);
                if (osc == sc) {
                    if (!sc.hasOuterClass())
                        continue;
                    osc = sc.getOuterClass();
                } else
                    deps.typesToHierarchy.add(osc.getType());
                // Get the InnerClassAttribute of the outer class
                InnerClassAttribute icat = (InnerClassAttribute) osc.getTag("InnerClassAttribute");
                if (icat == null) {
                    icat = new InnerClassAttribute();
                    osc.addTag(icat);
                }
                // Transfer the tag from the inner class to the outer class
                InnerClassTag newt = new InnerClassTag(ict.getInnerClass(), ict.getOuterClass(), ict.getShortName(), ict.getAccessFlags());
                icat.add(newt);
                // Remove the tag from the inner class as inner classes do
                // not have these tags in the Java / Soot semantics. The
                // DexPrinter will copy it back if we do dex->dex.
                innerTagIt.remove();
                // within the PackManager in method handleInnerClasses().
                if (!sc.hasTag("InnerClassTag")) {
                    if (((InnerClassTag) t).getInnerClass().replaceAll("/", ".").equals(sc.toString())) {
                        sc.addTag(t);
                    }
                }
            }
        }
        // remove tag if empty
        if (ica.getSpecs().isEmpty()) {
            sc.getTags().remove(ica);
        }
    }
    return deps;
}
Also used : InnerClassAttribute(soot.tagkit.InnerClassAttribute) SourceFileTag(soot.tagkit.SourceFileTag) SootMethod(soot.SootMethod) Method(org.jf.dexlib2.iface.Method) SootClass(soot.SootClass) SootField(soot.SootField) Field(org.jf.dexlib2.iface.Field) InnerClassTag(soot.tagkit.InnerClassTag) Dependencies(soot.javaToJimple.IInitialResolver.Dependencies) Tag(soot.tagkit.Tag) SourceFileTag(soot.tagkit.SourceFileTag) InnerClassTag(soot.tagkit.InnerClassTag)

Aggregations

AccessFlags (org.jf.dexlib2.AccessFlags)7 Method (org.jf.dexlib2.iface.Method)4 InnerClassTag (soot.tagkit.InnerClassTag)4 ParamNamesTag (soot.tagkit.ParamNamesTag)4 Tag (soot.tagkit.Tag)4 ArrayList (java.util.ArrayList)3 Annotation (org.jf.dexlib2.iface.Annotation)3 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)3 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)3 SootMethod (soot.SootMethod)3 Nonnull (javax.annotation.Nonnull)2 AnnotationElement (org.jf.dexlib2.iface.AnnotationElement)2 ClassDef (org.jf.dexlib2.iface.ClassDef)2 Field (org.jf.dexlib2.iface.Field)2 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)2 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)2 ImmutableEncodedValue (org.jf.dexlib2.immutable.value.ImmutableEncodedValue)2 ImmutableIntEncodedValue (org.jf.dexlib2.immutable.value.ImmutableIntEncodedValue)2 SootClass (soot.SootClass)2 SootField (soot.SootField)2