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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations