Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying some nodes with the lexicalPreservation enabled throws an exception #2517

Closed
quanturium opened this issue Feb 8, 2020 · 2 comments · Fixed by #3775
Closed

Modifying some nodes with the lexicalPreservation enabled throws an exception #2517

quanturium opened this issue Feb 8, 2020 · 2 comments · Fixed by #3775

Comments

@quanturium
Copy link

quanturium commented Feb 8, 2020

I'm trying to reorder the parameters in a constructor creation expr.
The first thing I've tried is to reorder the NodeList => Not sucessful (Looked normal since the parent is not aware of the sorting)
Second thing I've tried it to replace it manually, see code below:

      Map<Integer, Expression> positionToParameterMap = new HashMap<>();
      NodeList<Expression> arguments = constructorCallExpr.getArguments();
      for (int i = 0; i < arguments.size(); i++) {
          positionToParameterMap.put(i, arguments.get(i));
      }
      for (Pair<Integer, Integer> deltaPair : constructorChanges.deltaChanges) {
          Expression expression = positionToParameterMap.get(deltaPair.getValue()); // map to element at previous position
          constructorCallExpr.setArgument(deltaPair.getKey(), expression); // Replace element at new position with element from previous position
      }

This code above throws:

Caused by: java.lang.IllegalArgumentException: fromIndex(116) > toIndex(73)
        at java.util.SubList.<init>(AbstractList.java:624)
        at java.util.AbstractList.subList(AbstractList.java:484)
        at com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator$CalculatedSyntaxModel.sub(LexicalDifferenceCalculator.java:66)
        at com.github.javaparser.printer.lexicalpreservation.DifferenceElementCalculator.calculate(DifferenceElementCalculator.java:144)
        at com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.calculateListReplacementDifference(LexicalDifferenceCalculator.java:132)
        at com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter$Observer.concreteListReplacement(LexicalPreservingPrinter.java:307)
        at com.github.javaparser.ast.observer.PropagatingAstObserver.listReplacement(PropagatingAstObserver.java:82)
        at com.github.javaparser.ast.NodeList.lambda$notifyElementReplaced$3(NodeList.java:474)
        at java.util.ArrayList.forEach(ArrayList.java:1257)
        at com.github.javaparser.ast.NodeList.notifyElementReplaced(NodeList.java:474)
        at com.github.javaparser.ast.NodeList.set(NodeList.java:147)
        at com.github.javaparser.ast.nodeTypes.NodeWithArguments.setArgument(NodeWithArguments.java:55)

Any idea why? Am I doing something I shouldn't be doing? If no, is there a workaround for this?

@matozoid
Copy link
Contributor

matozoid commented Feb 8, 2020

It's most probably a bug, so someone will have to examine this.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Nov 11, 2020

This unit test reproduces the bug. The issue seems to be located in DifferenceElementCalculator.calculate(..)

public class Issue2517Test extends AbstractLexicalPreservingTest {

@Test
public void testMethoCallExpr() {

    String code = "public class A {\n" +
            "    public A(String a , String b) {\n" +
            "    }\n" +
            "    public static A m() {\n" +
            "      return new A(\"a\",\"b\");\n" +
            "    }\n" +
            "}";

    StaticJavaParser.setConfiguration(new ParserConfiguration());

    CompilationUnit cu = StaticJavaParser.parse(code);
    LexicalPreservingPrinter.setup(cu);

    ObjectCreationExpr cd = cu.findFirst(ObjectCreationExpr.class).get();
    NodeList<Expression> args = cd.getArguments();
    Expression a1 = args.get(0);
    Expression a2 = args.get(1);
    NodeList<Expression> newArgs = new NodeList<>(a2, a1);
    cd.setArguments(newArgs);

     System.out.println(LexicalPreservingPrinter.print(cu));
}
}

jlerbsc added a commit that referenced this issue Dec 3, 2022
Fix: #2517 Modifying some nodes with the lexicalPreservation enabled …
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants