Skip to content

Commit

Permalink
Java: Deprecate Field.getSourceDeclaration() and Field.isSourceDeclar…
Browse files Browse the repository at this point in the history
…ation()

Also follows the removal of the sourceid column of fields.
  • Loading branch information
igfoo committed Sep 24, 2024
1 parent 0be52f9 commit bda779a
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 41 deletions.
2 changes: 1 addition & 1 deletion java/ql/lib/definitions.qll
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private Element definition(Element e, string kind) {
e.(TypeAccess).getType().(RefType).getSourceDeclaration() = result and kind = "T"
or
exists(Variable v | v = e.(VarAccess).getVariable() |
result = v.(Field).getSourceDeclaration() or
result = v.(Field) or
result = v.(Parameter).getSourceDeclaration() or
result = v.(LocalVariableDecl)
) and
Expand Down
2 changes: 1 addition & 1 deletion java/ql/lib/semmle/code/Location.qll
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ predicate hasName(Element e, string name) {
or
methods(e, name, _, _, _, _)
or
fields(e, name, _, _, _)
fields(e, name, _, _)
or
packages(e, name)
or
Expand Down
2 changes: 1 addition & 1 deletion java/ql/lib/semmle/code/java/Dependency.qll
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ predicate depends(RefType t, RefType dep) {
or
// the declaring type of a field accessed in `t`,
exists(Field f | f.getAnAccess().getEnclosingCallable().getDeclaringType() = t |
usesType(f.getSourceDeclaration().getDeclaringType(), dep)
usesType(f.getDeclaringType(), dep)
)
or
// the type of a local variable declared in `t`,
Expand Down
2 changes: 1 addition & 1 deletion java/ql/lib/semmle/code/java/DependencyCounts.qll
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ predicate numDepends(RefType t, RefType dep, int value) {
elem = fa and
fa.getEnclosingCallable().getDeclaringType() = t
|
usesType(fa.getField().getSourceDeclaration().getDeclaringType(), dep)
usesType(fa.getField().getDeclaringType(), dep)
)
or
// the type of a local variable declared in `t`,
Expand Down
2 changes: 1 addition & 1 deletion java/ql/lib/semmle/code/java/Element.qll
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private predicate hasChildElement(Element parent, Element e) {
or
params(e, _, _, parent, _)
or
fields(e, _, _, parent, _)
fields(e, _, _, parent)
or
typeVars(e, _, _, parent)
}
18 changes: 6 additions & 12 deletions java/ql/lib/semmle/code/java/Member.qll
Original file line number Diff line number Diff line change
Expand Up @@ -756,13 +756,13 @@ class FieldDeclaration extends ExprParent, @fielddecl, Annotatable {
/** A class or instance field. */
class Field extends Member, ExprParent, @field, Variable {
/** Gets the declared type of this field. */
override Type getType() { fields(this, _, result, _, _) }
override Type getType() { fields(this, _, result, _) }

/** Gets the Kotlin type of this field. */
override KotlinType getKotlinType() { fieldsKotlinType(this, result) }

/** Gets the type in which this field is declared. */
override RefType getDeclaringType() { fields(this, _, _, result, _) }
override RefType getDeclaringType() { fields(this, _, _, result) }

/**
* Gets the field declaration in which this field is declared.
Expand Down Expand Up @@ -794,18 +794,12 @@ class Field extends Member, ExprParent, @field, Variable {
}

/**
* Gets the source declaration of this field.
*
* For fields that are members of a parameterized
* instance of a generic type, the source declaration is the
* corresponding field in the generic type.
*
* For all other fields, the source declaration is the field itself.
* DEPRECATED: The result is always `this`.
*/
Field getSourceDeclaration() { fields(this, _, _, _, result) }
deprecated Field getSourceDeclaration() { result = this }

/** Holds if this field is the same as its source declaration. */
predicate isSourceDeclaration() { this.getSourceDeclaration() = this }
/** DEPRECATED: This always holds. */
deprecated predicate isSourceDeclaration() { any() }

override predicate isPublic() {
Member.super.isPublic()
Expand Down
8 changes: 3 additions & 5 deletions java/ql/lib/semmle/code/java/Type.qll
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ predicate declaresMember(Type t, @member m) {
or
constrs(m, _, _, _, t, _)
or
fields(m, _, _, t, _)
fields(m, _, _, t)
or
enclInReftype(m, t) and
// Since the type `@member` in the dbscheme includes all `@reftype`s,
Expand Down Expand Up @@ -1195,12 +1195,10 @@ class EnumType extends Class {
EnumType() { isEnumType(this) }

/** Gets the enum constant with the specified name. */
EnumConstant getEnumConstant(string name) {
fields(result, _, _, this, _) and result.hasName(name)
}
EnumConstant getEnumConstant(string name) { fields(result, _, _, this) and result.hasName(name) }

/** Gets an enum constant declared in this enum type. */
EnumConstant getAnEnumConstant() { fields(result, _, _, this, _) }
EnumConstant getAnEnumConstant() { fields(result, _, _, this) }

override predicate isFinal() {
// JLS 8.9: An enum declaration is implicitly `final` unless it contains
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ImmutableField extends Field {
this.getType() instanceof ImmutableType and
// The field is only assigned to in a constructor or static initializer of the type it is declared in.
forall(FieldAccess fw, AnyAssignment ae |
fw.getField().getSourceDeclaration() = this and
fw.getField() = this and
fw = ae.getDest()
|
ae.getEnclosingCallable().getDeclaringType() = this.getDeclaringType() and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,10 @@ predicate isVMObserver(RefType rt) {
from Field f, FieldRead fr
where
f.fromSource() and
fr.getField().getSourceDeclaration() = f and
fr.getField() = f and
not f.getDeclaringType() instanceof EnumType and
forall(Assignment ae, Field g | ae.getDest() = g.getAnAccess() and g.getSourceDeclaration() = f |
ae.getSource() instanceof NullLiteral
) and
not exists(UnaryAssignExpr ua, Field g |
ua.getExpr() = g.getAnAccess() and
g.getSourceDeclaration() = f
) and
forall(Assignment ae | ae.getDest() = f.getAnAccess() | ae.getSource() instanceof NullLiteral) and
not exists(UnaryAssignExpr ua | ua.getExpr() = f.getAnAccess()) and
not f.isFinal() and
// Exclude fields that may be accessed reflectively.
not reflectivelyWritten(f) and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ where
not (f.isPublic() or f.isProtected()) and
f.fromSource() and
not f.getDeclaringType() instanceof EnumType and
not exists(VarAccess va | va.getVariable().(Field).getSourceDeclaration() = f) and
not exists(VarAccess va | va.getVariable() = f) and
// Exclude results in generated classes.
not f.getDeclaringType() instanceof GeneratedClass and
// Exclude fields that may be reflectively read (this includes standard serialization).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ private Field getField(Class c, string name, Type t) {

predicate thisAccess(LocalVariableDecl d, Field f) {
shadows(d, _, f, _) and
exists(VarAccess va | va.getVariable().(Field).getSourceDeclaration() = f |
exists(VarAccess va | va.getVariable() = f |
va.getQualifier() instanceof ThisAccess and
va.getEnclosingCallable() = d.getCallable()
)
}

predicate confusingAccess(LocalVariableDecl d, Field f) {
shadows(d, _, f, _) and
exists(VarAccess va | va.getVariable().(Field).getSourceDeclaration() = f |
exists(VarAccess va | va.getVariable() = f |
not exists(va.getQualifier()) and
va.getEnclosingCallable() = d.getCallable()
)
Expand All @@ -52,10 +52,7 @@ predicate assignmentToShadowingLocal(LocalVariableDecl d, Field f) {
exists(Expr assignedValue, Expr use |
d.getAnAssignedValue() = assignedValue and getARelevantChild(assignedValue) = use
|
exists(FieldAccess access, Field ff | access = assignedValue |
ff = access.getField() and
ff.getSourceDeclaration() = f
)
exists(FieldAccess access | access = assignedValue | f = access.getField())
or
exists(MethodCall get, Method getter | get = assignedValue and getter = get.getMethod() |
getterFor(getter, f)
Expand All @@ -73,10 +70,9 @@ predicate assignmentFromShadowingLocal(LocalVariableDecl d, Field f) {
setterFor(setter, f)
)
or
exists(Field instance, Expr assignedValue |
exists(Expr assignedValue |
access = getARelevantChild(assignedValue) and
assignedValue = instance.getAnAssignedValue() and
instance.getSourceDeclaration() = f
assignedValue = f.getAnAssignedValue()
)
)
}
Expand Down

0 comments on commit bda779a

Please sign in to comment.