Skip to content

Commit fdb60bf

Browse files
committed
Java: enchanced check if it is within same package
1 parent 6844b78 commit fdb60bf

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

java/ql/src/Violations of Best Practice/Implementation Hiding/VisibleForTestingAbuse.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ predicate isWithinType(Callable c, RefType t) { c.getDeclaringType() = t }
2222
/**
2323
* A `Callable` is within same package as the `RefType`
2424
*/
25-
predicate isWithinPackage(Callable c, RefType t) {
26-
c.getDeclaringType().getPackage() = t.getPackage()
25+
predicate isWithinPackage(Expr e, RefType t) {
26+
e.getCompilationUnit().getPackage() = t.getPackage()
2727
}
2828

2929
predicate withinStaticContext(NestedClass c) {
@@ -80,7 +80,7 @@ where
8080
or
8181
// if public or protected report when its used outside its package because package protected should have been enough (package only permitted)
8282
(v.getField().isPublic() or v.getField().isProtected()) and
83-
not isWithinPackage(v.getEnclosingCallable(), v.getField().getDeclaringType())
83+
not isWithinPackage(v, v.getField().getDeclaringType())
8484
)
8585
)
8686
or
@@ -92,7 +92,7 @@ where
9292
// if public report when its used outside its package because package protected should have been enough (package only permitted)
9393
(
9494
c.getConstructedType().isPublic() and
95-
not isWithinPackage(c.getEnclosingCallable(), c.getConstructedType())
95+
not isWithinPackage(c, c.getConstructedType())
9696
or
9797
// if its package protected report when its used outside its outer class bc it should have been private (outer class only permitted)
9898
c.getConstructedType().hasNoModifier() and
@@ -114,7 +114,7 @@ where
114114
or
115115
// if public or protected report when its used outside its package because package protected should have been enough (package only permitted)
116116
(c.getMethod().isPublic() or c.getMethod().isProtected()) and
117-
not isWithinPackage(c.getEnclosingCallable(), c.getMethod().getDeclaringType())
117+
not isWithinPackage(c, c.getMethod().getDeclaringType())
118118
)
119119
)
120120
) and

java/ql/test/query-tests/VisibleForTestingAbuse/VisibleForTestingAbuse.expected

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77
| packageone/SourcePackage.java:29:28:29:46 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
88
| packageone/SourcePackage.java:30:28:30:49 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
99
| packagetwo/Annotated.java:49:31:49:31 | m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
10-
| packagetwo/Annotated.java:50:32:50:33 | m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
11-
| packagetwo/Annotated.java:51:32:51:33 | m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
1210
| packagetwo/Annotated.java:54:26:54:28 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
13-
| packagetwo/Annotated.java:56:32:56:40 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
14-
| packagetwo/Annotated.java:57:35:57:46 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |
1511
| packagetwo/Annotated.java:64:28:64:28 | m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
1612
| packagetwo/Annotated.java:69:26:69:28 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
1713
| packagetwo/Source.java:8:20:8:30 | Annotated.m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
1814
| packagetwo/Source.java:14:17:14:29 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
1915
| packagetwo/Source.java:20:28:20:47 | new AnnotatedClass(...) | Access of $@ annotated with VisibleForTesting found in production code. | packageone/AnnotatedClass.java:4:14:4:27 | AnnotatedClass | element |
2016
| packagetwo/Source.java:24:30:24:40 | Annotated.m | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:7:19:7:19 | m | element |
21-
| packagetwo/Source.java:25:31:25:42 | Annotated.m1 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:9:29:9:30 | m1 | element |
22-
| packagetwo/Source.java:26:31:26:42 | Annotated.m2 | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:11:26:11:27 | m2 | element |
2317
| packagetwo/Source.java:28:27:28:39 | f(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:16:16:16:16 | f | element |
24-
| packagetwo/Source.java:29:28:29:46 | fPublic(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:26:23:26:29 | fPublic | element |
25-
| packagetwo/Source.java:30:28:30:49 | fProtected(...) | Access of $@ annotated with VisibleForTesting found in production code. | packagetwo/Annotated.java:31:26:31:35 | fProtected | element |

java/ql/test/query-tests/VisibleForTestingAbuse/packagetwo/Annotated.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ private static void resetPriorities() {
4747
private static void resetPriorities2() {
4848
Runnable task = () -> {
4949
String priority = m; // $ SPURIOUS: Alert
50-
String priority1 = m1; // $ SPURIOUS: Alert
51-
String priority2 = m2; // $ SPURIOUS: Alert
50+
String priority1 = m1;
51+
String priority2 = m2;
5252
String priority3 = m3;
5353

5454
int result = f(); // $ SPURIOUS: Alert
5555
int resultPrivate = fPrivate();
56-
int resultPublic = fPublic(); // $ SPURIOUS: Alert
57-
int resultProtected = fProtected(); // $ SPURIOUS: Alert
56+
int resultPublic = fPublic();
57+
int resultProtected = fProtected();
5858
};
5959
task.run();
6060
}

java/ql/test/query-tests/VisibleForTestingAbuse/packagetwo/Source.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ void f() {
2222
// Lambda usage
2323
Runnable lambda = () -> {
2424
String lambdaS = Annotated.m; // $ Alert
25-
String lambdaS1 = Annotated.m1; // $ SPURIOUS: Alert
26-
String lambdaS2 = Annotated.m2; // $ SPURIOUS: Alert
25+
String lambdaS1 = Annotated.m1;
26+
String lambdaS2 = Annotated.m2;
2727

2828
int lambdaI = Annotated.f(); // $ Alert
29-
int lambdaI2 = Annotated.fPublic(); // $ SPURIOUS: Alert
30-
int lambdaI3 = Annotated.fProtected(); // $ SPURIOUS: Alert
29+
int lambdaI2 = Annotated.fPublic();
30+
int lambdaI3 = Annotated.fProtected();
3131
};
3232
lambda.run();
3333
}

0 commit comments

Comments
 (0)