FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java/src/main/java/com/jsoniter/CodegenImplObject.java at master · akan/java · GitHub
akan
/
java
Public
forked from
json-iterator/java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
CodegenImplObject.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
345 lines (329 loc) · 15.3 KB
Breadcrumbs
java
/
src
/
main
/
java
/
com
/
jsoniter
/
CodegenImplObject.java
Copy path
File metadata and controls
345 lines (329 loc) · 15.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package
com
.
jsoniter
;
import
java
.
lang
.
reflect
.
Type
;
import
java
.
util
.*;
class
CodegenImplObject
{
final
static
Map
<
String
,
String
>
DEFAULT_VALUES
=
new
HashMap
<
String
,
String
>() {{
put
(
"float"
,
"0.0f"
);
put
(
"double"
,
"0.0d"
);
put
(
"boolean"
,
"false"
);
put
(
"byte"
,
"0"
);
put
(
"short"
,
"0"
);
put
(
"int"
,
"0"
);
put
(
"char"
,
"0"
);
put
(
"long"
,
"0"
);
}};
public
static
String
genObjectUsingSlice
(
Class
clazz
,
String
cacheKey
,
CustomizedConstructor
ctor
,
List
<
CustomizedSetter
>
setters
,
List
<
Binding
>
fields
) {
ArrayList
<
Binding
>
allBindings
=
new
ArrayList
<
Binding
>(
fields
);
allBindings
.
addAll
(
ctor
.
parameters
);
for
(
CustomizedSetter
setter
:
setters
) {
allBindings
.
addAll
(
setter
.
parameters
);
}
if
(
allBindings
.
isEmpty
()) {
return
genObjectUsingSkip
(
clazz
,
ctor
);
}
Map
<
Integer
,
Object
>
trieTree
=
buildTriTree
(
allBindings
);
StringBuilder
lines
=
new
StringBuilder
();
append
(
lines
,
"public static Object decode_(com.jsoniter.JsonIterator iter) {"
);
append
(
lines
,
"if (iter.readNull()) { return null; }"
);
if
(
ctor
.
parameters
.
isEmpty
()) {
append
(
lines
,
"{{clazz}} obj = {{newInst}};"
);
append
(
lines
,
"if (!com.jsoniter.CodegenAccess.readObjectStart(iter)) { return obj; }"
);
}
else
{
for
(
Binding
parameter
:
ctor
.
parameters
) {
appendVarDef
(
lines
,
parameter
);
}
append
(
lines
,
"if (!com.jsoniter.CodegenAccess.readObjectStart(iter)) { return {{newInst}}; }"
);
for
(
Binding
field
:
fields
) {
appendVarDef
(
lines
,
field
);
}
}
for
(
CustomizedSetter
setter
:
setters
) {
for
(
Binding
param
:
setter
.
parameters
) {
appendVarDef
(
lines
,
param
);
}
}
append
(
lines
,
"com.jsoniter.CodegenAccess.resetExistingObject(iter);"
);
append
(
lines
,
"com.jsoniter.Slice field = com.jsoniter.CodegenAccess.readObjectFieldAsSlice(iter);"
);
append
(
lines
,
"boolean once = true;"
);
append
(
lines
,
"while (once) {"
);
append
(
lines
,
"once = false;"
);
append
(
lines
,
"switch (field.len) {"
);
String
rendered
=
renderTriTree
(
cacheKey
,
trieTree
);
for
(
Binding
field
:
fields
) {
if
(
ctor
.
parameters
.
isEmpty
() &&
fields
.
contains
(
field
)) {
if
(
shouldReuseObject
(
field
.
valueType
)) {
rendered
=
rendered
.
replace
(
"_"
+
field
.
name
+
"_"
,
String
.
format
(
"obj.%s"
,
field
.
name
));
}
else
{
rendered
=
rendered
.
replace
(
"_"
+
field
.
name
+
"_"
,
String
.
format
(
"com.jsoniter.CodegenAccess.setExistingObject(iter, obj.%s);
\n
obj.%s"
,
field
.
name
,
field
.
name
));
}
}
}
append
(
lines
,
rendered
);
append
(
lines
,
"}"
);
// end of switch
append
(
lines
,
"iter.skip();"
);
append
(
lines
,
"}"
);
// end of while
append
(
lines
,
"while (com.jsoniter.CodegenAccess.nextToken(iter) == ',') {"
);
append
(
lines
,
"field = com.jsoniter.CodegenAccess.readObjectFieldAsSlice(iter);"
);
append
(
lines
,
"switch (field.len) {"
);
append
(
lines
,
rendered
);
append
(
lines
,
"}"
);
// end of switch
append
(
lines
,
"iter.skip();"
);
append
(
lines
,
"}"
);
// end of while
if
(!
ctor
.
parameters
.
isEmpty
()) {
append
(
lines
,
String
.
format
(
"%s obj = {{newInst}};"
,
CodegenImplNative
.
getTypeName
(
clazz
)));
for
(
Binding
field
:
fields
) {
append
(
lines
,
String
.
format
(
"obj.%s = _%s_;"
,
field
.
name
,
field
.
name
));
}
}
appendSetter
(
setters
,
lines
);
append
(
lines
,
"return obj;"
);
append
(
lines
,
"}"
);
return
lines
.
toString
()
.
replace
(
"{{clazz}}"
,
clazz
.
getCanonicalName
())
.
replace
(
"{{newInst}}"
,
genNewInstCode
(
clazz
,
ExtensionManager
.
getCtor
(
clazz
)));
}
private
static
String
renderTriTree
(
String
cacheKey
,
Map
<
Integer
,
Object
>
trieTree
) {
StringBuilder
switchBody
=
new
StringBuilder
();
for
(
Map
.
Entry
<
Integer
,
Object
>
entry
:
trieTree
.
entrySet
()) {
Integer
len
=
entry
.
getKey
();
append
(
switchBody
,
"case "
+
len
+
": "
);
Map
<
Byte
,
Object
>
current
= (
Map
<
Byte
,
Object
>)
entry
.
getValue
();
addFieldDispatch
(
switchBody
,
len
,
0
,
current
,
cacheKey
,
new
ArrayList
<
Byte
>());
append
(
switchBody
,
"break;"
);
}
return
switchBody
.
toString
();
}
private
static
Map
<
Integer
,
Object
>
buildTriTree
(
ArrayList
<
Binding
>
allBindings
) {
Map
<
Integer
,
Object
>
trieTree
=
new
HashMap
<
Integer
,
Object
>();
for
(
Binding
field
:
allBindings
) {
for
(
String
fromName
:
field
.
fromNames
) {
byte
[]
fromNameBytes
=
fromName
.
getBytes
();
Map
<
Byte
,
Object
>
current
= (
Map
<
Byte
,
Object
>)
trieTree
.
get
(
fromNameBytes
.
length
);
if
(
current
==
null
) {
current
=
new
HashMap
<
Byte
,
Object
>();
trieTree
.
put
(
fromNameBytes
.
length
,
current
);
}
for
(
int
i
=
0
;
i
<
fromNameBytes
.
length
-
1
;
i
++) {
byte
b
=
fromNameBytes
[
i
];
Map
<
Byte
,
Object
>
next
= (
Map
<
Byte
,
Object
>)
current
.
get
(
b
);
if
(
next
==
null
) {
next
=
new
HashMap
<
Byte
,
Object
>();
current
.
put
(
b
,
next
);
}
current
=
next
;
}
current
.
put
(
fromNameBytes
[
fromNameBytes
.
length
-
1
],
field
);
}
}
return
trieTree
;
}
private
static
void
addFieldDispatch
(
StringBuilder
lines
,
int
len
,
int
i
,
Map
<
Byte
,
Object
>
current
,
String
cacheKey
,
List
<
Byte
>
bytesToCompare
) {
for
(
Map
.
Entry
<
Byte
,
Object
>
entry
:
current
.
entrySet
()) {
Byte
b
=
entry
.
getKey
();
if
(
i
==
len
-
1
) {
append
(
lines
,
"if ("
);
for
(
int
j
=
0
;
j
<
bytesToCompare
.
size
();
j
++) {
Byte
a
=
bytesToCompare
.
get
(
j
);
append
(
lines
,
String
.
format
(
"field.at(%d)==%s && "
,
i
-
bytesToCompare
.
size
() +
j
,
a
));
}
append
(
lines
,
String
.
format
(
"field.at(%d)==%s"
,
i
,
b
));
append
(
lines
,
") {"
);
Binding
field
= (
Binding
)
entry
.
getValue
();
append
(
lines
,
String
.
format
(
"_%s_ = %s;"
,
field
.
name
,
CodegenImplNative
.
genField
(
field
,
cacheKey
)));
append
(
lines
,
"continue;"
);
append
(
lines
,
"}"
);
continue
;
}
Map
<
Byte
,
Object
>
next
= (
Map
<
Byte
,
Object
>)
entry
.
getValue
();
if
(
next
.
size
() ==
1
) {
ArrayList
<
Byte
>
nextBytesToCompare
=
new
ArrayList
<
Byte
>(
bytesToCompare
);
nextBytesToCompare
.
add
(
b
);
addFieldDispatch
(
lines
,
len
,
i
+
1
,
next
,
cacheKey
,
nextBytesToCompare
);
continue
;
}
append
(
lines
,
"if ("
);
for
(
int
j
=
0
;
j
<
bytesToCompare
.
size
();
j
++) {
Byte
a
=
bytesToCompare
.
get
(
j
);
append
(
lines
,
String
.
format
(
"field.at(%d)==%s && "
,
i
-
bytesToCompare
.
size
() +
j
,
a
));
}
append
(
lines
,
String
.
format
(
"field.at(%d)==%s"
,
i
,
b
));
append
(
lines
,
") {"
);
addFieldDispatch
(
lines
,
len
,
i
+
1
,
next
,
cacheKey
,
new
ArrayList
<
Byte
>());
append
(
lines
,
"continue;"
);
append
(
lines
,
"}"
);
}
}
public
static
String
genObjectUsingHash
(
Class
clazz
,
String
cacheKey
,
CustomizedConstructor
ctor
,
List
<
CustomizedSetter
>
setters
,
List
<
Binding
>
fields
) {
ArrayList
<
Binding
>
allBindings
=
new
ArrayList
<
Binding
>(
fields
);
allBindings
.
addAll
(
ctor
.
parameters
);
for
(
CustomizedSetter
setter
:
setters
) {
allBindings
.
addAll
(
setter
.
parameters
);
}
if
(
allBindings
.
isEmpty
()) {
return
genObjectUsingSkip
(
clazz
,
ctor
);
}
StringBuilder
lines
=
new
StringBuilder
();
append
(
lines
,
"public static Object decode_(com.jsoniter.JsonIterator iter) {"
);
append
(
lines
,
"if (iter.readNull()) { return null; }"
);
if
(
ctor
.
parameters
.
isEmpty
()) {
// has default ctor
append
(
lines
,
"{{clazz}} obj = {{newInst}};"
);
append
(
lines
,
"if (!com.jsoniter.CodegenAccess.readObjectStart(iter)) { return obj; }"
);
}
else
{
// ctor requires binding
for
(
Binding
parameter
:
ctor
.
parameters
) {
appendVarDef
(
lines
,
parameter
);
}
append
(
lines
,
"if (!com.jsoniter.CodegenAccess.readObjectStart(iter)) { return {{newInst}}; }"
);
for
(
Binding
field
:
fields
) {
appendVarDef
(
lines
,
field
);
}
}
for
(
CustomizedSetter
setter
:
setters
) {
for
(
Binding
param
:
setter
.
parameters
) {
appendVarDef
(
lines
,
param
);
}
}
append
(
lines
,
"com.jsoniter.CodegenAccess.resetExistingObject(iter);"
);
append
(
lines
,
"switch (com.jsoniter.CodegenAccess.readObjectFieldAsHash(iter)) {"
);
HashSet
<
Integer
>
knownHashes
=
new
HashSet
<
Integer
>();
for
(
Binding
field
:
allBindings
) {
for
(
String
fromName
:
field
.
fromNames
) {
long
hash
=
0x811c9dc5
;
for
(
byte
b
:
fromName
.
getBytes
()) {
hash
^=
b
;
hash
*=
0x1000193
;
}
int
intHash
= (
int
)
hash
;
if
(
intHash
==
0
) {
// hash collision, 0 can not be used as sentinel
return
genObjectUsingSlice
(
clazz
,
cacheKey
,
ctor
,
setters
,
fields
);
}
if
(
knownHashes
.
contains
(
intHash
)) {
// hash collision with other field can not be used as sentinel
return
genObjectUsingSlice
(
clazz
,
cacheKey
,
ctor
,
setters
,
fields
);
}
knownHashes
.
add
(
intHash
);
append
(
lines
,
"case "
+
intHash
+
": "
);
appendFieldSet
(
lines
,
cacheKey
,
ctor
,
fields
,
field
);
append
(
lines
,
"break;"
);
}
}
append
(
lines
,
"default:"
);
append
(
lines
,
"iter.skip();"
);
append
(
lines
,
"}"
);
append
(
lines
,
"while (com.jsoniter.CodegenAccess.nextToken(iter) == ',') {"
);
append
(
lines
,
"switch (com.jsoniter.CodegenAccess.readObjectFieldAsHash(iter)) {"
);
for
(
Binding
field
:
allBindings
) {
for
(
String
fromName
:
field
.
fromNames
) {
long
hash
=
0x811c9dc5
;
for
(
byte
b
:
fromName
.
getBytes
()) {
hash
^=
b
;
hash
*=
0x1000193
;
}
int
intHash
= (
int
)
hash
;
append
(
lines
,
"case "
+
intHash
+
": "
);
appendFieldSet
(
lines
,
cacheKey
,
ctor
,
fields
,
field
);
append
(
lines
,
"continue;"
);
}
}
append
(
lines
,
"}"
);
append
(
lines
,
"iter.skip();"
);
append
(
lines
,
"}"
);
if
(!
ctor
.
parameters
.
isEmpty
()) {
append
(
lines
,
CodegenImplNative
.
getTypeName
(
clazz
) +
" obj = {{newInst}};"
);
for
(
Binding
field
:
fields
) {
append
(
lines
,
String
.
format
(
"obj.%s = _%s_;"
,
field
.
name
,
field
.
name
));
}
}
appendSetter
(
setters
,
lines
);
append
(
lines
,
"return obj;"
);
append
(
lines
,
"}"
);
return
lines
.
toString
()
.
replace
(
"{{clazz}}"
,
clazz
.
getCanonicalName
())
.
replace
(
"{{newInst}}"
,
genNewInstCode
(
clazz
,
ctor
));
}
private
static
void
appendFieldSet
(
StringBuilder
lines
,
String
cacheKey
,
CustomizedConstructor
ctor
,
List
<
Binding
>
fields
,
Binding
field
) {
if
(
ctor
.
parameters
.
isEmpty
() &&
fields
.
contains
(
field
)) {
if
(!
shouldReuseObject
(
field
.
valueType
)) {
append
(
lines
,
String
.
format
(
"com.jsoniter.CodegenAccess.setExistingObject(iter, obj.%s);"
,
field
.
name
));
}
append
(
lines
,
String
.
format
(
"obj.%s = %s;"
,
field
.
name
,
CodegenImplNative
.
genField
(
field
,
cacheKey
)));
}
else
{
append
(
lines
,
String
.
format
(
"_%s_ = %s;"
,
field
.
name
,
CodegenImplNative
.
genField
(
field
,
cacheKey
)));
}
}
private
static
void
appendSetter
(
List
<
CustomizedSetter
>
setters
,
StringBuilder
lines
) {
for
(
CustomizedSetter
setter
:
setters
) {
lines
.
append
(
"obj."
);
lines
.
append
(
setter
.
methodName
);
appendInvocation
(
lines
,
setter
.
parameters
);
lines
.
append
(
";
\n
"
);
}
}
private
static
void
appendVarDef
(
StringBuilder
lines
,
Binding
parameter
) {
String
typeName
=
CodegenImplNative
.
getTypeName
(
parameter
.
valueType
);
append
(
lines
,
String
.
format
(
"%s _%s_ = %s;"
,
typeName
,
parameter
.
name
,
DEFAULT_VALUES
.
get
(
typeName
)));
}
private
static
String
genObjectUsingSkip
(
Class
clazz
,
CustomizedConstructor
ctor
) {
StringBuilder
lines
=
new
StringBuilder
();
append
(
lines
,
"public static Object decode_(com.jsoniter.JsonIterator iter) {"
);
append
(
lines
,
"if (iter.readNull()) { return null; }"
);
append
(
lines
,
"{{clazz}} obj = {{newInst}};"
);
append
(
lines
,
"iter.skip();"
);
append
(
lines
,
"return obj;"
);
append
(
lines
,
"}"
);
return
lines
.
toString
()
.
replace
(
"{{clazz}}"
,
clazz
.
getCanonicalName
())
.
replace
(
"{{newInst}}"
,
genNewInstCode
(
clazz
,
ctor
));
}
private
static
String
genNewInstCode
(
Class
clazz
,
CustomizedConstructor
ctor
) {
StringBuilder
code
=
new
StringBuilder
();
if
(
ctor
.
parameters
.
isEmpty
()) {
// nothing to bind, safe to reuse existing object
code
.
append
(
"(com.jsoniter.CodegenAccess.existingObject(iter) == null ? "
);
}
if
(
ctor
.
staticMethodName
==
null
) {
code
.
append
(
String
.
format
(
"new %s"
,
clazz
.
getCanonicalName
()));
}
else
{
code
.
append
(
String
.
format
(
"%s.%s"
,
clazz
.
getCanonicalName
(),
ctor
.
staticMethodName
));
}
List
<
Binding
>
params
=
ctor
.
parameters
;
appendInvocation
(
code
,
params
);
if
(
ctor
.
parameters
.
isEmpty
()) {
// nothing to bind, safe to reuse existing object
code
.
append
(
String
.
format
(
" : (%s)com.jsoniter.CodegenAccess.existingObject(iter))"
,
clazz
.
getCanonicalName
()));
}
return
code
.
toString
();
}
private
static
void
appendInvocation
(
StringBuilder
code
,
List
<
Binding
>
params
) {
code
.
append
(
"("
);
boolean
isFirst
=
true
;
for
(
Binding
ctorParam
:
params
) {
if
(
isFirst
) {
isFirst
=
false
;
}
else
{
code
.
append
(
","
);
}
code
.
append
(
String
.
format
(
"_%s_"
,
ctorParam
.
name
));
}
code
.
append
(
")"
);
}
private
static
void
append
(
StringBuilder
lines
,
String
str
) {
lines
.
append
(
str
);
lines
.
append
(
"
\n
"
);
}
public
static
boolean
shouldReuseObject
(
Type
valueType
) {
if
(
valueType
instanceof
Class
) {
Class
clazz
= (
Class
)
valueType
;
if
(
clazz
.
isArray
()) {
return
false
;
}
}
return
CodegenImplNative
.
isNative
(
valueType
);
}
}
Back
|
FazBrowse Home
|
New Git URL