-
Notifications
You must be signed in to change notification settings - Fork 725
SONARJAVA-6537 Implement rule S8948: "@OneToMany" relationships should use "mappedBy" or "@JoinColumn" #5716
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
14e64bd
implement S8948
NoemieBenard 683f8cf
update sonar way profile
NoemieBenard 0f8ef36
add precise issue location
NoemieBenard 5a1517d
fix autoscan
NoemieBenard 6944c96
update rule metadata
NoemieBenard 31c56c1
update rule metadata
NoemieBenard d1fdb2a
Address review comment: fix fp with JoinTable
NoemieBenard 1c6245b
raise on OneToMany annotations on getters
NoemieBenard 9f396ff
fix autoscan
NoemieBenard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
its/autoscan/src/test/resources/autoscan/diffs/diff_S8948.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S8948", | ||
| "hasTruePositives": false, | ||
| "falseNegatives": 10, | ||
| "falsePositives": 0 | ||
| } |
142 changes: 142 additions & 0 deletions
142
java-checks-test-sources/default/src/main/java/checks/OneToManyMappingCheckSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package checks; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.JoinTable; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.OneToMany; | ||
| import java.util.List; | ||
|
|
||
| public class OneToManyMappingCheckSample { | ||
|
|
||
| @Entity | ||
| class Author { | ||
| @OneToMany // Noncompliant {{Add "mappedBy" or "@JoinColumn" to this "@OneToMany" relationship.}} | ||
| // ^^^^^^^^^^ | ||
| private List<Book> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class Book { | ||
| @ManyToOne | ||
| private Author author; | ||
| } | ||
|
|
||
| // Compliant: uses mappedBy | ||
| @Entity | ||
| class AuthorWithMappedBy { | ||
| @OneToMany(mappedBy = "author") | ||
| private List<BookWithAuthor> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class BookWithAuthor { | ||
| @ManyToOne | ||
| @JoinColumn(name = "author_id") | ||
| private AuthorWithMappedBy author; | ||
| } | ||
|
|
||
| // Compliant: uses @JoinColumn on the @OneToMany field | ||
| @Entity | ||
| class AuthorWithJoinColumn { | ||
| @OneToMany | ||
| @JoinColumn(name = "author_id") | ||
| private List<BookNoRef> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class BookNoRef { | ||
| // No reference back to Author | ||
| } | ||
|
|
||
| @Entity | ||
| class AuthorUnidirectional { | ||
| @OneToMany // Noncompliant | ||
| private List<AnotherBook> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class AnotherBook { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Compliant: uses @JoinTable (explicit join table mapping) | ||
| @Entity | ||
| class AuthorWithJoinTable { | ||
| @OneToMany | ||
| @JoinTable(name = "author_books") | ||
| private List<BookWithJoinTable> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class BookWithJoinTable { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Property-access: noncompliant getter | ||
| @Entity | ||
| class AuthorPropertyAccess { | ||
| private List<BookPropertyAccess> books; | ||
|
|
||
| @OneToMany // Noncompliant | ||
| public List<BookPropertyAccess> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccess { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Property-access: compliant with mappedBy | ||
| @Entity | ||
| class AuthorPropertyAccessMappedBy { | ||
| private List<BookPropertyAccessMappedBy> books; | ||
|
|
||
| @OneToMany(mappedBy = "author") | ||
| public List<BookPropertyAccessMappedBy> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccessMappedBy { | ||
| @ManyToOne | ||
| private AuthorPropertyAccessMappedBy author; | ||
| } | ||
|
|
||
| // Property-access: compliant with @JoinColumn | ||
| @Entity | ||
| class AuthorPropertyAccessJoinColumn { | ||
| private List<BookPropertyAccessJoinColumn> books; | ||
|
|
||
| @OneToMany | ||
| @JoinColumn(name = "author_id") | ||
| public List<BookPropertyAccessJoinColumn> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccessJoinColumn { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Property-access: compliant with @JoinTable | ||
| @Entity | ||
| class AuthorPropertyAccessJoinTable { | ||
| private List<BookPropertyAccessJoinTable> books; | ||
|
|
||
| @OneToMany | ||
| @JoinTable(name = "author_books2") | ||
| public List<BookPropertyAccessJoinTable> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccessJoinTable { | ||
| // No reference back | ||
| } | ||
| } |
142 changes: 142 additions & 0 deletions
142
java-checks-test-sources/default/src/main/java/checks/OneToManyMappingCheckSampleJavax.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package checks; | ||
|
|
||
| import java.util.List; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.JoinColumn; | ||
| import javax.persistence.JoinTable; | ||
| import javax.persistence.ManyToOne; | ||
| import javax.persistence.OneToMany; | ||
|
|
||
| public class OneToManyMappingCheckSampleJavax { | ||
|
|
||
| @Entity | ||
| class Author { | ||
| @OneToMany // Noncompliant {{Add "mappedBy" or "@JoinColumn" to this "@OneToMany" relationship.}} | ||
| // ^^^^^^^^^^ | ||
| private List<Book> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class Book { | ||
| @ManyToOne | ||
| private Author author; | ||
| } | ||
|
|
||
| // Compliant: uses mappedBy | ||
| @Entity | ||
| class AuthorWithMappedBy { | ||
| @OneToMany(mappedBy = "author") | ||
| private List<BookWithAuthor> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class BookWithAuthor { | ||
| @ManyToOne | ||
| @JoinColumn(name = "author_id") | ||
| private AuthorWithMappedBy author; | ||
| } | ||
|
|
||
| // Compliant: uses @JoinColumn on the @OneToMany field | ||
| @Entity | ||
| class AuthorWithJoinColumn { | ||
| @OneToMany | ||
| @JoinColumn(name = "author_id") | ||
| private List<BookNoRef> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class BookNoRef { | ||
| // No reference back to Author | ||
| } | ||
|
|
||
| @Entity | ||
| class AuthorUnidirectional { | ||
| @OneToMany // Noncompliant | ||
| private List<AnotherBook> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class AnotherBook { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Compliant: uses @JoinTable (explicit join table mapping) | ||
| @Entity | ||
| class AuthorWithJoinTable { | ||
| @OneToMany | ||
| @JoinTable(name = "author_books") | ||
| private List<BookWithJoinTable> books; | ||
| } | ||
|
|
||
| @Entity | ||
| class BookWithJoinTable { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Property-access: noncompliant getter | ||
| @Entity | ||
| class AuthorPropertyAccess { | ||
| private List<BookPropertyAccess> books; | ||
|
|
||
| @OneToMany // Noncompliant | ||
| public List<BookPropertyAccess> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccess { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Property-access: compliant with mappedBy | ||
| @Entity | ||
| class AuthorPropertyAccessMappedBy { | ||
| private List<BookPropertyAccessMappedBy> books; | ||
|
|
||
| @OneToMany(mappedBy = "author") | ||
| public List<BookPropertyAccessMappedBy> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccessMappedBy { | ||
| @ManyToOne | ||
| private AuthorPropertyAccessMappedBy author; | ||
| } | ||
|
|
||
| // Property-access: compliant with @JoinColumn | ||
| @Entity | ||
| class AuthorPropertyAccessJoinColumn { | ||
| private List<BookPropertyAccessJoinColumn> books; | ||
|
|
||
| @OneToMany | ||
| @JoinColumn(name = "author_id") | ||
| public List<BookPropertyAccessJoinColumn> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccessJoinColumn { | ||
| // No reference back | ||
| } | ||
|
|
||
| // Property-access: compliant with @JoinTable | ||
| @Entity | ||
| class AuthorPropertyAccessJoinTable { | ||
| private List<BookPropertyAccessJoinTable> books; | ||
|
|
||
| @OneToMany | ||
| @JoinTable(name = "author_books2") | ||
| public List<BookPropertyAccessJoinTable> getBooks() { | ||
| return books; | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class BookPropertyAccessJoinTable { | ||
| // No reference back | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
java-checks/src/main/java/org/sonar/java/checks/OneToManyMappingCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * You can redistribute and/or modify this program under the terms of | ||
| * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.checks; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
| import org.sonar.check.Rule; | ||
| import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
| import org.sonar.plugins.java.api.tree.AnnotationTree; | ||
| import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; | ||
| import org.sonar.plugins.java.api.tree.IdentifierTree; | ||
| import org.sonar.plugins.java.api.tree.MethodTree; | ||
| import org.sonar.plugins.java.api.tree.Tree; | ||
| import org.sonar.plugins.java.api.tree.VariableTree; | ||
|
|
||
| @Rule(key = "S8948") | ||
| public class OneToManyMappingCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private static final Set<String> ONE_TO_MANY_ANNOTATIONS = Set.of( | ||
| "jakarta.persistence.OneToMany", | ||
| "javax.persistence.OneToMany"); | ||
|
|
||
| private static final Set<String> JOIN_COLUMN_ANNOTATIONS = Set.of( | ||
| "jakarta.persistence.JoinColumn", | ||
| "javax.persistence.JoinColumn"); | ||
|
|
||
| private static final Set<String> JOIN_TABLE_ANNOTATIONS = Set.of( | ||
| "jakarta.persistence.JoinTable", | ||
| "javax.persistence.JoinTable"); | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return List.of(Tree.Kind.VARIABLE, Tree.Kind.METHOD); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| var annotations = tree.is(Tree.Kind.METHOD) | ||
| ? ((MethodTree) tree).modifiers().annotations() | ||
| : ((VariableTree) tree).modifiers().annotations(); | ||
| checkAnnotations(annotations); | ||
| } | ||
|
|
||
| private void checkAnnotations(List<AnnotationTree> annotations) { | ||
| annotations.stream() | ||
| .filter(OneToManyMappingCheck::isOneToManyAnnotation) | ||
| .filter(annotation -> !hasMappedBy(annotation)) | ||
| .filter(annotation -> annotations.stream().noneMatch(OneToManyMappingCheck::isJoinColumnAnnotation)) | ||
| .filter(annotation -> annotations.stream().noneMatch(OneToManyMappingCheck::isJoinTableAnnotation)) | ||
| .forEach(annotation -> reportIssue(annotation, "Add \"mappedBy\" or \"@JoinColumn\" to this \"@OneToMany\" relationship.")); | ||
|
gitar-bot[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static boolean isOneToManyAnnotation(AnnotationTree annotation) { | ||
| return ONE_TO_MANY_ANNOTATIONS.stream().anyMatch(annotation.annotationType().symbolType()::is); | ||
| } | ||
|
|
||
| private static boolean isJoinColumnAnnotation(AnnotationTree annotation) { | ||
| return JOIN_COLUMN_ANNOTATIONS.stream().anyMatch(annotation.annotationType().symbolType()::is); | ||
| } | ||
|
|
||
| private static boolean isJoinTableAnnotation(AnnotationTree annotation) { | ||
| return JOIN_TABLE_ANNOTATIONS.stream().anyMatch(annotation.annotationType().symbolType()::is); | ||
| } | ||
|
|
||
| private static boolean hasMappedBy(AnnotationTree annotation) { | ||
| return annotation.arguments().stream() | ||
| .filter(arg -> arg.is(Tree.Kind.ASSIGNMENT)) | ||
| .map(AssignmentExpressionTree.class::cast) | ||
| .map(AssignmentExpressionTree::variable) | ||
| .filter(v -> v.is(Tree.Kind.IDENTIFIER)) | ||
| .map(IdentifierTree.class::cast) | ||
| .anyMatch(id -> "mappedBy".equals(id.name())); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.