Search in sources :

Example 16 with Method

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

the class ImplicitReferenceTest method testImplicitMethodLiteral.

@Test
public void testImplicitMethodLiteral() throws RecognitionException, IOException {
    ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".field public static field1:Ljava/lang/reflect/Method; = toString()V\n" + ".field public static field2:Ljava/lang/reflect/Method; = V()V\n" + ".field public static field3:Ljava/lang/reflect/Method; = I()V\n" + ".field public static field4:Ljava/lang/Class; = I");
    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.METHOD, field.getInitialValue().getValueType());
    MethodEncodedValue methodEncodedValue = (MethodEncodedValue) field.getInitialValue();
    Assert.assertEquals(classDef.getType(), methodEncodedValue.getValue().getDefiningClass());
    Assert.assertEquals("toString", methodEncodedValue.getValue().getName());
    field = fields.get("field2");
    Assert.assertNotNull(field);
    Assert.assertNotNull(field.getInitialValue());
    Assert.assertEquals(ValueType.METHOD, field.getInitialValue().getValueType());
    methodEncodedValue = (MethodEncodedValue) field.getInitialValue();
    Assert.assertEquals(classDef.getType(), methodEncodedValue.getValue().getDefiningClass());
    Assert.assertEquals("V", methodEncodedValue.getValue().getName());
    field = fields.get("field3");
    Assert.assertNotNull(field);
    Assert.assertNotNull(field.getInitialValue());
    Assert.assertEquals(ValueType.METHOD, field.getInitialValue().getValueType());
    methodEncodedValue = (MethodEncodedValue) field.getInitialValue();
    Assert.assertEquals(classDef.getType(), methodEncodedValue.getValue().getDefiningClass());
    Assert.assertEquals("I", methodEncodedValue.getValue().getName());
    field = fields.get("field4");
    Assert.assertNotNull(field);
    Assert.assertNotNull(field.getInitialValue());
    Assert.assertEquals(ValueType.TYPE, field.getInitialValue().getValueType());
    TypeEncodedValue typeEncodedValue = (TypeEncodedValue) field.getInitialValue();
    Assert.assertEquals("I", typeEncodedValue.getValue());
}
Also used : Field(org.jf.dexlib2.iface.Field) ClassDef(org.jf.dexlib2.iface.ClassDef) TypeEncodedValue(org.jf.dexlib2.iface.value.TypeEncodedValue) MethodEncodedValue(org.jf.dexlib2.iface.value.MethodEncodedValue) Test(org.junit.Test)

Example 17 with Method

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

the class ImplicitReferenceTest method testImplicitFieldReference.

@Test
public void testImplicitFieldReference() throws RecognitionException, IOException {
    ClassDef classDef = SmaliTestUtils.compileSmali("" + ".class public LHelloWorld;\n" + ".super Ljava/lang/Object;\n" + ".method public static main([Ljava/lang/String;)V\n" + "    .registers 1\n" + "    sget-object v0, someField:I\n" + "    sget-object v0, V:I\n" + "    sget-object v0, I:I\n" + "    return-void\n" + ".end method");
    Method mainMethod = null;
    for (Method method : classDef.getMethods()) {
        if (method.getName().equals("main")) {
            mainMethod = method;
        }
    }
    Assert.assertNotNull(mainMethod);
    MethodImplementation methodImpl = mainMethod.getImplementation();
    Assert.assertNotNull(methodImpl);
    List<Instruction> instructions = Lists.newArrayList(methodImpl.getInstructions());
    Instruction21c instruction = (Instruction21c) instructions.get(0);
    Assert.assertNotNull(instruction);
    Assert.assertEquals(Opcode.SGET_OBJECT, instruction.getOpcode());
    FieldReference field = (FieldReference) instruction.getReference();
    Assert.assertEquals(classDef.getType(), field.getDefiningClass());
    Assert.assertEquals("someField", field.getName());
    instruction = (Instruction21c) instructions.get(1);
    Assert.assertNotNull(instruction);
    Assert.assertEquals(Opcode.SGET_OBJECT, instruction.getOpcode());
    field = (FieldReference) instruction.getReference();
    Assert.assertEquals(classDef.getType(), field.getDefiningClass());
    Assert.assertEquals("V", field.getName());
    instruction = (Instruction21c) instructions.get(2);
    Assert.assertNotNull(instruction);
    Assert.assertEquals(Opcode.SGET_OBJECT, instruction.getOpcode());
    field = (FieldReference) instruction.getReference();
    Assert.assertEquals(classDef.getType(), field.getDefiningClass());
    Assert.assertEquals("I", field.getName());
}
Also used : MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) Instruction21c(org.jf.dexlib2.iface.instruction.formats.Instruction21c) ClassDef(org.jf.dexlib2.iface.ClassDef) FieldReference(org.jf.dexlib2.iface.reference.FieldReference) Method(org.jf.dexlib2.iface.Method) Instruction(org.jf.dexlib2.iface.instruction.Instruction) Test(org.junit.Test)

