Java 类com.intellij.psi.scope.processor.VariablesProcessor 实例源码

项目:intellij-ce-playground    文件:ReassignVariableUtil.java   
static VariablesProcessor findVariablesOfType(final PsiDeclarationStatement declaration, final PsiType type) {
  VariablesProcessor proc = new VariablesProcessor(false) {
    @Override
    protected boolean check(PsiVariable var, ResolveState state) {
      for (PsiElement element : declaration.getDeclaredElements()) {
        if (element == var) return false;
      }
      return TypeConversionUtil.isAssignable(var.getType(), type);
    }
  };
  PsiElement scope = declaration;
  while (scope != null) {
    if (scope instanceof PsiFile || 
        scope instanceof PsiMethod || 
        scope instanceof PsiLambdaExpression ||
        scope instanceof PsiClassInitializer) break;
    scope = scope.getParent();
  }
  if (scope == null) return proc;
  PsiScopesUtil.treeWalkUp(proc, declaration, scope);
  return proc;
}
项目:tools-idea    文件:ReassignVariableUtil.java   
static VariablesProcessor findVariablesOfType(final PsiDeclarationStatement declaration, final PsiType type) {
  VariablesProcessor proc = new VariablesProcessor(false) {
    @Override
    protected boolean check(PsiVariable var, ResolveState state) {
      for (PsiElement element : declaration.getDeclaredElements()) {
        if (element == var) return false;
      }
      return TypeConversionUtil.isAssignable(var.getType(), type);
    }
  };
  PsiElement scope = declaration;
  while (scope != null) {
    if (scope instanceof PsiFile || scope instanceof PsiMethod || scope instanceof PsiClassInitializer) break;
    scope = scope.getParent();
  }
  if (scope == null) return proc;
  PsiScopesUtil.treeWalkUp(proc, declaration, scope);
  return proc;
}
项目:consulo-java    文件:ReassignVariableUtil.java   
static VariablesProcessor findVariablesOfType(final PsiDeclarationStatement declaration, final PsiType type) {
  VariablesProcessor proc = new VariablesProcessor(false) {
    @Override
    protected boolean check(PsiVariable var, ResolveState state) {
      for (PsiElement element : declaration.getDeclaredElements()) {
        if (element == var) return false;
      }
      return TypeConversionUtil.isAssignable(var.getType(), type);
    }
  };
  PsiElement scope = declaration;
  while (scope != null) {
    if (scope instanceof PsiFile || scope instanceof PsiMethod || scope instanceof PsiClassInitializer) break;
    scope = scope.getParent();
  }
  if (scope == null) return proc;
  PsiScopesUtil.treeWalkUp(proc, declaration, scope);
  return proc;
}
项目:intellij-ce-playground    文件:ExtractMethodProcessor.java   
@Nullable
private PsiVariable getArtificialOutputVariable() {
  if (myOutputVariables.length == 0 && myExitStatements.isEmpty()) {
    if (myCanBeChainedConstructor) {
      final Set<PsiField> fields = new HashSet<PsiField>();
      for (PsiElement element : myElements) {
        element.accept(new JavaRecursiveElementWalkingVisitor() {
          @Override
          public void visitReferenceExpression(PsiReferenceExpression expression) {
            super.visitReferenceExpression(expression);
            final PsiElement resolve = expression.resolve();
            if (resolve instanceof PsiField && ((PsiField)resolve).hasModifierProperty(PsiModifier.FINAL) &&
                PsiUtil.isAccessedForWriting(expression)) {
              fields.add((PsiField)resolve);
            }
          }
        });
      }
      if (!fields.isEmpty()) {
        return fields.size() == 1 ? fields.iterator().next() : null;
      }
    }
    final VariablesProcessor processor = new VariablesProcessor(true) {
      @Override
      protected boolean check(PsiVariable var, ResolveState state) {
        return isDeclaredInside(var);
      }
    };
    PsiScopesUtil.treeWalkUp(processor, myElements[myElements.length - 1], myCodeFragmentMember);
    if (processor.size() == 1) {
      return processor.getResult(0);
    }
  }
  return null;
}
项目:intellij-ce-playground    文件:IntroduceVariableBase.java   
private static ExpressionOccurrenceManager createOccurrenceManager(PsiExpression expr, PsiElement tempContainer) {
  boolean skipForStatement = true;
  final PsiForStatement forStatement = PsiTreeUtil.getParentOfType(expr, PsiForStatement.class);
  if (forStatement != null) {
    final VariablesProcessor variablesProcessor = new VariablesProcessor(false) {
      @Override
      protected boolean check(PsiVariable var, ResolveState state) {
        return PsiTreeUtil.isAncestor(forStatement.getInitialization(), var, true);
      }
    };
    PsiScopesUtil.treeWalkUp(variablesProcessor, expr, null);
    skipForStatement = variablesProcessor.size() == 0;
  }

  PsiElement containerParent = tempContainer;
  PsiElement lastScope = tempContainer;
  while (true) {
    if (containerParent instanceof PsiFile) break;
    if (containerParent instanceof PsiMethod) break;
    if (containerParent instanceof PsiLambdaExpression) break;
    if (!skipForStatement && containerParent instanceof PsiForStatement) break;
    containerParent = containerParent.getParent();
    if (containerParent instanceof PsiCodeBlock) {
      lastScope = containerParent;
    }
  }

  return new ExpressionOccurrenceManager(expr, lastScope, NotInSuperCallOccurrenceFilter.INSTANCE);
}
项目:tools-idea    文件:IntroduceVariableBase.java   
private static ExpressionOccurrenceManager createOccurrenceManager(PsiExpression expr, PsiElement tempContainer) {
  boolean skipForStatement = true;
  final PsiForStatement forStatement = PsiTreeUtil.getParentOfType(expr, PsiForStatement.class);
  if (forStatement != null) {
    final VariablesProcessor variablesProcessor = new VariablesProcessor(false) {
      @Override
      protected boolean check(PsiVariable var, ResolveState state) {
        return PsiTreeUtil.isAncestor(forStatement.getInitialization(), var, true);
      }
    };
    PsiScopesUtil.treeWalkUp(variablesProcessor, expr, null);
    skipForStatement = variablesProcessor.size() == 0;
  }

  PsiElement containerParent = tempContainer;
  PsiElement lastScope = tempContainer;
  while (true) {
    if (containerParent instanceof PsiFile) break;
    if (containerParent instanceof PsiMethod) break;
    if (!skipForStatement && containerParent instanceof PsiForStatement) break;
    containerParent = containerParent.getParent();
    if (containerParent instanceof PsiCodeBlock) {
      lastScope = containerParent;
    }
  }

  return new ExpressionOccurrenceManager(expr, lastScope, NotInSuperCallOccurrenceFilter.INSTANCE);
}
项目:consulo-java    文件:ExtractMethodProcessor.java   
@Nullable
private PsiVariable getArtificialOutputVariable()
{
    if(myOutputVariables.length == 0 && myExitStatements.isEmpty())
    {
        if(myCanBeChainedConstructor)
        {
            final Set<PsiField> fields = new HashSet<PsiField>();
            for(PsiElement element : myElements)
            {
                element.accept(new JavaRecursiveElementWalkingVisitor()
                {
                    @Override
                    public void visitReferenceExpression(PsiReferenceExpression expression)
                    {
                        super.visitReferenceExpression(expression);
                        final PsiElement resolve = expression.resolve();
                        if(resolve instanceof PsiField && ((PsiField) resolve).hasModifierProperty(PsiModifier.FINAL) &&
                                PsiUtil.isAccessedForWriting(expression))
                        {
                            fields.add((PsiField) resolve);
                        }
                    }
                });
            }
            if(!fields.isEmpty())
            {
                return fields.size() == 1 ? fields.iterator().next() : null;
            }
        }
        final VariablesProcessor processor = new VariablesProcessor(true)
        {
            @Override
            protected boolean check(PsiVariable var, ResolveState state)
            {
                return isDeclaredInside(var);
            }
        };
        PsiScopesUtil.treeWalkUp(processor, myElements[myElements.length - 1], myCodeFragmentMember);
        if(processor.size() == 1)
        {
            return processor.getResult(0);
        }
    }
    return null;
}
项目:consulo-java    文件:IntroduceVariableBase.java   
private static ExpressionOccurrenceManager createOccurrenceManager(PsiExpression expr, PsiElement tempContainer)
{
    boolean skipForStatement = true;
    final PsiForStatement forStatement = PsiTreeUtil.getParentOfType(expr, PsiForStatement.class);
    if(forStatement != null)
    {
        final VariablesProcessor variablesProcessor = new VariablesProcessor(false)
        {
            @Override
            protected boolean check(PsiVariable var, ResolveState state)
            {
                return PsiTreeUtil.isAncestor(forStatement.getInitialization(), var, true);
            }
        };
        PsiScopesUtil.treeWalkUp(variablesProcessor, expr, null);
        skipForStatement = variablesProcessor.size() == 0;
    }

    PsiElement containerParent = tempContainer;
    PsiElement lastScope = tempContainer;
    while(true)
    {
        if(containerParent instanceof PsiFile)
        {
            break;
        }
        if(containerParent instanceof PsiMethod)
        {
            break;
        }
        if(containerParent instanceof PsiLambdaExpression)
        {
            break;
        }
        if(!skipForStatement && containerParent instanceof PsiForStatement)
        {
            break;
        }
        containerParent = containerParent.getParent();
        if(containerParent instanceof PsiCodeBlock)
        {
            lastScope = containerParent;
        }
    }

    return new ExpressionOccurrenceManager(expr, lastScope, NotInSuperCallOccurrenceFilter.INSTANCE);
}