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 - addMarkerAnnotation(String) #865

Closed
ThLeu opened this issue Mar 15, 2017 · 5 comments
Closed

Comments

@ThLeu
Copy link
Contributor

ThLeu commented Mar 15, 2017

Hello there

If you run the LexicalPreservingPrinterTest.handleOverrideAnnotation Test with addMarkerAnnotation(String) instead of addAnnotation(String) it will not work, because apparently ObservableProperty.getTokenType(String) does not yet support the property == ObvervableProperty.NAME.

I added the following condition to the method and that seems to work for this specific case, but I think some other Test fails afterwards so it might not really be the solution, it was just some quick test I tried.

        if (property == ObservableProperty.NAME) {
            return GeneratedJavaParserConstants.STRING_LITERAL;
        }

There seem to be some other methods which are not yet supported, I will open more issues for each I stumble upon when trying my specific use cases.

Kind regards

@matozoid
Copy link
Contributor

@ftomassetti should I mark this as a bug?

@ftomassetti
Copy link
Member

yes, I think so

@ftomassetti
Copy link
Member

It seems to work for me. I added this test:

    // See issue #865
    @Test
    public void handleAddingMarkerAnnotation() {
        String code = "public class TestPage extends Page {" + EOL +
                EOL +
                "   protected void test() {}" + EOL +
                EOL +
                "   @Override" + EOL +
                "   protected 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.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));
    }

And it pass. I had just to remove the parenthesis on @Override (it was expecting to find @Override() instead of @Override).

Did I wrote the test correctly? Could you help us reproduce the issue?

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

ThLeu commented Mar 17, 2017

You are absolutely right, sorry, I did another change which i forgot to mention.
With this test you you should be able reproduce it.

   @Test
    public void handleOverrideAnnotation() {
        String code = "public class TestPage extends Page {" + EOL +
                EOL +
                "   protected void test() {}" + EOL +
                EOL +
                "   protected 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;
                                    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));
    }

I also played around with this input

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

and changing addMarkerAnnotation to addAnnotation again, which gives a different error.
But it might be the same problem as you mentioned in #866 about the unexpected order of protected and the annotation.

@ftomassetti ftomassetti self-assigned this 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 ftomassetti mentioned this issue May 21, 2017
@ftomassetti
Copy link
Member

I can confirm this has been corrected. I added tests to double check it in #936

@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

No branches or pull requests

3 participants