Search in sources :

Example 1 with ImmutableField

use of org.jf.dexlib2.immutable.ImmutableField in project atlas by alibaba.

the class PatchFieldTool method addField.

public static void addField(String inFile, String outFile, DexBackedClassDef dexBackedClassDef, ImmutableField immutableField) throws IOException {
    DexFile dexFile = readDexFile(inFile);
    for (ClassDef classDef : dexFile.getClasses()) {
        if (dexBackedClassDef != null && dexBackedClassDef.getType().equals(classDef.getType())) {
            dexFile = addField(dexFile, classDef.getType(), immutableField);
        } else if (dexBackedClassDef == null) {
            dexFile = addField(dexFile, classDef.getType(), immutableField);
        }
    }
    reDexFile(dexFile);
    DexFileFactory.writeDexFile(outFile, new DexFile() {

        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<ClassDef>() {

                @Nonnull
                @Override
                public Iterator<ClassDef> iterator() {
                    return classes.iterator();
                }

                @Override
                public int size() {
                    return classes.size();
                }
            };
        }
    });
}
Also used : DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef) ImmutableClassDef(org.jf.dexlib2.immutable.ImmutableClassDef) Nonnull(javax.annotation.Nonnull)

Example 2 with ImmutableField

use of org.jf.dexlib2.immutable.ImmutableField in project smali by JesusFreke.

the class RollbackTest method testRollback.

@Test
public void testRollback() throws IOException {
    ClassDef class1 = new ImmutableClassDef("Lcls1;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls1;", "field1", "I", AccessFlags.PUBLIC.getValue(), null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls1", "method1", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("L", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null)));
    ClassDef class2 = new ImmutableClassDef("Lcls2;", AccessFlags.PUBLIC.getValue(), "Ljava/lang/Object;", null, null, Lists.newArrayList(new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lannotation2;", null)), Lists.<Field>newArrayList(new ImmutableField("Lcls2;", "field2", "D", AccessFlags.PUBLIC.getValue(), null, null)), Lists.<Method>newArrayList(new ImmutableMethod("Lcls2;", "method2", Lists.<MethodParameter>newArrayList(new ImmutableMethodParameter("D", null, null)), "V", AccessFlags.PUBLIC.getValue(), null, null)));
    RawDexFile dexFile1;
    {
        MemoryDataStore dataStore = new MemoryDataStore();
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        dexPool.internClass(class1);
        dexPool.mark();
        dexPool.internClass(class2);
        dexPool.reset();
        dexPool.writeTo(dataStore);
        dexFile1 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
    }
    RawDexFile dexFile2;
    {
        MemoryDataStore dataStore = new MemoryDataStore();
        DexPool dexPool = new DexPool(Opcodes.getDefault());
        dexPool.internClass(class1);
        dexPool.writeTo(dataStore);
        dexFile2 = new RawDexFile(Opcodes.getDefault(), dataStore.getData());
    }
    List<MapItem> mapItems1 = dexFile1.getMapItems();
    List<MapItem> mapItems2 = dexFile2.getMapItems();
    for (int i = 0; i < mapItems1.size(); i++) {
        Assert.assertEquals(mapItems1.get(i).getType(), mapItems2.get(i).getType());
        Assert.assertEquals(mapItems1.get(i).getItemCount(), mapItems2.get(i).getItemCount());
    }
}
Also used : DexPool(org.jf.dexlib2.writer.pool.DexPool) ClassDef(org.jf.dexlib2.iface.ClassDef) RawDexFile(org.jf.dexlib2.dexbacked.raw.RawDexFile) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) MapItem(org.jf.dexlib2.dexbacked.raw.MapItem) Test(org.junit.Test)

Example 3 with ImmutableField

use of org.jf.dexlib2.immutable.ImmutableField 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)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)2 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)2 Nonnull (javax.annotation.Nonnull)1 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)1 MapItem (org.jf.dexlib2.dexbacked.raw.MapItem)1 RawDexFile (org.jf.dexlib2.dexbacked.raw.RawDexFile)1 Annotation (org.jf.dexlib2.iface.Annotation)1 Field (org.jf.dexlib2.iface.Field)1 Method (org.jf.dexlib2.iface.Method)1 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)1 ImmutableAnnotation (org.jf.dexlib2.immutable.ImmutableAnnotation)1 ImmutableField (org.jf.dexlib2.immutable.ImmutableField)1 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)1 ImmutableAnnotationEncodedValue (org.jf.dexlib2.immutable.value.ImmutableAnnotationEncodedValue)1 ImmutableArrayEncodedValue (org.jf.dexlib2.immutable.value.ImmutableArrayEncodedValue)1 ImmutableBooleanEncodedValue (org.jf.dexlib2.immutable.value.ImmutableBooleanEncodedValue)1 ImmutableByteEncodedValue (org.jf.dexlib2.immutable.value.ImmutableByteEncodedValue)1 ImmutableCharEncodedValue (org.jf.dexlib2.immutable.value.ImmutableCharEncodedValue)1 ImmutableDoubleEncodedValue (org.jf.dexlib2.immutable.value.ImmutableDoubleEncodedValue)1 ImmutableEncodedValue (org.jf.dexlib2.immutable.value.ImmutableEncodedValue)1