Skip to content

Commit c831557

Browse files
committed
Java: Simplify own member access checks
1 parent 178c54e commit c831557

File tree

6 files changed

+9
-39
lines changed

6 files changed

+9
-39
lines changed

java/ql/src/Frameworks/Spring/Architecture/Refactoring Opportunities/UnusedBean.ql

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ class InstanceFieldWrite extends FieldWrite {
2121
not getEnclosingCallable().isStatic() and
2222
// Must be declared in this type or a supertype.
2323
getEnclosingCallable().getDeclaringType().inherits(getField()) and
24-
(
25-
// There must either be no qualifier - implied "this"
26-
not exists(getQualifier()) or
27-
// Or the qualifier implies we are accessing this or the super type
28-
getQualifier() instanceof ThisAccess or
29-
getQualifier() instanceof SuperAccess
30-
)
24+
isOwnFieldAccess()
3125
}
3226
}
3327

java/ql/src/Likely Bugs/Statements/Chaining.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ private predicate nonChainingReturn(Method m, ReturnStmt ret) {
4747
not hasSubtype*(m.getReturnType(), delegate.getReturnType())
4848
or
4949
// A method on the wrong object is called.
50-
not (
51-
delegateCall.getQualifier() instanceof ThisAccess or
52-
not exists(delegateCall.getQualifier())
53-
)
50+
not delegateCall.isOwnMethodAccess()
5451
or
5552
nonChaining(delegate)
5653
)

java/ql/src/Likely Bugs/Termination/SpinOnField.ql

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,10 @@ class EmptyLoop extends Stmt {
5050
}
5151
}
5252

53-
/** An access to a field in this object. */
54-
class FieldAccessInThis extends VarAccess {
55-
FieldAccessInThis() {
56-
this.getVariable() instanceof Field and
57-
(not this.hasQualifier() or this.getQualifier() instanceof ThisAccess)
58-
}
59-
}
60-
61-
from EmptyLoop loop, FieldAccessInThis access, Field field, ComparisonOrEqTestExpr expr
53+
from EmptyLoop loop, FieldAccess access, Field field, ComparisonOrEqTestExpr expr
6254
where
6355
loop.getCondition() = expr and
56+
access.isOwnFieldAccess() and
6457
access.getParent() = expr and
6558
field = access.getVariable() and
6659
field.isStatic() and

java/ql/src/Violations of Best Practice/Dead Code/DeadLocals.qll

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,8 @@ private predicate constructorHasEffect(Constructor c) {
113113
or
114114
exists(Assignment a | a.getEnclosingCallable() = c |
115115
not exists(VarAccess va | va = a.getDest() |
116-
va.getVariable() instanceof LocalVariableDecl
117-
or
118-
exists(Field f | f = va.getVariable() |
119-
va.getQualifier() instanceof ThisAccess or
120-
not exists(va.getQualifier())
121-
)
116+
va.getVariable() instanceof LocalVariableDecl or
117+
va.(FieldAccess).isOwnFieldAccess()
122118
)
123119
)
124120
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,15 @@
1212

1313
import java
1414

15-
/** Access to a method in `this` object. */
16-
class MethodAccessInThis extends MethodAccess {
17-
MethodAccessInThis() {
18-
not this.hasQualifier() or
19-
this.getQualifier() instanceof ThisAccess
20-
}
21-
}
22-
23-
from Class c, MethodAccess getResource, MethodAccessInThis getClass
15+
from Class c, MethodAccess getResource, MethodAccess getClass
2416
where
2517
getResource.getNumArgument() = 1 and
2618
(
2719
getResource.getMethod().hasName("getResource") or
2820
getResource.getMethod().hasName("getResourceAsStream")
2921
) and
3022
getResource.getQualifier() = getClass and
23+
getClass.isOwnMethodAccess() and
3124
getClass.getNumArgument() = 0 and
3225
getClass.getMethod().hasName("getClass") and
3326
getResource.getEnclosingCallable().getDeclaringType() = c and

java/ql/src/Violations of Best Practice/Undesirable Calls/NextFromIterator.ql

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ from MethodAccess m
1616
where
1717
m.getMethod().hasName("next") and
1818
m.getMethod().getNumberOfParameters() = 0 and
19-
(
20-
not m.hasQualifier() or
21-
m.getQualifier() instanceof ThisAccess
22-
) and
19+
m.isOwnMethodAccess() and
2320
exists(Interface i, Method hasNext |
2421
i.getSourceDeclaration().hasQualifiedName("java.util", "Iterator") and
2522
m.getEnclosingCallable() = hasNext and

0 commit comments

Comments
 (0)