-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-5115 FP in S5803: when using (VisibleForTesting.PROTECTED) (#…
- Loading branch information
1 parent
f40dbdc
commit d543e1d
Showing
10 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...checks/VisibleForTestingProtectedUsageCheck/CheckOtherwiseString/IssueStringMyObject.java
This file contains 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,14 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck.CheckOtherwiseString; | ||
|
||
public class IssueStringMyObject { | ||
|
||
IssueStringMyObject() { | ||
} | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) | ||
String bar; | ||
|
||
@VisibleForTesting(otherwise = "3") | ||
String foo; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
.../checks/VisibleForTestingProtectedUsageCheck/CheckOtherwiseString/IssueStringService.java
This file contains 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,13 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck.CheckOtherwiseString; | ||
|
||
public class IssueStringService { | ||
|
||
public String test() { | ||
return new IssueStringMyObject().foo; // Noncompliant {{Remove this usage of "foo", it is annotated with @VisibleForTesting and should not be accessed from production code.}} | ||
} | ||
|
||
public String test2() { | ||
return new IssueStringMyObject().bar; // Noncompliant {{Remove this usage of "bar", it is annotated with @VisibleForTesting and should not be accessed from production code.}} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...a/checks/VisibleForTestingProtectedUsageCheck/CheckOtherwiseString/VisibleForTesting.java
This file contains 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,16 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck.CheckOtherwiseString; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
|
||
@Retention(value = CLASS) | ||
@Target(value = {TYPE, METHOD, FIELD}) | ||
public @interface VisibleForTesting { | ||
String otherwise(); | ||
String PROTECTED = "4"; | ||
} |
17 changes: 17 additions & 0 deletions
17
...rces/default/src/main/java/checks/VisibleForTestingProtectedUsageCheck/IssueMyObject.java
This file contains 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,17 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck; | ||
|
||
public class IssueMyObject { | ||
|
||
IssueMyObject() { | ||
} | ||
|
||
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED, othertestcase=1, othertypecase="S") | ||
String bar; | ||
|
||
// androidx.annotation.VisibleForTesting | ||
// @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) | ||
// where VisibleForTesting.PROTECTED = 4 | ||
@VisibleForTesting(otherwise = 3, othertestcase=1, othertypecase="F") | ||
String foo; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...urces/default/src/main/java/checks/VisibleForTestingProtectedUsageCheck/IssueService.java
This file contains 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,13 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck; | ||
|
||
public class IssueService { | ||
|
||
public String test() { | ||
return new IssueMyObject().foo; // Noncompliant {{Remove this usage of "foo", it is annotated with @VisibleForTesting and should not be accessed from production code.}} | ||
} | ||
|
||
public String test2() { | ||
return new IssueMyObject().bar; // Compliant {{bar has valid otherwise = VisibleForTesting.PROTECTED set so can be used here}} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...t-sources/default/src/main/java/checks/VisibleForTestingProtectedUsageCheck/MyObject.java
This file contains 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,30 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck; | ||
|
||
public class MyObject { | ||
|
||
MyObject() {} | ||
|
||
// androidx.annotation.VisibleForTesting | ||
// @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) | ||
// where VisibleForTesting.PROTECTED = 4 | ||
@VisibleForTesting(otherwise = 4, othertestcase=1, othertypecase="F") | ||
String foo; | ||
|
||
@VisibleForTesting(otherwise = 4, othertestcase=1, othertypecase="F") | ||
int answer() { | ||
return 42; | ||
} | ||
|
||
int answer(int result) { | ||
return result; | ||
} | ||
|
||
@VisibleForTesting(otherwise = 4, othertestcase=1, othertypecase="F") | ||
class Nested {} | ||
} | ||
|
||
@VisibleForTesting(otherwise = 4, othertestcase=1, othertypecase="F") | ||
class Outer {} | ||
|
||
|
||
|
45 changes: 45 additions & 0 deletions
45
...st-sources/default/src/main/java/checks/VisibleForTestingProtectedUsageCheck/Service.java
This file contains 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,45 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
public class Service { | ||
|
||
public String f(int param) { | ||
|
||
TestOnly testOnly = null; // Compliant, TestOnly class visible if it is private | ||
|
||
MyObject.Nested nested = null; // Noncompliant {{Remove this usage of "Nested", it is annotated with @VisibleForTesting and should not be accessed from production code.}} | ||
|
||
Outer outer = null; // Noncompliant {{Remove this usage of "Outer", it is annotated with @VisibleForTesting and should not be accessed from production code.}} | ||
|
||
String foo = new MyObj().bar; // False negative MyObj and Service are in the same file but if 'bar' is private it wouldn't be visible here) | ||
|
||
String bar = new MyObj().bar; | ||
|
||
return new MyObject().foo; // Already reported in line 21 | ||
|
||
} | ||
|
||
public int g(@Deprecated int param) { | ||
MyObject myObject = new MyObject(); | ||
|
||
myObject.answer(123); // Compliant, no annotation | ||
myObject.answer(param); // Compliant, no annotation | ||
|
||
return myObject.answer(); // Noncompliant {{Remove this usage of "answer", it is annotated with @VisibleForTesting and should not be accessed from production code.}} | ||
} | ||
|
||
} | ||
|
||
class MyObj { | ||
@VisibleForTesting | ||
String bar; | ||
} | ||
|
||
|
||
@VisibleForTesting | ||
class TestOnly { | ||
|
||
} | ||
|
||
|
18 changes: 18 additions & 0 deletions
18
.../default/src/main/java/checks/VisibleForTestingProtectedUsageCheck/VisibleForTesting.java
This file contains 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,18 @@ | ||
package checks.VisibleForTestingProtectedUsageCheck; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
|
||
@Retention(value = CLASS) | ||
@Target(value = {TYPE, METHOD, FIELD}) | ||
public @interface VisibleForTesting { | ||
int otherwise(); | ||
int othertestcase(); | ||
String othertypecase(); | ||
int PROTECTED = 4; | ||
} |
This file contains 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
This file contains 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