Search in sources :

Example 6 with SignatureTag

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

the class AbstractASMBackend method generateMethods.

/**
 * Emits the bytecode for all methods of the class
 */
protected void generateMethods() {
    List<SootMethod> sortedMethods = new ArrayList<SootMethod>(sc.getMethods());
    Collections.sort(sortedMethods, new SootMethodComparator());
    for (SootMethod sm : sortedMethods) {
        if (sm.isPhantom())
            continue;
        int access = getModifiers(sm.getModifiers(), sm);
        String name = sm.getName();
        StringBuilder descBuilder = new StringBuilder(5);
        descBuilder.append('(');
        for (Type t : sm.getParameterTypes()) {
            descBuilder.append(toTypeDesc(t));
        }
        descBuilder.append(')');
        descBuilder.append(toTypeDesc(sm.getReturnType()));
        String sig = null;
        if (sm.hasTag("SignatureTag")) {
            SignatureTag genericSignature = (SignatureTag) sm.getTag("SignatureTag");
            sig = genericSignature.getSignature();
        }
        List<SootClass> exceptionList = sm.getExceptionsUnsafe();
        String[] exceptions;
        if (exceptionList != null) {
            exceptions = new String[exceptionList.size()];
            int i = 0;
            for (SootClass exc : sm.getExceptions()) {
                exceptions[i] = slashify(exc.getName());
                ++i;
            }
        } else
            exceptions = new String[0];
        MethodVisitor mv = cv.visitMethod(access, name, descBuilder.toString(), sig, exceptions);
        if (mv != null) {
            // Visit parameter annotations
            for (Tag t : sm.getTags()) {
                if (t instanceof VisibilityParameterAnnotationTag) {
                    VisibilityParameterAnnotationTag vpt = (VisibilityParameterAnnotationTag) t;
                    ArrayList<VisibilityAnnotationTag> tags = vpt.getVisibilityAnnotations();
                    if (tags != null) {
                        for (int j = 0; j < tags.size(); ++j) {
                            VisibilityAnnotationTag va = tags.get(j);
                            if (va == null) {
                                continue;
                            }
                            for (AnnotationTag at : va.getAnnotations()) {
                                AnnotationVisitor av = mv.visitParameterAnnotation(j, at.getType(), (va.getVisibility() == AnnotationConstants.RUNTIME_VISIBLE));
                                generateAnnotationElems(av, at.getElems(), true);
                            }
                        }
                    }
                }
            }
            generateAnnotations(mv, sm);
            generateAttributes(mv, sm);
            if (sm.hasActiveBody()) {
                mv.visitCode();
                generateMethodBody(mv, sm);
                /*
					 * Correct values are computed automatically by ASM,
					 * but we need the call anyways.
					 */
                mv.visitMaxs(0, 0);
            }
            mv.visitEnd();
        }
    }
}
Also used : VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) ArrayList(java.util.ArrayList) MethodVisitor(org.objectweb.asm.MethodVisitor) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationTag(soot.tagkit.AnnotationTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) SignatureTag(soot.tagkit.SignatureTag) Tag(soot.tagkit.Tag) SourceFileTag(soot.tagkit.SourceFileTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) OuterClassTag(soot.tagkit.OuterClassTag) SignatureTag(soot.tagkit.SignatureTag) AnnotationTag(soot.tagkit.AnnotationTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag)

Example 7 with SignatureTag

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

the class AbstractJasminClass method emitMethod.

