Skip to content

Commit 1506925

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: add draft of test case
1 parent 134577e commit 1506925

File tree

7 files changed

+335
-68
lines changed

7 files changed

+335
-68
lines changed

java/ql/lib/ext/TopJdkApis.model.yml

Lines changed: 70 additions & 68 deletions
Large diffs are not rendered by default.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
message,_col1
2+
"java.lang.StringBuilder#append(String)",
3+
"java.util.List#get(int)",
4+
"java.util.List#add(Object)",
5+
"java.util.Map#put(Object,Object)",
6+
"java.lang.String#equals(Object)",
7+
"java.util.Map#get(Object)",
8+
"java.util.List#size()",
9+
"java.util.Collection#stream()",
10+
"java.lang.Object#getClass()",
11+
"java.util.stream.Stream#collect(Collector)",
12+
"java.util.Objects#equals(Object,Object)",
13+
"java.lang.String#format(String,Object\[\])",
14+
"java.util.stream.Stream#map(Function)",
15+
"java.lang.Throwable#getMessage()",
16+
"java.util.Arrays#asList(Object\[\])",
17+
"java.lang.String#equalsIgnoreCase(String)",
18+
"java.util.List#isEmpty()",
19+
"java.util.Set#add(Object)",
20+
"java.util.HashMap#put(Object,Object)",
21+
"java.util.stream.Collectors#toList()",
22+
"java.lang.StringBuilder#append(char)",
23+
"java.util.stream.Stream#filter(Predicate)",
24+
"java.lang.String#length()",
25+
"java.lang.Enum#name()",
26+
"java.lang.Object#toString()",
27+
"java.util.Optional#get()",
28+
"java.lang.StringBuilder#toString()",
29+
"java.lang.IllegalArgumentException#IllegalArgumentException(String)",
30+
"java.lang.Class#getName()",
31+
"java.lang.Enum#Enum(String,int)",
32+
"java.io.PrintWriter#write(String)",
33+
"java.util.Entry#getValue()",
34+
"java.util.Entry#getKey()",
35+
"java.util.Iterator#next()",
36+
"java.lang.Object#hashCode()",
37+
"java.util.Optional#orElse(Object)",
38+
"java.lang.StringBuffer#append(String)",
39+
"java.util.Collections#singletonList(Object)",
40+
"java.lang.Iterable#forEach(Consumer)",
41+
"java.util.Optional#of(Object)",
42+
"java.lang.String#contains(CharSequence)",
43+
"java.util.ArrayList#add(Object)",
44+
"java.util.Optional#ofNullable(Object)",
45+
"java.util.Collections#emptyList()",
46+
"java.math.BigDecimal#BigDecimal(String)",
47+
"java.lang.System#currentTimeMillis()",
48+
"java.lang.Object#equals(Object)",
49+
"java.util.Map#containsKey(Object)",
50+
"java.util.Optional#isPresent()",
51+
"java.lang.String#trim()",
52+
"java.util.List#addAll(Collection)",
53+
"java.util.Set#contains(Object)",
54+
"java.util.Optional#map(Function)",
55+
"java.util.Map#entrySet()",
56+
"java.util.Optional#empty()",
57+
"java.lang.Integer#parseInt(String)",
58+
"java.lang.String#startsWith(String)",
59+
"java.lang.IllegalStateException#IllegalStateException(String)",
60+
"java.lang.Enum#equals(Object)",
61+
"java.util.Iterator#hasNext()",
62+
"java.util.List#contains(Object)",
63+
"java.lang.String#substring(int,int)",
64+
"java.util.List#of(Object)",
65+
"java.util.Objects#hash(Object\[\])",
66+
"java.lang.RuntimeException#RuntimeException(String)",
67+
"java.lang.String#isEmpty()",
68+
"java.lang.String#replace(CharSequence,CharSequence)",
69+
"java.util.Set#size()",
70+
"java.io.File#File(String)",
71+
"java.lang.StringBuilder#append(Object)",
72+
"java.lang.String#split(String)",
73+
"java.util.Map#values()",
74+
"java.util.UUID#randomUUID()",
75+
"java.util.ArrayList#ArrayList(Collection)",
76+
"java.util.Map#keySet()",
77+
"java.sql.ResultSet#getString(String)",
78+
"java.lang.String#hashCode()",
79+
"java.lang.Throwable#Throwable(Throwable)",
80+
"java.util.HashMap#get(Object)",
81+
"java.lang.Class#getSimpleName()",
82+
"java.util.Set#isEmpty()",
83+
"java.util.Map#size()",
84+
"java.lang.String#substring(int)",
85+
"java.util.Map#remove(Object)",
86+
"java.lang.Throwable#printStackTrace()",
87+
"java.util.stream.Stream#findFirst()",
88+
"java.util.Optional#ifPresent(Consumer)",
89+
"java.lang.String#valueOf(Object)",
90+
"java.lang.String#toLowerCase()",
91+
"java.util.UUID#toString()",
92+
"java.lang.StringBuilder#append(int)",
93+
"java.util.Objects#requireNonNull(Object,String)",
94+
"java.nio.file.Path#resolve(String)",
95+
"java.lang.Enum#toString()",
96+
"java.lang.RuntimeException#RuntimeException(Throwable)",
97+
"java.util.Collection#size()",
98+
"java.lang.String#charAt(int)",
99+
"java.util.stream.Stream#forEach(Consumer)",
100+
"java.util.Map#isEmpty()",
101+
"java.lang.String#valueOf(int)"
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/** Provides classes and predicates for the Top JDK APIs. */
2+
3+
import java
4+
private import semmle.code.java.dataflow.FlowSummary
5+
private import semmle.code.java.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
6+
private import semmle.code.java.dataflow.ExternalFlow // for paramsString
7+
8+
// Note: from ExternalApi.qll for getting the api name returned in the telemetry query results
9+
// /**
10+
// * Gets information about the external API in the form expected by the CSV modeling framework.
11+
// */
12+
// string getApiName() {
13+
// result =
14+
// this.getDeclaringType().getPackage() + "." + this.getDeclaringType().getSourceDeclaration() +
15+
// "#" + this.getName() + paramsString(this)
16+
// }
17+
class TopJdkApi extends Callable {
18+
TopJdkApi() {
19+
// (
20+
// this instanceof SummarizedCallable or
21+
// this instanceof FlowSummaryImpl::Public::NegativeSummarizedCallable
22+
// ) and
23+
// top 101 jdk apis
24+
//this.asCallable().getQualifiedName() in ["java.util.Set.add"]
25+
exists(string api |
26+
api =
27+
this.getDeclaringType().getPackage() + "." + this.getDeclaringType().getSourceDeclaration() +
28+
"#" + this.getName() + paramsString(this) and
29+
api in [
30+
"java.lang.StringBuilder#append(String)", "java.util.List#get(int)",
31+
"java.util.List#add(Object)", "java.util.Map#put(Object,Object)",
32+
"java.lang.String#equals(Object)", "java.util.Map#get(Object)", "java.util.List#size()",
33+
"java.util.Collection#stream()", "java.lang.Object#getClass()",
34+
"java.util.stream.Stream#collect(Collector)", "java.util.Objects#equals(Object,Object)",
35+
"java.lang.String#format(String,Object[])", "java.util.stream.Stream#map(Function)",
36+
"java.lang.Throwable#getMessage()", "java.util.Arrays#asList(Object[])",
37+
"java.lang.String#equalsIgnoreCase(String)", "java.util.List#isEmpty()",
38+
"java.util.Set#add(Object)", "java.util.HashMap#put(Object,Object)",
39+
"java.util.stream.Collectors#toList()", "java.lang.StringBuilder#append(char)",
40+
"java.util.stream.Stream#filter(Predicate)", "java.lang.String#length()",
41+
"java.lang.Enum#name()", "java.lang.Object#toString()", "java.util.Optional#get()",
42+
"java.lang.StringBuilder#toString()",
43+
"java.lang.IllegalArgumentException#IllegalArgumentException(String)",
44+
"java.lang.Class#getName()", "java.lang.Enum#Enum(String,int)",
45+
"java.io.PrintWriter#write(String)", "java.util.Entry#getValue()",
46+
"java.util.Entry#getKey()", "java.util.Iterator#next()", "java.lang.Object#hashCode()",
47+
"java.util.Optional#orElse(Object)", "java.lang.StringBuffer#append(String)",
48+
"java.util.Collections#singletonList(Object)", "java.lang.Iterable#forEach(Consumer)",
49+
"java.util.Optional#of(Object)", "java.lang.String#contains(CharSequence)",
50+
"java.util.ArrayList#add(Object)", "java.util.Optional#ofNullable(Object)",
51+
"java.util.Collections#emptyList()", "java.math.BigDecimal#BigDecimal(String)",
52+
"java.lang.System#currentTimeMillis()", "java.lang.Object#equals(Object)",
53+
"java.util.Map#containsKey(Object)", "java.util.Optional#isPresent()",
54+
"java.lang.String#trim()", "java.util.List#addAll(Collection)",
55+
"java.util.Set#contains(Object)", "java.util.Optional#map(Function)",
56+
"java.util.Map#entrySet()", "java.util.Optional#empty()",
57+
"java.lang.Integer#parseInt(String)", "java.lang.String#startsWith(String)",
58+
"java.lang.IllegalStateException#IllegalStateException(String)",
59+
"java.lang.Enum#equals(Object)", "java.util.Iterator#hasNext()",
60+
"java.util.List#contains(Object)", "java.lang.String#substring(int,int)",
61+
"java.util.List#of(Object)", "java.util.Objects#hash(Object[])",
62+
"java.lang.RuntimeException#RuntimeException(String)", "java.lang.String#isEmpty()",
63+
"java.lang.String#replace(CharSequence,CharSequence)", "java.util.Set#size()",
64+
"java.io.File#File(String)", "java.lang.StringBuilder#append(Object)",
65+
"java.lang.String#split(String)", "java.util.Map#values()", "java.util.UUID#randomUUID()",
66+
"java.util.ArrayList#ArrayList(Collection)", "java.util.Map#keySet()",
67+
"java.sql.ResultSet#getString(String)", "java.lang.String#hashCode()",
68+
"java.lang.Throwable#Throwable(Throwable)", "java.util.HashMap#get(Object)",
69+
"java.lang.Class#getSimpleName()", "java.util.Set#isEmpty()", "java.util.Map#size()",
70+
"java.lang.String#substring(int)", "java.util.Map#remove(Object)",
71+
"java.lang.Throwable#printStackTrace()", "java.util.stream.Stream#findFirst()",
72+
"java.util.Optional#ifPresent(Consumer)", "java.lang.String#valueOf(Object)",
73+
"java.lang.String#toLowerCase()", "java.util.UUID#toString()",
74+
"java.lang.StringBuilder#append(int)", "java.util.Objects#requireNonNull(Object,String)",
75+
"java.nio.file.Path#resolve(String)", "java.lang.Enum#toString()",
76+
"java.lang.RuntimeException#RuntimeException(Throwable)", "java.util.Collection#size()",
77+
"java.lang.String#charAt(int)", "java.util.stream.Stream#forEach(Consumer)",
78+
"java.util.Map#isEmpty()", "java.lang.String#valueOf(int)"
79+
]
80+
)
81+
}
82+
83+
/** Holds if this API has a supported summary model. */
84+
private predicate hasSummary() { this = any(SummarizedCallable sc).asCallable() }
85+
86+
/** Holds if this API has a supported neutral model. */
87+
private predicate hasNeutral() {
88+
this = any(FlowSummaryImpl::Public::NegativeSummarizedCallable nsc).asCallable()
89+
}
90+
91+
// ! note: the below will hold for either manual or generated models, should I restrict to just manual?
92+
/** Holds if this API has a MaD model. */
93+
predicate hasMadModel() { this.hasSummary() or this.hasNeutral() }
94+
}
95+
// class TopJdkApiSummary extends SummarizedCallableBase {
96+
// TopJdkApiSummary() {
97+
// this instanceof SummarizedCallable and
98+
// this.asCallable().getQualifiedName() in ["java.util.Objects."]
99+
// }
100+
// }
101+
// class TopJdkApiNeutral extends SummarizedCallableBase {
102+
// TopJdkApiNeutral() {
103+
// this instanceof FlowSummaryImpl::Public::NegativeSummarizedCallable and
104+
// this.asCallable().getCompilationUnit().getPackage().getName() = "java.util"
105+
// }
106+
// }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| 87 |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
import java.util.Objects;
5+
import java.util.UUID;
6+
import java.util.stream.Collectors;
7+
import java.util.Map;
8+
import java.util.HashMap;
9+
import java.math.BigDecimal;
10+
import java.sql.ResultSet;
11+
import java.lang.System;
12+
import java.lang.IllegalStateException;
13+
14+
public class TopJdkApisTest { }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java
2+
import semmle.code.java.dataflow.FlowSummary
3+
import semmle.code.java.dataflow.internal.FlowSummaryImpl as FlowSummaryImpl
4+
import semmle.code.java.dataflow.ExternalFlow // for paramsString
5+
import TopJdkApis
6+
7+
// from SummarizedCallable sc //, FlowSummaryImpl::Public::NegativeSummarizedCallable nsc
8+
// where
9+
// // sc.asCallable().getDeclaringType().getName() = "String" and
10+
// // sc.asCallable().getName() = "format" and
11+
// // sc.asCallable().getQualifiedName() = "java.lang.String.format" and
12+
// sc.asCallable().getDeclaringType().getPackage() + "." +
13+
// sc.asCallable().getDeclaringType().getSourceDeclaration() + "#" + sc.asCallable().getName() +
14+
// paramsString(sc.asCallable()) = "java.lang.String#format(String,Object[])"
15+
// select sc, sc.asCallable().getQualifiedName(),
16+
// /*
17+
// * sc.asCallable().paramsString(),
18+
// * sc.asCallable().getSignature(), sc.asCallable().getStringSignature(),
19+
// * sc.asCallable().getDeclaringType().getSourceDeclaration(),
20+
// */
21+
// sc.asCallable().getDeclaringType().getPackage() + "." +
22+
// sc.asCallable().getDeclaringType().getSourceDeclaration() + "#" + sc.asCallable().getName() +
23+
// paramsString(sc.asCallable())
24+
// * get string representation of al modelled topjdkapis
25+
// from TopJdkApi t, string api
26+
// where
27+
// /*t.hasMadModel() and*/
28+
// api =
29+
// t.getDeclaringType().getPackage() + "." + t.getDeclaringType().getSourceDeclaration() + "#" +
30+
// t.getName() + paramsString(t)
31+
// select api order by api
32+
// * get count of all modelled topjdkapis
33+
select count(string api |
34+
exists(TopJdkApi t |
35+
/*t.hasMadModel() and*/
36+
api =
37+
t.getDeclaringType().getPackage() + "." + t.getDeclaringType().getSourceDeclaration() + "#" +
38+
t.getName() + paramsString(t)
39+
)
40+
)
41+
// from TopJdkApi t
42+
// where t.hasMadModel()
43+
// select t order by t

java/ql/test/ext/TopJdkApis/TopJdkApisTest.qlref

Whitespace-only changes.

0 commit comments

Comments
 (0)