Search in sources :

Example 1 with SmaliClass

use of org.jf.smalidea.psi.impl.SmaliClass in project smali by JesusFreke.

the class SmalideaMethodTest method testSparseSwitch.

public void testSparseSwitch() {
    String text = ".class public LFormat31t;\n" + ".super Ljava/lang/Object;\n" + ".source \"Format31t.smali\"" + "\n" + ".method public test_sparse-switch()V\n" + "    .registers 1\n" + "    .annotation runtime Lorg/junit/Test;\n" + "    .end annotation\n" + "\n" + "    const v0, 13\n" + "\n" + ":switch\n" + "    sparse-switch v0, :SparseSwitch\n" + "\n" + ":Label10\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label20\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label15\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":Label13\n" + "    return-void\n" + "\n" + ":Label99\n" + "    invoke-static {}, Lorg/junit/Assert;->fail()V\n" + "    return-void\n" + "\n" + ":SparseSwitch\n" + "    .sparse-switch\n" + "        10 -> :Label10\n" + "        13 -> :Label13\n" + "        15 -> :Label15\n" + "        20 -> :Label20\n" + "        99 -> :Label99\n" + "    .end sparse-switch\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<Instruction> instructions = Lists.newArrayList(impl.getInstructions());
    SparseSwitchPayload sparseSwitchPayload = (SparseSwitchPayload) instructions.get(11);
    List<? extends SwitchElement> switchElements = sparseSwitchPayload.getSwitchElements();
    Assert.assertEquals(5, switchElements.size());
    checkSwitchElement(switchElements.get(0), 10, 6);
    checkSwitchElement(switchElements.get(1), 13, 30);
    checkSwitchElement(switchElements.get(2), 15, 22);
    checkSwitchElement(switchElements.get(3), 20, 14);
    checkSwitchElement(switchElements.get(4), 99, 32);
}
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) Instruction(org.jf.dexlib2.iface.instruction.Instruction)

Example 2 with SmaliClass

use of org.jf.smalidea.psi.impl.SmaliClass in project smali by JesusFreke.

the class SmaliClassTest method testGetSuperclass.

public void testGetSuperclass() {
    myFixture.addFileToProject("base.smali", ".class public interface Lbase; .super Ljava/lang/Object;");
    myFixture.addFileToProject("iface.smali", ".class public interface Liface; .super Ljava/lang/Object;");
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("blah.smali", ".class public Lblah; .super Lbase; .implements Liface;");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("blah", smaliClass.getQualifiedName());
    PsiClass superClass = smaliClass.getSuperClass();
    Assert.assertNotNull(superClass);
    Assert.assertEquals("base", smaliClass.getSuperClass().getQualifiedName());
    Assert.assertEquals(2, smaliClass.getSupers().length);
    Assert.assertEquals("base", smaliClass.getSupers()[0].getQualifiedName());
    Assert.assertEquals("iface", smaliClass.getSupers()[1].getQualifiedName());
    Assert.assertEquals(2, smaliClass.getSuperTypes().length);
    Assert.assertEquals("base", smaliClass.getSuperTypes()[0].getCanonicalText());
    Assert.assertEquals("iface", smaliClass.getSuperTypes()[1].getCanonicalText());
    Assert.assertEquals(1, smaliClass.getInterfaces().length);
    Assert.assertEquals("iface", smaliClass.getInterfaces()[0].getQualifiedName());
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) PsiClass(com.intellij.psi.PsiClass)

Example 3 with SmaliClass

use of org.jf.smalidea.psi.impl.SmaliClass in project smali by JesusFreke.

the class SmaliClassTest method testIsInheritor.

public void testIsInheritor() {
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("blah.smali", ".class public Lblah; .super Ljava/lang/Exception;");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("blah", smaliClass.getQualifiedName());
    PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
    PsiClassType throwableType = factory.createTypeByFQClassName("java.lang.Throwable", file.getResolveScope());
    PsiClass throwableClass = throwableType.resolve();
    Assert.assertNotNull(throwableClass);
    PsiClassType exceptionType = factory.createTypeByFQClassName("java.lang.Exception", file.getResolveScope());
    PsiClass exceptionClass = exceptionType.resolve();
    Assert.assertNotNull(exceptionClass);
    PsiClassType objectType = factory.createTypeByFQClassName("java.lang.Object", file.getResolveScope());
    PsiClass objectClass = objectType.resolve();
    Assert.assertNotNull(objectClass);
    Assert.assertTrue(smaliClass.isInheritor(exceptionClass, true));
    Assert.assertTrue(smaliClass.isInheritor(throwableClass, true));
    Assert.assertTrue(smaliClass.isInheritor(objectClass, true));
    Assert.assertTrue(smaliClass.isInheritorDeep(exceptionClass, null));
    Assert.assertTrue(smaliClass.isInheritorDeep(throwableClass, null));
    Assert.assertTrue(smaliClass.isInheritorDeep(objectClass, null));
    Assert.assertTrue(smaliClass.isInheritor(exceptionClass, false));
    Assert.assertFalse(smaliClass.isInheritor(throwableClass, false));
    Assert.assertFalse(smaliClass.isInheritor(objectClass, false));
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) PsiClassType(com.intellij.psi.PsiClassType) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiClass(com.intellij.psi.PsiClass)

