Search in sources :

Example 1 with ReadWriteAccessDetector

use of com.intellij.codeInsight.highlighting.ReadWriteAccessDetector in project intellij-community by JetBrains.

the class IdentifierHighlighterPass method getUsages.

@NotNull
private static Couple<Collection<TextRange>> getUsages(@NotNull PsiElement target, PsiElement psiElement, boolean withDeclarations, boolean detectAccess) {
    List<TextRange> readRanges = new ArrayList<>();
    List<TextRange> writeRanges = new ArrayList<>();
    final ReadWriteAccessDetector detector = detectAccess ? ReadWriteAccessDetector.findDetector(target) : null;
    final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(target.getProject())).getFindUsagesManager();
    final FindUsagesHandler findUsagesHandler = findUsagesManager.getFindUsagesHandler(target, true);
    final LocalSearchScope scope = new LocalSearchScope(psiElement);
    Collection<PsiReference> refs = findUsagesHandler != null ? findUsagesHandler.findReferencesToHighlight(target, scope) : ReferencesSearch.search(target, scope).findAll();
    for (PsiReference psiReference : refs) {
        if (psiReference == null) {
            LOG.error("Null reference returned, findUsagesHandler=" + findUsagesHandler + "; target=" + target + " of " + target.getClass());
            continue;
        }
        List<TextRange> destination;
        if (detector == null || detector.getReferenceAccess(target, psiReference) == ReadWriteAccessDetector.Access.Read) {
            destination = readRanges;
        } else {
            destination = writeRanges;
        }
        HighlightUsagesHandler.collectRangesToHighlight(psiReference, destination);
    }
    if (withDeclarations) {
        final TextRange declRange = HighlightUsagesHandler.getNameIdentifierRange(psiElement.getContainingFile(), target);
        if (declRange != null) {
            if (detector != null && detector.isDeclarationWriteAccess(target)) {
                writeRanges.add(declRange);
            } else {
                readRanges.add(declRange);
            }
        }
    }
    return Couple.<Collection<TextRange>>of(readRanges, writeRanges);
}
Also used : FindManagerImpl(com.intellij.find.impl.FindManagerImpl) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) FindUsagesHandler(com.intellij.find.findUsages.FindUsagesHandler) ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector) TextRange(com.intellij.openapi.util.TextRange) FindUsagesManager(com.intellij.find.findUsages.FindUsagesManager) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ReadWriteAccessDetector

use of com.intellij.codeInsight.highlighting.ReadWriteAccessDetector in project intellij-community by JetBrains.

the class MoveJavaMemberHandler method checkConflictsOnUsage.

@Override
public void checkConflictsOnUsage(@NotNull MoveMembersProcessor.MoveMembersUsageInfo usageInfo, @Nullable PsiModifierList modifierListCopy, @NotNull PsiClass targetClass, @NotNull Set<PsiMember> membersToMove, @NotNull MoveMembersOptions moveMembersOptions, @NotNull MultiMap<PsiElement, String> conflicts) {
    final PsiElement element = usageInfo.getElement();
    if (element == null)
        return;
    final PsiMember member = usageInfo.member;
    if (element instanceof PsiReferenceExpression) {
        PsiExpression qualifier = ((PsiReferenceExpression) element).getQualifierExpression();
        PsiClass accessObjectClass = null;
        if (qualifier != null) {
            accessObjectClass = (PsiClass) PsiUtil.getAccessObjectClass(qualifier).getElement();
        }
        if (!JavaResolveUtil.isAccessible(member, targetClass, modifierListCopy, element, accessObjectClass, null)) {
            String newVisibility = moveMembersOptions.getExplicitMemberVisibility();
            String visibility = newVisibility != null ? newVisibility : VisibilityUtil.getVisibilityStringToDisplay(member);
            String message = RefactoringBundle.message("0.with.1.visibility.is.not.accessible.from.2", RefactoringUIUtil.getDescription(member, false), visibility, RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
            conflicts.putValue(member, CommonRefactoringUtil.capitalize(message));
        }
    }
    if (member instanceof PsiField && targetClass.isInterface()) {
        ReadWriteAccessDetector accessDetector = ReadWriteAccessDetector.findDetector(member);
        if (accessDetector != null) {
            ReadWriteAccessDetector.Access access = accessDetector.getExpressionAccess(element);
            if (access != ReadWriteAccessDetector.Access.Read) {
                String message = RefactoringUIUtil.getDescription(member, true) + " has write access but is moved to an interface";
                conflicts.putValue(element, CommonRefactoringUtil.capitalize(message));
            }
        }
    } else if (member instanceof PsiField && usageInfo.reference instanceof PsiExpression && member.hasModifierProperty(PsiModifier.FINAL) && PsiUtil.isAccessedForWriting((PsiExpression) usageInfo.reference) && !RefactoringHierarchyUtil.willBeInTargetClass(usageInfo.reference, membersToMove, targetClass, true)) {
        conflicts.putValue(usageInfo.member, "final variable initializer won't be available after move.");
    }
    if (toBeConvertedToEnum(moveMembersOptions, member, targetClass) && !isEnumAcceptable(element, targetClass)) {
        conflicts.putValue(element, "Enum type won't be applicable in the current context");
    }
    final PsiReference reference = usageInfo.getReference();
    if (reference != null) {
        RefactoringConflictsUtil.checkAccessibilityConflicts(reference, member, modifierListCopy, targetClass, membersToMove, conflicts);
    }
}
Also used : ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)

