Search in sources :

Example 46 with PsiField

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

the class AndroidPsiAugmentProvider method getAugments.

@SuppressWarnings("unchecked")
@NotNull
@Override
public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
    if ((type != PsiClass.class && type != PsiField.class) || !(element instanceof PsiExtensibleClass)) {
        return Collections.emptyList();
    }
    final PsiExtensibleClass aClass = (PsiExtensibleClass) element;
    final String className = aClass.getName();
    final boolean rClassAugment = AndroidUtils.R_CLASS_NAME.equals(className) && type == PsiClass.class;
    if (DumbService.isDumb(element.getProject())) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because of dumb mode");
        }
        return Collections.emptyList();
    }
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because no facet");
        }
        return Collections.emptyList();
    }
    final PsiFile containingFile = element.getContainingFile();
    if (containingFile == null) {
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because of no containing file");
        }
        return Collections.emptyList();
    }
    if (type == PsiClass.class) {
        if (AndroidUtils.R_CLASS_NAME.equals(className) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
            final Set<String> existingInnerClasses = getOwnInnerClasses(aClass);
            final Set<ResourceType> types = ResourceReferenceConverter.getResourceTypesInCurrentModule(facet);
            final List<Psi> result = new ArrayList<Psi>();
            for (ResourceType resType : types) {
                if (!existingInnerClasses.contains(resType.getName())) {
                    final AndroidLightClass resClass = new ResourceTypeClass(facet, resType.getName(), aClass);
                    result.add((Psi) resClass);
                }
            }
            if (rClassAugment) {
                LOG.debug("R_CLASS_AUGMENT: " + result.size() + " classes added");
            }
            return result;
        } else if (AndroidUtils.MANIFEST_CLASS_NAME.equals(className) && AndroidResourceUtil.isManifestJavaFile(facet, containingFile)) {
            return Arrays.asList((Psi) new PermissionClass(facet, aClass), (Psi) new PermissionGroupClass(facet, aClass));
        }
        if (rClassAugment) {
            LOG.debug("R_CLASS_AUGMENT: empty because containing file is not actual R.java file");
        }
    } else if (type == PsiField.class && !(aClass instanceof AndroidLightClass)) {
        // extend existing inner classes, not provided by this augment (ex. they can be generated by maven)
        final PsiClass parentClass = aClass.getContainingClass();
        if (parentClass != null && AndroidUtils.R_CLASS_NAME.equals(parentClass.getName()) && AndroidResourceUtil.isRJavaFile(facet, containingFile)) {
            final String resClassName = aClass.getName();
            if (resClassName != null && ResourceType.getEnum(resClassName) != null) {
                final Set<String> existingFields = getOwnFields(aClass);
                final PsiField[] newFields = ResourceTypeClass.buildLocalResourceFields(facet, resClassName, aClass);
                final List<Psi> result = new ArrayList<Psi>();
                for (PsiField field : newFields) {
                    if (!existingFields.contains(field.getName())) {
                        result.add((Psi) field);
                    }
                }
                return result;
            }
        }
    }
    return Collections.emptyList();
}
Also used : HashSet(com.intellij.util.containers.HashSet) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) PsiField(com.intellij.psi.PsiField) PsiExtensibleClass(com.intellij.psi.impl.source.PsiExtensibleClass) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with PsiField

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

the class AndroidFindUsagesHandlerFactory method canFindUsages.

@Override
public boolean canFindUsages(@NotNull PsiElement element) {
    PsiElement element1 = LazyValueResourceElementWrapper.computeLazyElement(element);
    if (element1 == null) {
        return false;
    }
    if (element1 instanceof XmlAttributeValue) {
        XmlAttributeValue value = (XmlAttributeValue) element1;
        if (AndroidResourceUtil.findIdFields(value).length > 0) {
            return true;
        }
    }
    element1 = correctResourceElement(element1);
    if (element1 instanceof PsiField) {
        return AndroidResourceUtil.isResourceField((PsiField) element1);
    } else if (element1 instanceof PsiFile || element1 instanceof XmlTag) {
        final AndroidFacet facet = AndroidFacet.getInstance(element1);
        if (facet != null) {
            if (element1 instanceof PsiFile) {
                return facet.getLocalResourceManager().getFileResourceFolderType((PsiFile) element1) != null;
            } else {
                final ResourceFolderType fileResType = facet.getLocalResourceManager().getFileResourceFolderType(element1.getContainingFile());
                if (ResourceFolderType.VALUES == fileResType) {
                    return AndroidResourceUtil.getResourceTypeByValueResourceTag((XmlTag) element1) != null;
                }
            }
        }
    }
    return false;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) PsiField(com.intellij.psi.PsiField) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 48 with PsiField

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

