Search in sources :

Example 6 with SmaliClass

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

the class SmaliFieldTest method testBasicField.

public void testBasicField() {
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" + ".field public myField:I");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("my.pkg.blah", smaliClass.getQualifiedName());
    SmaliField[] fields = smaliClass.getFields();
    Assert.assertEquals(1, fields.length);
    Assert.assertEquals("myField", fields[0].getName());
    Assert.assertTrue(fields[0].getType() instanceof PsiPrimitiveType);
    Assert.assertEquals("int", fields[0].getType().getCanonicalText());
    PsiTypeElement typeElement = fields[0].getTypeElement();
    Assert.assertNotNull("I", typeElement);
    Assert.assertEquals("I", typeElement.getText());
    SmaliModifierList modifierList = fields[0].getModifierList();
    Assert.assertNotNull(modifierList);
    Assert.assertEquals(AccessFlags.PUBLIC.getValue(), modifierList.getAccessFlags());
    Assert.assertTrue(modifierList.hasExplicitModifier("public"));
    Assert.assertTrue(modifierList.hasModifierProperty("public"));
    Assert.assertTrue(fields[0].hasModifierProperty("public"));
    PsiField[] psifields = smaliClass.getAllFields();
    Assert.assertEquals(1, psifields.length);
    Assert.assertEquals("myField", psifields[0].getName());
    PsiField field = smaliClass.findFieldByName("myField", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField", field.getName());
    field = smaliClass.findFieldByName("nonExistantField", true);
    Assert.assertNull(field);
    field = smaliClass.findFieldByName("nonExistantField", false);
    Assert.assertNull(field);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) PsiPrimitiveType(com.intellij.psi.PsiPrimitiveType) PsiTypeElement(com.intellij.psi.PsiTypeElement) PsiField(com.intellij.psi.PsiField) SmaliField(org.jf.smalidea.psi.impl.SmaliField) SmaliModifierList(org.jf.smalidea.psi.impl.SmaliModifierList)

Example 7 with SmaliClass

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

the class SmaliFieldTest method testSmaliSuperField.

public void testSmaliSuperField() {
    myFixture.addFileToProject("my/pkg/base.smali", ".class public Lmy/pkg/base; .super Ljava/lang/Object;\n" + ".field public baseField:I");
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", ".class public Lmy/pkg/blah; .super Lmy/pkg/base;\n" + ".field public myField:I");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("my.pkg.blah", smaliClass.getQualifiedName());
    PsiField[] fields = smaliClass.getFields();
    Assert.assertEquals(1, fields.length);
    Assert.assertEquals("myField", fields[0].getName());
    fields = smaliClass.getAllFields();
    Assert.assertEquals(2, fields.length);
    Assert.assertTrue(fields[0].getName().equals("myField") || fields[1].getName().equals("myField"));
    Assert.assertTrue(fields[0].getName().equals("baseField") || fields[1].getName().equals("baseField"));
    PsiField field = smaliClass.findFieldByName("myField", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField", field.getName());
    field = smaliClass.findFieldByName("myField", false);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField", field.getName());
    field = smaliClass.findFieldByName("baseField", false);
    Assert.assertNull(field);
    field = smaliClass.findFieldByName("baseField", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("baseField", field.getName());
    field = smaliClass.findFieldByName("nonExistantField", true);
    Assert.assertNull(field);
    field = smaliClass.findFieldByName("nonExistantField", false);
    Assert.assertNull(field);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) PsiField(com.intellij.psi.PsiField)

Example 8 with SmaliClass

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

the class SmaliAnnotationTest method testClassAnnotation.

// TODO: test default values
public void testClassAnnotation() {
    myFixture.addFileToProject("my/TestAnnotation.smali", ".class public interface abstract annotation Lmy/TestAnnotation;\n" + ".super Ljava/lang/Object;\n" + ".implements Ljava/lang/annotation/Annotation;\n" + "\n" + ".method public abstract testBooleanValue()Z\n" + ".end method\n" + "\n" + ".method public abstract testStringArrayValue()[Ljava/lang/String;\n" + ".end method\n" + "\n" + ".method public abstract testStringValue()Ljava/lang/String;\n" + ".end method");
    myFixture.addFileToProject("my/TestAnnotation2.smali", ".class public interface abstract annotation Lmy/TestAnnotation2;\n" + ".super Ljava/lang/Object;\n" + ".implements Ljava/lang/annotation/Annotation;\n");
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" + "\n" + ".annotation runtime Lmy/TestAnnotation;\n" + "    testBooleanValue = true\n" + "    testStringValue = \"blah\"\n" + "    testStringArrayValue = {\n" + "        \"blah1\",\n" + "        \"blah2\"\n" + "    }\n" + ".end annotation\n" + "\n" + ".annotation runtime Lmy/TestAnnotation2;\n" + ".end annotation");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("my.pkg.blah", smaliClass.getQualifiedName());
    doTest(smaliClass);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass)

Example 9 with SmaliClass

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

the class SmaliAnnotationTest method testMethodAnnotation.

public void testMethodAnnotation() {
    myFixture.addFileToProject("my/TestAnnotation.smali", ".class public interface abstract annotation Lmy/TestAnnotation;\n" + ".super Ljava/lang/Object;\n" + ".implements Ljava/lang/annotation/Annotation;\n" + "\n" + ".method public abstract testBooleanValue()Z\n" + ".end method\n" + "\n" + ".method public abstract testStringArrayValue()[Ljava/lang/String;\n" + ".end method\n" + "\n" + ".method public abstract testStringValue()Ljava/lang/String;\n" + ".end method");
    myFixture.addFileToProject("my/TestAnnotation2.smali", ".class public interface abstract annotation Lmy/TestAnnotation2;\n" + ".super Ljava/lang/Object;\n" + ".implements Ljava/lang/annotation/Annotation;\n");
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" + "\n" + ".method public myMethod()V\n" + "    .annotation runtime Lmy/TestAnnotation;\n" + "        testBooleanValue = true\n" + "        testStringValue = \"blah\"\n" + "        testStringArrayValue = {\n" + "            \"blah1\",\n" + "            \"blah2\"\n" + "        }\n" + "    .end annotation\n" + "    .annotation runtime Lmy/TestAnnotation2;\n" + "    .end annotation\n" + ".end method");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("my.pkg.blah", smaliClass.getQualifiedName());
    SmaliMethod method = smaliClass.getMethods()[0];
    doTest(method);
}
Also used : SmaliFile(org.jf.smalidea.psi.impl.SmaliFile) SmaliClass(org.jf.smalidea.psi.impl.SmaliClass) SmaliMethod(org.jf.smalidea.psi.impl.SmaliMethod)

Example 10 with SmaliClass

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

the class SmaliAnnotationTest method doTest.

public void doTest(PsiAnnotationOwner annotationOwner) {
    Assert.assertEquals(2, annotationOwner.getAnnotations().length);
    Assert.assertEquals("my.TestAnnotation", annotationOwner.getAnnotations()[0].getQualifiedName());
    PsiJavaCodeReferenceElement annotationNameRef = annotationOwner.getAnnotations()[0].getNameReferenceElement();
    Assert.assertNotNull(annotationNameRef);
    SmaliClass smaliAnnotationClass = (SmaliClass) annotationNameRef.resolve();
    Assert.assertNotNull(smaliAnnotationClass);
    Assert.assertEquals("my.TestAnnotation", smaliAnnotationClass.getQualifiedName());
    Assert.assertEquals("my.TestAnnotation2", annotationOwner.getAnnotations()[1].getQualifiedName());
    annotationNameRef = annotationOwner.getAnnotations()[1].getNameReferenceElement();
    Assert.assertNotNull(annotationNameRef);
    smaliAnnotationClass = (SmaliClass) annotationNameRef.resolve();
    Assert.assertNotNull(smaliAnnotationClass);
    Assert.assertEquals("my.TestAnnotation2", smaliAnnotationClass.getQualifiedName());
    PsiAnnotation smaliAnnotation = annotationOwner.findAnnotation("my.TestAnnotation");
    Assert.assertNotNull(smaliAnnotation);
    Assert.assertEquals("my.TestAnnotation", smaliAnnotation.getQualifiedName());
    PsiAnnotationOwner owner = smaliAnnotation.getOwner();
    Assert.assertNotNull(owner);
    Assert.assertSame(annotationOwner, owner);
    annotationNameRef = smaliAnnotation.getNameReferenceElement();
    Assert.assertNotNull(annotationNameRef);
    smaliAnnotationClass = (SmaliClass) annotationNameRef.resolve();
    Assert.assertNotNull(smaliAnnotationClass);
    Assert.assertEquals("my.TestAnnotation", smaliAnnotationClass.getQualifiedName());
    PsiAnnotationParameterList parameterList = smaliAnnotation.getParameterList();
    Assert.assertNotNull(parameterList);
    Assert.assertEquals(3, parameterList.getAttributes().length);
    Assert.assertEquals("testBooleanValue", parameterList.getAttributes()[0].getName());
    PsiAnnotationMemberValue value = parameterList.getAttributes()[0].getValue();
    Assert.assertNotNull(value);
    // TODO: test the values rather than the text
    Assert.assertEquals("true", value.getText());
    Assert.assertEquals("testStringValue", parameterList.getAttributes()[1].getName());
    value = parameterList.getAttributes()[1].getValue();
    Assert.assertNotNull(value);
    Assert.assertEquals("\"blah\"", value.getText());
    Assert.assertEquals("testStringArrayValue", parameterList.getAttributes()[2].getName());
    value = parameterList.getAttributes()[2].getValue();
    Assert.assertNotNull(value);
    // TODO: test the individual values, once the array literal stuff is implemented
    value = smaliAnnotation.findAttributeValue("testBooleanValue");
    Assert.assertNotNull(value);
    Assert.assertEquals("true", value.getText());
    value = smaliAnnotation.findAttributeValue("testStringValue");
    Assert.assertNotNull(value);
    Assert.assertEquals("\"blah\"", value.getText());
    value = smaliAnnotation.findAttributeValue("testStringArrayValue");
    Assert.assertNotNull(value);
    // TODO: test findAttributeValue vs findDeclaredAttributeValue for default values
    smaliAnnotation = annotationOwner.findAnnotation("my.TestAnnotation2");
    Assert.assertNotNull(smaliAnnotation);
    Assert.assertEquals("my.TestAnnotation2", smaliAnnotation.getQualifiedName());
    owner = smaliAnnotation.getOwner();
    Assert.assertNotNull(owner);
    Assert.assertSame(annotationOwner, owner);
    annotationNameRef = smaliAnnotation.getNameReferenceElement();
    Assert.assertNotNull(annotationNameRef);
    smaliAnnotationClass = (SmaliClass) annotationNameRef.resolve();
    Assert.assertNotNull(smaliAnnotationClass);
    Assert.assertEquals("my.TestAnnotation2", smaliAnnotationClass.getQualifiedName());
    parameterList = smaliAnnotation.getParameterList();
    Assert.assertNotNull(parameterList);
    Assert.assertEquals(0, parameterList.getAttributes().length);
}
Also used : SmaliClass(org.jf.smalidea.psi.impl.SmaliClass)

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