Skip to content

Commit 7ce9b16

Browse files
committed
Java: Performance tweaks
1 parent 83bba47 commit 7ce9b16

File tree

116 files changed

+266
-244
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+266
-244
lines changed

java/ql/examples/snippets/extend_class.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
import java
1414

1515
from RefType type
16-
where type.getASupertype+().hasQualifiedName("com.example", "Class")
16+
where type.getAStrictAncestor().hasQualifiedName("com.example", "Class")
1717
select type

java/ql/examples/snippets/throw_exception.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
import java
1010

1111
from ThrowStmt throw
12-
where throw.getThrownExceptionType().getASupertype*().hasQualifiedName("com.example", "AnException")
12+
where throw.getThrownExceptionType().getAnAncestor().hasQualifiedName("com.example", "AnException")
1313
select throw, "Don't throw com.example.AnException"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private module ControlFlowGraphImpl {
236236
*/
237237
private predicate mustCatch(CatchClause c, ThrowableType thrown) {
238238
thrown = thrownInBody(c.getTry()) and
239-
hasSubtype*(c.getACaughtType(), thrown)
239+
hasDescendant(c.getACaughtType(), thrown)
240240
}
241241

242242
/**
@@ -250,7 +250,7 @@ private module ControlFlowGraphImpl {
250250
*/
251251
private predicate mayNotCatch(CatchClause c, ThrowableType thrown) {
252252
thrown = thrownInBody(c.getTry()) and
253-
not hasSubtype*(c.getACaughtType(), thrown)
253+
not hasDescendant(c.getACaughtType(), thrown)
254254
}
255255

256256
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ class Argument extends Expr {
20932093
p.isVarargs() and
20942094
ptyp = p.getType() and
20952095
(
2096-
hasSubtype*(ptyp, typ)
2096+
hasDescendant(ptyp, typ)
20972097
or
20982098
// If the types don't match then we'll guess based on whether there are type variables involved.
20992099
hasInstantiation(ptyp.(Array).getComponentType())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AnnotatedGeneratedClass extends GeneratedClass {
1818
/** A Java class generated by an ANTLR scanner or parser class. */
1919
class AntlrGenerated extends GeneratedClass {
2020
AntlrGenerated() {
21-
exists(RefType t | this.getASupertype+() = t |
21+
exists(RefType t | this.getAStrictAncestor() = t |
2222
// ANTLR v3
2323
t.hasQualifiedName("org.antlr.runtime", "Lexer") or
2424
t.hasQualifiedName("org.antlr.runtime", "Parser") or

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class TypeNumber extends RefType {
114114

115115
/** A (reflexive, transitive) subtype of `java.lang.Number`. */
116116
class NumberType extends RefType {
117-
NumberType() { exists(TypeNumber number | hasSubtype*(number, this)) }
117+
NumberType() { exists(TypeNumber number | hasDescendant(number, this)) }
118118
}
119119

120120
/** A numeric type, including both primitive and boxed types. */
@@ -436,13 +436,13 @@ class ArrayLengthField extends Field {
436436

437437
/** A (reflexive, transitive) subtype of `java.lang.Throwable`. */
438438
class ThrowableType extends RefType {
439-
ThrowableType() { exists(TypeThrowable throwable | hasSubtype*(throwable, this)) }
439+
ThrowableType() { exists(TypeThrowable throwable | hasDescendant(throwable, this)) }
440440
}
441441

442442
/** An unchecked exception. That is, a (reflexive, transitive) subtype of `java.lang.Error` or `java.lang.RuntimeException`. */
443443
class UncheckedThrowableType extends RefType {
444444
UncheckedThrowableType() {
445-
exists(TypeError e | hasSubtype*(e, this)) or
446-
exists(TypeRuntimeException e | hasSubtype*(e, this))
445+
exists(TypeError e | hasDescendant(e, this)) or
446+
exists(TypeRuntimeException e | hasDescendant(e, this))
447447
}
448448
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ predicate catchesNFE(TryStmt t) {
6363
exists(CatchClause cc, LocalVariableDeclExpr v |
6464
t.getACatchClause() = cc and
6565
cc.getVariable() = v and
66-
v.getType().(RefType).getASubtype*() instanceof NumberFormatException
66+
v.getType().(RefType).getADescendant() instanceof NumberFormatException
6767
)
6868
}
6969

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class NewInstance extends MethodAccess {
295295
// If we cast the result of this method, then this is either the type specified, or a
296296
// sub-type of that type. Make sure we exclude overly generic types such as `Object`.
297297
not overlyGenericType(cast.getType()) and
298-
hasSubtype*(cast.getType(), result)
298+
hasDescendant(cast.getType(), result)
299299
)
300300
}
301301
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class DeserializableField extends Field { }
2424
*/
2525
library class StandardSerializableField extends SerializableField, DeserializableField {
2626
StandardSerializableField() {
27-
this.getDeclaringType().getASupertype*() instanceof TypeSerializable and
27+
this.getDeclaringType().getAnAncestor() instanceof TypeSerializable and
2828
not this.isTransient()
2929
}
3030
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ predicate hasSubtype(RefType t, Type sub) {
3737
typeVarSubtypeBound(t, sub) and t != sub
3838
}
3939

40+
/**
41+
* Holds if reference type `anc` is a direct or indirect supertype of `sub`, including itself.
42+
*/
43+
cached
44+
predicate hasDescendant(RefType anc, Type sub) {
45+
anc = sub
46+
or
47+
exists(RefType mid | hasSubtype(anc, mid) and hasDescendant(mid, sub))
48+
}
49+
4050
private predicate typeVarSubtypeBound(RefType t, TypeVariable tv) {
4151
if tv.hasTypeBound() then t = tv.getATypeBound().getType() else t instanceof TypeObject
4252
}
@@ -394,11 +404,17 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
394404
/** Gets a direct subtype of this type. */
395405
RefType getASubtype() { hasSubtype(this, result) }
396406

407+
/** Gets a direct or indirect descendant of this type, including itself. */
408+
RefType getADescendant() { hasDescendant(this, result) }
409+
397410
/** Gets a direct supertype of this type. */
398411
RefType getASupertype() { hasSubtype(result, this) }
399412

400413
/** Gets a direct or indirect supertype of this type, including itself. */
401-
RefType getAnAncestor() { hasSubtype*(result, this) }
414+
RefType getAnAncestor() { hasDescendant(result, this) }
415+
416+
/** Gets a direct or indirect supertype of this type, not including itself. */
417+
RefType getAStrictAncestor() { result = this.getAnAncestor() and result != this }
402418

403419
/**
404420
* Gets the source declaration of a direct supertype of this type, excluding itself.

0 commit comments

Comments
 (0)