Example 18 with Method

use of org.jf.dexlib2.iface.Method 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 19 with Method

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

the class JumboStringConversionTest method testJumboStringConversion_NonMethodBuilder.

@Test
public void testJumboStringConversion_NonMethodBuilder() throws IOException {
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    final List<Instruction> instructions = Lists.newArrayList();
    for (int i = 0; i < 66000; i++) {
        final StringReference ref = dexBuilder.internStringReference(String.format("%08d", i));
        instructions.add(new Instruction21c() {

            @Override
            public int getRegisterA() {
                return 0;
            }

            @Nonnull
            @Override
            public Reference getReference() {
                return ref;
            }

            @Override
            public int getReferenceType() {
                return ReferenceType.STRING;
            }

            @Override
            public Opcode getOpcode() {
                return Opcode.CONST_STRING;
            }

            @Override
            public int getCodeUnits() {
                return getOpcode().format.size / 2;
            }
        });
    }
    instructions.add(new ImmutableInstruction10x(Opcode.RETURN_VOID));
    MethodImplementation methodImpl = new MethodImplementation() {

        @Override
        public int getRegisterCount() {
            return 1;
        }

        @Nonnull
        @Override
        public Iterable<? extends Instruction> getInstructions() {
            return instructions;
        }

        @Nonnull
        @Override
        public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() {
            return ImmutableList.of();
        }

        @Nonnull
        @Override
        public Iterable<? extends DebugItem> getDebugItems() {
            return ImmutableList.of();
        }
    };
    dexBuilder.internClassDef("Ltest;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.<Annotation>of(), null, ImmutableList.of(dexBuilder.internMethod("Ltest;", "test", null, "V", 0, ImmutableSet.<Annotation>of(), methodImpl)));
    MemoryDataStore dexStore = new MemoryDataStore();
    dexBuilder.writeTo(dexStore);
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dexStore.getData());
    ClassDef classDef = Iterables.getFirst(dexFile.getClasses(), null);
    Assert.assertNotNull(classDef);
    Method method = Iterables.getFirst(classDef.getMethods(), null);
    Assert.assertNotNull(method);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<? extends Instruction> actualInstructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(66001, actualInstructions.size());
    for (int i = 0; i < 65536; i++) {
        Assert.assertEquals(Opcode.CONST_STRING, actualInstructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) actualInstructions.get(i)).getReference()).getString());
    }
    for (int i = 65536; i < 66000; i++) {
        Assert.assertEquals(Opcode.CONST_STRING_JUMBO, actualInstructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) actualInstructions.get(i)).getReference()).getString());
    }
    Assert.assertEquals(Opcode.RETURN_VOID, actualInstructions.get(66000).getOpcode());
}
Also used : BuilderInstruction21c(org.jf.dexlib2.builder.instruction.BuilderInstruction21c) Instruction21c(org.jf.dexlib2.iface.instruction.formats.Instruction21c) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) Nonnull(javax.annotation.Nonnull) Reference(org.jf.dexlib2.iface.reference.Reference) StringReference(org.jf.dexlib2.iface.reference.StringReference) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) Opcode(org.jf.dexlib2.Opcode) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) StringReference(org.jf.dexlib2.iface.reference.StringReference) ImmutableInstruction10x(org.jf.dexlib2.immutable.instruction.ImmutableInstruction10x) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) Test(org.junit.Test)

