Skip to content

Commit 54dbd7c

Browse files
[Test gen] Add more support method implementations
1 parent ef5bf87 commit 54dbd7c

File tree

1 file changed

+86
-7
lines changed

1 file changed

+86
-7
lines changed

java/ql/src/utils/FlowTestCaseSupportMethods.qll

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ private class IteratorGetMethod extends GetMethod {
201201
override string getCall(string arg) { result = "getElement(" + arg + ")" }
202202
}
203203

204+
private class EnumerationGetMethod extends GetMethod {
205+
EnumerationGetMethod() { this = "enumerationgetmethod" }
206+
207+
override predicate appliesTo(Type t, Content c) {
208+
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Enumeration") and
209+
c instanceof CollectionContent
210+
}
211+
212+
override string getDefinition() {
213+
result = "<T> T getElement(Enumeration<T> it) { return it.nextElement(); }"
214+
}
215+
216+
bindingset[arg]
217+
override string getCall(string arg) { result = "getElement(" + arg + ")" }
218+
}
219+
204220
private class OptionalGetMethod extends GetMethod {
205221
OptionalGetMethod() { this = "optionalgetmethod" }
206222

@@ -215,8 +231,8 @@ private class OptionalGetMethod extends GetMethod {
215231
override string getCall(string arg) { result = "getElement(" + arg + ")" }
216232
}
217233

218-
private class MapGetKeyMethod extends GetMethod {
219-
MapGetKeyMethod() { this = "mapgetkeymethod" }
234+
private class MapGetKeytMethod extends GetMethod {
235+
MapGetKeytMethod() { this = "mapgetkeymethod" }
220236

221237
override predicate appliesTo(Type t, Content c) {
222238
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map") and
@@ -231,8 +247,20 @@ private class MapGetKeyMethod extends GetMethod {
231247
override string getCall(string arg) { result = "getMapKey(" + arg + ")" }
232248
}
233249

234-
private class MapValueGetMethod extends GetMethod {
235-
MapValueGetMethod() { this = "MapValueGetMethod" }
250+
private class MapEntryGetKeyMethod extends GetMethod {
251+
MapEntryGetKeyMethod() { this = "mapentrygetkeymethod" }
252+
253+
override predicate appliesTo(Type t, Content c) {
254+
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map$Entry") and
255+
c instanceof MapKeyContent
256+
}
257+
258+
bindingset[arg]
259+
override string getCall(string arg) { result = arg + ".getKey()" }
260+
}
261+
262+
private class MapGetValueMethod extends GetMethod {
263+
MapGetValueMethod() { this = "MapGetValueMethod" }
236264

237265
override predicate appliesTo(Type t, Content c) {
238266
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map") and
@@ -247,6 +275,18 @@ private class MapValueGetMethod extends GetMethod {
247275
override string getCall(string arg) { result = "getMapValue(" + arg + ")" }
248276
}
249277

278+
private class MapEntryGetValueMethod extends GetMethod {
279+
MapEntryGetValueMethod() { this = "mapentrygetvaluemethod" }
280+
281+
override predicate appliesTo(Type t, Content c) {
282+
t.(RefType).getASourceSupertype*().hasQualifiedName("java.util", "Map$Entry") and
283+
c instanceof MapValueContent
284+
}
285+
286+
bindingset[arg]
287+
override string getCall(string arg) { result = arg + ".getValue()" }
288+
}
289+
250290
private class ArrayGetMethod extends GetMethod {
251291
ArrayGetMethod() { this = "arraygetmethod" }
252292

@@ -314,7 +354,8 @@ private class ListGenMethod extends GenMethod {
314354

315355
override predicate appliesTo(Type t, Content c) {
316356
exists(GenericType list | list.hasQualifiedName("java.util", "List") |
317-
t = list or list.getAParameterizedType().getASupertype*() = t
357+
t.getErasure() = list.getASourceSupertype*().getErasure() or // cover things like Iterable and Collection
358+
list.getAParameterizedType().getASupertype*() = t
318359
) and
319360
c instanceof CollectionContent
320361
}
@@ -327,8 +368,8 @@ private class OptionalGenMethod extends GenMethod {
327368
OptionalGenMethod() { this = "optionalgenmethod" }
328369

329370
override predicate appliesTo(Type t, Content c) {
330-
exists(GenericType list | list.hasQualifiedName("java.util", "List") |
331-
list.getAParameterizedType().getASupertype*() = t
371+
exists(GenericType op | op.hasQualifiedName("java.util", "Optional") |
372+
op.getAParameterizedType().getASupertype*() = t
332373
) and
333374
c instanceof CollectionContent
334375
}
@@ -351,6 +392,25 @@ private class MapGenKeyMethod extends GenMethod {
351392
override string getCall(string arg) { result = "Map.of(" + arg + ", null)" }
352393
}
353394

395+
private class MapEntryGenKeyMethod extends GenMethod {
396+
MapEntryGenKeyMethod() { this = "mapentrygenkeymethod" }
397+
398+
override predicate appliesTo(Type t, Content c) {
399+
exists(GenericType map | map.hasQualifiedName("java.util", "Map$Entry") |
400+
map.getAParameterizedType().getASupertype*() = t
401+
) and
402+
c instanceof MapKeyContent
403+
}
404+
405+
override string getDefinition() {
406+
result =
407+
"<K> Map.Entry<K,?> newEntryWithMapKey(K key) { return Map.of(key, null).entrySet().iterator().next(); }"
408+
}
409+
410+
bindingset[arg]
411+
override string getCall(string arg) { result = "newEntryWithMapKey(" + arg + ")" }
412+
}
413+
354414
private class MapGenValueMethod extends GenMethod {
355415
MapGenValueMethod() { this = "mapvaluegenmethod" }
356416

@@ -365,6 +425,25 @@ private class MapGenValueMethod extends GenMethod {
365425
override string getCall(string arg) { result = "Map.of(null, " + arg + ")" }
366426
}
367427

428+
private class MapEntryGenValueMethod extends GenMethod {
429+
MapEntryGenValueMethod() { this = "mapentrygenvaluemethod" }
430+
431+
override predicate appliesTo(Type t, Content c) {
432+
exists(GenericType map | map.hasQualifiedName("java.util", "Map$Entry") |
433+
map.getAParameterizedType().getASupertype*() = t
434+
) and
435+
c instanceof MapValueContent
436+
}
437+
438+
override string getDefinition() {
439+
result =
440+
"<V> Map.Entry<?,V> newEntryWithMapValue(V value) { return Map.of(null, value).entrySet().iterator().next(); }"
441+
}
442+
443+
bindingset[arg]
444+
override string getCall(string arg) { result = "newEntryWithMapValue(" + arg + ")" }
445+
}
446+
368447
/**
369448
* Returns a cast to type `t` if `t` is not `java.lang.Object`, or an empty string otherwise.
370449
*/

0 commit comments

Comments
 (0)