Search in sources :

Example 1 with ElementNeedsThis

use of com.intellij.refactoring.util.classMembers.ElementNeedsThis in project intellij-community by JetBrains.

the class ExtractMethodProcessor method applyChosenClassAndExtract.

private boolean applyChosenClassAndExtract(List<PsiVariable> inputVariables, @Nullable Pass<ExtractMethodProcessor> extractPass) throws PrepareFailedException {
    myStatic = shouldBeStatic();
    final Set<PsiField> fields = new LinkedHashSet<>();
    if (!PsiUtil.isLocalOrAnonymousClass(myTargetClass) && (myTargetClass.getContainingClass() == null || myTargetClass.hasModifierProperty(PsiModifier.STATIC))) {
        boolean canBeStatic = true;
        if (myTargetClass.isInterface()) {
            final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(myCodeFragmentMember, PsiMethod.class, false);
            canBeStatic = containingMethod == null || containingMethod.hasModifierProperty(PsiModifier.STATIC);
        }
        if (canBeStatic) {
            ElementNeedsThis needsThis = new ElementNeedsThis(myTargetClass) {

                @Override
                protected void visitClassMemberReferenceElement(PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
                    if (classMember instanceof PsiField && !classMember.hasModifierProperty(PsiModifier.STATIC)) {
                        final PsiExpression expression = PsiTreeUtil.getParentOfType(classMemberReference, PsiExpression.class, false);
                        if (expression == null || !PsiUtil.isAccessedForWriting(expression)) {
                            fields.add((PsiField) classMember);
                            return;
                        }
                    }
                    super.visitClassMemberReferenceElement(classMember, classMemberReference);
                }
            };
            for (int i = 0; i < myElements.length && !needsThis.usesMembers(); i++) {
                PsiElement element = myElements[i];
                element.accept(needsThis);
            }
            myCanBeStatic = !needsThis.usesMembers();
        } else {
            myCanBeStatic = false;
        }
    } else {
        myCanBeStatic = false;
    }
    myInputVariables = new InputVariables(inputVariables, myProject, new LocalSearchScope(myElements), isFoldingApplicable());
    myInputVariables.setUsedInstanceFields(fields);
    if (!checkExitPoints()) {
        return false;
    }
    checkCanBeChainedConstructor();
    if (extractPass != null) {
        extractPass.pass(this);
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ElementNeedsThis(com.intellij.refactoring.util.classMembers.ElementNeedsThis)

Example 2 with ElementNeedsThis

use of com.intellij.refactoring.util.classMembers.ElementNeedsThis in project intellij-community by JetBrains.

the class AnonymousToInnerHandler method needsThis.

public boolean needsThis() {
    if (cachedNeedsThis == null) {
        ElementNeedsThis memberNeedsThis = new ElementNeedsThis(myTargetClass, myAnonClass);
        myAnonClass.accept(memberNeedsThis);
        class HasExplicitThis extends JavaRecursiveElementWalkingVisitor {

            boolean hasExplicitThis;

            @Override
            public void visitReferenceExpression(PsiReferenceExpression expression) {
            }

            @Override
            public void visitThisExpression(PsiThisExpression expression) {
                hasExplicitThis = true;
            }
        }
        final HasExplicitThis hasExplicitThis = new HasExplicitThis();
        PsiExpressionList argList = myAnonClass.getArgumentList();
        if (argList != null)
            argList.accept(hasExplicitThis);
        cachedNeedsThis = memberNeedsThis.usesMembers() || hasExplicitThis.hasExplicitThis;
    }
    return cachedNeedsThis.booleanValue();
}
Also used : ElementNeedsThis(com.intellij.refactoring.util.classMembers.ElementNeedsThis)

Aggregations

ElementNeedsThis (com.intellij.refactoring.util.classMembers.ElementNeedsThis)2 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1