Example 20 with Method

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

the class JumboStringConversionTest method testJumboStringConversion.

@Test
public void testJumboStringConversion() throws IOException {
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    MethodImplementationBuilder methodBuilder = new MethodImplementationBuilder(1);
    for (int i = 0; i < 66000; i++) {
        methodBuilder.addInstruction(new BuilderInstruction21c(Opcode.CONST_STRING, 0, dexBuilder.internStringReference(String.format("%08d", i))));
    }
    methodBuilder.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));
    dexBuilder.internClassDef("Ltest;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.<Annotation>of(), null, ImmutableList.of(dexBuilder.internMethod("Ltest;", "test", null, "V", 0, ImmutableSet.<Annotation>of(), methodBuilder.getMethodImplementation())));
    MemoryDataStore dexStore = new MemoryDataStore();
    dexBuilder.writeTo(dexStore);
    DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dexStore.getData());
    ClassDef classDef = Iterables.getFirst(dexFile.getClasses(), null);
    Assert.assertNotNull(classDef);
    Method method = Iterables.getFirst(classDef.getMethods(), null);
    Assert.assertNotNull(method);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<? extends Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    Assert.assertEquals(66001, instructions.size());
    for (int i = 0; i < 65536; i++) {
        Assert.assertEquals(Opcode.CONST_STRING, instructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) instructions.get(i)).getReference()).getString());
    }
    for (int i = 65536; i < 66000; i++) {
        Assert.assertEquals(Opcode.CONST_STRING_JUMBO, instructions.get(i).getOpcode());
        Assert.assertEquals(String.format("%08d", i), ((StringReference) ((ReferenceInstruction) instructions.get(i)).getReference()).getString());
    }
    Assert.assertEquals(Opcode.RETURN_VOID, instructions.get(66000).getOpcode());
}
Also used : BuilderInstruction10x(org.jf.dexlib2.builder.instruction.BuilderInstruction10x) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) MemoryDataStore(org.jf.dexlib2.writer.io.MemoryDataStore) BuilderInstruction21c(org.jf.dexlib2.builder.instruction.BuilderInstruction21c) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) MethodImplementationBuilder(org.jf.dexlib2.builder.MethodImplementationBuilder) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder) Test(org.junit.Test)

Aggregations

ClassDef (org.jf.dexlib2.iface.ClassDef)21 Method (org.jf.dexlib2.iface.Method)21 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)19 Test (org.junit.Test)16 Instruction (org.jf.dexlib2.iface.instruction.Instruction)15 DexFile (org.jf.dexlib2.iface.DexFile)13 ImmutableMethod (org.jf.dexlib2.immutable.ImmutableMethod)13 ImmutableClassDef (org.jf.dexlib2.immutable.ImmutableClassDef)11 ImmutableDexFile (org.jf.dexlib2.immutable.ImmutableDexFile)10 MethodImplementationBuilder (org.jf.dexlib2.builder.MethodImplementationBuilder)7 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)7 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)7 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)7 ImmutableMethodParameter (org.jf.dexlib2.immutable.ImmutableMethodParameter)7 Nonnull (javax.annotation.Nonnull)6 Opcode (org.jf.dexlib2.Opcode)6 BuilderInstruction21t (org.jf.dexlib2.builder.instruction.BuilderInstruction21t)6 BuilderInstruction22c (org.jf.dexlib2.builder.instruction.BuilderInstruction22c)6 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)6 MethodReference (org.jf.dexlib2.iface.reference.MethodReference)6