Search in sources :

Example 41 with PsiField

use of com.intellij.psi.PsiField in project android by JetBrains.

the class StringResourceSafeDeleteProcessorDelegate method getElementsToSearch.

@NotNull
@Override
public Collection<? extends PsiElement> getElementsToSearch(@NotNull PsiElement element, @Nullable Module module, @NotNull Collection<PsiElement> elementsToDelete) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        XmlAttribute attribute = tag.getAttribute(ATTR_NAME);
        assert attribute != null;
        Collection<PsiElement> elements = new ArrayList<>();
        // Find usages of the string element's name attribute value, such as @string/string_name references in XML files
        elements.add(attribute.getValueElement());
        // R.string.string_name references in Java files that are not LightElements
        Collection<PsiField> fields = Arrays.stream(AndroidResourceUtil.findResourceFieldsForValueResource(tag, true)).filter(field -> !(field instanceof LightElement)).collect(Collectors.toList());
        elements.addAll(fields);
        return elements;
    } else if (element instanceof XmlAttributeValue) {
        return getElementsToSearch(element.getParent().getParent(), module, elementsToDelete);
    } else {
        return Collections.emptyList();
    }
}
Also used : java.util(java.util) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ATTR_NAME(com.android.SdkConstants.ATTR_NAME) LightElement(com.intellij.psi.impl.light.LightElement) UsageInfo(com.intellij.usageView.UsageInfo) TAG_STRING(com.android.SdkConstants.TAG_STRING) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) SafeDeleteProcessorDelegateBase(com.intellij.refactoring.safeDelete.SafeDeleteProcessorDelegateBase) PsiElement(com.intellij.psi.PsiElement) AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil) Project(com.intellij.openapi.project.Project) PsiField(com.intellij.psi.PsiField) PsiNamedElement(com.intellij.psi.PsiNamedElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) SafeDeleteProcessor(com.intellij.refactoring.safeDelete.SafeDeleteProcessor) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) XmlTag(com.intellij.psi.xml.XmlTag) NonCodeUsageSearchInfo(com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiField(com.intellij.psi.PsiField) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) LightElement(com.intellij.psi.impl.light.LightElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with PsiField

use of com.intellij.psi.PsiField in project android by JetBrains.

the class AndroidResourceUtilTest method testFindResourceFields.

public void testFindResourceFields() {
    myFixture.copyFileToProject("util/strings.xml", "res/values/strings.xml");
    myFixture.copyFileToProject("R.java", "gen/p1/p2/R.java");
    PsiField[] fields = AndroidResourceUtil.findResourceFields(myFacet, "string", "hello", false);
    for (PsiField field : fields) {
        assertEquals("hello", field.getName());
        assertEquals("p2", field.getContainingFile().getContainingDirectory().getName());
    }
    assertEquals(1, fields.length);
}
Also used : PsiField(com.intellij.psi.PsiField)

Example 43 with PsiField

use of com.intellij.psi.PsiField in project smali by JesusFreke.

the class FieldReferenceTest method testSmaliReferenceFromJava.

/**
     * Test a reference to a smali field from a java class
     */
public void testSmaliReferenceFromJava() throws Exception {
    createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" + ".field public static blort:I");
    String text = "public class blah { public static void something() {" + "blarg.bl<ref>ort = 10;" + "}}";
    PsiReference fieldReference = configureByFileText(text, "blah.java");
    Assert.assertNotNull(fieldReference);
    PsiField resolvedField = (PsiField) fieldReference.resolve();
    Assert.assertNotNull(resolvedField);
    Assert.assertEquals("blort", resolvedField.getName());
    Assert.assertNotNull(resolvedField.getContainingClass());
    Assert.assertEquals("blarg", resolvedField.getContainingClass().getQualifiedName());
    Assert.assertEquals("int", resolvedField.getType().getCanonicalText());
}
Also used : PsiField(com.intellij.psi.PsiField) PsiReference(com.intellij.psi.PsiReference)

Example 44 with PsiField

use of com.intellij.psi.PsiField in project smali by JesusFreke.

the class FieldReferenceTest method testSmaliReferenceFromSmali.

/**
     * Test a reference to a smali field from a smali class
     */
public void testSmaliReferenceFromSmali() throws Exception {
    createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" + ".field public static blort:I");
    String text = ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" + ".method public blah()V\n" + "    .locals 1\n" + "    sget v0, Lblarg;->bl<ref>ort:I\n" + "    return-void\n" + ".end method";
    SmaliFieldReference fieldReference = (SmaliFieldReference) configureByFileText(text, "blah.smali");
    Assert.assertNotNull(fieldReference);
    Assert.assertEquals("blort", fieldReference.getName());
    Assert.assertNotNull(fieldReference.getFieldType());
    Assert.assertEquals("int", fieldReference.getFieldType().getType().getCanonicalText());
    PsiField resolvedField = fieldReference.resolve();
    Assert.assertNotNull(resolvedField);
    Assert.assertEquals("blort", resolvedField.getName());
    Assert.assertNotNull(resolvedField.getContainingClass());
    Assert.assertEquals("blarg", resolvedField.getContainingClass().getQualifiedName());
    Assert.assertEquals("int", resolvedField.getType().getCanonicalText());
}
Also used : SmaliFieldReference(org.jf.smalidea.psi.impl.SmaliFieldReference) PsiField(com.intellij.psi.PsiField)

Example 45 with PsiField

use of com.intellij.psi.PsiField 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

PsiField (com.intellij.psi.PsiField)86 PsiClass (com.intellij.psi.PsiClass)34 PsiElement (com.intellij.psi.PsiElement)27 ArrayList (java.util.ArrayList)13 PsiMethod (com.intellij.psi.PsiMethod)11 NotNull (org.jetbrains.annotations.NotNull)11 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)8 PsiReference (com.intellij.psi.PsiReference)7 PsiType (com.intellij.psi.PsiType)7 PsiLocalVariable (com.intellij.psi.PsiLocalVariable)6 Nullable (org.jetbrains.annotations.Nullable)6 Nullable (com.android.annotations.Nullable)5 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)5 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)5 PsiExpression (com.intellij.psi.PsiExpression)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 ResourceType (com.android.resources.ResourceType)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)4 PsiFile (com.intellij.psi.PsiFile)4