Search in sources :

Example 1 with ConstantValueTag

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

the class DexPrinter method makeConstantItem.

private EncodedValue makeConstantItem(SootField sf, Tag t) {
    if (!(t instanceof ConstantValueTag))
        throw new RuntimeException("error: t not ConstantValueTag.");
    if (t instanceof IntegerConstantValueTag) {
        Type sft = sf.getType();
        IntegerConstantValueTag i = (IntegerConstantValueTag) t;
        if (sft instanceof BooleanType) {
            int v = i.getIntValue();
            if (v == 0) {
                return ImmutableBooleanEncodedValue.FALSE_VALUE;
            } else if (v == 1) {
                return ImmutableBooleanEncodedValue.TRUE_VALUE;
            } else {
                throw new RuntimeException("error: boolean value from int with value != 0 or 1.");
            }
        } else if (sft instanceof CharType) {
            return new ImmutableCharEncodedValue((char) i.getIntValue());
        } else if (sft instanceof ByteType) {
            return new ImmutableByteEncodedValue((byte) i.getIntValue());
        } else if (sft instanceof IntType) {
            return new ImmutableIntEncodedValue(i.getIntValue());
        } else if (sft instanceof ShortType) {
            return new ImmutableShortEncodedValue((short) i.getIntValue());
        } else {
            throw new RuntimeException("error: unexpected constant tag type: " + t + " for field " + sf);
        }
    } else if (t instanceof LongConstantValueTag) {
        LongConstantValueTag l = (LongConstantValueTag) t;
        return new ImmutableLongEncodedValue(l.getLongValue());
    } else if (t instanceof DoubleConstantValueTag) {
        DoubleConstantValueTag d = (DoubleConstantValueTag) t;
        return new ImmutableDoubleEncodedValue(d.getDoubleValue());
    } else if (t instanceof FloatConstantValueTag) {
        FloatConstantValueTag f = (FloatConstantValueTag) t;
        return new ImmutableFloatEncodedValue(f.getFloatValue());
    } else if (t instanceof StringConstantValueTag) {
        StringConstantValueTag s = (StringConstantValueTag) t;
        if (sf.getType().equals(RefType.v("java.lang.String")))
            return new ImmutableStringEncodedValue(s.getStringValue());
        else
            // Results in "Bogus static initialization"
            return null;
    } else
        throw new RuntimeException("Unexpected constant type");
}
Also used : ImmutableShortEncodedValue(org.jf.dexlib2.immutable.value.ImmutableShortEncodedValue) ImmutableCharEncodedValue(org.jf.dexlib2.immutable.value.ImmutableCharEncodedValue) ImmutableByteEncodedValue(org.jf.dexlib2.immutable.value.ImmutableByteEncodedValue) ShortType(soot.ShortType) BooleanType(soot.BooleanType) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) ByteType(soot.ByteType) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) IntType(soot.IntType) ConstantValueTag(soot.tagkit.ConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) 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) ImmutableDoubleEncodedValue(org.jf.dexlib2.immutable.value.ImmutableDoubleEncodedValue) ImmutableFloatEncodedValue(org.jf.dexlib2.immutable.value.ImmutableFloatEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) LongConstantValueTag(soot.tagkit.LongConstantValueTag) ImmutableIntEncodedValue(org.jf.dexlib2.immutable.value.ImmutableIntEncodedValue) CharType(soot.CharType) StringConstantValueTag(soot.tagkit.StringConstantValueTag) ImmutableLongEncodedValue(org.jf.dexlib2.immutable.value.ImmutableLongEncodedValue) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag)

Example 2 with ConstantValueTag

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

the class DexPrinter method addAsClassDefItem.

private void addAsClassDefItem(SootClass c) {
    // add source file tag if any
    String sourceFile = null;
    if (c.hasTag("SourceFileTag")) {
        SourceFileTag sft = (SourceFileTag) c.getTag("SourceFileTag");
        sourceFile = sft.getSourceFile();
    }
    String classType = SootToDexUtils.getDexTypeDescriptor(c.getType());
    int accessFlags = c.getModifiers();
    String superClass = c.hasSuperclass() ? SootToDexUtils.getDexTypeDescriptor(c.getSuperclass().getType()) : null;
    List<String> interfaces = null;
    if (!c.getInterfaces().isEmpty()) {
        interfaces = new ArrayList<String>();
        for (SootClass ifc : c.getInterfaces()) interfaces.add(SootToDexUtils.getDexTypeDescriptor(ifc.getType()));
    }
    List<Field> fields = null;
    if (!c.getFields().isEmpty()) {
        fields = new ArrayList<Field>();
        for (SootField f : c.getFields()) {
            // We do not want to write out phantom fields
            if (f.isPhantom())
                continue;
            // Look for a static initializer
            EncodedValue staticInit = null;
            for (Tag t : f.getTags()) {
                if (t instanceof ConstantValueTag) {
                    if (staticInit != null) {
                        LOGGER.warn("More than one constant tag for field \"{}\": \"{}\"", f, t);
                    } else {
                        staticInit = makeConstantItem(f, t);
                    }
                }
            }
            if (staticInit == null)
                staticInit = BuilderEncodedValues.defaultValueForType(SootToDexUtils.getDexTypeDescriptor(f.getType()));
            // Build field annotations
            Set<Annotation> fieldAnnotations = buildFieldAnnotations(f);
            ImmutableField field = new ImmutableField(classType, f.getName(), SootToDexUtils.getDexTypeDescriptor(f.getType()), f.getModifiers(), staticInit, fieldAnnotations);
            fields.add(field);
        }
    }
    Collection<Method> methods = toMethods(c);
    ClassDef classDef = new ImmutableClassDef(classType, accessFlags, superClass, interfaces, sourceFile, buildClassAnnotations(c), fields, methods);
    dexBuilder.internClass(classDef);
}
Also used : ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) SourceFileTag(soot.tagkit.SourceFileTag) Method(org.jf.dexlib2.iface.Method) ImmutableMethod(org.jf.dexlib2.immutable.ImmutableMethod) SootMethod(soot.SootMethod) SootClass(soot.SootClass) Annotation(org.jf.dexlib2.iface.Annotation) ImmutableAnnotation(org.jf.dexlib2.immutable.ImmutableAnnotation) ConstantValueTag(soot.tagkit.ConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) Field(org.jf.dexlib2.iface.Field) ImmutableField(org.jf.dexlib2.immutable.ImmutableField) SootField(soot.SootField) ImmutableNullEncodedValue(org.jf.dexlib2.immutable.value.ImmutableNullEncodedValue) ImmutableFieldEncodedValue(org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue) ImmutableByteEncodedValue(org.jf.dexlib2.immutable.value.ImmutableByteEncodedValue) ImmutableStringEncodedValue(org.jf.dexlib2.immutable.value.ImmutableStringEncodedValue) ImmutableEncodedValue(org.jf.dexlib2.immutable.value.ImmutableEncodedValue) ImmutableAnnotationEncodedValue(org.jf.dexlib2.immutable.value.ImmutableAnnotationEncodedValue) ImmutableCharEncodedValue(org.jf.dexlib2.immutable.value.ImmutableCharEncodedValue) EncodedValue(org.jf.dexlib2.iface.value.EncodedValue) ImmutableFloatEncodedValue(org.jf.dexlib2.immutable.value.ImmutableFloatEncodedValue) ImmutableLongEncodedValue(org.jf.dexlib2.immutable.value.ImmutableLongEncodedValue) ImmutableShortEncodedValue(org.jf.dexlib2.immutable.value.ImmutableShortEncodedValue) ImmutableTypeEncodedValue(org.jf.dexlib2.immutable.value.ImmutableTypeEncodedValue) ImmutableDoubleEncodedValue(org.jf.dexlib2.immutable.value.ImmutableDoubleEncodedValue) ImmutableMethodEncodedValue(org.jf.dexlib2.immutable.value.ImmutableMethodEncodedValue) ImmutableArrayEncodedValue(org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue) ImmutableEnumEncodedValue(org.jf.dexlib2.immutable.value.ImmutableEnumEncodedValue) ImmutableIntEncodedValue(org.jf.dexlib2.immutable.value.ImmutableIntEncodedValue) ImmutableBooleanEncodedValue(org.jf.dexlib2.immutable.value.ImmutableBooleanEncodedValue) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) ClassDef(org.jf.dexlib2.iface.ClassDef) ImmutableField(org.jf.dexlib2.immutable.ImmutableField) SootField(soot.SootField) ConstantValueTag(soot.tagkit.ConstantValueTag) SourceFileTag(soot.tagkit.SourceFileTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) VisibilityAnnotationTag(soot.tagkit.VisibilityAnnotationTag) InnerClassTag(soot.tagkit.InnerClassTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) AnnotationTag(soot.tagkit.AnnotationTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) VisibilityParameterAnnotationTag(soot.tagkit.VisibilityParameterAnnotationTag) Tag(soot.tagkit.Tag) LineNumberTag(soot.tagkit.LineNumberTag) ParamNamesTag(soot.tagkit.ParamNamesTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) SignatureTag(soot.tagkit.SignatureTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag)

