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

LexicalPreservingPrinter: Unsupported operation - remove() #866

Closed
ThLeu opened this issue Mar 15, 2017 · 6 comments · Fixed by #932
Closed

LexicalPreservingPrinter: Unsupported operation - remove() #866

ThLeu opened this issue Mar 15, 2017 · 6 comments · Fixed by #932

Comments

@ThLeu
Copy link
Contributor

ThLeu commented Mar 15, 2017

Not sure about this one, if it even is supposed to work like that, but I just put together some quick test of a specific use case I would like to use the LexicalPreservingPrinter for.

Basically "moving" some Annotations around by first removing them and adding them again.
The test I put together (copy of LexicalPreservingPrinterTest.handleOverrideAnnotation) fails with...

java.lang.UnsupportedOperationException: removed child(MarkerAnnotationExpr) vs TokenTextElement(73) {protected}

	at com.github.javaparser.printer.lexicalpreservation.Difference.apply(Difference.java:520)
	at com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter$1.concreteListChange(LexicalPreservingPrinter.java:164)
....

The test case

    @Test
    public void moveOverrideAnnotations() {
        String code = "public class TestPage extends Page {" + EOL +
                EOL +
                "   protected void test() {}" + EOL +
                EOL +
                "   protected @Override void initializePage() {}" + EOL +
                "}";

        Pair<ParseResult<CompilationUnit>, LexicalPreservingPrinter> result = LexicalPreservingPrinter
                .setup(ParseStart.COMPILATION_UNIT, Providers.provider(code));

        CompilationUnit cu = result.a.getResult().get();

        cu.getTypes().stream()
                .forEach(type -> {
                    type.getMembers().stream()
                            .forEach(member -> {
                                if (member instanceof MethodDeclaration) {
                                    MethodDeclaration methodDeclaration = (MethodDeclaration) member;
                                    if (methodDeclaration.getAnnotationByName("Override").isPresent()) {
                                        methodDeclaration.getAnnotations().stream()
                                                .forEach(anno -> anno.remove());

                                        methodDeclaration.addMarkerAnnotation("Override");
                                    }
                                }
                            });
                });
        assertEquals("public class TestPage extends Page {" + EOL +
                EOL +
                "   @Override" + EOL +
                "   protected void test() {}" + EOL +
                EOL +
                "   @Override" + EOL +
                "   protected void initializePage() {}" + EOL +
                "}", result.b.print(cu));
    }

Kind regards

@ftomassetti
Copy link
Member

The first thing I noticed is that looking at the calculated different it would expect to find some annotation to delete before the protected keyword.

However it find the protected keyword in first position and not the annotation so it complains
screen shot 2017-03-15 at 23 43 02

@ftomassetti
Copy link
Member

This happens because looking at the CSM it expects to find the annotation before the protected keyword, while it is after

screen shot 2017-03-15 at 23 50 33

This is something that would require special handling I think, because it is a case in which some elements can be in an unexpected order.

@ThLeu
Copy link
Contributor Author

ThLeu commented Mar 15, 2017

Yes the "unexpected order" to say it nicely with your words :) is kind of the problem with our legacy code (problably not only ours) that is why I would like to refactor some things mostly automatically but keep everyhting else in place, hence it would be nice to use the LexicalPreservingPrinter for this.

@ftomassetti
Copy link
Member

I think we have two options to resolver this:

  1. An ad-hoc solution for MethodDeclaration (and ConstructorDeclaration?)
  2. A generalized solution to permite mixed elements in the CSM

The first one should be relatively easy. What do you think?

ftomassetti added a commit to ftomassetti/javaparser that referenced this issue Mar 16, 2017
@matozoid
Copy link
Contributor

I think modifiers can be found in many places (about ten,) so it should be fixed everywhere. I can't think of another place where things can be mixed like this, so it can be a solution specific to modifiers and annotations.

ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 15, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 16, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 17, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 17, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 17, 2017
@ftomassetti
Copy link
Member

Ok, a solution is almost ready. The only issue is now related to whitespace reconciliation: I get an extra whitespace... I introduced a new concept named CsmMix to handle CsmElement that can be in random order. It is quite tricky because we want to understand how the position of new elements is related to position of old elements to determine the right order.

ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 21, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 21, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 21, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 21, 2017
ftomassetti added a commit to ftomassetti/javaparser that referenced this issue May 21, 2017
@matozoid matozoid added this to the next release milestone May 22, 2017
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