Search in sources :

Example 1 with Wrapper

use of il.org.spartan.Leonidas.auxilary_layer.Wrapper in project Main by SpartanRefactoring.

the class Matcher method getMatchingResult.

public MatchingResult getMatchingResult(PsiElement treeToMatch, Wrapper<Integer> numberOfNeighbors) {
    MatchingResult mr = new MatchingResult(false);
    for (int i = 1; i <= Utils.getNumberOfRootsPossible(treeToMatch); i++) {
        List<Encapsulator> potentialRoots = new ArrayList<>();
        PsiElement c = treeToMatch;
        MatchingResult m = new MatchingResult(true);
        for (int j = 0; j < i; j++) {
            potentialRoots.add(Encapsulator.buildTreeFromPsi(c));
            c = c.getNextSibling();
        }
        m.combineWith(matchingRecursion(new EncapsulatorIterator(roots), new EncapsulatorIterator(potentialRoots)));
        if (m.matches()) {
            mr.combineWith(m);
            mr.setMatches();
            numberOfNeighbors.set(i);
            break;
        }
    }
    if (mr.notMatches())
        return mr;
    Map<Integer, List<PsiElement>> info = mr.getMap();
    return info.keySet().stream().allMatch(id -> constrains.getOrDefault(id, new LinkedList<>()).stream().allMatch(c -> info.get(id).stream().allMatch(c::match))) ? mr : mr.setNotMatches();
}
Also used : Encapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator) Utils(il.org.spartan.Leonidas.auxilary_layer.Utils) java.util(java.util) Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) Leonidas.auxilary_layer.iz(il.org.spartan.Leonidas.auxilary_layer.iz) PsiElement(com.intellij.psi.PsiElement) GenericEncapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.GenericEncapsulator) Leonidas.auxilary_layer.az(il.org.spartan.Leonidas.auxilary_layer.az) Collectors(java.util.stream.Collectors) EncapsulatorIterator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator) InvocationTargetException(java.lang.reflect.InvocationTargetException) Encapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator) GenericEncapsulator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.GenericEncapsulator) PsiElement(com.intellij.psi.PsiElement) EncapsulatorIterator(il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator)

Example 2 with Wrapper

use of il.org.spartan.Leonidas.auxilary_layer.Wrapper 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 Wrapper

use of il.org.spartan.Leonidas.auxilary_layer.Wrapper in project Main by SpartanRefactoring.

the class JavadocMarkerNanoPattern method createReplacement.

@Override
public final PsiElement createReplacement(PsiMethod m) {
    String docOld = step.docCommentString(m), docNew = docOld + tag();
    final Wrapper<String> methodText = new Wrapper<>("");
    m.acceptChildren(new JavaElementVisitor() {

        @Override
        public void visitElement(PsiElement e) {
            if (!iz.javadoc(e))
                methodText.set(methodText.get() + e.getText());
            super.visitElement(e);
        }
    });
    return JavaPsiFacade.getElementFactory(m.getProject()).createMethodFromText("/**" + docNew + "*/" + methodText, m.getContext());
}
Also used : Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) PsiElement(com.intellij.psi.PsiElement)

Example 4 with Wrapper

use of il.org.spartan.Leonidas.auxilary_layer.Wrapper in project Main by SpartanRefactoring.

the class Toolbox method executeSingleTipper.

/**
     * @param e Psi tree
     * @param tipperName The name of the tipper to execure on e.
     * @return True if the tipper changed anything, false otherwise.
     * */
public boolean executeSingleTipper(PsiElement e, String tipperName) {
    Tipper tipper = getTipperByName(tipperName);
    if (tipper == null) {
        System.out.println("\nNull tipper!\n");
        return false;
    }
    if (e == null) {
        System.out.println("\nNull element!\n");
        return false;
    }
    Wrapper<PsiElement> toReplace = new Wrapper<>(null);
    Wrapper<Boolean> modified = new Wrapper<>(false);
    e.accept(new JavaRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement el) {
            super.visitElement(el);
            if (modified.get()) {
                return;
            }
            if (tipper.canTip(el)) {
                toReplace.set(el);
                modified.set(true);
            }
        }
    });
    if (!modified.get()) {
        return false;
    }
    tipper.tip(toReplace.get()).go(new PsiRewrite().psiFile(e.getContainingFile()).project(e.getProject()));
    return true;
}
Also used : Tipper(il.org.spartan.Leonidas.plugin.tipping.Tipper) Wrapper(il.org.spartan.Leonidas.auxilary_layer.Wrapper) PsiRewrite(il.org.spartan.Leonidas.auxilary_layer.PsiRewrite) JavaRecursiveElementVisitor(com.intellij.psi.JavaRecursiveElementVisitor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Wrapper (il.org.spartan.Leonidas.auxilary_layer.Wrapper)4 PsiElement (com.intellij.psi.PsiElement)3 Encapsulator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.Encapsulator)2 JavaElementVisitor (com.intellij.psi.JavaElementVisitor)1 JavaRecursiveElementVisitor (com.intellij.psi.JavaRecursiveElementVisitor)1 PsiIfStatement (com.intellij.psi.PsiIfStatement)1 PsiRewrite (il.org.spartan.Leonidas.auxilary_layer.PsiRewrite)1 Utils (il.org.spartan.Leonidas.auxilary_layer.Utils)1 Leonidas.auxilary_layer.az (il.org.spartan.Leonidas.auxilary_layer.az)1 Leonidas.auxilary_layer.iz (il.org.spartan.Leonidas.auxilary_layer.iz)1 EncapsulatorIterator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.EncapsulatorIterator)1 GenericEncapsulator (il.org.spartan.Leonidas.plugin.leonidas.BasicBlocks.GenericEncapsulator)1 Tipper (il.org.spartan.Leonidas.plugin.tipping.Tipper)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1