Java: Simplify own member access checks

This commit is contained in:
Marcono1234
2021-03-04 22:45:52 +01:00
parent 178c54e69b
commit c8315577fe
6 changed files with 9 additions and 39 deletions

View File

@@ -21,13 +21,7 @@ class InstanceFieldWrite extends FieldWrite {
not getEnclosingCallable().isStatic() and
// Must be declared in this type or a supertype.
getEnclosingCallable().getDeclaringType().inherits(getField()) and
(
// There must either be no qualifier - implied "this"
not exists(getQualifier()) or
// Or the qualifier implies we are accessing this or the super type
getQualifier() instanceof ThisAccess or
getQualifier() instanceof SuperAccess
)
isOwnFieldAccess()
}
}

View File

@@ -47,10 +47,7 @@ private predicate nonChainingReturn(Method m, ReturnStmt ret) {
not hasSubtype*(m.getReturnType(), delegate.getReturnType())
or
// A method on the wrong object is called.
not (
delegateCall.getQualifier() instanceof ThisAccess or
not exists(delegateCall.getQualifier())
)
not delegateCall.isOwnMethodAccess()
or
nonChaining(delegate)
)

View File

@@ -50,17 +50,10 @@ class EmptyLoop extends Stmt {
}
}
/** An access to a field in this object. */
class FieldAccessInThis extends VarAccess {
FieldAccessInThis() {
this.getVariable() instanceof Field and
(not this.hasQualifier() or this.getQualifier() instanceof ThisAccess)
}
}
from EmptyLoop loop, FieldAccessInThis access, Field field, ComparisonOrEqTestExpr expr
from EmptyLoop loop, FieldAccess access, Field field, ComparisonOrEqTestExpr expr
where
loop.getCondition() = expr and
access.isOwnFieldAccess() and
access.getParent() = expr and
field = access.getVariable() and
field.isStatic() and

View File

@@ -113,12 +113,8 @@ private predicate constructorHasEffect(Constructor c) {
or
exists(Assignment a | a.getEnclosingCallable() = c |
not exists(VarAccess va | va = a.getDest() |
va.getVariable() instanceof LocalVariableDecl
or
exists(Field f | f = va.getVariable() |
va.getQualifier() instanceof ThisAccess or
not exists(va.getQualifier())
)
va.getVariable() instanceof LocalVariableDecl or
va.(FieldAccess).isOwnFieldAccess()
)
)
}

View File

@@ -12,15 +12,7 @@
import java
/** Access to a method in `this` object. */
class MethodAccessInThis extends MethodAccess {
MethodAccessInThis() {
not this.hasQualifier() or
this.getQualifier() instanceof ThisAccess
}
}
from Class c, MethodAccess getResource, MethodAccessInThis getClass
from Class c, MethodAccess getResource, MethodAccess getClass
where
getResource.getNumArgument() = 1 and
(
@@ -28,6 +20,7 @@ where
getResource.getMethod().hasName("getResourceAsStream")
) and
getResource.getQualifier() = getClass and
getClass.isOwnMethodAccess() and
getClass.getNumArgument() = 0 and
getClass.getMethod().hasName("getClass") and
getResource.getEnclosingCallable().getDeclaringType() = c and

View File

@@ -16,10 +16,7 @@ from MethodAccess m
where
m.getMethod().hasName("next") and
m.getMethod().getNumberOfParameters() = 0 and
(
not m.hasQualifier() or
m.getQualifier() instanceof ThisAccess
) and
m.isOwnMethodAccess() and
exists(Interface i, Method hasNext |
i.getSourceDeclaration().hasQualifiedName("java.util", "Iterator") and
m.getEnclosingCallable() = hasNext and