Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Fastjson
Commits
0985590c
Commit
0985590c
authored
7 years ago
by
wenshao
Browse files
Options
Download
Email Patches
Plain Diff
improved support kotlin data class. #1374
parent
f4e746a6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java
+7
-4
src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java
src/main/java/com/alibaba/fastjson/util/TypeUtils.java
+49
-13
src/main/java/com/alibaba/fastjson/util/TypeUtils.java
src/test/java/com/alibaba/json/bvt/kotlin/DataClassTest.java
+1
-1
src/test/java/com/alibaba/json/bvt/kotlin/DataClassTest.java
src/test/java/com/alibaba/json/bvt/kotlin/MyStateObjectTest.java
+38
-0
...t/java/com/alibaba/json/bvt/kotlin/MyStateObjectTest.java
src/test/resources/kotlin/MyStateObject.clazz
+0
-0
src/test/resources/kotlin/MyStateObject.clazz
with
95 additions
and
18 deletions
+95
-18
src/main/java/com/alibaba/fastjson/parser/JavaBeanInfo.java
+
7
-
4
View file @
0985590c
...
...
@@ -286,9 +286,10 @@ class JavaBeanInfo {
Type
[]
getGenericParameterTypes
=
fieldGenericSupport
//
?
creatorConstructor
.
getGenericParameterTypes
()
//
:
parameterTypes
;
Annotation
[][]
paramAnnotationArrays
=
creatorConstructor
.
getParameterAnnotations
();
for
(
int
i
=
0
;
i
<
parameterTypes
.
length
;
++
i
)
{
Annotation
[]
paramAnnotations
=
creatorConstructor
.
getParameter
Annotation
s
()
[
i
];
Annotation
[]
paramAnnotations
=
param
Annotation
Arrays
[
i
];
JSONField
fieldAnnotation
=
null
;
for
(
Annotation
paramAnnotation
:
paramAnnotations
)
{
if
(
paramAnnotation
instanceof
JSONField
)
{
...
...
@@ -341,8 +342,9 @@ class JavaBeanInfo {
?
factoryMethod
.
getGenericParameterTypes
()
//
:
parameterTypes
;
Annotation
[][]
paramAnnotationArrays
=
factoryMethod
.
getParameterAnnotations
();
for
(
int
i
=
0
;
i
<
parameterTypes
.
length
;
++
i
)
{
Annotation
[]
paramAnnotations
=
factoryMethod
.
getParameter
Annotation
s
()
[
i
];
Annotation
[]
paramAnnotations
=
param
Annotation
Arrays
[
i
];
JSONField
fieldAnnotation
=
null
;
for
(
Annotation
paramAnnotation
:
paramAnnotations
)
{
if
(
paramAnnotation
instanceof
JSONField
)
{
...
...
@@ -397,10 +399,11 @@ class JavaBeanInfo {
?
creatorConstructor
.
getGenericParameterTypes
()
//
:
parameterTypes
;
Annotation
[][]
paramAnnotationArrays
=
creatorConstructor
.
getParameterAnnotations
();
for
(
int
i
=
0
;
i
<
parameterTypes
.
length
;
++
i
)
{
String
paramName
=
parameters
[
i
];
Annotation
[]
paramAnnotations
=
creatorConstructor
.
getParameter
Annotation
s
()
[
i
];
Annotation
[]
paramAnnotations
=
param
Annotation
Arrays
[
i
];
JSONField
fieldAnnotation
=
null
;
for
(
Annotation
paramAnnotation
:
paramAnnotations
)
{
if
(
paramAnnotation
instanceof
JSONField
)
{
...
...
This diff is collapsed.
Click to expand it.
src/main/java/com/alibaba/fastjson/util/TypeUtils.java
+
49
-
13
View file @
0985590c
...
...
@@ -15,25 +15,14 @@
*/
package
com.alibaba.fastjson.util
;
import
java.lang.annotation.Annotation
;
import
java.lang.reflect.*
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.security.AccessControlException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.TreeSet
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
...
...
@@ -926,6 +915,8 @@ public class TypeUtils {
Map
<
String
,
FieldInfo
>
fieldInfoMap
=
new
LinkedHashMap
<
String
,
FieldInfo
>();
Map
<
Class
<?>,
Field
[]>
classFieldCache
=
new
HashMap
<
Class
<?>,
Field
[]>();
boolean
kotlin
=
TypeUtils
.
isKotlin
(
clazz
);
Field
[]
declaredFields
=
clazz
.
getDeclaredFields
();
if
(!
fieldOnly
)
{
List
<
Method
>
methodList
=
new
ArrayList
<
Method
>();
...
...
@@ -954,6 +945,11 @@ public class TypeUtils {
}
}
// for kotlin
Constructor
[]
constructors
=
null
;
Annotation
[][]
paramAnnotationArrays
=
null
;
String
[]
paramNames
=
null
;
short
[]
paramNameMapping
=
null
;
for
(
Method
method
:
methodList
)
{
String
methodName
=
method
.
getName
();
...
...
@@ -970,6 +966,46 @@ public class TypeUtils {
annotation
=
getSupperMethodAnnotation
(
clazz
,
method
);
}
if
(
annotation
==
null
&&
kotlin
)
{
if
(
constructors
==
null
)
{
constructors
=
clazz
.
getDeclaredConstructors
();
if
(
constructors
.
length
==
1
)
{
paramAnnotationArrays
=
constructors
[
0
].
getParameterAnnotations
();
paramNames
=
TypeUtils
.
getKoltinConstructorParameters
(
clazz
);
if
(
paramNames
!=
null
)
{
String
[]
paramNames_sorted
=
new
String
[
paramNames
.
length
];
System
.
arraycopy
(
paramNames
,
0
,
paramNames_sorted
,
0
,
paramNames
.
length
);
Arrays
.
sort
(
paramNames_sorted
);
paramNameMapping
=
new
short
[
paramNames
.
length
];
for
(
short
p
=
0
;
p
<
paramNames
.
length
;
p
++)
{
int
index
=
Arrays
.
binarySearch
(
paramNames_sorted
,
paramNames
[
p
]);
paramNameMapping
[
index
]
=
p
;
}
paramNames
=
paramNames_sorted
;
}
}
}
if
(
paramNames
!=
null
&&
paramNameMapping
!=
null
&&
methodName
.
startsWith
(
"get"
))
{
String
propertyName
=
decapitalize
(
methodName
.
substring
(
3
));
int
p
=
Arrays
.
binarySearch
(
paramNames
,
propertyName
);
if
(
p
>=
0
)
{
short
index
=
paramNameMapping
[
p
];
Annotation
[]
paramAnnotations
=
paramAnnotationArrays
[
index
];
if
(
paramAnnotations
!=
null
)
{
for
(
Annotation
paramAnnotation
:
paramAnnotations
)
{
if
(
paramAnnotation
instanceof
JSONField
)
{
annotation
=
(
JSONField
)
paramAnnotation
;
break
;
}
}
}
}
}
}
if
(
annotation
!=
null
)
{
if
(!
annotation
.
serialize
())
{
continue
;
...
...
This diff is collapsed.
Click to expand it.
src/test/java/com/alibaba/json/bvt/kotlin/DataClassTest.java
+
1
-
1
View file @
0985590c
...
...
@@ -18,7 +18,7 @@ public class DataClassTest extends TestCase {
String
json
=
"{\"aa\":1001,\"bb\":1002}"
;
Object
obj
=
JSON
.
parseObject
(
json
,
clazz
);
assertEquals
(
"{\"a\":1001,\"b\":1002}"
,
JSON
.
toJSONString
(
obj
));
assertEquals
(
"{\"a
a
\":1001,\"b
b
\":1002}"
,
JSON
.
toJSONString
(
obj
));
}
public
static
class
ExtClassLoader
extends
ClassLoader
{
...
...
This diff is collapsed.
Click to expand it.
src/test/java/com/alibaba/json/bvt/kotlin/MyStateObjectTest.java
0 → 100644
+
38
-
0
View file @
0985590c
package
com.alibaba.json.bvt.kotlin
;
import
com.alibaba.fastjson.JSON
;
import
junit.framework.TestCase
;
import
org.apache.commons.io.IOUtils
;
import
java.io.IOException
;
import
java.io.InputStream
;
/**
* Created by wenshao on 06/08/2017.
*/
public
class
MyStateObjectTest
extends
TestCase
{
public
void
test_user
()
throws
Exception
{
ExtClassLoader
classLoader
=
new
ExtClassLoader
();
Class
clazz
=
classLoader
.
loadClass
(
"MyStateObject"
);
String
json
=
"{\"name\":\"wenshao\",\"age\":99}"
;
Object
obj
=
JSON
.
parseObject
(
json
,
clazz
);
assertEquals
(
"{\"age\":99,\"name\":\"wenshao\"}"
,
JSON
.
toJSONString
(
obj
));
}
public
static
class
ExtClassLoader
extends
ClassLoader
{
public
ExtClassLoader
()
throws
IOException
{
super
(
Thread
.
currentThread
().
getContextClassLoader
());
{
byte
[]
bytes
;
InputStream
is
=
Thread
.
currentThread
().
getContextClassLoader
().
getResourceAsStream
(
"kotlin/MyStateObject.clazz"
);
bytes
=
IOUtils
.
toByteArray
(
is
);
is
.
close
();
super
.
defineClass
(
"MyStateObject"
,
bytes
,
0
,
bytes
.
length
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/test/resources/kotlin/MyStateObject.clazz
0 → 100644
+
0
-
0
View file @
0985590c
File added
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help