Skip to content

Commit

Permalink
SONARJAVA-4612 Supported jakarta package in SpringComponentWithNonAut…
Browse files Browse the repository at this point in the history
…owiredMembersCheck, AbstractRegexCheck (#4474)
  • Loading branch information
ADarko22 authored Oct 5, 2023
1 parent a6dd968 commit 912ab49
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 9 deletions.
4 changes: 2 additions & 2 deletions its/ruling/src/test/java/org/sonar/java/it/AutoScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void javaCheckTestSources() throws Exception {

assertThat(newDiffs).containsExactlyInAnyOrderElementsOf(knownDiffs);
assertThat(newTotal).isEqualTo(knownTotal);
assertThat(rulesCausingFPs).hasSize(5);
assertThat(rulesCausingFPs).hasSize(6);
assertThat(rulesNotReporting).hasSize(7);
assertThat(rulesSilenced).hasSize(68);

Expand All @@ -188,7 +188,7 @@ public void javaCheckTestSources() throws Exception {
* No differences would mean that we find the same issues with and without the bytecode and libraries
*/
String differences = Files.readString(pathFor(TARGET_ACTUAL + PROJECT_KEY + "-no-binaries_differences"));
assertThat(differences).isEqualTo("Issues differences: 3253");
assertThat(differences).isEqualTo("Issues differences: 3254");
}

private static Path pathFor(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,7 @@
"ruleKey": "S5854",
"hasTruePositives": true,
"falseNegatives": 3,
"falsePositives": 0
"falsePositives": 1
},
{
"ruleKey": "S5855",
Expand Down
6 changes: 6 additions & 0 deletions java-checks-test-sources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public ConstructorInjection1(String env, String yyyAdaptor) {
class MyRepo {
@PersistenceContext
private EntityManager em; // Compliant

// ...
}

Expand All @@ -177,3 +176,15 @@ private static void isTrue(boolean condition, String message) {
}
}
}

@Repository
class JakartaRepo {
@jakarta.persistence.PersistenceContext
private EntityManager em2; // Compliant

@jakarta.inject.Inject
String email = null; // Compliant

@jakarta.annotation.Resource
String email2 = null; // Compliant
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,25 @@ private String id(String s) {
return s;
}

@jakarta.validation.constraints.Pattern( // Noncompliant {{[^@]+const@}}
regexp = "[^@]+" + CONST + "@")
private String jakartaField1;

@jakarta.validation.constraints.Pattern(regexp = "a+", flags = jakarta.validation.constraints.Pattern.Flag.CASE_INSENSITIVE) // Noncompliant {{a+,initialFlags=2}}
private String jakartaField2;

@jakarta.validation.constraints.Pattern(regexp = "a+", flags = { jakarta.validation.constraints.Pattern.Flag.CASE_INSENSITIVE, jakarta.validation.constraints.Pattern.Flag.DOTALL }) // Noncompliant {{a+,initialFlags=34}}
private String jakartaField3;

@jakarta.validation.constraints.Email(flags = jakarta.validation.constraints.Pattern.Flag.CASE_INSENSITIVE)
private String jakartaField4;

@jakarta.validation.constraints.Email(regexp = "[^@]+@[^@]+") // Noncompliant {{[^@]+@[^@]+}}
private String jakartaField5;

@jakarta.validation.constraints.Email(regexp = "a+", flags = jakarta.validation.constraints.Pattern.Flag.CASE_INSENSITIVE) // Noncompliant {{a+,initialFlags=2}}
private String jakartaField6;

@jakarta.validation.constraints.Email(regexp = "a+", flags = { jakarta.validation.constraints.Pattern.Flag.CASE_INSENSITIVE, jakarta.validation.constraints.Pattern.Flag.DOTALL }) // Noncompliant {{a+,initialFlags=34}}
private String jakartaField7;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class AnchorPrecedenceCheck {
@Email(regexp = "^a|b|c$") // Noncompliant [[sc=20;ec=27]]
String email;

@jakarta.validation.constraints.Email(regexp = "^a|b|c$") // Noncompliant [[sc=51;ec=58]]
String email2;

void noncompliant(String str) {
str.matches("^a|b|c$"); // Noncompliant [[sc=18;ec=25]] {{Group parts of the regex together to make the intended operator precedence explicit.}}
str.matches("^a|b|cd"); // Noncompliant [[sc=18;ec=25]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ public class CanonEqFlagInRegexCheck {
@Email(regexp = "éeéc", flags = Flag.CANON_EQ) // Compliant
String email2;

@jakarta.validation.constraints.Email(regexp = "éeéc") // Noncompliant [[sc=51;ec=55;secondary=15,15]] {{Use the CANON_EQ flag with this pattern.}}
String email3;

@jakarta.validation.constraints.Email(regexp = "éeéc", flags = jakarta.validation.constraints.Pattern.Flag.CANON_EQ) // Compliant
String email4;

void noncompliant(String str) {
Pattern.compile("éeéc"); // Noncompliant [[sc=22;ec=26;secondary=16,16]] {{Use the CANON_EQ flag with this pattern.}}
Pattern.compile("éeéc"); // Noncompliant [[sc=22;ec=26;secondary=22,22]] {{Use the CANON_EQ flag with this pattern.}}
Pattern.compile("é"); // Noncompliant
Pattern.compile("é|è"); // Noncompliant
Pattern.compile("à"); // Noncompliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -106,6 +107,15 @@ public abstract class AbstractRegexCheck extends IssuableSubscriptionVisitor imp
.addParametersMatcher(JAVA_LANG_STRING, JAVA_LANG_STRING, JAVA_LANG_STRING)
.build());

private static final Set<String> ANNOTATIONS_TO_FILTER = Set.of(
"javax.validation.constraints.Pattern",
"jakarta.validation.constraints.Pattern",
"javax.validation.constraints.Email",
"jakarta.validation.constraints.Email",
"org.hibernate.validator.constraints.URL",
"org.hibernate.validator.constraints.Email"
);

private RegexScannerContext regexContext;

// We want to report only one issue per element for one rule.
Expand All @@ -130,10 +140,7 @@ protected MethodMatchers getMethodInvocationMatchers() {

protected boolean filterAnnotation(AnnotationTree annotation) {
Type type = annotation.symbolType();
return type.is("javax.validation.constraints.Pattern") ||
type.is("javax.validation.constraints.Email") ||
type.is("org.hibernate.validator.constraints.URL") ||
type.is("org.hibernate.validator.constraints.Email");
return ANNOTATIONS_TO_FILTER.stream().anyMatch(type::is);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ public class SpringComponentWithNonAutowiredMembersCheck extends IssuableSubscri
private static final List<String> SPRING_INJECTION_ANNOTATION = Arrays.asList(
"org.springframework.beans.factory.annotation.Autowired",
"javax.inject.Inject",
"jakarta.inject.Inject",
"javax.annotation.Resource",
"jakarta.annotation.Resource",
"javax.persistence.PersistenceContext",
"jakarta.persistence.PersistenceContext",
"org.springframework.beans.factory.annotation.Value");

private static final List<String> SPRING_SINGLETON_ANNOTATION = Arrays.asList(
Expand Down

0 comments on commit 912ab49

Please sign in to comment.