/
ThriftTests.java
193 lines (187 loc) · 7.46 KB
/
ThriftTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.uber.nullaway;
import java.util.Arrays;
import org.junit.Test;
public class ThriftTests extends NullAwayTestsBase {
@Test
public void testThriftIsSet() {
defaultCompilationHelper
.addSourceLines("TBase.java", "package org.apache.thrift;", "public interface TBase {}")
.addSourceLines(
"Generated.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"public class Generated implements org.apache.thrift.TBase {",
" public @Nullable Object id;",
" public boolean isFixed;",
" @Nullable public Object getId() { return this.id; }",
" // this is to ensure we don't crash on unions",
" public boolean isSet() { return false; }",
" public boolean isSetId() { return this.id != null; }",
" public boolean isFixed() { return this.isFixed; }",
" public boolean isSetIsFixed() { return false; }",
"}")
.addSourceLines(
"Client.java",
"package com.uber;",
"public class Client {",
" public void testNeg(Generated g) {",
" if (g.isSetId()) {",
" g.getId().toString();",
" g.id.hashCode();",
" }",
" if (g.isSetIsFixed()) {",
" g.isFixed();",
" }",
" if (g.isSet()) {}",
" }",
" public void testPos(Generated g) {",
" if (!g.isSetId()) {",
" // BUG: Diagnostic contains: dereferenced expression g.getId() is @Nullable",
" g.getId().hashCode();",
" } else {",
" g.id.toString();",
" }",
" java.util.List<Generated> l = new java.util.ArrayList<>();",
" if (l.get(0).isSetId()) {",
" l.get(0).getId().hashCode();",
" }",
" }",
"}")
.doTest();
}
@Test
public void testThriftIsSetWithGenerics() {
defaultCompilationHelper
.addSourceLines(
"TBase.java", "package org.apache.thrift;", "public interface TBase<T, F> {}")
.addSourceLines(
"Generated.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"public class Generated implements org.apache.thrift.TBase<String, Integer> {",
" public @Nullable Object id;",
" @Nullable public Object getId() { return this.id; }",
" public boolean isSetId() { return this.id != null; }",
"}")
.addSourceLines(
"Client.java",
"package com.uber;",
"public class Client {",
" public void testNeg(Generated g) {",
" if (!g.isSetId()) {",
" return;",
" }",
" Object x = g.getId();",
" if (x.toString() == null) return;",
" g.getId().toString();",
" g.id.hashCode();",
" }",
"}")
.doTest();
}
@Test
public void testThriftIsSetWithArg() {
defaultCompilationHelper
.addSourceLines(
"TBase.java",
"package org.apache.thrift;",
"public interface TBase {",
" boolean isSet(String fieldName);",
"}")
.addSourceLines(
"Client.java",
"package com.uber;",
"public class Client {",
" public void testNeg(org.apache.thrift.TBase tBase) {",
" if (tBase.isSet(\"Hello\")) {",
" System.out.println(\"set\");",
" }",
" }",
"}")
.doTest();
}
/** we do not have proper support for Thrift unions yet; just checks that we don't crash */
@Test
public void testThriftUnion() {
defaultCompilationHelper
.addSourceLines(
"TBase.java", "package org.apache.thrift;", "public interface TBase<T, F> {}")
.addSourceLines(
"TUnion.java",
"package org.apache.thrift;",
"public abstract class TUnion<T, F> implements TBase<T, F> {",
" protected Object value_;",
" public Object getFieldValue() {",
" return this.value_;",
" }",
"}")
.addSourceLines(
"Generated.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"public class Generated extends org.apache.thrift.TUnion<String, Integer> {",
" public Object getId() { return getFieldValue(); }",
" public boolean isSetId() { return true; }",
"}")
.addSourceLines(
"Client.java",
"package com.uber;",
"public class Client {",
" public void testNeg(Generated g) {",
" if (!g.isSetId()) {",
" return;",
" }",
" Object x = g.getId();",
" if (x.toString() == null) return;",
" }",
"}")
.doTest();
}
@Test
public void testThriftAndCastToNonNull() {
makeTestHelperWithArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
// We give the following in Regexp format to test that support
"-XepOpt:NullAway:UnannotatedSubPackages=com.uber.nullaway.[a-zA-Z0-9.]+.unannotated",
"-XepOpt:NullAway:ExcludedClassAnnotations=com.uber.nullaway.testdata.TestAnnot",
"-XepOpt:NullAway:CastToNonNullMethod=com.uber.nullaway.testdata.Util.castToNonNull",
"-XepOpt:NullAway:TreatGeneratedAsUnannotated=true",
"-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true"))
.addSourceFile("Util.java")
.addSourceLines("TBase.java", "package org.apache.thrift;", "public interface TBase {}")
.addSourceLines(
"GeneratedClass.java",
"package com.uber.lib.unannotated;",
"import javax.annotation.Nullable;",
"import javax.annotation.Generated;",
"@Generated(\"test\")",
"public class GeneratedClass implements org.apache.thrift.TBase {",
" public @Nullable Object id;",
" @Nullable public Object getId() { return this.id; }",
" // this is to ensure we don't crash on unions",
" public boolean isSet() { return false; }",
" public boolean isSetId() { return this.id != null; }",
"}")
.addSourceLines(
"Client.java",
"package com.uber;",
"import static com.uber.nullaway.testdata.Util.castToNonNull;",
"import com.uber.lib.unannotated.GeneratedClass;",
"public class Client {",
" public void testPos(GeneratedClass g) {",
" // g.getId() is @NonNull because it's treated as unannotated code and RestrictiveAnnotationHandler exempts it",
" // BUG: Diagnostic contains: passing known @NonNull parameter 'g.getId()' to CastToNonNullMethod",
" Object o = castToNonNull(g.getId());",
" o.toString();",
" }",
" public void testNeg(GeneratedClass g) {",
" Object o = g.getId();",
" o.toString();",
" }",
"}")
.doTest();
}
}