Example 4 with SmaliClass

use of org.jf.smalidea.psi.impl.SmaliClass in project smali by JesusFreke.

the class SmaliClassTest method testGetSuperclassForInterface.

public void testGetSuperclassForInterface() {
    myFixture.addFileToProject("iface.smali", ".class public interface Liface; .super Ljava/lang/Object;");
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("blah.smali", ".class public interface Lblah; .super Ljava/lang/Object; .implements Liface;");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("blah", smaliClass.getQualifiedName());
    PsiClass superClass = smaliClass.getSuperClass();
    Assert.assertNotNull(superClass);
    Assert.assertEquals("java.lang.Object", smaliClass.getSuperClass().getQualifiedName());
    Assert.assertEquals(2, smaliClass.getSupers().length);
    Assert.assertEquals("java.lang.Object", smaliClass.getSupers()[0].getQualifiedName());
    Assert.assertEquals("iface", smaliClass.getSupers()[1].getQualifiedName());
    Assert.assertEquals(1, smaliClass.getSuperTypes().length);
    Assert.assertEquals("iface", smaliClass.getSuperTypes()[0].getCanonicalText());
    Assert.assertEquals(1, smaliClass.getInterfaces().length);
    Assert.assertEquals("iface", smaliClass.getInterfaces()[0].getQualifiedName());
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) PsiClass(com.intellij.psi.PsiClass)

Example 5 with SmaliClass

use of org.jf.smalidea.psi.impl.SmaliClass in project smali by JesusFreke.

the class SmaliClassTypeElementTest method testGetType.

public void testGetType() {
    myFixture.addFileToProject("my/blarg.smali", ".class public Lmy/blarg; " + ".super Ljava/lang/Object;");
    String text = ".class public Lmy/pkg/blah; " + ".super Lmy/bl<ref>arg;";
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", text.replace("<ref>", ""));
    SmaliClassTypeElement typeElement = (SmaliClassTypeElement) file.findReferenceAt(text.indexOf("<ref>"));
    Assert.assertNotNull(typeElement);
    SmaliClassType type = typeElement.getType();
    Assert.assertEquals("blarg", typeElement.getName());
    Assert.assertEquals("my.blarg", typeElement.getCanonicalText());
    Assert.assertEquals("blarg", type.getClassName());
    Assert.assertEquals("my.blarg", type.getCanonicalText());
    SmaliClass resolvedClass = (SmaliClass) typeElement.resolve();
    Assert.assertNotNull(resolvedClass);
    Assert.assertEquals("my.blarg", resolvedClass.getQualifiedName());
    resolvedClass = (SmaliClass) type.resolve();
    Assert.assertNotNull(resolvedClass);
    Assert.assertEquals("my.blarg", resolvedClass.getQualifiedName());
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliClassTypeElement(org.jf.smalidea.psi.impl.SmaliClassTypeElement) SmaliClassType(org.jf.smalidea.psi.impl.SmaliClassType)

Aggregations

SmaliClass (org.jf.smalidea.psi.impl.SmaliClass)31 SmaliFile (org.jf.smalidea.psi.impl.SmaliFile)28 SmaliMethod (org.jf.smalidea.psi.impl.SmaliMethod)7 SmaliModifierList (org.jf.smalidea.psi.impl.SmaliModifierList)7 PsiClass (com.intellij.psi.PsiClass)5 PsiField (com.intellij.psi.PsiField)5 MethodImplementation (org.jf.dexlib2.iface.MethodImplementation)5 Instruction (org.jf.dexlib2.iface.instruction.Instruction)3 SmaliField (org.jf.smalidea.psi.impl.SmaliField)3 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)2 PsiTypeElement (com.intellij.psi.PsiTypeElement)2 SmaliAnnotation (org.jf.smalidea.psi.impl.SmaliAnnotation)2 SmaliClassTypeElement (org.jf.smalidea.psi.impl.SmaliClassTypeElement)2 SmaliExtendsList (org.jf.smalidea.psi.impl.SmaliExtendsList)2 SmaliImplementsList (org.jf.smalidea.psi.impl.SmaliImplementsList)2 JavaResolveResult (com.intellij.psi.JavaResolveResult)1 PsiClassType (com.intellij.psi.PsiClassType)1 PsiElementFactory (com.intellij.psi.PsiElementFactory)1 PsiModifierListOwner (com.intellij.psi.PsiModifierListOwner)1 PsiReference (com.intellij.psi.PsiReference)1