简体   繁体   中英

How to avoid “code smell” when using @NamedEntityGraphs annotation?

In IntellyJ Idea I have installed SonarLint.

Code analizer says that I have to remove the "NamedEntityGraphs" wrapper from this annotation group. Here is my code:

@NamedEntityGraphs({
        @NamedEntityGraph(
                name = Application.ONLY_NAME,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar")
                }
        ),
        @NamedEntityGraph(name = Application.FULL,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar"),
                        @NamedAttributeNode("buzz")
                }
        )
})
public class SomeClass {  ...  }

Here are the arguments from SonarLint:

Annotation repetitions should not be wrapped Code smell, Minor, java:S1710

Before Java 8 if you needed to use multiple instances of the same annotation, they had to be wrapped in a container annotation. With Java 8, that's no longer necessary, allowing for cleaner, more readable code. Note that this rule is automatically disabled when the project's sonar.java.source is lower than 8.

Noncompliant Code Example

@SomeAnnotations({ // Noncompliant
        @SomeAnnotation(..a..)
        @SomeAnnotation(..b..)
        @SomeAnnotation(..c..)
})
public class SomeClass {  ...  }

Compliant Solution

@SomeAnnotation(..a..)
@SomeAnnotation(..b..)
@SomeAnnotation(..c..)
public class SomeClass {  ...  }

QUESTION:

Does enybody know how to organize @NamedEntityGraphs in order to get complient code?

Just do as the tool told you (and also @Slaw in the comments): Remove the outer annotation and just repeat @NamedEntityGraph which is ok since it is @Repeatable with JPA 2.2

@NamedEntityGraph(
        name = Application.ONLY_NAME,
        attributeNodes = {
                @NamedAttributeNode("foo"),
                @NamedAttributeNode("bar")
        }
),
@NamedEntityGraph(name = Application.FULL,
        attributeNodes = {
                 @NamedAttributeNode("foo"),
                 @NamedAttributeNode("bar"),
                 @NamedAttributeNode("buzz")
       }
)
public class SomeClass {  ...  }

You have given your answer in your code itself. Just remove @NamedEntityGraphs

@NamedEntityGraph(name = Application.ONLY_NAME,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar")
                }
        )
@NamedEntityGraph(name = Application.FULL,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar"),
                        @NamedAttributeNode("buzz")
                }
        )
public class SomeClass {  ...  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM