Search in sources :

Example 21 with ExceptionHandler

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

the class ClassPool method internCode.

private void internCode(@Nonnull Method method) {
    // this also handles parameter names, which aren't directly tied to the MethodImplementation, even though the debug items are
    boolean hasInstruction = false;
    MethodImplementation methodImpl = method.getImplementation();
    if (methodImpl != null) {
        for (Instruction instruction : methodImpl.getInstructions()) {
            hasInstruction = true;
            if (instruction instanceof ReferenceInstruction) {
                Reference reference = ((ReferenceInstruction) instruction).getReference();
                switch(instruction.getOpcode().referenceType) {
                    case ReferenceType.STRING:
                        dexPool.stringSection.intern((StringReference) reference);
                        break;
                    case ReferenceType.TYPE:
                        dexPool.typeSection.intern((TypeReference) reference);
                        break;
                    case ReferenceType.FIELD:
                        dexPool.fieldSection.intern((FieldReference) reference);
                        break;
                    case ReferenceType.METHOD:
                        dexPool.methodSection.intern((MethodReference) reference);
                        break;
                    default:
                        throw new ExceptionWithContext("Unrecognized reference type: %d", instruction.getOpcode().referenceType);
                }
            }
        }
        List<? extends TryBlock> tryBlocks = methodImpl.getTryBlocks();
        if (!hasInstruction && tryBlocks.size() > 0) {
            throw new ExceptionWithContext("Method %s has no instructions, but has try blocks.", ReferenceUtil.getMethodDescriptor(method));
        }
        for (TryBlock<? extends ExceptionHandler> tryBlock : methodImpl.getTryBlocks()) {
            for (ExceptionHandler handler : tryBlock.getExceptionHandlers()) {
                dexPool.typeSection.internNullable(handler.getExceptionType());
            }
        }
    }
}
Also used : MutableMethodImplementation(org.jf.dexlib2.builder.MutableMethodImplementation) ExceptionWithContext(org.jf.util.ExceptionWithContext) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction) Instruction(org.jf.dexlib2.iface.instruction.Instruction) ReferenceInstruction(org.jf.dexlib2.iface.instruction.ReferenceInstruction)

Example 22 with ExceptionHandler

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

the class TryListBuilderTest method testSingleCatch_Beginning.

@Test
public void testSingleCatch_Beginning() {
    TryListBuilder tlb = new TryListBuilder();
    tlb.addHandler(0, 10, new ImmutableExceptionHandler("Ljava/lang/Exception;", 5));
    List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = tlb.getTryBlocks();
    List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(0, 10, ImmutableList.of(new ImmutableExceptionHandler("Ljava/lang/Exception;", 5))));
    Assert.assertEquals(expected, tryBlocks);
}
Also used : ImmutableTryBlock(org.jf.dexlib2.immutable.ImmutableTryBlock) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler) Test(org.junit.Test)

Example 23 with ExceptionHandler

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

the class TryListBuilderTest method testOverlap_Before_End.

@Test
public void testOverlap_Before_End() {
    TryListBuilder tlb = new TryListBuilder();
    tlb.addHandler(5, 10, new ImmutableExceptionHandler("LException1;", 5));
    tlb.addHandler(0, 10, new ImmutableExceptionHandler("LException2;", 6));
    List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = tlb.getTryBlocks();
    List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(0, 5, ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6))), new ImmutableTryBlock(5, 5, ImmutableList.of(new ImmutableExceptionHandler("LException1;", 5), new ImmutableExceptionHandler("LException2;", 6))));
    Assert.assertEquals(expected, tryBlocks);
}
Also used : ImmutableTryBlock(org.jf.dexlib2.immutable.ImmutableTryBlock) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler) Test(org.junit.Test)

Example 24 with ExceptionHandler

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

the class TryListBuilderTest method testHandlerMerge_DifferentType.

@Test
public void testHandlerMerge_DifferentType() {
    TryListBuilder tlb = new TryListBuilder();
    tlb.addHandler(5, 10, new ImmutableExceptionHandler("LException1;", 5));
    tlb.addHandler(0, 15, new ImmutableExceptionHandler("LException2;", 6));
    List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = tlb.getTryBlocks();
    List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(0, 5, ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6))), new ImmutableTryBlock(5, 5, ImmutableList.of(new ImmutableExceptionHandler("LException1;", 5), new ImmutableExceptionHandler("LException2;", 6))), new ImmutableTryBlock(10, 5, ImmutableList.of(new ImmutableExceptionHandler("LException2;", 6))));
    Assert.assertEquals(expected, tryBlocks);
}
Also used : ImmutableTryBlock(org.jf.dexlib2.immutable.ImmutableTryBlock) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler) Test(org.junit.Test)

Example 25 with ExceptionHandler

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

the class TryListBuilderTest method testHandlerMerge_Catchall_Exception.

@Test
public void testHandlerMerge_Catchall_Exception() {
    TryListBuilder tlb = new TryListBuilder();
    tlb.addHandler(5, 10, new ImmutableExceptionHandler(null, 5));
    tlb.addHandler(0, 15, new ImmutableExceptionHandler("LException1;", 6));
    List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = tlb.getTryBlocks();
    List<? extends TryBlock> expected = ImmutableList.of(new ImmutableTryBlock(0, 5, ImmutableList.of(new ImmutableExceptionHandler("LException1;", 6))), new ImmutableTryBlock(5, 5, ImmutableList.of(new ImmutableExceptionHandler(null, 5), new ImmutableExceptionHandler("LException1;", 6))), new ImmutableTryBlock(10, 5, ImmutableList.of(new ImmutableExceptionHandler("LException1;", 6))));
    Assert.assertEquals(expected, tryBlocks);
}
Also used : ImmutableTryBlock(org.jf.dexlib2.immutable.ImmutableTryBlock) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)26 ImmutableExceptionHandler (org.jf.dexlib2.immutable.ImmutableExceptionHandler)24 ImmutableTryBlock (org.jf.dexlib2.immutable.ImmutableTryBlock)24 Instruction (org.jf.dexlib2.iface.instruction.Instruction)5 ReferenceInstruction (org.jf.dexlib2.iface.instruction.ReferenceInstruction)4 ExceptionWithContext (org.jf.util.ExceptionWithContext)4 ExceptionHandler (org.jf.dexlib2.iface.ExceptionHandler)3 Opcode (org.jf.dexlib2.Opcode)2 MutableMethodImplementation (org.jf.dexlib2.builder.MutableMethodImplementation)2 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)2 OneRegisterInstruction (org.jf.dexlib2.iface.instruction.OneRegisterInstruction)2 SmaliMethod (org.jf.smalidea.psi.impl.SmaliMethod)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BitSet (java.util.BitSet)1 Nonnull (javax.annotation.Nonnull)1 Nullable (org.jetbrains.annotations.Nullable)1 BuilderInstruction10t (org.jf.dexlib2.builder.instruction.BuilderInstruction10t)1 BuilderInstruction10x (org.jf.dexlib2.builder.instruction.BuilderInstruction10x)1 BuilderInstruction21c (org.jf.dexlib2.builder.instruction.BuilderInstruction21c)1 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)1