Search in sources :

Example 1 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project Main by SpartanRefactoring.

the class MatcherTest method testMatch.

/**
     * constraints(){
     *     element(1).isNot( () -> {
     *         if(booleanExpression(3){
     *             statement(4);
     *         }
     *     });
     *     element(2).is(() -> {
     *          return null;
     *     });
     *     element(3).isNot(() -> !booleanExpression(5));
     *     element(1).isNot(() -> {
     *          return null;
     *     });
     * }
     *
     *
     *
     * matcher()
     * if(booleanExpression(0){
     *     statement(1);
     *     statement(2);
     * }
     * @throws Exception
     */
public void testMatch() throws Exception {
    Map<Integer, List<Matcher.Constraint>> constrains = new HashMap<>();
    PsiIfStatement ifs = createTestIfStatement("booleanExpression(0)", "statement(1);\n statement(2);");
    Encapsulator n = buildTemplate(ifs);
    constrains.putIfAbsent(1, new LinkedList<>());
    constrains.putIfAbsent(2, new LinkedList<>());
    constrains.putIfAbsent(3, new LinkedList<>());
    Encapsulator firstConstraint = buildTemplate(createTestIfStatement("booleanExpression(3)", "statement(4);"));
    Encapsulator secondConstraint = buildTemplate(createTestReturnStatement("null"));
    Encapsulator thirdConstraint = buildTemplate(createTestExpression("!booleanExpression(5)"));
    Encapsulator forthConstraint = buildTemplate(createTestReturnStatement("null"));
    constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(firstConstraint)));
    constrains.get(2).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.IS, Utils.wrapWithList(secondConstraint)));
    constrains.get(3).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(thirdConstraint)));
    constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(forthConstraint)));
    Matcher m = new Matcher(Utils.wrapWithList(n), constrains);
    PsiIfStatement tm1 = createTestIfStatement("x > 2", "\nx++; \nreturn null;");
    assertTrue(m.match(tm1));
    PsiIfStatement tm2 = createTestIfStatement("x > 2", "\nif(!(x > 4)){x--;} \nreturn null;");
    assertTrue(m.match(tm2));
    PsiIfStatement tm3 = createTestIfStatement("x > 2", "\nx++; \nx--;");
    assertFalse(m.match(tm3));
    PsiIfStatement tm4 = createTestIfStatement("x > 2", "\nreturn null; \nreturn null;");
    assertFalse(m.match(tm4));
    PsiIfStatement tm5 = createTestIfStatement("x > 2", "\nif(x < 3){x--;} \nreturn null;");
    assertFalse(m.match(tm5));
    PsiIfStatement tm6 = createTestIfStatement("x > 2", "\nif(x > 4){x--;} \nreturn null;");
    assertFalse(m.match(tm6));
}
Also used : HashMap(java.util.HashMap) Encapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator) PsiIfStatement(com.intellij.psi.PsiIfStatement) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project Main by SpartanRefactoring.

the class MatcherTest method testExtractInfo.

public void testExtractInfo() throws Exception {
    Map<Integer, List<Matcher.Constraint>> constrains = new HashMap<>();
    PsiIfStatement ifs = createTestIfStatement("booleanExpression(0)", "statement(1);\n statement(2);");
    Encapsulator n = buildTemplate(ifs);
    constrains.putIfAbsent(1, new LinkedList<>());
    constrains.putIfAbsent(2, new LinkedList<>());
    constrains.putIfAbsent(3, new LinkedList<>());
    Encapsulator firstConstraint = buildTemplate(createTestIfStatement("booleanExpression(3)", "statement(4);"));
    Encapsulator secondConstraint = buildTemplate(createTestReturnStatement("null"));
    Encapsulator thirdConstraint = buildTemplate(createTestExpression("!booleanExpression(5)"));
    Encapsulator forthConstraint = buildTemplate(createTestReturnStatement("null"));
    constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(firstConstraint)));
    constrains.get(2).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.IS, Utils.wrapWithList(secondConstraint)));
    constrains.get(3).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(thirdConstraint)));
    constrains.get(1).add(new Matcher.StructuralConstraint(Matcher.StructuralConstraint.ConstraintType.ISNOT, Utils.wrapWithList(forthConstraint)));
    Matcher m = new Matcher(Utils.wrapWithList(n), constrains);
    PsiIfStatement tm1 = createTestIfStatement("x > 2", "\nx++; \nreturn null;");
    Wrapper<Integer> i = new Wrapper<>(0);
    Map<Integer, List<PsiElement>> map = m.extractInfo(tm1, i);
    assertEquals(map.get(0).get(0).getText(), "x > 2");
    assertEquals(map.get(1).get(0).getText(), "x++;");
    assertEquals(map.get(2).get(0).getText(), "return null;");
    PsiIfStatement tm2 = createTestIfStatement("x > 2", "\nif(!(x > 4)){x--;} \nreturn null;");
    map = m.extractInfo(tm2, i);
    assertEquals(map.get(1).get(0).getText(), "if(!(x > 4)){x--;}");
    assertEquals(map.get(2).get(0).getText(), "return null;");
    PsiIfStatement tm3 = createTestIfStatement("x > 2", "\nx++; \nx--;");
    map = m.extractInfo(tm3, i);
    assertEquals(map.get(1).get(0).getText(), "x++;");
    assertEquals(map.get(2).get(0).getText(), "x--;");
    PsiIfStatement tm4 = createTestIfStatement("x > 2", "\nreturn null; \nreturn null;");
    map = m.extractInfo(tm4, i);
    assertEquals(map.get(1).get(0).getText(), "return null;");
    assertEquals(map.get(2).get(0).getText(), "return null;");
    PsiIfStatement tm5 = createTestIfStatement("x > 2", "\nif(x < 3){x--;} \nreturn null;");
    map = m.extractInfo(tm5, i);
    assertEquals(map.get(1).get(0).getText(), "if(x < 3){x--;}");
    assertEquals(map.get(2).get(0).getText(), "return null;");
    PsiIfStatement tm6 = createTestIfStatement("x > 2", "\nif(x > 4){x--;} \nreturn null;");
    map = m.extractInfo(tm6, i);
    assertEquals(map.get(1).get(0).getText(), "if(x > 4){x--;}");
    assertEquals(map.get(2).get(0).getText(), "return null;");
}
Also used : Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) HashMap(java.util.HashMap) Encapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator) PsiIfStatement(com.intellij.psi.PsiIfStatement) List(java.util.List) LinkedList(java.util.LinkedList)

