Search in sources :

Example 1 with ParamNamesTag

use of soot.tagkit.ParamNamesTag 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 2 with ParamNamesTag

use of soot.tagkit.ParamNamesTag in project soot by Sable.

the class DexAnnotation method handleMethodAnnotation.

/**
 * Converts method and method parameters annotations from Dexlib to Jimple
 *
 * @param h
 * @param method
 */
public void handleMethodAnnotation(Host h, Method method) {
    Set<? extends Annotation> aSet = method.getAnnotations();
    if (!(aSet == null || aSet.isEmpty())) {
        List<Tag> tags = handleAnnotation(aSet, null);
        if (tags != null)
            for (Tag t : tags) if (t != null) {
                h.addTag(t);
            }
    }
    String[] parameterNames = null;
    int i = 0;
    for (MethodParameter p : method.getParameters()) {
        String name = p.getName();
        if (name != null) {
            parameterNames = new String[method.getParameters().size()];
            parameterNames[i] = name;
        }
        i++;
    }
    if (parameterNames != null) {
        h.addTag(new ParamNamesTag(parameterNames));
    }
    // Is there any parameter annotation?
    boolean doParam = false;
    List<? extends MethodParameter> parameters = method.getParameters();
    for (MethodParameter p : parameters) {
        if (p.getAnnotations().size() > 0) {
            doParam = true;
            break;
        }
    }
    if (doParam) {
        VisibilityParameterAnnotationTag tag = new VisibilityParameterAnnotationTag(parameters.size(), AnnotationConstants.RUNTIME_VISIBLE);
        for (MethodParameter p : parameters) {
            List<Tag> tags = handleAnnotation(p.getAnnotations(), null);
            // so that we keep the order intact.
            if (tags == null) {
                tag.addVisibilityAnnotation(null);
                continue;
            }
            VisibilityAnnotationTag paramVat = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_VISIBLE);
            tag.addVisibilityAnnotation(paramVat);
            for (Tag t : tags) {
                if (t == null)
                    continue;
                AnnotationTag vat = null;
                if (!(t instanceof VisibilityAnnotationTag)) {
                    if (t instanceof DeprecatedTag) {
                        vat = new AnnotationTag("Ljava/lang/Deprecated;");
                    } else if (t instanceof SignatureTag) {
                        SignatureTag sig = (SignatureTag) t;
                        ArrayList<AnnotationElem> sigElements = new ArrayList<AnnotationElem>();
                        for (String s : SootToDexUtils.splitSignature(sig.getSignature())) sigElements.add(new AnnotationStringElem(s, 's', "value"));
                        AnnotationElem elem = new AnnotationArrayElem(sigElements, '[', "value");
                        vat = new AnnotationTag("Ldalvik/annotation/Signature;", Collections.singleton(elem));
                    } else {
                        throw new RuntimeException("error: unhandled tag for parameter annotation in method " + h + " (" + t + ").");
                    }
                } else {
                    vat = ((VisibilityAnnotationTag) t).getAnnotations().get(0);
                }
                paramVat.addAnnotation(vat);
            }
        }
        if (tag.getVisibilityAnnotations().size() > 0)
            h.addTag(tag);
    }
}
Also used : DeprecatedTag(soot.tagkit.DeprecatedTag) AnnotationStringElem(soot.tagkit.AnnotationStringElem) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ArrayList(java.util.ArrayList) AnnotationArrayElem(soot.tagkit.AnnotationArrayElem) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ParamNamesTag(soot.tagkit.ParamNamesTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) SignatureTag(soot.tagkit.SignatureTag) Tag(soot.tagkit.Tag) ParamNamesTag(soot.tagkit.ParamNamesTag) DeprecatedTag(soot.tagkit.DeprecatedTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) SignatureTag(soot.tagkit.SignatureTag) AnnotationTag(soot.tagkit.AnnotationTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) MethodParameter(org.jf.dexlib2.iface.MethodParameter) AnnotationElem(soot.tagkit.AnnotationElem) AnnotationAnnotationElem(soot.tagkit.AnnotationAnnotationElem)

Aggregations

ArrayList (java.util.ArrayList)2 MethodParameter (org.jf.dexlib2.iface.MethodParameter)2 ParamNamesTag (soot.tagkit.ParamNamesTag)2 Method (org.jf.dexlib2.iface.Method)1 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)1 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)1 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)1 BooleanType (soot.BooleanType)1 ByteType (soot.ByteType)1 CharType (soot.CharType)1 IntType (soot.IntType)1 RefType (soot.RefType)1 ShortType (soot.ShortType)1 SootMethod (soot.SootMethod)1 Type (soot.Type)1 DexType (soot.dexpler.DexType)1 AnnotationAnnotationElem (soot.tagkit.AnnotationAnnotationElem)1 AnnotationArrayElem (soot.tagkit.AnnotationArrayElem)1 AnnotationDefaultTag (soot.tagkit.AnnotationDefaultTag)1 AnnotationElem (soot.tagkit.AnnotationElem)1