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

Modifier is removed when removing an annotation. #3588

Closed
nimakarimipour opened this issue May 24, 2022 · 7 comments
Closed

Modifier is removed when removing an annotation. #3588

nimakarimipour opened this issue May 24, 2022 · 7 comments

Comments

@nimakarimipour
Copy link

When using LexicalPreservation printer, it mistakenly removes the modifier when we are removing an annotation from a Node under the following circumstances:

  1. Node is of type FieldDeclaration.
  2. FieldDeclaration contains a modifier.
  3. FieldDeclaraion contains an annotation.
  4. Modifier is written on the left side of the annotation. (e.g. public @Annot Object field;).

I tested the above issue with all modifiers (e.g. public, private, static and ...) and all cases had the same issue.

Please find the code below where the error reported in this issue is reproduced:

CompilationUnit tmp =
        StaticJavaParser.parse(
            "public class Foo{" + "\n" + "    protected @Nullable Object bar;" + "\n" + "}");
    LexicalPreservingPrinter.setup(tmp);
    Optional<ClassOrInterfaceDeclaration> optional = tmp.getClassByName("Foo");
    optional
        .get()
        .getMembers()
        .forEach(
            bodyDeclaration ->
                bodyDeclaration.ifFieldDeclaration(
                    fieldDeclaration -> {
                      NodeList<VariableDeclarator> vars =
                          fieldDeclaration.asFieldDeclaration().getVariables();
                      for (VariableDeclarator v : vars) {
                        if (v.getName().asString().equals("bar")) {
                          fieldDeclaration
                              .getAnnotations()
                              .removeIf(
                                  annotationExpr ->
                                      annotationExpr.getName().asString().equals("Nullable"));
                        }
                      }
                    }));
System.out.println(LexicalPreservingPrinter.print(tmp));

The output of the code above is:

public class Foo{
    @Nullable Object bar;
}

The annotation is not removed and the modifier is deleted. Please let me know if you recommend another way of removing annotation from Nodes or need more information.

Best,

Nima

@jlerbsc
Copy link
Collaborator

jlerbsc commented May 31, 2022

Thank you for reporting this bug. I will try to look at this issue over the next few days. Thank you for your patience.

@jlerbsc
Copy link
Collaborator

jlerbsc commented Jun 1, 2022

It looks more complex than I thought because annotations can be placed before or after the modifier. I think it's related to the concrete syntax model declaration but I'm not sure. Also when we declare concrete syntax model differently (with a mix) we generate other errors on the tests related to the Lexical Preserving Printer (the end of line characters are no longer in conformity with what should be). Maybe @MysterAitch or @matozoid can help us to resolve this issue.

@matozoid
Copy link
Contributor

matozoid commented Jun 1, 2022

I'm not touching that thing ever again :-)

jlerbsc added a commit that referenced this issue Jun 2, 2022
Fix issue #3588 Modifier is removed when removing an annotation
@msridhar
Copy link
Contributor

msridhar commented Jun 3, 2022

@jlerbsc should this issue be marked as fixed now? Thanks so much for digging in, we really appreciate it!

@jlerbsc
Copy link
Collaborator

jlerbsc commented Jun 3, 2022

Yes this issue is fixed.

@nimakarimipour
Copy link
Author

Hi all, thank you very much for resolving this issue, Lexical Preserving Printer is crucial for our tool. Please let me know if there is anything I can do to help. Thanks again for your great tool.

@jlerbsc jlerbsc closed this as completed Jun 6, 2022
@matozoid
Copy link
Contributor

matozoid commented Jun 7, 2022

@nimakarimipour - there is tons of work to do, if you want to help, come to the gitter channel and chat :-)

wadoon added a commit to jmltoolkit/jmlparser that referenced this issue Jun 14, 2022
# By Renovate Bot (8) and others
# Via GitHub (2) and renovate[bot] (1)
* upstream/master:
  chore(deps): update dependency biz.aqute.bnd:bnd-maven-plugin to v6.3.1
  chore(deps): update dependency com.squareup.okhttp3:okhttp to v4.10.0
  chore(deps): update dependency org.apache.maven.plugins:maven-surefire-plugin to v3.0.0-m7
  chore(deps): update dependency org.mockito:mockito-inline to v4.6.1
  Fix issue javaparser#3588 Modifier is removed when removing an annotation
  chore(deps): update dependency biz.aqute.bnd:bnd-maven-plugin to v6.3.0
  chore(deps): update dependency org.assertj:assertj-core to v3.23.1
  Fix lambda generic types that are always resolved to the first type param
  chore(deps): update dependency org.mockito:mockito-inline to v4.6.0
  chore(deps): update dependency org.assertj:assertj-core to v3.23.0

# Conflicts:
#	javaparser-core/src/main/java/com/github/javaparser/printer/ConcreteSyntaxModel.java
#	pom.xml
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

4 participants