Example 3 with ReadWriteAccessDetector

use of com.intellij.codeInsight.highlighting.ReadWriteAccessDetector in project intellij-community by JetBrains.

the class RefCountHolder method getAccess.

private static ReadWriteAccessDetector.Access getAccess(@NotNull PsiReference ref, @NotNull PsiElement resolved) {
    PsiElement start = resolved.getLanguage() == ref.getElement().getLanguage() ? resolved : ref.getElement();
    ReadWriteAccessDetector detector = ReadWriteAccessDetector.findDetector(start);
    if (detector != null) {
        return detector.getReferenceAccess(resolved, ref);
    }
    return null;
}
Also used : ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)

Example 4 with ReadWriteAccessDetector

use of com.intellij.codeInsight.highlighting.ReadWriteAccessDetector in project intellij-community by JetBrains.

the class JavaIntroduceParameterObjectDelegate method collectInternalUsages.

@Override
public <M1 extends PsiNamedElement, P1 extends ParameterInfo> ReadWriteAccessDetector.Access collectInternalUsages(Collection<FixableUsageInfo> usages, PsiMethod overridingMethod, IntroduceParameterObjectClassDescriptor<M1, P1> classDescriptor, P1 parameterInfo, String mergedParamName) {
    final LocalSearchScope localSearchScope = new LocalSearchScope(overridingMethod);
    final PsiParameter[] params = overridingMethod.getParameterList().getParameters();
    final PsiParameter parameter = params[parameterInfo.getOldIndex()];
    final ReadWriteAccessDetector detector = ReadWriteAccessDetector.findDetector(parameter);
    assert detector != null;
    final String setter = classDescriptor.getSetterName(parameterInfo, overridingMethod);
    final String getter = classDescriptor.getGetterName(parameterInfo, overridingMethod);
    final ReadWriteAccessDetector.Access[] accessor = new ReadWriteAccessDetector.Access[] { null };
    ReferencesSearch.search(parameter, localSearchScope).forEach(reference -> {
        final PsiElement refElement = reference.getElement();
        if (refElement instanceof PsiReferenceExpression) {
            final PsiReferenceExpression paramUsage = (PsiReferenceExpression) refElement;
            final ReadWriteAccessDetector.Access access = detector.getExpressionAccess(refElement);
            if (access == ReadWriteAccessDetector.Access.Read) {
                usages.add(new ReplaceParameterReferenceWithCall(paramUsage, mergedParamName, getter));
                if (accessor[0] == null) {
                    accessor[0] = ReadWriteAccessDetector.Access.Read;
                }
            } else {
                if (access == ReadWriteAccessDetector.Access.ReadWrite) {
                    usages.add(new ReplaceParameterIncrementDecrement(paramUsage, mergedParamName, setter, getter));
                } else {
                    usages.add(new ReplaceParameterAssignmentWithCall(paramUsage, mergedParamName, setter, getter));
                }
                accessor[0] = ReadWriteAccessDetector.Access.Write;
            }
        }
        return true;
    });
    return accessor[0];
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReadWriteAccessDetector(com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)

Aggregations

ReadWriteAccessDetector (com.intellij.codeInsight.highlighting.ReadWriteAccessDetector)4 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)2 FindUsagesHandler (com.intellij.find.findUsages.FindUsagesHandler)1 FindUsagesManager (com.intellij.find.findUsages.FindUsagesManager)1 FindManagerImpl (com.intellij.find.impl.FindManagerImpl)1 TextRange (com.intellij.openapi.util.TextRange)1 NotNull (org.jetbrains.annotations.NotNull)1