Search in sources :

Example 16 with Field

use of org.jf.dexlib2.iface.Field in project smali by JesusFreke.

the class ClassDefinition method writeStaticFields.

private Set<String> writeStaticFields(IndentingWriter writer) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();
    Iterable<? extends Field> staticFields;
    if (classDef instanceof DexBackedClassDef) {
        staticFields = ((DexBackedClassDef) classDef).getStaticFields(false);
    } else {
        staticFields = classDef.getStaticFields();
    }
    for (Field field : staticFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# static fields");
            wroteHeader = true;
        }
        writer.write('\n');
        boolean setInStaticConstructor;
        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
            setInStaticConstructor = false;
        } else {
            setInStaticConstructor = fieldsSetInStaticConstructor.contains(fieldString);
        }
        FieldDefinition.writeTo(options, fieldWriter, field, setInStaticConstructor);
    }
    return writtenFields;
}
Also used : IndentingWriter(org.jf.util.IndentingWriter) DexBackedClassDef(org.jf.dexlib2.dexbacked.DexBackedClassDef)

Example 17 with Field

use of org.jf.dexlib2.iface.Field in project smali by JesusFreke.

the class ImplicitReferenceTest method testImplicitFieldLiteral.

@Test
public void testImplicitFieldLiteral() throws RecognitionException, IOException {
    ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".field public static field1:Ljava/lang/reflect/Field; = someField:I\n" + ".field public static field2:Ljava/lang/reflect/Field; = V:I\n" + ".field public static field3:Ljava/lang/reflect/Field; = I:I\n");
    Map<String, Field> fields = Maps.newHashMap();
    for (Field field : classDef.getFields()) {
        fields.put(field.getName(), field);
    }
    Field field = fields.get("field1");
    Assert.assertNotNull(field);
    Assert.assertNotNull(field.getInitialValue());
    Assert.assertEquals(ValueType.FIELD, field.getInitialValue().getValueType());
    FieldEncodedValue fieldEncodedValue = (FieldEncodedValue) field.getInitialValue();
    Assert.assertEquals(classDef.getType(), fieldEncodedValue.getValue().getDefiningClass());
    Assert.assertEquals("someField", fieldEncodedValue.getValue().getName());
    field = fields.get("field2");
    Assert.assertNotNull(field);
    Assert.assertNotNull(field.getInitialValue());
    Assert.assertEquals(ValueType.FIELD, field.getInitialValue().getValueType());
    fieldEncodedValue = (FieldEncodedValue) field.getInitialValue();
    Assert.assertEquals(classDef.getType(), fieldEncodedValue.getValue().getDefiningClass());
    Assert.assertEquals("V", fieldEncodedValue.getValue().getName());
    field = fields.get("field3");
    Assert.assertNotNull(field);
    Assert.assertNotNull(field.getInitialValue());
    Assert.assertEquals(ValueType.FIELD, field.getInitialValue().getValueType());
    fieldEncodedValue = (FieldEncodedValue) field.getInitialValue();
    Assert.assertEquals(classDef.getType(), fieldEncodedValue.getValue().getDefiningClass());
    Assert.assertEquals("I", fieldEncodedValue.getValue().getName());
}
Also used : Field(org.jf.dexlib2.iface.Field) ClassDef(org.jf.dexlib2.iface.ClassDef) FieldEncodedValue(org.jf.dexlib2.iface.value.FieldEncodedValue) Test(org.junit.Test)

Aggregations

Field (org.jf.dexlib2.iface.Field)6 ClassDef (org.jf.dexlib2.iface.ClassDef)5 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)4 FieldReference (org.jf.dexlib2.iface.reference.FieldReference)4 IndentingWriter (org.jf.util.IndentingWriter)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 EncodedValue (org.jf.dexlib2.iface.value.EncodedValue)3 ExceptionWithContext (org.jf.util.ExceptionWithContext)3 Opcode (org.jf.dexlib2.Opcode)2 Method (org.jf.dexlib2.iface.Method)2 ImmutableFieldReference (org.jf.dexlib2.immutable.reference.ImmutableFieldReference)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 ClassProto (org.jf.dexlib2.analysis.ClassProto)1 DexReader (org.jf.dexlib2.dexbacked.DexReader)1 DexBackedInstruction (org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction)1