protected void emitMethod(SootMethod method) {
    if (method.isPhantom())
        return;
    // Emit prologue
    emit(".method " + Modifier.toString(method.getModifiers()) + " " + method.getName() + jasminDescriptorOf(method.makeRef()));
    Iterator<SootClass> throwsIt = method.getExceptions().iterator();
    while (throwsIt.hasNext()) {
        SootClass exceptClass = throwsIt.next();
        emit(".throws " + exceptClass.getName());
    }
    if (method.hasTag("SyntheticTag") || Modifier.isSynthetic(method.getModifiers())) {
        emit(".synthetic");
    }
    if (method.hasTag("DeprecatedTag")) {
        emit(".deprecated");
    }
    if (method.hasTag("SignatureTag")) {
        String sigAttr = ".signature_attr ";
        SignatureTag sigTag = (SignatureTag) method.getTag("SignatureTag");
        sigAttr += "\"" + sigTag.getSignature() + "\"";
        emit(sigAttr);
    }
    if (method.hasTag("AnnotationDefaultTag")) {
        String annotDefAttr = ".annotation_default ";
        AnnotationDefaultTag annotDefTag = (AnnotationDefaultTag) method.getTag("AnnotationDefaultTag");
        annotDefAttr += getElemAttr(annotDefTag.getDefaultVal());
        annotDefAttr += ".end .annotation_default";
        emit(annotDefAttr);
    }
    Iterator<Tag> vit = method.getTags().iterator();
    while (vit.hasNext()) {
        Tag t = (Tag) vit.next();
        if (t.getName().equals("VisibilityAnnotationTag")) {
            emit(getVisibilityAnnotationAttr((VisibilityAnnotationTag) t));
        }
        if (t.getName().equals("VisibilityParameterAnnotationTag")) {
            emit(getVisibilityParameterAnnotationAttr((VisibilityParameterAnnotationTag) t));
        }
    }
    if (method.isConcrete()) {
        if (!method.hasActiveBody())
            throw new RuntimeException("method: " + method.getName() + " has no active body!");
        else
            emitMethodBody(method);
    }
    // Emit epilogue
    emit(".end method");
    Iterator<Tag> it = method.getTags().iterator();
    while (it.hasNext()) {
        Tag tag = (Tag) it.next();
        if (tag instanceof Attribute)
            emit(".method_attribute " + tag.getName() + " \"" + new String(Base64.encode(tag.getValue())) + "\"");
    }
}
Also used : VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) Attribute(soot.tagkit.Attribute) InnerClassAttribute(soot.tagkit.InnerClassAttribute) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) SignatureTag(soot.tagkit.SignatureTag) Tag(soot.tagkit.Tag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) SignatureTag(soot.tagkit.SignatureTag) AnnotationTag(soot.tagkit.AnnotationTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SourceFileTag(soot.tagkit.SourceFileTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag)

Example 8 with SignatureTag

use of soot.tagkit.SignatureTag in project robovm by robovm.

the class ObjCMemberPlugin method createGenericSignatureForMsgSend.

private void createGenericSignatureForMsgSend(SootMethod annotatedMethod, SootMethod m, List<Type> paramTypes, boolean extensions) {
    SignatureTag tag = (SignatureTag) annotatedMethod.getTag(SignatureTag.class.getSimpleName());
    if (tag != null) {
        SootMethodType type = new SootMethodType(annotatedMethod);
        List<String> genericParameterTypes = new ArrayList<>();
        for (org.robovm.compiler.util.generic.Type t : type.getGenericParameterTypes()) {
            genericParameterTypes.add(t.toGenericSignature());
        }
        if (!extensions) {
            /*
                 * For non extensions the generic signature is missing the
                 * receiver as parameter 1.
                 */
            genericParameterTypes.add(0, Types.getDescriptor(paramTypes.get(0)));
        }
        /*
             * Always insert the selector as parameter 2 of the generic
             * signature.
             */
        genericParameterTypes.add(1, Types.getDescriptor(paramTypes.get(1)));
        StringBuilder sb = new StringBuilder("(");
        sb.append(StringUtils.join(genericParameterTypes, ""));
        sb.append(")");
        sb.append(type.getGenericReturnType().toGenericSignature());
        m.addTag(new SignatureTag(sb.toString()));
    }
}
Also used : SootMethodType(org.robovm.compiler.util.generic.SootMethodType) ArrayList(java.util.ArrayList) SignatureTag(soot.tagkit.SignatureTag)

Example 9 with SignatureTag

use of soot.tagkit.SignatureTag in project robovm by robovm.

the class ObjCBlockPluginTest method signatureToType.

private Type signatureToType(String desc) {
    String rawDesc = desc.replaceAll("<.*>", "");
    String internalName = rawDesc.replaceAll("^\\[*", "");
    int dims = rawDesc.length() - internalName.length();
    internalName = Types.getInternalNameFromDescriptor(internalName);
    soot.Type sootType = SootResolver.v().makeClassRef(internalName.replace('/', '.')).getType();
    for (int i = 0; i < dims; i++) {
        sootType = sootType.makeArrayType();
    }
    SootMethod m = new SootMethod("foo", Arrays.asList(sootType), VoidType.v());
    m.addTag(new SignatureTag("(" + desc + ")V"));
    SootMethodType mType = new SootMethodType(m);
    return mType.getGenericParameterTypes()[0];
}
Also used : SootMethodType(org.robovm.compiler.util.generic.SootMethodType) SootMethod(soot.SootMethod) SignatureTag(soot.tagkit.SignatureTag)

Example 10 with SignatureTag

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

the class AbstractASMBackend method generateFields.

/**
 * Emits the bytecode for all fields of the class
 */
protected void generateFields() {
    for (SootField f : sc.getFields()) {
        if (f.isPhantom())
            continue;
        String name = f.getName();
        String desc = toTypeDesc(f.getType());
        String sig = null;
        if (f.hasTag("SignatureTag")) {
            SignatureTag genericSignature = (SignatureTag) f.getTag("SignatureTag");
            sig = genericSignature.getSignature();
        }
        Object value = getDefaultValue(f);
        int access = getModifiers(f.getModifiers(), f);
        FieldVisitor fv = cv.visitField(access, name, desc, sig, value);
        if (fv != null) {
            generateAnnotations(fv, f);
            generateAttributes(fv, f);
            fv.visitEnd();
        }
    }
}
Also used : SignatureTag(soot.tagkit.SignatureTag) FieldVisitor(org.objectweb.asm.FieldVisitor)

Aggregations

SignatureTag (soot.tagkit.SignatureTag)13 ArrayList (java.util.ArrayList)7 EnclosingMethodTag (soot.tagkit.EnclosingMethodTag)7 InnerClassTag (soot.tagkit.InnerClassTag)7 AnnotationDefaultTag (soot.tagkit.AnnotationDefaultTag)6 AnnotationTag (soot.tagkit.AnnotationTag)5 SourceFileTag (soot.tagkit.SourceFileTag)5 Tag (soot.tagkit.Tag)5 SootClass (soot.SootClass)4 SootMethod (soot.SootMethod)4 VisibilityAnnotationTag (soot.tagkit.VisibilityAnnotationTag)4 VisibilityParameterAnnotationTag (soot.tagkit.VisibilityParameterAnnotationTag)4 RefType (soot.RefType)3 DoubleConstantValueTag (soot.tagkit.DoubleConstantValueTag)3 FloatConstantValueTag (soot.tagkit.FloatConstantValueTag)3 IntegerConstantValueTag (soot.tagkit.IntegerConstantValueTag)3 LongConstantValueTag (soot.tagkit.LongConstantValueTag)3 StringConstantValueTag (soot.tagkit.StringConstantValueTag)3 Annotation (org.jf.dexlib2.iface.Annotation)2 SootMethodType (org.robovm.compiler.util.generic.SootMethodType)2