Search in sources :

Example 1 with SmaliField

use of org.jf.smalidea.psi.impl.SmaliField 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 2 with SmaliField

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

the class SmaliFieldTest method testMultipleField.

public void testMultipleField() {
    SmaliFile file = (SmaliFile) myFixture.addFileToProject("my/pkg/blah.smali", ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" + ".field public myField:I\n" + ".field public myField2:Ljava/lang/String;\n" + ".field public myField3:[Ljava/lang/String;\n" + ".field public myField4:[[[Ljava/lang/String;\n");
    SmaliClass smaliClass = file.getPsiClass();
    Assert.assertEquals("my.pkg.blah", smaliClass.getQualifiedName());
    SmaliField[] fields = smaliClass.getFields();
    Assert.assertEquals(4, fields.length);
    Assert.assertEquals("myField", fields[0].getName());
    Assert.assertEquals("myField2", fields[1].getName());
    Assert.assertEquals("myField3", fields[2].getName());
    Assert.assertEquals("myField4", fields[3].getName());
    Assert.assertEquals("int", fields[0].getType().getCanonicalText());
    Assert.assertEquals("java.lang.String", fields[1].getType().getCanonicalText());
    Assert.assertEquals("java.lang.String[]", fields[2].getType().getCanonicalText());
    Assert.assertEquals("java.lang.String[][][]", fields[3].getType().getCanonicalText());
    PsiField field = smaliClass.findFieldByName("myField", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField", field.getName());
    field = smaliClass.findFieldByName("myField2", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField2", field.getName());
    field = smaliClass.findFieldByName("myField3", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField3", field.getName());
    field = smaliClass.findFieldByName("myField4", true);
    Assert.assertNotNull(field);
    Assert.assertEquals("myField4", 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) SmaliField(org.jf.smalidea.psi.impl.SmaliField)

Example 3 with SmaliField

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

the class SmalideaField method getAccessFlags.

@Override
public int getAccessFlags() {
    if (psiField instanceof SmaliField) {
        return ((SmaliField) psiField).getModifierList().getAccessFlags();
    } else {
        int flags = 0;
        PsiModifierList modifierList = psiField.getModifierList();
        if (modifierList == null) {
            return flags;
        }
        if (modifierList.hasModifierProperty("public")) {
            flags |= AccessFlags.PUBLIC.getValue();
        } else if (modifierList.hasModifierProperty("protected")) {
            flags |= AccessFlags.PROTECTED.getValue();
        } else if (modifierList.hasModifierProperty("private")) {
            flags |= AccessFlags.PRIVATE.getValue();
        }
        if (modifierList.hasModifierProperty("static")) {
            flags |= AccessFlags.STATIC.getValue();
        }
        if (modifierList.hasModifierProperty("final")) {
            flags |= AccessFlags.FINAL.getValue();
        }
        if (modifierList.hasModifierProperty("volatile")) {
            flags |= AccessFlags.VOLATILE.getValue();
        }
        return flags;
    }
}
Also used : SmaliField(org.jf.smalidea.psi.impl.SmaliField) PsiModifierList(com.intellij.psi.PsiModifierList)

Example 4 with SmaliField

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

the class SmaliFieldTest method testFieldAnnotations.

public void testFieldAnnotations() {
    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)

Aggregations

SmaliField (org.jf.smalidea.psi.impl.SmaliField)4 PsiField (com.intellij.psi.PsiField)3 SmaliClass (org.jf.smalidea.psi.impl.SmaliClass)3 SmaliFile (org.jf.smalidea.psi.impl.SmaliFile)3 PsiPrimitiveType (com.intellij.psi.PsiPrimitiveType)2 PsiTypeElement (com.intellij.psi.PsiTypeElement)2 SmaliModifierList (org.jf.smalidea.psi.impl.SmaliModifierList)2 PsiModifierList (com.intellij.psi.PsiModifierList)1