Search in sources :

Example 51 with PsiField

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

the class AndroidInlineLayoutProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    if (myUsageElement != null) {
        return new UsageInfo[] { new UsageInfo(myUsageElement) };
    }
    final Set<UsageInfo> usages = new HashSet<UsageInfo>();
    AndroidInlineUtil.addReferences(myLayoutFile, usages);
    for (PsiField field : AndroidResourceUtil.findResourceFieldsForFileResource(myLayoutFile, false)) {
        AndroidInlineUtil.addReferences(field, usages);
    }
    return usages.toArray(new UsageInfo[usages.size()]);
}
Also used : PsiField(com.intellij.psi.PsiField) UsageInfo(com.intellij.usageView.UsageInfo) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with PsiField

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

the class AndroidResourceUtilTest method testFindResourceFieldsWithMultipleResourceNames.

public void testFindResourceFieldsWithMultipleResourceNames() {
    myFixture.copyFileToProject("util/strings.xml", "res/values/strings.xml");
    myFixture.copyFileToProject("R.java", "gen/p1/p2/R.java");
    PsiField[] fields = AndroidResourceUtil.findResourceFields(myFacet, "string", ImmutableList.of("hello", "goodbye"), false);
    Set<String> fieldNames = Sets.newHashSet();
    for (PsiField field : fields) {
        fieldNames.add(field.getName());
        assertEquals("p2", field.getContainingFile().getContainingDirectory().getName());
    }
    assertEquals(ImmutableSet.of("hello", "goodbye"), fieldNames);
    assertEquals(2, fields.length);
}
Also used : PsiField(com.intellij.psi.PsiField)

Example 53 with PsiField

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

the class AndroidResourceUtilTest method testIsRJavaFileImportedNoManifest.

/** Tests that a module without an Android Manifest can still import a lib's R class */
public void testIsRJavaFileImportedNoManifest() throws Exception {
    Module libModule = myAdditionalModules.get(0);
    // Remove the current lib manifest (has wrong package name) and copy a manifest with proper package into the lib module.
    deleteManifest(libModule);
    myFixture.copyFileToProject("util/lib/AndroidManifest.xml", "additionalModules/lib/AndroidManifest.xml");
    // Copy an empty R class with the proper package into the lib module.
    VirtualFile libRFile = myFixture.copyFileToProject("util/lib/R.java", "additionalModules/lib/gen/p1/p2/lib/R.java");
    // Add some lib string resources.
    myFixture.copyFileToProject("util/lib/strings.xml", "additionalModules/lib/res/values/strings.xml");
    // Remove the manifest from the main module.
    deleteManifest(myModule);
    // The main module doesn't get a generated R class and inherit fields (lack of manifest)
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertThat(facet).isNotNull();
    PsiField[] mainFields = AndroidResourceUtil.findResourceFields(facet, "string", "lib_hello", false);
    assertEmpty(mainFields);
    // However, if the main module happens to get a handle on the lib's R class
    // (e.g., via "import p1.p2.lib.R;"), then that R class should be recognized
    // (e.g., for goto navigation).
    PsiManager psiManager = PsiManager.getInstance(getProject());
    PsiFile libRClassFile = psiManager.findFile(libRFile);
    assertNotNull(libRClassFile);
    assertTrue(AndroidResourceUtil.isRJavaFile(myFacet, libRClassFile));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiField(com.intellij.psi.PsiField) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 54 with PsiField

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

the class AndroidResourceUtilTest method testFindResourceFieldsWithInheritance.

/** Tests that "inherited" resource references are found (R fields in generated in dependent modules). */
public void testFindResourceFieldsWithInheritance() throws Exception {
    myFixture.copyFileToProject("R.java", "gen/p1/p2/R.java");
    Module libModule = myAdditionalModules.get(0);
    // Remove the current manifest (has wrong package name) and copy a manifest with proper package into the lib module.
    deleteManifest(libModule);
    myFixture.copyFileToProject("util/lib/AndroidManifest.xml", "additionalModules/lib/AndroidManifest.xml");
    // Copy an empty R class with the proper package into the lib module.
    myFixture.copyFileToProject("util/lib/R.java", "additionalModules/lib/gen/p1/p2/lib/R.java");
    // Add some lib string resources.
    myFixture.copyFileToProject("util/lib/strings.xml", "additionalModules/lib/res/values/strings.xml");
    AndroidFacet facet = AndroidFacet.getInstance(libModule);
    assertThat(facet).isNotNull();
    PsiField[] fields = AndroidResourceUtil.findResourceFields(facet, "string", "lib_hello", false);
    Set<String> dirNames = Sets.newHashSet();
    for (PsiField field : fields) {
        assertEquals("lib_hello", field.getName());
        dirNames.add(field.getContainingFile().getContainingDirectory().getName());
    }
    assertEquals(ImmutableSet.of("p2", "lib"), dirNames);
    assertEquals(2, fields.length);
}
Also used : PsiField(com.intellij.psi.PsiField) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 55 with PsiField

use of com.intellij.psi.PsiField in project intellij-community by JetBrains.

the class ExtensionPointImpl method collectMissingWithTags.

@Override
public List<PsiField> collectMissingWithTags() {
    PsiClass beanClass = getBeanClass().getValue();
    if (beanClass == null) {
        return Collections.emptyList();
    }
    final List<PsiField> result = new SmartList<>();
    for (PsiField field : beanClass.getAllFields()) {
        if (ExtensionDomExtender.isClassField(field.getName()) && ExtensionDomExtender.findWithElement(getWithElements(), field) == null) {
            result.add(field);
        }
    }
    return result;
}
Also used : PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) SmartList(com.intellij.util.SmartList)

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