Search in sources :

Example 31 with ExceptionHandler

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

the class TryListBuilderTest method testSingleCatchAll_Middle.

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

Example 32 with ExceptionHandler

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

the class TryListBuilderTest method testOverlap_Before_Start.

@Test
public void testOverlap_Before_Start() {
    TryListBuilder tlb = new TryListBuilder();
    tlb.addHandler(5, 10, new ImmutableExceptionHandler("LException1;", 5));
    tlb.addHandler(0, 5, 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))));
    Assert.assertEquals(expected, tryBlocks);
}
Also used : ImmutableTryBlock(org.jf.dexlib2.immutable.ImmutableTryBlock) ImmutableExceptionHandler(org.jf.dexlib2.immutable.ImmutableExceptionHandler) Test(org.junit.Test)

Example 33 with ExceptionHandler

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

the class SmalideaMethodTest method testCatchBlocks.

public void testCatchBlocks() {
    String text = ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" + ".method public onCreateEngine()Landroid/service/wallpaper/WallpaperService$Engine;\n" + "    .registers 5\n" + "\n" + "    .prologue\n" + "    .line 88\n" + "    new-instance v0, Lorg/jf/Penroser/PenroserLiveWallpaper$PenroserGLEngine;\n" + "\n" + "    invoke-direct {v0, p0}, Lorg/jf/Penroser/PenroserLiveWallpaper$PenroserGLEngine;-><init>(Lorg/jf/Penroser/PenroserLiveWallpaper;)V\n" + "\n" + "    .line 89\n" + "    .local v0, \"engine\":Lorg/jf/Penroser/PenroserLiveWallpaper$PenroserGLEngine;\n" + "    sget-object v1, Lorg/jf/Penroser/PenroserLiveWallpaper;->engines:Ljava/util/LinkedList;\n" + "\n" + "    monitor-enter v1\n" + "\n" + "    .line 90\n" + "    :try_start_8\n" + "    sget-object v2, Lorg/jf/Penroser/PenroserLiveWallpaper;->engines:Ljava/util/LinkedList;\n" + "\n" + "    new-instance v3, Ljava/lang/ref/WeakReference;\n" + "\n" + "    invoke-direct {v3, v0}, Ljava/lang/ref/WeakReference;-><init>(Ljava/lang/Object;)V\n" + "\n" + "    invoke-virtual {v2, v3}, Ljava/util/LinkedList;->addLast(Ljava/lang/Object;)V\n" + "\n" + "    .line 91\n" + "    monitor-exit v1\n" + "\n" + "    .line 92\n" + "    return-object v0\n" + "\n" + "    .line 91\n" + "    :catchall_14\n" + "    move-exception v2\n" + "\n" + "    monitor-exit v1\n" + "    :try_end_16\n" + "    .catch Ljava/lang/RuntimeException; {:try_start_8 .. :try_end_16} :newcatch\n" + "    .catchall {:try_start_8 .. :try_end_16} :catchall_14\n" + "\n" + "    throw v2\n" + "\n" + "    :newcatch\n" + "    move-exception v2\n" + "    throw v2\n" + ".end method";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text);
    SmaliClass smaliClass = file.getPsiClass();
    SmaliMethod smaliMethod = smaliClass.getMethods()[0];
    SmalideaMethod method = new SmalideaMethod(smaliMethod);
    MethodImplementation impl = method.getImplementation();
    Assert.assertNotNull(impl);
    List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks = impl.getTryBlocks();
    Assert.assertEquals(2, tryBlocks.size());
    TryBlock<? extends ExceptionHandler> tryBlock = tryBlocks.get(0);
    Assert.assertEquals(8, tryBlock.getStartCodeAddress());
    Assert.assertEquals(14, tryBlock.getCodeUnitCount());
    Assert.assertEquals(1, tryBlock.getExceptionHandlers().size());
    Assert.assertEquals("Ljava/lang/RuntimeException;", tryBlock.getExceptionHandlers().get(0).getExceptionType());
    Assert.assertEquals(23, tryBlock.getExceptionHandlers().get(0).getHandlerCodeAddress());
    tryBlock = tryBlocks.get(1);
    Assert.assertEquals(8, tryBlock.getStartCodeAddress());
    Assert.assertEquals(14, tryBlock.getCodeUnitCount());
    Assert.assertEquals(1, tryBlock.getExceptionHandlers().size());
    Assert.assertEquals(null, tryBlock.getExceptionHandlers().get(0).getExceptionType());
    Assert.assertEquals(20, tryBlock.getExceptionHandlers().get(0).getHandlerCodeAddress());
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) MethodImplementation(org.jf.dexlib2.iface.MethodImplementation) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod)

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