Search in sources :

Example 1 with BaseStringReference

use of org.jf.dexlib2.base.reference.BaseStringReference in project smali by JesusFreke.

the class InstructionMethodItemTest method testInvalidReference.

@Test
public void testInvalidReference() throws IOException {
    Instruction21c instruction = new Instruction21c() {

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

        @Nonnull
        @Override
        public Reference getReference() {
            return new BaseStringReference() {

                @Override
                public void validateReference() throws InvalidReferenceException {
                    throw new InvalidReferenceException("blahblahblah");
                }

                @Nonnull
                @Override
                public String getString() {
                    throw new RuntimeException("invalid reference");
                }
            };
        }

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

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

        @Override
        public int getCodeUnits() {
            return Format.Format21c.size / 2;
        }
    };
    MethodImplementation methodImplementation = new MethodImplementation() {

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

        @Nonnull
        @Override
        public Iterable<? extends Instruction> getInstructions() {
            return ImmutableList.of(instruction);
        }

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

        @Nonnull
        @Override
        public Iterable<? extends DebugItem> getDebugItems() {
            return ImmutableList.of();
        }
    };
    Method method = new TestMethod(methodImplementation);
    ClassDefinition classDefinition = new ClassDefinition(new BaksmaliOptions(), new TestClassDef());
    MethodDefinition methodDefinition = new MethodDefinition(classDefinition, method, methodImplementation);
    methodDefinition.registerFormatter = new RegisterFormatter(new BaksmaliOptions(), 1, 0);
    InstructionMethodItem methodItem = new InstructionMethodItem<Instruction21c>(methodDefinition, 0, instruction);
    StringWriter stringWriter = new StringWriter();
    BaksmaliWriter writer = new BaksmaliWriter(stringWriter);
    methodItem.writeTo(writer);
    Assert.assertEquals("#Invalid reference\n#const-string v0, blahblahblah\nnop", stringWriter.toString());
}
Also used : Instruction21c(org.jf.dexlib2.iface.instruction.formats.Instruction21c) ClassDefinition(org.jf.baksmali.Adaptors.ClassDefinition) RegisterFormatter(org.jf.baksmali.Adaptors.RegisterFormatter) BaseStringReference(org.jf.dexlib2.base.reference.BaseStringReference) InstructionMethodItem(org.jf.baksmali.Adaptors.Format.InstructionMethodItem) StringWriter(java.io.StringWriter) MethodDefinition(org.jf.baksmali.Adaptors.MethodDefinition) BaksmaliWriter(org.jf.baksmali.formatter.BaksmaliWriter) Test(org.junit.Test)

Aggregations

StringWriter (java.io.StringWriter)1 ClassDefinition (org.jf.baksmali.Adaptors.ClassDefinition)1 InstructionMethodItem (org.jf.baksmali.Adaptors.Format.InstructionMethodItem)1 MethodDefinition (org.jf.baksmali.Adaptors.MethodDefinition)1 RegisterFormatter (org.jf.baksmali.Adaptors.RegisterFormatter)1 BaksmaliWriter (org.jf.baksmali.formatter.BaksmaliWriter)1 BaseStringReference (org.jf.dexlib2.base.reference.BaseStringReference)1 Instruction21c (org.jf.dexlib2.iface.instruction.formats.Instruction21c)1 Test (org.junit.Test)1