Example 3 with ConstantValueTag

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

the class ConstantInitializerToTagTransformer method transformClass.

/**
 * Transforms the given class, i.e. scans for a <clinit> method and
 * generates new constant value tags for all constant assignments to static
 * final fields.
 *
 * @param sc
 *            The class to transform
 * @param removeAssignments
 *            True if the assignments inside the <clinit> method shall be
 *            removed, otherwise false
 */
public void transformClass(SootClass sc, boolean removeAssignments) {
    // If this class has no <clinit> method, we're done
    SootMethod smInit = sc.getMethodByNameUnsafe("<clinit>");
    if (smInit == null || !smInit.isConcrete())
        return;
    Set<SootField> nonConstantFields = new HashSet<SootField>();
    Map<SootField, ConstantValueTag> newTags = new HashMap<SootField, ConstantValueTag>();
    // in case of
    Set<SootField> removeTagList = new HashSet<SootField>();
    for (Iterator<Unit> itU = smInit.getActiveBody().getUnits().snapshotIterator(); itU.hasNext(); ) {
        Unit u = itU.next();
        if (u instanceof AssignStmt) {
            AssignStmt assign = (AssignStmt) u;
            if (assign.getLeftOp() instanceof StaticFieldRef && assign.getRightOp() instanceof Constant) {
                SootField field = null;
                try {
                    field = ((StaticFieldRef) assign.getLeftOp()).getField();
                    if (field == null || nonConstantFields.contains(field))
                        continue;
                } catch (ConflictingFieldRefException ex) {
                    // Ignore this statement
                    continue;
                }
                if (field.getDeclaringClass().equals(sc) && field.isStatic() && field.isFinal()) {
                    // Do we already have a constant value for this field?
                    boolean found = false;
                    for (Tag t : field.getTags()) {
                        if (t instanceof ConstantValueTag) {
                            if (checkConstantValue((ConstantValueTag) t, (Constant) assign.getRightOp())) {
                                // the assignment.
                                if (removeAssignments)
                                    itU.remove();
                            } else {
                                logger.debug("" + "WARNING: Constant value for field '" + field + "' mismatch between code (" + assign.getRightOp() + ") and constant table (" + t + ")");
                                removeTagList.add(field);
                            }
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        // tags.
                        if (!checkConstantValue(newTags.get(field), (Constant) assign.getRightOp())) {
                            nonConstantFields.add(field);
                            newTags.remove(field);
                            removeTagList.add(field);
                            continue;
                        }
                        ConstantValueTag newTag = createConstantTagFromValue((Constant) assign.getRightOp());
                        if (newTag != null)
                            newTags.put(field, newTag);
                    }
                }
            } else if (assign.getLeftOp() instanceof StaticFieldRef) {
                // a non-constant is assigned to the field
                try {
                    SootField sf = ((StaticFieldRef) assign.getLeftOp()).getField();
                    if (sf != null)
                        removeTagList.add(sf);
                } catch (ConflictingFieldRefException ex) {
                // let's assume that a broken field doesn't cause any
                // harm
                }
            }
        }
    }
    // Do the actual assignment
    for (Entry<SootField, ConstantValueTag> entry : newTags.entrySet()) {
        SootField field = entry.getKey();
        if (removeTagList.contains(field))
            continue;
        field.addTag(entry.getValue());
    }
    if (removeAssignments && !newTags.isEmpty())
        for (Iterator<Unit> itU = smInit.getActiveBody().getUnits().snapshotIterator(); itU.hasNext(); ) {
            Unit u = itU.next();
            if (u instanceof AssignStmt) {
                AssignStmt assign = (AssignStmt) u;
                if (assign.getLeftOp() instanceof FieldRef)
                    try {
                        SootField fld = ((FieldRef) assign.getLeftOp()).getField();
                        if (fld != null && newTags.containsKey(fld))
                            itU.remove();
                    } catch (ConflictingFieldRefException ex) {
                    // Ignore broken code
                    }
            }
        }
    // remove constant tags
    for (SootField sf : removeTagList) {
        if (removeTagList.contains(sf)) {
            List<Tag> toRemoveTagList = new ArrayList<Tag>();
            for (Tag t : sf.getTags()) {
                if (t instanceof ConstantValueTag) {
                    toRemoveTagList.add(t);
                }
            }
            for (Tag t : toRemoveTagList) {
                sf.getTags().remove(t);
            }
        }
    }
}
Also used : FieldRef(soot.jimple.FieldRef) StaticFieldRef(soot.jimple.StaticFieldRef) HashMap(java.util.HashMap) AssignStmt(soot.jimple.AssignStmt) Constant(soot.jimple.Constant) LongConstant(soot.jimple.LongConstant) DoubleConstant(soot.jimple.DoubleConstant) IntConstant(soot.jimple.IntConstant) StringConstant(soot.jimple.StringConstant) FloatConstant(soot.jimple.FloatConstant) ArrayList(java.util.ArrayList) Unit(soot.Unit) ConflictingFieldRefException(soot.ConflictingFieldRefException) StaticFieldRef(soot.jimple.StaticFieldRef) ConstantValueTag(soot.tagkit.ConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) Iterator(java.util.Iterator) SootMethod(soot.SootMethod) SootField(soot.SootField) Tag(soot.tagkit.Tag) ConstantValueTag(soot.tagkit.ConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) HashSet(java.util.HashSet)

Example 4 with ConstantValueTag

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

the class Util method resolveFromClassFile.

public void resolveFromClassFile(SootClass aClass, InputStream is, String filePath, Collection<Type> references) {
    SootClass bclass = aClass;
    String className = bclass.getName();
    ClassFile coffiClass = new ClassFile(className);
    // Load up class file, and retrieve bclass from class manager.
    {
        boolean success = coffiClass.loadClassFile(is);
        if (!success) {
            if (!Scene.v().allowsPhantomRefs())
                throw new RuntimeException("Could not load classfile: " + bclass.getName());
            else {
                logger.warn("" + className + " is a phantom class!");
                bclass.setPhantomClass();
                return;
            }
        }
        CONSTANT_Class_info c = (CONSTANT_Class_info) coffiClass.constant_pool[coffiClass.this_class];
        String name = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[c.name_index])).convert();
        name = name.replace('/', '.');
        if (!name.equals(bclass.getName())) {
            throw new RuntimeException("Error: class " + name + " read in from a classfile in which " + bclass.getName() + " was expected.");
        }
    }
    // Set modifier
    bclass.setModifiers(coffiClass.access_flags & (~0x0020));
    // don't want the ACC_SUPER flag, it is always supposed to be set
    // anyways
    // Set superclass
    {
        if (coffiClass.super_class != 0) {
            // This object is not java.lang.Object, so must have a super
            // class
            CONSTANT_Class_info c = (CONSTANT_Class_info) coffiClass.constant_pool[coffiClass.super_class];
            String superName = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[c.name_index])).convert();
            superName = superName.replace('/', '.');
            references.add(RefType.v(superName));
            bclass.setSuperclass(SootResolver.v().makeClassRef(superName));
        }
    }
    // Add interfaces to the bclass
    {
        for (int i = 0; i < coffiClass.interfaces_count; i++) {
            CONSTANT_Class_info c = (CONSTANT_Class_info) coffiClass.constant_pool[coffiClass.interfaces[i]];
            String interfaceName = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[c.name_index])).convert();
            interfaceName = interfaceName.replace('/', '.');
            references.add(RefType.v(interfaceName));
            SootClass interfaceClass = SootResolver.v().makeClassRef(interfaceName);
            interfaceClass.setModifiers(interfaceClass.getModifiers() | Modifier.INTERFACE);
            bclass.addInterface(interfaceClass);
        }
    }
    // Add every field to the bclass
    for (int i = 0; i < coffiClass.fields_count; i++) {
        field_info fieldInfo = coffiClass.fields[i];
        String fieldName = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[fieldInfo.name_index])).convert();
        String fieldDescriptor = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[fieldInfo.descriptor_index])).convert();
        int modifiers = fieldInfo.access_flags;
        Type fieldType = jimpleTypeOfFieldDescriptor(fieldDescriptor);
        SootField field = Scene.v().makeSootField(fieldName, fieldType, modifiers);
        bclass.addField(field);
        references.add(fieldType);
        // add initialization constant, if any
        for (int j = 0; j < fieldInfo.attributes_count; j++) {
            // add constant value attributes
            if (fieldInfo.attributes[j] instanceof ConstantValue_attribute) {
                ConstantValue_attribute attr = (ConstantValue_attribute) fieldInfo.attributes[j];
                cp_info cval = coffiClass.constant_pool[attr.constantvalue_index];
                ConstantValueTag tag;
                switch(cval.tag) {
                    case cp_info.CONSTANT_Integer:
                        tag = new IntegerConstantValueTag((int) ((CONSTANT_Integer_info) cval).bytes);
                        break;
                    case cp_info.CONSTANT_Float:
                        // tag = new
                        // FloatConstantValueTag((int)((CONSTANT_Float_info)cval).bytes);
                        tag = new FloatConstantValueTag(((CONSTANT_Float_info) cval).convert());
                        break;
                    case cp_info.CONSTANT_Long:
                        {
                            CONSTANT_Long_info lcval = (CONSTANT_Long_info) cval;
                            tag = new LongConstantValueTag((lcval.high << 32) + lcval.low);
                            break;
                        }
                    case cp_info.CONSTANT_Double:
                        {
                            CONSTANT_Double_info dcval = (CONSTANT_Double_info) cval;
                            // tag = new DoubleConstantValueTag((dcval.high << 32) +
                            // dcval.low);
                            tag = new DoubleConstantValueTag(dcval.convert());
                            break;
                        }
                    case cp_info.CONSTANT_String:
                        {
                            CONSTANT_String_info scval = (CONSTANT_String_info) cval;
                            CONSTANT_Utf8_info ucval = (CONSTANT_Utf8_info) coffiClass.constant_pool[scval.string_index];
                            tag = new StringConstantValueTag(ucval.convert());
                            break;
                        }
                    default:
                        throw new RuntimeException("unexpected ConstantValue: " + cval);
                }
                field.addTag(tag);
            } else // add synthetic tag
            if (fieldInfo.attributes[j] instanceof Synthetic_attribute) {
                field.addTag(new SyntheticTag());
            } else // add deprecated tag
            if (fieldInfo.attributes[j] instanceof Deprecated_attribute) {
                field.addTag(new DeprecatedTag());
            } else // add signature tag
            if (fieldInfo.attributes[j] instanceof Signature_attribute) {
                String generic_sig = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[((Signature_attribute) fieldInfo.attributes[j]).signature_index])).convert();
                field.addTag(new SignatureTag(generic_sig));
            } else if (fieldInfo.attributes[j] instanceof RuntimeVisibleAnnotations_attribute || fieldInfo.attributes[j] instanceof RuntimeInvisibleAnnotations_attribute) {
                addAnnotationVisibilityAttribute(field, fieldInfo.attributes[j], coffiClass, references);
            } else if (fieldInfo.attributes[j] instanceof Generic_attribute) {
                Generic_attribute attr = (Generic_attribute) fieldInfo.attributes[j];
                String name = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[attr.attribute_name])).convert();
                field.addTag(new GenericAttribute(name, attr.info));
            }
        }
    }
    // Add every method to the bclass
    for (int i = 0; i < coffiClass.methods_count; i++) {
        method_info methodInfo = coffiClass.methods[i];
        if ((coffiClass.constant_pool[methodInfo.name_index]) == null) {
            logger.debug("method index: " + methodInfo.toName(coffiClass.constant_pool));
            throw new RuntimeException("method has no name");
        }
        String methodName = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[methodInfo.name_index])).convert();
        String methodDescriptor = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[methodInfo.descriptor_index])).convert();
        List<Type> parameterTypes;
        Type returnType;
        // Generate parameterTypes & returnType
        {
            Type[] types = jimpleTypesOfFieldOrMethodDescriptor(methodDescriptor);
            parameterTypes = new ArrayList<Type>();
            for (int j = 0; j < types.length - 1; j++) {
                references.add(types[j]);
                parameterTypes.add(types[j]);
            }
            returnType = types[types.length - 1];
            references.add(returnType);
        }
        int modifiers = methodInfo.access_flags;
        SootMethod method;
        method = Scene.v().makeSootMethod(methodName, parameterTypes, returnType, modifiers);
        bclass.addMethod(method);
        methodInfo.jmethod = method;
        // add exceptions to method
        {
            for (int j = 0; j < methodInfo.attributes_count; j++) {
                if (methodInfo.attributes[j] instanceof Exception_attribute) {
                    Exception_attribute exceptions = (Exception_attribute) methodInfo.attributes[j];
                    for (int k = 0; k < exceptions.number_of_exceptions; k++) {
                        CONSTANT_Class_info c = (CONSTANT_Class_info) coffiClass.constant_pool[exceptions.exception_index_table[k]];
                        String exceptionName = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[c.name_index])).convert();
                        exceptionName = exceptionName.replace('/', '.');
                        references.add(RefType.v(exceptionName));
                        method.addExceptionIfAbsent(SootResolver.v().makeClassRef(exceptionName));
                    }
                } else if (methodInfo.attributes[j] instanceof Synthetic_attribute) {
                    method.addTag(new SyntheticTag());
                } else if (methodInfo.attributes[j] instanceof Deprecated_attribute) {
                    method.addTag(new DeprecatedTag());
                } else if (methodInfo.attributes[j] instanceof Signature_attribute) {
                    String generic_sig = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[((Signature_attribute) methodInfo.attributes[j]).signature_index])).convert();
                    method.addTag(new SignatureTag(generic_sig));
                } else if (methodInfo.attributes[j] instanceof RuntimeVisibleAnnotations_attribute || methodInfo.attributes[j] instanceof RuntimeInvisibleAnnotations_attribute) {
                    addAnnotationVisibilityAttribute(method, methodInfo.attributes[j], coffiClass, references);
                } else if (methodInfo.attributes[j] instanceof RuntimeVisibleParameterAnnotations_attribute || methodInfo.attributes[j] instanceof RuntimeInvisibleParameterAnnotations_attribute) {
                    addAnnotationVisibilityParameterAttribute(method, methodInfo.attributes[j], coffiClass, references);
                } else if (methodInfo.attributes[j] instanceof AnnotationDefault_attribute) {
                    AnnotationDefault_attribute attr = (AnnotationDefault_attribute) methodInfo.attributes[j];
                    element_value[] input = new element_value[1];
                    input[0] = attr.default_value;
                    ArrayList<AnnotationElem> list = createElementTags(1, coffiClass, input);
                    method.addTag(new AnnotationDefaultTag(list.get(0)));
                } else if (methodInfo.attributes[j] instanceof Generic_attribute) {
                    Generic_attribute attr = (Generic_attribute) methodInfo.attributes[j];
                    String name = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[attr.attribute_name])).convert();
                    method.addTag(new GenericAttribute(name, attr.info));
                }
            }
        }
        // Go through the constant pool, forcing all mentioned classes to be
        // resolved.
        {
            for (int k = 0; k < coffiClass.constant_pool_count; k++) {
                if (coffiClass.constant_pool[k] instanceof CONSTANT_Class_info) {
                    CONSTANT_Class_info c = (CONSTANT_Class_info) coffiClass.constant_pool[k];
                    String desc = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[c.name_index])).convert();
                    String name = desc.replace('/', '.');
                    if (name.startsWith("["))
                        references.add(jimpleTypeOfFieldDescriptor(desc));
                    else
                        references.add(RefType.v(name));
                }
                if (coffiClass.constant_pool[k] instanceof CONSTANT_Fieldref_info || coffiClass.constant_pool[k] instanceof CONSTANT_Methodref_info || coffiClass.constant_pool[k] instanceof CONSTANT_InterfaceMethodref_info) {
                    Type[] types = jimpleTypesOfFieldOrMethodDescriptor(cp_info.getTypeDescr(coffiClass.constant_pool, k));
                    for (Type element : types) {
                        references.add(element);
                    }
                }
            }
        }
    }
    // Set coffi source of method
    for (int i = 0; i < coffiClass.methods_count; i++) {
        method_info methodInfo = coffiClass.methods[i];
        // methodInfo.jmethod.setSource(coffiClass, methodInfo);
        methodInfo.jmethod.setSource(new CoffiMethodSource(coffiClass, methodInfo));
    }
    // Set "SourceFile" attribute tag
    for (int i = 0; i < coffiClass.attributes_count; i++) {
        if (coffiClass.attributes[i] instanceof SourceFile_attribute) {
            SourceFile_attribute attr = (SourceFile_attribute) coffiClass.attributes[i];
            String sourceFile = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[attr.sourcefile_index])).convert();
            if (sourceFile.indexOf(' ') >= 0) {
                logger.debug("" + "Warning: Class " + className + " has invalid SourceFile attribute (will be ignored).");
            } else {
                bclass.addTag(new SourceFileTag(sourceFile, filePath));
            }
        } else // Set "InnerClass" attribute tag
        if (coffiClass.attributes[i] instanceof InnerClasses_attribute) {
            InnerClasses_attribute attr = (InnerClasses_attribute) coffiClass.attributes[i];
            for (int j = 0; j < attr.inner_classes_length; j++) {
                inner_class_entry e = attr.inner_classes[j];
                String inner = null;
                String outer = null;
                String name = null;
                if (e.inner_class_index != 0)
                    inner = ((CONSTANT_Utf8_info) coffiClass.constant_pool[((CONSTANT_Class_info) coffiClass.constant_pool[e.inner_class_index]).name_index]).convert();
                if (e.outer_class_index != 0)
                    outer = ((CONSTANT_Utf8_info) coffiClass.constant_pool[((CONSTANT_Class_info) coffiClass.constant_pool[e.outer_class_index]).name_index]).convert();
                if (e.name_index != 0)
                    name = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[e.name_index])).convert();
                bclass.addTag(new InnerClassTag(inner, outer, name, e.access_flags));
            }
        } else // set synthetic tags
        if (coffiClass.attributes[i] instanceof Synthetic_attribute) {
            bclass.addTag(new SyntheticTag());
        } else // set deprectaed tags
        if (coffiClass.attributes[i] instanceof Deprecated_attribute) {
            bclass.addTag(new DeprecatedTag());
        } else if (coffiClass.attributes[i] instanceof Signature_attribute) {
            String generic_sig = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[((Signature_attribute) coffiClass.attributes[i]).signature_index])).convert();
            bclass.addTag(new SignatureTag(generic_sig));
        } else if (coffiClass.attributes[i] instanceof EnclosingMethod_attribute) {
            EnclosingMethod_attribute attr = (EnclosingMethod_attribute) coffiClass.attributes[i];
            String class_name = ((CONSTANT_Utf8_info) coffiClass.constant_pool[((CONSTANT_Class_info) coffiClass.constant_pool[attr.class_index]).name_index]).convert();
            CONSTANT_NameAndType_info info = (CONSTANT_NameAndType_info) coffiClass.constant_pool[attr.method_index];
            String method_name = "";
            String method_sig = "";
            if (info != null) {
                method_name = ((CONSTANT_Utf8_info) coffiClass.constant_pool[info.name_index]).convert();
                method_sig = ((CONSTANT_Utf8_info) coffiClass.constant_pool[info.descriptor_index]).convert();
            }
            bclass.addTag(new EnclosingMethodTag(class_name, method_name, method_sig));
        } else if (coffiClass.attributes[i] instanceof RuntimeVisibleAnnotations_attribute || coffiClass.attributes[i] instanceof RuntimeInvisibleAnnotations_attribute) {
            addAnnotationVisibilityAttribute(bclass, coffiClass.attributes[i], coffiClass, references);
        } else if (coffiClass.attributes[i] instanceof Generic_attribute) {
            Generic_attribute attr = (Generic_attribute) coffiClass.attributes[i];
            String name = ((CONSTANT_Utf8_info) (coffiClass.constant_pool[attr.attribute_name])).convert();
            bclass.addTag(new GenericAttribute(name, attr.info));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SourceFileTag(soot.tagkit.SourceFileTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) GenericAttribute(soot.tagkit.GenericAttribute) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) ConstantValueTag(soot.tagkit.ConstantValueTag) DoubleConstantValueTag(soot.tagkit.DoubleConstantValueTag) LongConstantValueTag(soot.tagkit.LongConstantValueTag) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) SootMethod(soot.SootMethod) SootField(soot.SootField) FloatConstantValueTag(soot.tagkit.FloatConstantValueTag) SyntheticTag(soot.tagkit.SyntheticTag) InnerClassTag(soot.tagkit.InnerClassTag) StringConstantValueTag(soot.tagkit.StringConstantValueTag) DeprecatedTag(soot.tagkit.DeprecatedTag) AnnotationDefaultTag(soot.tagkit.AnnotationDefaultTag) IntegerConstantValueTag(soot.tagkit.IntegerConstantValueTag) SootClass(soot.SootClass) RefType(soot.RefType) ShortType(soot.ShortType) BooleanType(soot.BooleanType) ByteType(soot.ByteType) Type(soot.Type) UnknownType(soot.UnknownType) DoubleType(soot.DoubleType) FloatType(soot.FloatType) IntType(soot.IntType) CharType(soot.CharType) LongType(soot.LongType) ArrayType(soot.ArrayType) VoidType(soot.VoidType) EnclosingMethodTag(soot.tagkit.EnclosingMethodTag) SignatureTag(soot.tagkit.SignatureTag)

