Java 类com.intellij.psi.PsiForeachStatement 实例源码

项目:manifold-ij    文件:VarHandler.java   
private PsiType processForeach( PsiElement parentDeclarationScope )
{
  PsiType result = null;
  if( parentDeclarationScope instanceof PsiForeachStatement )
  {
    final PsiForeachStatement foreachStatement = (PsiForeachStatement)parentDeclarationScope;
    final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
    if( iteratedValue != null )
    {
      result = JavaGenericsUtil.getCollectionItemType( iteratedValue );
    }
  }
  return result;
}
项目:tools-idea    文件:ParameterNamingConventionInspection.java   
@Override
public void visitParameter(@NotNull PsiParameter variable) {
  final PsiElement scope = variable.getDeclarationScope();
  if (scope instanceof PsiCatchSection ||
      scope instanceof PsiForeachStatement) {
    return;
  }
  final String name = variable.getName();
  if (name == null || isValid(name)) {
    return;
  }
  registerVariableError(variable, name);
}
项目:lombok-intellij-plugin    文件:ValProcessor.java   
public void verifyParameter(@NotNull final PsiParameter psiParameter, @NotNull final ProblemsHolder holder) {
  final PsiTypeElement typeElement = psiParameter.getTypeElement();
  boolean isVal = null != typeElement && isSameName(typeElement.getText());
  boolean isVar = null != typeElement && isVar(typeElement.getText());
  if (isVar || isVal) {
    PsiElement scope = psiParameter.getDeclarationScope();
    boolean isForeachStatement = scope instanceof PsiForeachStatement;
    boolean isForStatement = scope instanceof PsiForStatement;
    if (isVal && !isForeachStatement) {
      holder.registerProblem(psiParameter, "'val' works only on local variables and on foreach loops", ProblemHighlightType.ERROR);
    } else if (isVar && !(isForeachStatement || isForStatement)) {
      holder.registerProblem(psiParameter, "'var' works only on local variables and on for/foreach loops", ProblemHighlightType.ERROR);
    }
  }
}
项目:lombok-intellij-plugin    文件:ValProcessor.java   
private PsiType processParameterDeclaration(PsiElement parentDeclarationScope) {
  PsiType result = null;
  if (parentDeclarationScope instanceof PsiForeachStatement) {
    final PsiForeachStatement foreachStatement = (PsiForeachStatement) parentDeclarationScope;
    final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
    if (iteratedValue != null) {
      result = JavaGenericsUtil.getCollectionItemType(iteratedValue);
    }
  }
  return result;
}
项目:consulo-java    文件:MissingForeachBodyFixer.java   
@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException
{
    PsiForeachStatement forStatement = getForeachStatementParent(psiElement);
    if(forStatement == null)
    {
        return;
    }

    final Document doc = editor.getDocument();

    PsiElement body = forStatement.getBody();
    if(body instanceof PsiBlockStatement)
    {
        return;
    }
    if(body != null && startLine(doc, body) == startLine(doc, forStatement))
    {
        return;
    }

    PsiElement eltToInsertAfter = forStatement.getRParenth();
    String text = "{}";
    if(eltToInsertAfter == null)
    {
        eltToInsertAfter = forStatement;
        text = "){}";
    }
    doc.insertString(eltToInsertAfter.getTextRange().getEndOffset(), text);
}
项目:consulo-java    文件:MissingForeachBodyFixer.java   
private static PsiForeachStatement getForeachStatementParent(PsiElement psiElement)
{
    PsiForeachStatement statement = PsiTreeUtil.getParentOfType(psiElement, PsiForeachStatement.class);
    if(statement == null)
    {
        return null;
    }

    PsiExpression iterated = statement.getIteratedValue();
    PsiParameter parameter = statement.getIterationParameter();

    return PsiTreeUtil.isAncestor(iterated, psiElement, false) || PsiTreeUtil.isAncestor(parameter, psiElement, false) ? statement : null;
}
项目:consulo-java    文件:ParameterNamingConventionInspection.java   
@Override
public void visitParameter(@NotNull PsiParameter variable) {
  final PsiElement scope = variable.getDeclarationScope();
  if (scope instanceof PsiCatchSection ||
      scope instanceof PsiForeachStatement) {
    return;
  }
  final String name = variable.getName();
  if (name == null || isValid(name)) {
    return;
  }
  registerVariableError(variable, name);
}
项目:manifold-ij    文件:VarHandler.java   
private boolean isVarForEach( @NotNull PsiParameter psiParameter )
{
  return psiParameter.getParent() instanceof PsiForeachStatement && isVar( psiParameter.getTypeElement().getText() );
}
项目:Android_Lint_SRP_Practice_Example    文件:PsiClassStructureDetector.java   
@Override
public void visitForeachStatement(PsiForeachStatement statement) {
    mVisitor.report("PsiForeachStatement", statement.getText(), statement);
    super.visitStatement(statement);
}
项目:intellij-ce-playground    文件:ReplaceForEachLoopWithOptimizedIndexedForLoopIntention.java   
@Override
protected void createForLoopDeclaration(PsiForeachStatement statement,
                                        PsiExpression iteratedValue,
                                        boolean isArray,
                                        String iteratedValueText,
                                        StringBuilder newStatement, final String indexText) {

  final String lengthText;
  if (isArray) {
    lengthText = createVariableName(iteratedValueText + "Length", PsiType.INT, statement);
  }
  else {
    lengthText = createVariableName(iteratedValueText + "Size", PsiType.INT, statement);
  }

  newStatement.append("for(int ");
  newStatement.append(indexText);
  newStatement.append(" = 0, ");
  newStatement.append(lengthText);
  newStatement.append(" = ");
  if (iteratedValue instanceof PsiTypeCastExpression) {
    newStatement.append('(');
    newStatement.append(iteratedValueText);
    newStatement.append(')');
  }
  else {
    newStatement.append(iteratedValueText);
  }
  if (isArray) {
    newStatement.append(".length;");
  }
  else {
    newStatement.append(".size();");
  }
  newStatement.append(indexText);
  newStatement.append('<');
  newStatement.append(lengthText);
  newStatement.append(';');
  newStatement.append(indexText);
  newStatement.append("++)");
  newStatement.append("{ ");
}
项目:tools-idea    文件:ReplaceForEachLoopWithOptimizedIndexedForLoopIntention.java   
@Override
protected void createForLoopDeclaration(PsiForeachStatement statement,
                                        PsiExpression iteratedValue,
                                        boolean isArray,
                                        String iteratedValueText,
                                        StringBuilder newStatement, final String indexText) {

  final String lengthText;
  if (isArray) {
    lengthText = createVariableName(iteratedValueText + "Length", PsiType.INT, statement);
  }
  else {
    lengthText = createVariableName(iteratedValueText + "Size", PsiType.INT, statement);
  }

  newStatement.append("for(int ");
  newStatement.append(indexText);
  newStatement.append(" = 0, ");
  newStatement.append(lengthText);
  newStatement.append(" = ");
  if (iteratedValue instanceof PsiTypeCastExpression) {
    newStatement.append('(');
    newStatement.append(iteratedValueText);
    newStatement.append(')');
  }
  else {
    newStatement.append(iteratedValueText);
  }
  if (isArray) {
    newStatement.append(".length;");
  }
  else {
    newStatement.append(".size();");
  }
  newStatement.append(indexText);
  newStatement.append('<');
  newStatement.append(lengthText);
  newStatement.append(';');
  newStatement.append(indexText);
  newStatement.append("++)");
  newStatement.append("{ ");
}
项目:lombok-intellij-plugin    文件:ValProcessor.java   
private boolean isValOrVarForEach(@NotNull PsiParameter psiParameter) {
  return psiParameter.getParent() instanceof PsiForeachStatement && isValOrVar(psiParameter.getTypeElement().getText());
}
项目:consulo-java    文件:ReplaceForEachLoopWithOptimizedIndexedForLoopIntention.java   
@Override
protected void createForLoopDeclaration(PsiForeachStatement statement,
                                        PsiExpression iteratedValue,
                                        boolean isArray,
                                        String iteratedValueText,
                                        StringBuilder newStatement, final String indexText) {

  final String lengthText;
  if (isArray) {
    lengthText = createVariableName(iteratedValueText + "Length", PsiType.INT, statement);
  }
  else {
    lengthText = createVariableName(iteratedValueText + "Size", PsiType.INT, statement);
  }

  newStatement.append("for(int ");
  newStatement.append(indexText);
  newStatement.append(" = 0, ");
  newStatement.append(lengthText);
  newStatement.append(" = ");
  if (iteratedValue instanceof PsiTypeCastExpression) {
    newStatement.append('(');
    newStatement.append(iteratedValueText);
    newStatement.append(')');
  }
  else {
    newStatement.append(iteratedValueText);
  }
  if (isArray) {
    newStatement.append(".length;");
  }
  else {
    newStatement.append(".size();");
  }
  newStatement.append(indexText);
  newStatement.append('<');
  newStatement.append(lengthText);
  newStatement.append(';');
  newStatement.append(indexText);
  newStatement.append("++)");
  newStatement.append("{ ");
}