the class AndroidJavaCompletionContributor method filterPrivateResources.

public void filterPrivateResources(@NotNull CompletionParameters parameters, @NotNull final CompletionResultSet resultSet, AndroidFacet facet) {
    final ResourceVisibilityLookup lookup = AppResourceRepository.getAppResources(facet, true).getResourceVisibility(facet);
    if (lookup.isEmpty()) {
        return;
    }
    resultSet.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult result) {
            final Object obj = result.getLookupElement().getObject();
            if (obj instanceof PsiField) {
                PsiField field = (PsiField) obj;
                PsiClass containingClass = field.getContainingClass();
                if (containingClass != null) {
                    PsiClass rClass = containingClass.getContainingClass();
                    if (rClass != null && rClass.getName().equals(R_CLASS)) {
                        ResourceType type = ResourceType.getEnum(containingClass.getName());
                        if (type != null && lookup.isPrivate(type, field.getName())) {
                            return;
                        }
                    }
                }
            }
            resultSet.passResult(result);
        }
    });
}
Also used : CompletionResult(com.intellij.codeInsight.completion.CompletionResult) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) ResourceType(com.android.resources.ResourceType) ResourceVisibilityLookup(com.android.ide.common.repository.ResourceVisibilityLookup)

Example 49 with PsiField

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

the class AndroidFindUsagesHandlerFactory method createFindUsagesHandler.

@Override
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
    PsiElement e = LazyValueResourceElementWrapper.computeLazyElement(element);
    if (e == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(e);
    if (facet == null) {
        return null;
    }
    if (e instanceof XmlAttributeValue) {
        XmlAttributeValue value = (XmlAttributeValue) e;
        PsiField[] fields = AndroidResourceUtil.findIdFields(value);
        if (fields.length > 0) {
            e = wrapIfNecessary(value);
            return new MyFindUsagesHandler(e, fields);
        }
    }
    e = correctResourceElement(e);
    if (e instanceof PsiFile) {
        // resource file
        PsiField[] fields = AndroidResourceUtil.findResourceFieldsForFileResource((PsiFile) e, true);
        if (fields.length == 0) {
            return null;
        }
        return new MyFindUsagesHandler(e, fields);
    } else if (e instanceof XmlTag) {
        // value resource
        XmlTag tag = (XmlTag) e;
        PsiField[] fields = AndroidResourceUtil.findResourceFieldsForValueResource(tag, true);
        if (fields.length == 0) {
            return null;
        }
        PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, true);
        if (styleableFields.length > 0) {
            fields = ObjectArrays.concat(fields, styleableFields, PsiField.class);
        }
        final XmlAttribute nameAttr = tag.getAttribute(ATTR_NAME);
        final XmlAttributeValue nameValue = nameAttr != null ? nameAttr.getValueElement() : null;
        assert nameValue != null;
        return new MyFindUsagesHandler(nameValue, fields);
    } else if (e instanceof PsiField) {
        PsiField field = (PsiField) e;
        List<PsiElement> resources = AndroidResourceUtil.findResourcesByField(field);
        if (resources.size() == 0) {
            return new MyFindUsagesHandler(e);
        }
        // ignore alternative resources because their usages are the same
        PsiElement resource = resources.get(0);
        return createFindUsagesHandler(resource, forHighlightUsages);
    }
    return null;
}
Also used : PsiField(com.intellij.psi.PsiField) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 50 with PsiField

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

the class BrShortNamesCache method getFieldsByName.

@NotNull
@Override
public PsiField[] getFieldsByName(@NotNull @NonNls String name, @NotNull GlobalSearchScope scope) {
    if (!isMyScope(scope)) {
        return PsiField.EMPTY_ARRAY;
    }
    PsiClass[] psiClasses = filterByScope(myComponent.getDataBindingEnabledFacets(), scope);
    if (psiClasses.length == 0) {
        return PsiField.EMPTY_ARRAY;
    }
    List<PsiField> result = Lists.newArrayList();
    for (PsiClass psiClass : psiClasses) {
        PsiField field = psiClass.findFieldByName(name, false);
        if (field != null) {
            result.add(field);
        }
    }
    return result.toArray(new PsiField[result.size()]);
}
Also used : PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) NotNull(org.jetbrains.annotations.NotNull)

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