Aggregations

ConstantValueTag (soot.tagkit.ConstantValueTag)4 DoubleConstantValueTag (soot.tagkit.DoubleConstantValueTag)4 FloatConstantValueTag (soot.tagkit.FloatConstantValueTag)4 IntegerConstantValueTag (soot.tagkit.IntegerConstantValueTag)4 LongConstantValueTag (soot.tagkit.LongConstantValueTag)4 StringConstantValueTag (soot.tagkit.StringConstantValueTag)4 SootField (soot.SootField)3 SootMethod (soot.SootMethod)3 ArrayList (java.util.ArrayList)2 ImmutableByteEncodedValue (org.jf.dexlib2.immutable.value.ImmutableByteEncodedValue)2 ImmutableCharEncodedValue (org.jf.dexlib2.immutable.value.ImmutableCharEncodedValue)2 ImmutableDoubleEncodedValue (org.jf.dexlib2.immutable.value.ImmutableDoubleEncodedValue)2 ImmutableFloatEncodedValue (org.jf.dexlib2.immutable.value.ImmutableFloatEncodedValue)2 ImmutableIntEncodedValue (org.jf.dexlib2.immutable.value.ImmutableIntEncodedValue)2 ImmutableLongEncodedValue (org.jf.dexlib2.immutable.value.ImmutableLongEncodedValue)2 SootClass (soot.SootClass)2 AnnotationDefaultTag (soot.tagkit.AnnotationDefaultTag)2 EnclosingMethodTag (soot.tagkit.EnclosingMethodTag)2 InnerClassTag (soot.tagkit.InnerClassTag)2 SignatureTag (soot.tagkit.SignatureTag)2