Search in sources :

Example 1 with PsiExtensibleClass

use of com.intellij.psi.impl.source.PsiExtensibleClass 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)

Aggregations

ResourceType (com.android.resources.ResourceType)1 PsiClass (com.intellij.psi.PsiClass)1 PsiField (com.intellij.psi.PsiField)1 PsiFile (com.intellij.psi.PsiFile)1 PsiExtensibleClass (com.intellij.psi.impl.source.PsiExtensibleClass)1 HashSet (com.intellij.util.containers.HashSet)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 NotNull (org.jetbrains.annotations.NotNull)1