Search in sources :

Example 71 with PsiField

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

the class StaticFieldCanBeMovedToUseInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefField)) {
        return null;
    }
    final RefField refField = (RefField) refEntity;
    final PsiField field = refField.getElement();
    if (field == null) {
        return null;
    }
    final PsiType type = field.getType();
    if (!type.equals(PsiType.BOOLEAN)) {
        return null;
    }
    final RefClass fieldClass = refField.getOwnerClass();
    final Collection<RefElement> inReferences = refField.getInReferences();
    final RefJavaUtil refUtil = RefJavaUtil.getInstance();
    final Set<RefClass> classesUsed = new HashSet<>();
    for (RefElement inReference : inReferences) {
        final RefClass referringClass = refUtil.getOwnerClass(inReference);
        if (referringClass == null) {
            return null;
        }
        if (referringClass.equals(fieldClass)) {
            return null;
        }
        classesUsed.add(referringClass);
        if (classesUsed.size() > 1) {
            return null;
        }
    }
    if (classesUsed.size() != 1) {
        return null;
    }
    final RefClass referencingClass = classesUsed.iterator().next();
    final String errorString = "Static field " + refEntity.getName() + " is only accessed in subclass " + referencingClass.getName();
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiField(com.intellij.psi.PsiField) PsiType(com.intellij.psi.PsiType) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 72 with PsiField

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

the class GebContentDeclarationSearcher method findDeclarationsAt.

@Override
public void findDeclarationsAt(@NotNull PsiElement element, int offsetInElement, Consumer<PomTarget> consumer) {
    PsiElement grCall = element.getParent();
    if (!(grCall instanceof GrMethodCall))
        return;
    PsiElement grClosure = grCall.getParent();
    if (!(grClosure instanceof GrClosableBlock))
        return;
    PsiElement contentField = grClosure.getParent();
    if (!(contentField instanceof GrField))
        return;
    GrField field = (GrField) contentField;
    if (!"content".equals(field.getName()) || !field.hasModifierProperty(PsiModifier.STATIC))
        return;
    PsiClass containingClass = field.getContainingClass();
    if (!InheritanceUtil.isInheritor(containingClass, "geb.Page") && !InheritanceUtil.isInheritor(containingClass, "geb.Module"))
        return;
    Map<String, PsiField> elements = GebUtil.getContentElements(containingClass);
    for (PsiField f : elements.values()) {
        if (f.getNavigationElement() == element) {
            consumer.consume(f);
            return;
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement)

Example 73 with PsiField

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

the class ConvertMethodToClosureIntention method processIntention.

@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final GrMethod method;
    if (element.getParent() instanceof GrMethod) {
        method = (GrMethod) element.getParent();
    } else {
        final PsiReference ref = element.getReference();
        LOG.assertTrue(ref != null);
        final PsiElement resolved = ref.resolve();
        LOG.assertTrue(resolved instanceof GrMethod);
        method = (GrMethod) resolved;
    }
    final PsiClass containingClass = method.getContainingClass();
    final String methodName = method.getName();
    final PsiField field = containingClass.findFieldByName(methodName, true);
    if (field != null) {
        conflicts.putValue(field, GroovyIntentionsBundle.message("field.already.exists", methodName));
    }
    final Collection<PsiReference> references = MethodReferencesSearch.search(method).findAll();
    final Collection<GrReferenceExpression> usagesToConvert = new HashSet<>(references.size());
    for (PsiReference ref : references) {
        final PsiElement psiElement = ref.getElement();
        if (!GroovyLanguage.INSTANCE.equals(psiElement.getLanguage())) {
            conflicts.putValue(psiElement, GroovyIntentionsBundle.message("method.is.used.outside.of.groovy"));
        } else if (!PsiUtil.isMethodUsage(psiElement)) {
            if (psiElement instanceof GrReferenceExpression) {
                if (((GrReferenceExpression) psiElement).hasMemberPointer()) {
                    usagesToConvert.add((GrReferenceExpression) psiElement);
                }
            }
        }
    }
    if (!conflicts.isEmpty()) {
        ConflictsDialog conflictsDialog = new ConflictsDialog(project, conflicts, () -> execute(method, usagesToConvert));
        conflictsDialog.show();
        if (conflictsDialog.getExitCode() != DialogWrapper.OK_EXIT_CODE)
            return;
    }
    execute(method, usagesToConvert);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) MultiMap(com.intellij.util.containers.MultiMap) PsiField(com.intellij.psi.PsiField) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) PsiElement(com.intellij.psi.PsiElement) HashSet(com.intellij.util.containers.HashSet)

Example 74 with PsiField

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

the class ComponentTree method getData.

/**
   * Provides {@link PlatformDataKeys#NAVIGATABLE} to navigate to
   * binding of currently selected component (if any)
   */
public Object getData(final String dataId) {
    if (GuiEditor.DATA_KEY.is(dataId)) {
        return myEditor;
    }
    if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        return myDeleteProvider;
    }
    if (PlatformDataKeys.COPY_PROVIDER.is(dataId) || PlatformDataKeys.CUT_PROVIDER.is(dataId) || PlatformDataKeys.PASTE_PROVIDER.is(dataId)) {
        return myEditor == null ? null : myEditor.getData(dataId);
    }
    if (LW_INSPECTION_SUPPRESSION_ARRAY_DATA_KEY.is(dataId)) {
        Collection<LwInspectionSuppression> elements = getSelectedElements(LwInspectionSuppression.class);
        return elements.size() == 0 ? null : elements.toArray(new LwInspectionSuppression[elements.size()]);
    }
    if (PlatformDataKeys.HELP_ID.is(dataId)) {
        return ourHelpID;
    }
    if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
        return myFormEditor;
    }
    if (!CommonDataKeys.NAVIGATABLE.is(dataId)) {
        return null;
    }
    final RadComponent selectedComponent = getSelectedComponent();
    if (selectedComponent == null) {
        return null;
    }
    final String classToBind = myEditor.getRootContainer().getClassToBind();
    if (classToBind == null) {
        return null;
    }
    final PsiClass aClass = FormEditingUtil.findClassToBind(myEditor.getModule(), classToBind);
    if (aClass == null) {
        return null;
    }
    if (selectedComponent instanceof RadRootContainer) {
        return EditSourceUtil.getDescriptor(aClass);
    }
    final String binding = selectedComponent.getBinding();
    if (binding == null) {
        return null;
    }
    final PsiField[] fields = aClass.getFields();
    for (final PsiField field : fields) {
        if (binding.equals(field.getName())) {
            return EditSourceUtil.getDescriptor(field);
        }
    }
    return null;
}
Also used : LwInspectionSuppression(com.intellij.uiDesigner.lw.LwInspectionSuppression) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass)

Example 75 with PsiField

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

the class EncapsulateFieldsAction method isAvailableOnElementInEditorAndFile.

@Override
protected boolean isAvailableOnElementInEditorAndFile(@NotNull PsiElement element, @NotNull Editor editor, @NotNull PsiFile file, @NotNull DataContext context) {
    final PsiElement psiElement = file.findElementAt(editor.getCaretModel().getOffset());
    final PsiClass containingClass = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class, false);
    if (containingClass != null) {
        final PsiField[] fields = containingClass.getFields();
        for (PsiField field : fields) {
            if (isAcceptedField(field))
                return true;
        }
    }
    return false;
}
Also used : PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement)

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