Skip to content

Commit bda779a

Browse files
committed
Java: Deprecate Field.getSourceDeclaration() and Field.isSourceDeclaration()
Also follows the removal of the sourceid column of fields.
1 parent 0be52f9 commit bda779a

File tree

11 files changed

+24
-41
lines changed

11 files changed

+24
-41
lines changed

java/ql/lib/definitions.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private Element definition(Element e, string kind) {
159159
e.(TypeAccess).getType().(RefType).getSourceDeclaration() = result and kind = "T"
160160
or
161161
exists(Variable v | v = e.(VarAccess).getVariable() |
162-
result = v.(Field).getSourceDeclaration() or
162+
result = v.(Field) or
163163
result = v.(Parameter).getSourceDeclaration() or
164164
result = v.(LocalVariableDecl)
165165
) and

java/ql/lib/semmle/code/Location.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ predicate hasName(Element e, string name) {
1818
or
1919
methods(e, name, _, _, _, _)
2020
or
21-
fields(e, name, _, _, _)
21+
fields(e, name, _, _)
2222
or
2323
packages(e, name)
2424
or

java/ql/lib/semmle/code/java/Dependency.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ predicate depends(RefType t, RefType dep) {
5252
or
5353
// the declaring type of a field accessed in `t`,
5454
exists(Field f | f.getAnAccess().getEnclosingCallable().getDeclaringType() = t |
55-
usesType(f.getSourceDeclaration().getDeclaringType(), dep)
55+
usesType(f.getDeclaringType(), dep)
5656
)
5757
or
5858
// the type of a local variable declared in `t`,

java/ql/lib/semmle/code/java/DependencyCounts.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ predicate numDepends(RefType t, RefType dep, int value) {
6464
elem = fa and
6565
fa.getEnclosingCallable().getDeclaringType() = t
6666
|
67-
usesType(fa.getField().getSourceDeclaration().getDeclaringType(), dep)
67+
usesType(fa.getField().getDeclaringType(), dep)
6868
)
6969
or
7070
// the type of a local variable declared in `t`,

java/ql/lib/semmle/code/java/Element.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private predicate hasChildElement(Element parent, Element e) {
115115
or
116116
params(e, _, _, parent, _)
117117
or
118-
fields(e, _, _, parent, _)
118+
fields(e, _, _, parent)
119119
or
120120
typeVars(e, _, _, parent)
121121
}

java/ql/lib/semmle/code/java/Member.qll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -756,13 +756,13 @@ class FieldDeclaration extends ExprParent, @fielddecl, Annotatable {
756756
/** A class or instance field. */
757757
class Field extends Member, ExprParent, @field, Variable {
758758
/** Gets the declared type of this field. */
759-
override Type getType() { fields(this, _, result, _, _) }
759+
override Type getType() { fields(this, _, result, _) }
760760

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

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

767767
/**
768768
* Gets the field declaration in which this field is declared.
@@ -794,18 +794,12 @@ class Field extends Member, ExprParent, @field, Variable {
794794
}
795795

796796
/**
797-
* Gets the source declaration of this field.
798-
*
799-
* For fields that are members of a parameterized
800-
* instance of a generic type, the source declaration is the
801-
* corresponding field in the generic type.
802-
*
803-
* For all other fields, the source declaration is the field itself.
797+
* DEPRECATED: The result is always `this`.
804798
*/
805-
Field getSourceDeclaration() { fields(this, _, _, _, result) }
799+
deprecated Field getSourceDeclaration() { result = this }
806800

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

810804
override predicate isPublic() {
811805
Member.super.isPublic()

java/ql/lib/semmle/code/java/Type.qll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ predicate declaresMember(Type t, @member m) {
324324
or
325325
constrs(m, _, _, _, t, _)
326326
or
327-
fields(m, _, _, t, _)
327+
fields(m, _, _, t)
328328
or
329329
enclInReftype(m, t) and
330330
// Since the type `@member` in the dbscheme includes all `@reftype`s,
@@ -1195,12 +1195,10 @@ class EnumType extends Class {
11951195
EnumType() { isEnumType(this) }
11961196

11971197
/** Gets the enum constant with the specified name. */
1198-
EnumConstant getEnumConstant(string name) {
1199-
fields(result, _, _, this, _) and result.hasName(name)
1200-
}
1198+
EnumConstant getEnumConstant(string name) { fields(result, _, _, this) and result.hasName(name) }
12011199

12021200
/** Gets an enum constant declared in this enum type. */
1203-
EnumConstant getAnEnumConstant() { fields(result, _, _, this, _) }
1201+
EnumConstant getAnEnumConstant() { fields(result, _, _, this) }
12041202

12051203
override predicate isFinal() {
12061204
// JLS 8.9: An enum declaration is implicitly `final` unless it contains

java/ql/src/Advisory/Declarations/NonFinalImmutableField.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ImmutableField extends Field {
4040
this.getType() instanceof ImmutableType and
4141
// The field is only assigned to in a constructor or static initializer of the type it is declared in.
4242
forall(FieldAccess fw, AnyAssignment ae |
43-
fw.getField().getSourceDeclaration() = this and
43+
fw.getField() = this and
4444
fw = ae.getDest()
4545
|
4646
ae.getEnclosingCallable().getDeclaringType() = this.getDeclaringType() and

java/ql/src/Violations of Best Practice/Dead Code/NonAssignedFields.ql

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,10 @@ predicate isVMObserver(RefType rt) {
6363
from Field f, FieldRead fr
6464
where
6565
f.fromSource() and
66-
fr.getField().getSourceDeclaration() = f and
66+
fr.getField() = f and
6767
not f.getDeclaringType() instanceof EnumType and
68-
forall(Assignment ae, Field g | ae.getDest() = g.getAnAccess() and g.getSourceDeclaration() = f |
69-
ae.getSource() instanceof NullLiteral
70-
) and
71-
not exists(UnaryAssignExpr ua, Field g |
72-
ua.getExpr() = g.getAnAccess() and
73-
g.getSourceDeclaration() = f
74-
) and
68+
forall(Assignment ae | ae.getDest() = f.getAnAccess() | ae.getSource() instanceof NullLiteral) and
69+
not exists(UnaryAssignExpr ua | ua.getExpr() = f.getAnAccess()) and
7570
not f.isFinal() and
7671
// Exclude fields that may be accessed reflectively.
7772
not reflectivelyWritten(f) and

java/ql/src/Violations of Best Practice/Dead Code/UnusedField.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ where
1919
not (f.isPublic() or f.isProtected()) and
2020
f.fromSource() and
2121
not f.getDeclaringType() instanceof EnumType and
22-
not exists(VarAccess va | va.getVariable().(Field).getSourceDeclaration() = f) and
22+
not exists(VarAccess va | va.getVariable() = f) and
2323
// Exclude results in generated classes.
2424
not f.getDeclaringType() instanceof GeneratedClass and
2525
// Exclude fields that may be reflectively read (this includes standard serialization).

0 commit comments

Comments
 (0)