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