Search in sources :

Example 6 with VariablesProcessor

use of com.intellij.psi.scope.processor.VariablesProcessor in project intellij-community by JetBrains.

the class JavaChangeSignatureUsageProcessor method createDefaultValue.

@Nullable
private static PsiExpression createDefaultValue(JavaChangeInfo changeInfo, final PsiElementFactory factory, final JavaParameterInfo info, final PsiExpressionList list, PsiSubstitutor substitutor) throws IncorrectOperationException {
    if (info.isUseAnySingleVariable()) {
        final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(list.getProject()).getResolveHelper();
        final PsiType type = info.getTypeWrapper().getType(changeInfo.getMethod(), list.getManager());
        final VariablesProcessor processor = new VariablesProcessor(false) {

            @Override
            protected boolean check(PsiVariable var, ResolveState state) {
                if (var instanceof PsiField && !resolveHelper.isAccessible((PsiField) var, list, null))
                    return false;
                if (var instanceof PsiLocalVariable && list.getTextRange().getStartOffset() <= var.getTextRange().getStartOffset())
                    return false;
                if (PsiTreeUtil.isAncestor(var, list, false))
                    return false;
                final PsiType varType = state.get(PsiSubstitutor.KEY).substitute(var.getType());
                return type.isAssignableFrom(varType);
            }

            @Override
            public boolean execute(@NotNull PsiElement pe, @NotNull ResolveState state) {
                super.execute(pe, state);
                return size() < 2;
            }
        };
        PsiScopesUtil.treeWalkUp(processor, list, null);
        if (processor.size() == 1) {
            final PsiVariable result = processor.getResult(0);
            return factory.createExpressionFromText(result.getName(), list);
        }
        if (processor.size() == 0) {
            final PsiClass parentClass = PsiTreeUtil.getParentOfType(list, PsiClass.class);
            if (parentClass != null) {
                PsiClass containingClass = parentClass;
                final Set<PsiClass> containingClasses = new HashSet<>();
                while (containingClass != null) {
                    if (type.isAssignableFrom(factory.createType(containingClass, PsiSubstitutor.EMPTY))) {
                        containingClasses.add(containingClass);
                    }
                    containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class);
                }
                if (containingClasses.size() == 1) {
                    return RefactoringChangeUtil.createThisExpression(parentClass.getManager(), containingClasses.contains(parentClass) ? null : containingClasses.iterator().next());
                }
            }
        }
    }
    final PsiCallExpression callExpression = PsiTreeUtil.getParentOfType(list, PsiCallExpression.class);
    final String defaultValue = info.getDefaultValue();
    return callExpression != null ? (PsiExpression) info.getActualValue(callExpression, substitutor) : !StringUtil.isEmpty(defaultValue) ? factory.createExpressionFromText(defaultValue, list) : null;
}
Also used : VariablesProcessor(com.intellij.psi.scope.processor.VariablesProcessor) NotNull(org.jetbrains.annotations.NotNull) HashSet(com.intellij.util.containers.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with VariablesProcessor

use of com.intellij.psi.scope.processor.VariablesProcessor in project intellij-community by JetBrains.

the class GenerateDelegateHandler method collectTargetsInClass.

private static void collectTargetsInClass(PsiElement element, final PsiClass targetClass, final PsiClass aClass, List<PsiElementClassMember> result) {
    final PsiField[] fields = aClass.getAllFields();
    PsiResolveHelper helper = JavaPsiFacade.getInstance(aClass.getProject()).getResolveHelper();
    for (PsiField field : fields) {
        final PsiType type = field.getType();
        if (helper.isAccessible(field, aClass, aClass) && type instanceof PsiClassType && !(PsiTreeUtil.isAncestor(field, element, false) && targetClass != aClass)) {
            final PsiClass containingClass = field.getContainingClass();
            if (containingClass != null) {
                result.add(new PsiFieldMember(field, TypeConversionUtil.getSuperClassSubstitutor(containingClass, aClass, PsiSubstitutor.EMPTY)));
            }
        }
    }
    final PsiMethod[] methods = aClass.getAllMethods();
    for (PsiMethod method : methods) {
        final PsiClass containingClass = method.getContainingClass();
        if (containingClass == null || CommonClassNames.JAVA_LANG_OBJECT.equals(containingClass.getQualifiedName()))
            continue;
        final PsiType returnType = method.getReturnType();
        if (returnType != null && PropertyUtil.isSimplePropertyGetter(method) && helper.isAccessible(method, aClass, aClass) && returnType instanceof PsiClassType && !(PsiTreeUtil.isAncestor(method, element, false) && targetClass != aClass)) {
            result.add(new PsiMethodMember(method, TypeConversionUtil.getSuperClassSubstitutor(containingClass, aClass, PsiSubstitutor.EMPTY)));
        }
    }
    if (aClass instanceof PsiAnonymousClass) {
        VariablesProcessor proc = new VariablesProcessor(false) {

            @Override
            protected boolean check(PsiVariable var, ResolveState state) {
                return var.hasModifierProperty(PsiModifier.FINAL) && var instanceof PsiLocalVariable || var instanceof PsiParameter;
            }
        };
        PsiElement scope = aClass;
        while (scope != null) {
            if (scope instanceof PsiFile || scope instanceof PsiMethod || scope instanceof PsiClassInitializer)
                break;
            scope = scope.getParent();
        }
        if (scope != null) {
            PsiScopesUtil.treeWalkUp(proc, aClass, scope);
            for (int i = 0; i < proc.size(); i++) {
                final PsiVariable psiVariable = proc.getResult(i);
                final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(aClass.getProject());
                final PsiType type = psiVariable.getType();
                if (LambdaUtil.notInferredType(type)) {
                    continue;
                }
                result.add(new PsiFieldMember(elementFactory.createField(psiVariable.getName(), type instanceof PsiEllipsisType ? ((PsiEllipsisType) type).toArrayType() : type)) {

                    @Override
                    protected PsiClass getContainingClass() {
                        return aClass;
                    }
                });
            }
        }
    }
}
Also used : VariablesProcessor(com.intellij.psi.scope.processor.VariablesProcessor)

Aggregations

VariablesProcessor (com.intellij.psi.scope.processor.VariablesProcessor)7 Nullable (org.jetbrains.annotations.Nullable)4 HashSet (com.intellij.util.containers.HashSet)2 NotNull (org.jetbrains.annotations.NotNull)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Shortcut (com.intellij.openapi.actionSystem.Shortcut)1 VisualPosition (com.intellij.openapi.editor.VisualPosition)1 Keymap (com.intellij.openapi.keymap.Keymap)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 JBList (com.intellij.ui.components.JBList)1 ArrayList (java.util.ArrayList)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)1