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

项目:rxlint    文件:DanglingSubscriptionDetector.java   
@Override
public void visitMethod(JavaContext context, JavaElementVisitor visitor, PsiMethodCallExpression call, PsiMethod method) {
    super.visitMethod(context, visitor, call, method);
    if (isRxSubscribeableClass(method.getContainingClass()) && !PsiType.VOID.equals(method.getReturnType())) {
        PsiElement element = LintUtils.skipParentheses(call.getParent());
        if (element instanceof PsiExpressionStatement) {
            String message;
            if (isRx2(method.getContainingClass())) {
                message = "No reference to the disposable is kept";
            } else {
                message = "No reference to the subscription is kept";
            }
            context.report(ISSUE, call, context.getLocation(call), message);
        }
    }
}
项目:intellij-ce-playground    文件:ResultOfObjectAllocationIgnoredInspection.java   
@Override
public void visitExpressionStatement(@NotNull PsiExpressionStatement statement) {
  super.visitExpressionStatement(statement);
  final PsiExpression expression = statement.getExpression();
  if (!(expression instanceof PsiNewExpression)) {
    return;
  }
  final PsiNewExpression newExpression = (PsiNewExpression)expression;
  final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions();
  if (arrayDimensions.length != 0) {
    return;
  }
  if (newExpression.getArrayInitializer() != null) {
    return;
  }
  registerNewExpressionError(newExpression);
}
项目:mparticle-android-sdk    文件:MpApiDetector.java   
private PsiMethodCallExpression findMethodCall(PsiElement element) {
    // This covers the case if there is a method being used to initialize a variable..
    // i.e int a = random();
    if (element instanceof PsiDeclarationStatement) {
        PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) element;
        for (PsiElement declarationElement : declarationStatement.getDeclaredElements()) {
            if (declarationElement instanceof PsiVariable) {
                PsiVariable variable = (PsiVariable) declarationElement;
                PsiExpression initializer = variable.getInitializer();
                if (initializer instanceof PsiMethodCallExpression) {
                    return (PsiMethodCallExpression) initializer;
                }
            }
        }
    }
    if (element instanceof PsiExpressionStatement) {
        PsiExpression expression = ((PsiExpressionStatement) element).getExpression();
        if (expression instanceof PsiMethodCallExpression) {
            return (PsiMethodCallExpression) expression;
        }
    }
    return null;
}
项目:tools-idea    文件:ResultOfObjectAllocationIgnoredInspection.java   
@Override
public void visitExpressionStatement(@NotNull PsiExpressionStatement statement) {
  super.visitExpressionStatement(statement);
  final PsiExpression expression = statement.getExpression();
  if (!(expression instanceof PsiNewExpression)) {
    return;
  }
  final PsiNewExpression newExpression = (PsiNewExpression)expression;
  final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions();
  if (arrayDimensions.length != 0) {
    return;
  }
  if (newExpression.getArrayInitializer() != null) {
    return;
  }
  registerNewExpressionError(newExpression);
}
项目:lombok-intellij-plugin    文件:LombokInlineMethodHandler.java   
private boolean isChainingConstructor(PsiMethod constructor) {
  PsiCodeBlock body = constructor.getBody();
  if (body != null) {
    PsiStatement[] statements = body.getStatements();
    if (statements.length == 1 && statements[0] instanceof PsiExpressionStatement) {
      PsiExpression expression = ((PsiExpressionStatement) statements[0]).getExpression();
      if (expression instanceof PsiMethodCallExpression) {
        PsiReferenceExpression methodExpr = ((PsiMethodCallExpression) expression).getMethodExpression();
        if ("this".equals(methodExpr.getReferenceName())) {
          PsiElement resolved = methodExpr.resolve();
          return resolved instanceof PsiMethod && ((PsiMethod) resolved).isConstructor(); //delegated via "this" call
        }
      }
    }
  }
  return false;
}
项目:consulo-java    文件:DeleteSideEffectsAwareFix.java   
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException
{
    PsiExpressionStatement statement = myPointer.getElement();
    if(statement == null)
    {
        return;
    }
    PsiExpression expression = statement.getExpression();
    List<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(expression);
    PsiStatement[] statements = StatementExtractor.generateStatements(sideEffects, expression);
    if(statements.length > 0)
    {
        BlockUtils.addBefore(statement, statements);
    }
    statement.delete();
}
项目:consulo-java    文件:ResultOfObjectAllocationIgnoredInspection.java   
@Override
public void visitExpressionStatement(@NotNull PsiExpressionStatement statement) {
  super.visitExpressionStatement(statement);
  final PsiExpression expression = statement.getExpression();
  if (!(expression instanceof PsiNewExpression)) {
    return;
  }
  final PsiNewExpression newExpression = (PsiNewExpression)expression;
  final PsiExpression[] arrayDimensions = newExpression.getArrayDimensions();
  if (arrayDimensions.length != 0) {
    return;
  }
  if (newExpression.getArrayInitializer() != null) {
    return;
  }
  registerNewExpressionError(newExpression);
}
项目:consulo-java    文件:TrivialIfInspection.java   
private static void replaceSimplifiableAssignment(
  PsiIfStatement statement)
  throws IncorrectOperationException {
  final PsiExpression condition = statement.getCondition();
  if (condition == null) {
    return;
  }
  final String conditionText = condition.getText();
  final PsiStatement thenBranch = statement.getThenBranch();
  final PsiExpressionStatement assignmentStatement =
    (PsiExpressionStatement)
      ControlFlowUtils.stripBraces(thenBranch);
  final PsiAssignmentExpression assignmentExpression =
    (PsiAssignmentExpression)assignmentStatement.getExpression();
  final PsiJavaToken operator =
    assignmentExpression.getOperationSign();
  final String operand = operator.getText();
  final PsiExpression lhs = assignmentExpression.getLExpression();
  final String lhsText = lhs.getText();
  replaceStatement(statement,
                   lhsText + operand + conditionText + ';');
}
项目:consulo-java    文件:TrivialIfInspection.java   
private static void replaceSimplifiableAssignmentNegated(
  PsiIfStatement statement)
  throws IncorrectOperationException {
  final PsiExpression condition = statement.getCondition();
  if (condition == null) {
    return;
  }
  final String conditionText =
    BoolUtils.getNegatedExpressionText(condition);
  final PsiStatement thenBranch = statement.getThenBranch();
  final PsiExpressionStatement assignmentStatement =
    (PsiExpressionStatement)
      ControlFlowUtils.stripBraces(thenBranch);
  final PsiAssignmentExpression assignmentExpression =
    (PsiAssignmentExpression)
      assignmentStatement.getExpression();
  final PsiJavaToken operator =
    assignmentExpression.getOperationSign();
  final String operand = operator.getText();
  final PsiExpression lhs = assignmentExpression.getLExpression();
  final String lhsText = lhs.getText();
  replaceStatement(statement,
                   lhsText + operand + conditionText + ';');
}
项目:intellij-ce-playground    文件:ExpressionHandler.java   
public boolean match(PsiElement patternNode, PsiElement matchedNode, MatchContext context) {
  if (!super.match(patternNode,matchedNode,context)) {
    return false;
  }

  return context.getMatcher().match(
    ((PsiExpressionStatement)patternNode).getExpression(),
    matchedNode
  );
}
项目:intellij-ce-playground    文件:CallToSimpleGetterInClassInspection.java   
@Override
protected void inline(Project project, PsiReferenceExpression methodExpression, PsiMethod method) {
  final PsiElement statement = methodExpression.getParent().getParent();
  if (statement instanceof PsiExpressionStatement) {
    statement.delete();
  } else {
    super.inline(project, methodExpression, method);
  }
}
项目:tools-idea    文件:NestedAssignmentInspection.java   
@Override
public void visitAssignmentExpression(
  @NotNull PsiAssignmentExpression expression) {
  super.visitAssignmentExpression(expression);
  final PsiElement parent = expression.getParent();
  if (parent == null) {
    return;
  }
  final PsiElement grandparent = parent.getParent();
  if (parent instanceof PsiExpressionStatement ||
      grandparent instanceof PsiExpressionListStatement) {
    return;
  }
  registerError(expression);
}
项目:consulo-java    文件:DeleteSideEffectsAwareFix.java   
public DeleteSideEffectsAwareFix(PsiExpressionStatement statement)
{
    myPointer = SmartPointerManager.getInstance(statement.getProject()).createSmartPsiElementPointer(statement);
    PsiExpression expression = statement.getExpression();
    List<PsiExpression> sideEffects = SideEffectChecker.extractSideEffectExpressions(expression);
    if(sideEffects.isEmpty())
    {
        myMessage = JavaQuickFixBundle.message("delete.element.fix.text");
    }
    else if(sideEffects.size() == 1 && sideEffects.get(0) == PsiUtil.skipParenthesizedExprDown(expression))
    {
        // "Remove unnecessary parentheses" action is already present which will do the same
        myMessage = "";
    }
    else
    {
        PsiStatement[] statements = StatementExtractor.generateStatements(sideEffects, expression);
        if(statements.length == 1)
        {
            if(statements[0] instanceof PsiIfStatement)
            {
                myMessage = JavaQuickFixBundle.message("extract.side.effects.convert.to.if");
            }
            else
            {
                myMessage = JavaQuickFixBundle.message("extract.side.effects.single");
            }
        }
        else
        {
            myMessage = JavaQuickFixBundle.message("extract.side.effects.multiple");
        }
    }
}
项目:consulo-java    文件:NestedAssignmentInspection.java   
@Override
public void visitAssignmentExpression(
  @NotNull PsiAssignmentExpression expression) {
  super.visitAssignmentExpression(expression);
  final PsiElement parent = expression.getParent();
  if (parent == null) {
    return;
  }
  final PsiElement grandparent = parent.getParent();
  if (parent instanceof PsiExpressionStatement ||
      grandparent instanceof PsiExpressionListStatement) {
    return;
  }
  registerError(expression);
}
项目:consulo-java    文件:TrivialIfInspection.java   
private static void replaceSimplifiableImplicitAssignment(
  PsiIfStatement statement) throws IncorrectOperationException {
  final PsiElement prevStatement =
    PsiTreeUtil.skipSiblingsBackward(statement,
                                     PsiWhiteSpace.class);
  if (prevStatement == null) {
    return;
  }
  final PsiExpression condition = statement.getCondition();
  if (condition == null) {
    return;
  }
  final String conditionText = condition.getText();
  final PsiStatement thenBranch = statement.getThenBranch();
  final PsiExpressionStatement assignmentStatement =
    (PsiExpressionStatement)
      ControlFlowUtils.stripBraces(thenBranch);
  final PsiAssignmentExpression assignmentExpression =
    (PsiAssignmentExpression)assignmentStatement.getExpression();
  final PsiJavaToken operator =
    assignmentExpression.getOperationSign();
  final String operand = operator.getText();
  final PsiExpression lhs = assignmentExpression.getLExpression();
  final String lhsText = lhs.getText();
  replaceStatement(statement,
                   lhsText + operand + conditionText + ';');
  deleteElement(prevStatement);
}
项目:consulo-java    文件:TrivialIfInspection.java   
public static boolean isSimplifiableAssignment(PsiIfStatement ifStatement) {
  PsiStatement thenBranch = ifStatement.getThenBranch();
  thenBranch = ControlFlowUtils.stripBraces(thenBranch);
  PsiStatement elseBranch = ifStatement.getElseBranch();
  elseBranch = ControlFlowUtils.stripBraces(elseBranch);
  if (ConditionalUtils.isAssignment(thenBranch, PsiKeyword.TRUE) &&
      ConditionalUtils.isAssignment(elseBranch, PsiKeyword.FALSE)) {
    final PsiExpressionStatement thenExpressionStatement =
      (PsiExpressionStatement)thenBranch;
    final PsiAssignmentExpression thenExpression =
      (PsiAssignmentExpression)
        thenExpressionStatement.getExpression();
    final PsiExpressionStatement elseExpressionStatement =
      (PsiExpressionStatement)elseBranch;
    final PsiAssignmentExpression elseExpression =
      (PsiAssignmentExpression)
        elseExpressionStatement.getExpression();
    final IElementType thenTokenType = thenExpression.getOperationTokenType();
    if (!thenTokenType.equals(elseExpression.getOperationTokenType())) {
      return false;
    }
    final PsiExpression thenLhs = thenExpression.getLExpression();
    final PsiExpression elseLhs = elseExpression.getLExpression();
    return EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(thenLhs,
                                                       elseLhs);
  }
  else {
    return false;
  }
}
项目:consulo-java    文件:TrivialIfInspection.java   
public static boolean isSimplifiableAssignmentNegated(
  PsiIfStatement ifStatement) {
  PsiStatement thenBranch = ifStatement.getThenBranch();
  thenBranch = ControlFlowUtils.stripBraces(thenBranch);
  PsiStatement elseBranch = ifStatement.getElseBranch();
  elseBranch = ControlFlowUtils.stripBraces(elseBranch);
  if (ConditionalUtils.isAssignment(thenBranch, PsiKeyword.FALSE) &&
      ConditionalUtils.isAssignment(elseBranch, PsiKeyword.TRUE)) {
    final PsiExpressionStatement thenExpressionStatement =
      (PsiExpressionStatement)thenBranch;
    final PsiAssignmentExpression thenExpression =
      (PsiAssignmentExpression)
        thenExpressionStatement.getExpression();
    final PsiExpressionStatement elseExpressionStatement =
      (PsiExpressionStatement)elseBranch;
    final PsiAssignmentExpression elseExpression =
      (PsiAssignmentExpression)
        elseExpressionStatement.getExpression();
    final IElementType thenTokenType = thenExpression.getOperationTokenType();
    if (!thenTokenType.equals(elseExpression.getOperationTokenType())) {
      return false;
    }
    final PsiExpression thenLhs = thenExpression.getLExpression();
    final PsiExpression elseLhs = elseExpression.getLExpression();
    return EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(thenLhs,
                                                       elseLhs);
  }
  else {
    return false;
  }
}
项目:consulo-java    文件:TrivialIfInspection.java   
public static boolean isSimplifiableImplicitAssignment(
  PsiIfStatement ifStatement) {
  if (ifStatement.getElseBranch() != null) {
    return false;
  }
  PsiStatement thenBranch = ifStatement.getThenBranch();
  thenBranch = ControlFlowUtils.stripBraces(thenBranch);
  final PsiElement nextStatement =
    PsiTreeUtil.skipSiblingsBackward(ifStatement,
                                     PsiWhiteSpace.class);
  if (!(nextStatement instanceof PsiStatement)) {
    return false;
  }
  PsiStatement elseBranch = (PsiStatement)nextStatement;
  elseBranch = ControlFlowUtils.stripBraces(elseBranch);
  if (ConditionalUtils.isAssignment(thenBranch, PsiKeyword.TRUE) &&
      ConditionalUtils.isAssignment(elseBranch, PsiKeyword.FALSE)) {
    final PsiExpressionStatement thenExpressionStatement =
      (PsiExpressionStatement)thenBranch;
    final PsiAssignmentExpression thenExpression =
      (PsiAssignmentExpression)
        thenExpressionStatement.getExpression();
    final PsiExpressionStatement elseExpressionStatement =
      (PsiExpressionStatement)elseBranch;
    final PsiAssignmentExpression elseExpression =
      (PsiAssignmentExpression)
        elseExpressionStatement.getExpression();
    final IElementType thenTokenType = thenExpression.getOperationTokenType();
    if (!thenTokenType.equals(elseExpression.getOperationTokenType())) {
      return false;
    }
    final PsiExpression thenLhs = thenExpression.getLExpression();
    final PsiExpression elseLhs = elseExpression.getLExpression();
    return EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(thenLhs,
                                                       elseLhs);
  }
  else {
    return false;
  }
}
项目:consulo-java    文件:TrivialIfInspection.java   
public static boolean isSimplifiableImplicitAssignmentNegated(
  PsiIfStatement ifStatement) {
  if (ifStatement.getElseBranch() != null) {
    return false;
  }
  PsiStatement thenBranch = ifStatement.getThenBranch();
  thenBranch = ControlFlowUtils.stripBraces(thenBranch);
  final PsiElement nextStatement =
    PsiTreeUtil.skipSiblingsBackward(ifStatement,
                                     PsiWhiteSpace.class);
  if (!(nextStatement instanceof PsiStatement)) {
    return false;
  }
  PsiStatement elseBranch = (PsiStatement)nextStatement;
  elseBranch = ControlFlowUtils.stripBraces(elseBranch);
  if (ConditionalUtils.isAssignment(thenBranch, PsiKeyword.FALSE) &&
      ConditionalUtils.isAssignment(elseBranch, PsiKeyword.TRUE)) {
    final PsiExpressionStatement thenExpressionStatement =
      (PsiExpressionStatement)thenBranch;
    final PsiAssignmentExpression thenExpression =
      (PsiAssignmentExpression)
        thenExpressionStatement.getExpression();
    final PsiExpressionStatement elseExpressionStatement =
      (PsiExpressionStatement)elseBranch;
    final PsiAssignmentExpression elseExpression =
      (PsiAssignmentExpression)
        elseExpressionStatement.getExpression();
    final IElementType thenTokenType = thenExpression.getOperationTokenType();
    if (!thenTokenType.equals(elseExpression.getOperationTokenType())) {
      return false;
    }
    final PsiExpression thenLhs = thenExpression.getLExpression();
    final PsiExpression elseLhs = elseExpression.getLExpression();
    return EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(thenLhs,
                                                       elseLhs);
  }
  else {
    return false;
  }
}
项目:Android_Lint_SRP_Practice_Example    文件:PsiClassStructureDetector.java   
@Override
public void visitExpressionStatement(PsiExpressionStatement statement) {
    mVisitor.report("PsiExpressionStatement", statement.getText(), statement);
    super.visitStatement(statement);
}
项目:consulo-java    文件:RemoveInitializerFix.java   
protected void sideEffectAwareRemove(Project project, PsiElement psiInitializer, PsiElement elementToDelete, PsiVariable variable)
{
    if(!FileModificationService.getInstance().prepareFileForWrite(elementToDelete.getContainingFile()))
    {
        return;
    }

    final PsiElement declaration = variable.getParent();
    final List<PsiElement> sideEffects = new ArrayList<PsiElement>();
    boolean hasSideEffects = RemoveUnusedVariableUtil.checkSideEffects(psiInitializer, variable, sideEffects);
    int res = RemoveUnusedVariableUtil.DELETE_ALL;
    if(hasSideEffects)
    {
        hasSideEffects = PsiUtil.isStatement(psiInitializer);
        res = RemoveUnusedVariableFix.showSideEffectsWarning(sideEffects, variable, FileEditorManager.getInstance(project).getSelectedTextEditor
                (), hasSideEffects, sideEffects.get(0).getText(), variable.getTypeElement().getText() +
                " " +
                variable.getName() +
                ";<br>" +
                PsiExpressionTrimRenderer.render((PsiExpression) psiInitializer));
    }
    try
    {
        if(res == RemoveUnusedVariableUtil.DELETE_ALL)
        {
            elementToDelete.delete();
        }
        else if(res == RemoveUnusedVariableUtil.MAKE_STATEMENT)
        {
            final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
            final PsiStatement statementFromText = factory.createStatementFromText(psiInitializer.getText() + ";", null);
            final PsiElement parent = elementToDelete.getParent();
            if(parent instanceof PsiExpressionStatement)
            {
                parent.replace(statementFromText);
            }
            else
            {
                declaration.getParent().addAfter(statementFromText, declaration);
                elementToDelete.delete();
            }
        }
    }
    catch(IncorrectOperationException e)
    {
        LOG.error(e);
    }
}