Example 3 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project timber by JakeWharton.

the class WrongTimberUsageDetector method checkConditionalUsage.

private static boolean checkConditionalUsage(JavaContext context, PsiMethodCallExpression call, PsiElement element) {
    PsiElement thenElement;
    PsiElement elseElement;
    if (element instanceof PsiIfStatement) {
        PsiIfStatement ifArg = (PsiIfStatement) element;
        thenElement = ifArg.getThenBranch();
        elseElement = ifArg.getElseBranch();
    } else if (element instanceof PsiConditionalExpression) {
        PsiConditionalExpression inlineIfArg = (PsiConditionalExpression) element;
        thenElement = inlineIfArg.getThenExpression();
        elseElement = inlineIfArg.getElseExpression();
    } else {
        return false;
    }
    if (checkElement(context, call, thenElement)) {
        return false;
    }
    return checkElement(context, call, elseElement);
}
Also used : PsiIfStatement(com.intellij.psi.PsiIfStatement) PsiElement(com.intellij.psi.PsiElement) PsiConditionalExpression(com.intellij.psi.PsiConditionalExpression)

Example 4 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class JavaPsiFormattingTest method testReferenceExpressionSpaceInsertion.

public void testReferenceExpressionSpaceInsertion() {
    String text = "if(x&y);";
    PsiFile file = createFile("A.java", "class C{{\n" + text + "\n}}");
    PsiElement element = file.findElementAt(file.getText().indexOf(text));
    PsiIfStatement statement = PsiTreeUtil.getParentOfType(element, PsiIfStatement.class);
    assertNotNull(statement);
    Ref<PsiElement> result = Ref.create();
    CommandProcessor.getInstance().executeCommand(getProject(), () -> WriteAction.run(() -> result.set(CodeStyleManager.getInstance(getProject()).reformat(statement))), "", null);
    PsiExpression expr = ((PsiIfStatement) result.get()).getCondition();
    assertEquals("PsiBinaryExpression:x & y\n" + "  PsiReferenceExpression:x\n" + "    PsiReferenceParameterList\n" + "      <empty list>\n" + "    PsiIdentifier:x('x')\n" + "  PsiWhiteSpace(' ')\n" + "  PsiJavaToken:AND('&')\n" + "  PsiWhiteSpace(' ')\n" + "  PsiReferenceExpression:y\n" + "    PsiReferenceParameterList\n" + "      <empty list>\n" + "    PsiIdentifier:y('y')\n", DebugUtil.psiToString(expr, false));
}
Also used : PsiExpression(com.intellij.psi.PsiExpression) PsiIfStatement(com.intellij.psi.PsiIfStatement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PsiIfStatement

use of com.intellij.psi.PsiIfStatement in project intellij-community by JetBrains.

the class JavaIfUnwrapper method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    PsiStatement then = ((PsiIfStatement) element).getThenBranch();
    context.extractFromBlockOrSingleStatement(then, element);
    context.delete(element);
}
Also used : PsiStatement(com.intellij.psi.PsiStatement) PsiIfStatement(com.intellij.psi.PsiIfStatement)

Aggregations

PsiIfStatement (com.intellij.psi.PsiIfStatement)13 PsiElement (com.intellij.psi.PsiElement)7 PsiJavaToken (com.intellij.psi.PsiJavaToken)4 PsiStatement (com.intellij.psi.PsiStatement)4 Encapsulator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiConditionalExpression (com.intellij.psi.PsiConditionalExpression)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiFile (com.intellij.psi.PsiFile)1 PsiKeyword (com.intellij.psi.PsiKeyword)1 PsiElementPredicate (com.siyeh.ipp.base.PsiElementPredicate)1 Wrapper (il.org.spartan.Leonidas.auxilary_layer.Wrapper)1 ArrayList (java.util.ArrayList)1 NotNull (org.jetbrains.annotations.NotNull)1