Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
593de838
Commit
593de838
authored
May 17, 2022
by
Akka
Committed by
Melledy
May 16, 2022
Browse files
optimized the lua serializer
parent
1925bf64
Changes
15
Hide whitespace changes
Inline
Side-by-side
build.gradle
View file @
593de838
...
@@ -87,6 +87,7 @@ dependencies {
...
@@ -87,6 +87,7 @@ dependencies {
implementation
group:
'org.luaj'
,
name:
'luaj-jse'
,
version:
'3.0.1'
implementation
group:
'org.luaj'
,
name:
'luaj-jse'
,
version:
'3.0.1'
implementation
group:
'ch.ethz.globis.phtree'
,
name
:
'phtree'
,
version:
'2.5.0'
implementation
group:
'ch.ethz.globis.phtree'
,
name
:
'phtree'
,
version:
'2.5.0'
implementation
group:
'com.esotericsoftware'
,
name
:
'reflectasm'
,
version:
'1.11.9'
protobuf
files
(
'proto/'
)
protobuf
files
(
'proto/'
)
...
...
src/main/java/emu/grasscutter/scripts/SceneScriptManager.java
View file @
593de838
...
@@ -241,7 +241,7 @@ public class SceneScriptManager {
...
@@ -241,7 +241,7 @@ public class SceneScriptManager {
}
}
public
void
spawnGadgetsInGroup
(
SceneGroup
group
,
SceneSuite
suite
)
{
public
void
spawnGadgetsInGroup
(
SceneGroup
group
,
SceneSuite
suite
)
{
List
<
SceneGadget
>
gadgets
=
group
.
gadgets
;
var
gadgets
=
group
.
gadgets
.
values
()
;
if
(
suite
!=
null
)
{
if
(
suite
!=
null
)
{
gadgets
=
suite
.
sceneGadgets
;
gadgets
=
suite
.
sceneGadgets
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneBlock.java
View file @
593de838
...
@@ -6,6 +6,8 @@ import emu.grasscutter.Grasscutter;
...
@@ -6,6 +6,8 @@ import emu.grasscutter.Grasscutter;
import
emu.grasscutter.scripts.SceneIndexManager
;
import
emu.grasscutter.scripts.SceneIndexManager
;
import
emu.grasscutter.scripts.ScriptLoader
;
import
emu.grasscutter.scripts.ScriptLoader
;
import
emu.grasscutter.utils.Position
;
import
emu.grasscutter.utils.Position
;
import
lombok.Setter
;
import
lombok.ToString
;
import
javax.script.Bindings
;
import
javax.script.Bindings
;
import
javax.script.CompiledScript
;
import
javax.script.CompiledScript
;
...
@@ -14,6 +16,8 @@ import java.util.List;
...
@@ -14,6 +16,8 @@ import java.util.List;
import
static
emu
.
grasscutter
.
Configuration
.
SCRIPT
;
import
static
emu
.
grasscutter
.
Configuration
.
SCRIPT
;
@ToString
@Setter
public
class
SceneBlock
{
public
class
SceneBlock
{
public
int
id
;
public
int
id
;
public
Position
max
;
public
Position
max
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneConfig.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
emu.grasscutter.utils.Position
;
import
emu.grasscutter.utils.Position
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneConfig
{
public
class
SceneConfig
{
public
Position
vision_anchor
;
public
Position
vision_anchor
;
public
Position
born_pos
;
public
Position
born_pos
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneGadget.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneGadget
extends
SceneObject
{
public
class
SceneGadget
extends
SceneObject
{
public
int
gadget_id
;
public
int
gadget_id
;
public
int
state
;
public
int
state
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneGroup.java
View file @
593de838
...
@@ -3,19 +3,22 @@ package emu.grasscutter.scripts.data;
...
@@ -3,19 +3,22 @@ package emu.grasscutter.scripts.data;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.scripts.ScriptLoader
;
import
emu.grasscutter.scripts.ScriptLoader
;
import
emu.grasscutter.utils.Position
;
import
emu.grasscutter.utils.Position
;
import
it.unimi.dsi.fastutil.ints.Int2ObjectMap
;
import
lombok.Setter
;
import
it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
;
import
lombok.ToString
;
import
javax.script.Bindings
;
import
javax.script.Bindings
;
import
javax.script.CompiledScript
;
import
javax.script.CompiledScript
;
import
javax.script.ScriptException
;
import
javax.script.ScriptException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
static
emu
.
grasscutter
.
Configuration
.
SCRIPTS_FOLDER
;
import
static
emu
.
grasscutter
.
Configuration
.
SCRIPTS_FOLDER
;
@ToString
@Setter
public
class
SceneGroup
{
public
class
SceneGroup
{
public
transient
int
block_id
;
// Not an actual variable in the scripts but we will keep it here for reference
public
transient
int
block_id
;
// Not an actual variable in the scripts but we will keep it here for reference
...
@@ -27,7 +30,10 @@ public class SceneGroup {
...
@@ -27,7 +30,10 @@ public class SceneGroup {
* ConfigId - Monster
* ConfigId - Monster
*/
*/
public
Map
<
Integer
,
SceneMonster
>
monsters
;
public
Map
<
Integer
,
SceneMonster
>
monsters
;
public
List
<
SceneGadget
>
gadgets
;
/**
* ConfigId - Gadget
*/
public
Map
<
Integer
,
SceneGadget
>
gadgets
;
public
List
<
SceneTrigger
>
triggers
;
public
List
<
SceneTrigger
>
triggers
;
public
List
<
SceneRegion
>
regions
;
public
List
<
SceneRegion
>
regions
;
public
List
<
SceneSuite
>
suites
;
public
List
<
SceneSuite
>
suites
;
...
@@ -76,8 +82,15 @@ public class SceneGroup {
...
@@ -76,8 +82,15 @@ public class SceneGroup {
// Set
// Set
monsters
=
ScriptLoader
.
getSerializer
().
toList
(
SceneMonster
.
class
,
bindings
.
get
(
"monsters"
)).
stream
()
monsters
=
ScriptLoader
.
getSerializer
().
toList
(
SceneMonster
.
class
,
bindings
.
get
(
"monsters"
)).
stream
()
.
collect
(
Collectors
.
toMap
(
x
->
x
.
config_id
,
y
->
y
));
.
collect
(
Collectors
.
toMap
(
x
->
x
.
config_id
,
y
->
y
));
gadgets
=
ScriptLoader
.
getSerializer
().
toList
(
SceneGadget
.
class
,
bindings
.
get
(
"gadgets"
));
monsters
.
values
().
forEach
(
m
->
m
.
groupId
=
id
);
gadgets
=
ScriptLoader
.
getSerializer
().
toList
(
SceneGadget
.
class
,
bindings
.
get
(
"gadgets"
)).
stream
()
.
collect
(
Collectors
.
toMap
(
x
->
x
.
config_id
,
y
->
y
));
gadgets
.
values
().
forEach
(
m
->
m
.
groupId
=
id
);
triggers
=
ScriptLoader
.
getSerializer
().
toList
(
SceneTrigger
.
class
,
bindings
.
get
(
"triggers"
));
triggers
=
ScriptLoader
.
getSerializer
().
toList
(
SceneTrigger
.
class
,
bindings
.
get
(
"triggers"
));
triggers
.
forEach
(
t
->
t
.
currentGroup
=
this
);
suites
=
ScriptLoader
.
getSerializer
().
toList
(
SceneSuite
.
class
,
bindings
.
get
(
"suites"
));
suites
=
ScriptLoader
.
getSerializer
().
toList
(
SceneSuite
.
class
,
bindings
.
get
(
"suites"
));
regions
=
ScriptLoader
.
getSerializer
().
toList
(
SceneRegion
.
class
,
bindings
.
get
(
"regions"
));
regions
=
ScriptLoader
.
getSerializer
().
toList
(
SceneRegion
.
class
,
bindings
.
get
(
"regions"
));
init_config
=
ScriptLoader
.
getSerializer
().
toObject
(
SceneInitConfig
.
class
,
bindings
.
get
(
"init_config"
));
init_config
=
ScriptLoader
.
getSerializer
().
toObject
(
SceneInitConfig
.
class
,
bindings
.
get
(
"init_config"
));
...
@@ -85,35 +98,21 @@ public class SceneGroup {
...
@@ -85,35 +98,21 @@ public class SceneGroup {
// Add variables to suite
// Add variables to suite
variables
=
ScriptLoader
.
getSerializer
().
toList
(
SceneVar
.
class
,
bindings
.
get
(
"variables"
));
variables
=
ScriptLoader
.
getSerializer
().
toList
(
SceneVar
.
class
,
bindings
.
get
(
"variables"
));
// Add monsters to suite
// Add monsters to suite TODO optimize
Int2ObjectMap
<
Object
>
map
=
new
Int2ObjectOpenHashMap
<>();
monsters
.
entrySet
().
forEach
(
m
->
map
.
put
(
m
.
getValue
().
config_id
,
m
));
monsters
.
values
().
forEach
(
m
->
m
.
groupId
=
id
);
gadgets
.
forEach
(
m
->
map
.
put
(
m
.
config_id
,
m
));
gadgets
.
forEach
(
m
->
m
.
groupId
=
id
);
for
(
SceneSuite
suite
:
suites
)
{
for
(
SceneSuite
suite
:
suites
)
{
suite
.
sceneMonsters
=
new
ArrayList
<>(
suite
.
monsters
.
size
());
suite
.
sceneMonsters
=
new
ArrayList
<>(
suite
.
monsters
.
forEach
(
id
->
{
suite
.
monsters
.
stream
()
Object
objEntry
=
map
.
get
(
id
.
intValue
());
.
filter
(
monsters:
:
containsKey
)
if
(
objEntry
instanceof
Map
.
Entry
<?,?>
monsterEntry
)
{
.
map
(
monsters:
:
get
)
Object
monster
=
monsterEntry
.
getValue
();
.
toList
()
if
(
monster
instanceof
SceneMonster
sceneMonster
){
);
suite
.
sceneMonsters
.
add
(
sceneMonster
);
}
suite
.
sceneGadgets
=
new
ArrayList
<>(
}
suite
.
gadgets
.
stream
()
});
.
filter
(
gadgets:
:
containsKey
)
.
map
(
gadgets:
:
get
)
suite
.
sceneGadgets
=
new
ArrayList
<>(
suite
.
gadgets
.
size
());
.
toList
()
for
(
int
id
:
suite
.
gadgets
)
{
);
try
{
SceneGadget
gadget
=
(
SceneGadget
)
map
.
get
(
id
);
if
(
gadget
!=
null
)
{
suite
.
sceneGadgets
.
add
(
gadget
);
}
}
catch
(
Exception
ignored
)
{
}
}
}
}
}
catch
(
ScriptException
e
)
{
}
catch
(
ScriptException
e
)
{
...
...
src/main/java/emu/grasscutter/scripts/data/SceneInitConfig.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
emu.grasscutter.utils.Position
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneInitConfig
{
public
class
SceneInitConfig
{
public
int
suite
;
public
int
suite
;
public
int
end_suite
;
public
int
end_suite
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneMeta.java
View file @
593de838
...
@@ -5,6 +5,9 @@ import ch.ethz.globis.phtree.v16.PhTree16;
...
@@ -5,6 +5,9 @@ import ch.ethz.globis.phtree.v16.PhTree16;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.scripts.SceneIndexManager
;
import
emu.grasscutter.scripts.SceneIndexManager
;
import
emu.grasscutter.scripts.ScriptLoader
;
import
emu.grasscutter.scripts.ScriptLoader
;
import
lombok.Data
;
import
lombok.Setter
;
import
lombok.ToString
;
import
javax.script.Bindings
;
import
javax.script.Bindings
;
import
javax.script.CompiledScript
;
import
javax.script.CompiledScript
;
...
@@ -15,6 +18,8 @@ import java.util.stream.Collectors;
...
@@ -15,6 +18,8 @@ import java.util.stream.Collectors;
import
static
emu
.
grasscutter
.
Configuration
.
SCRIPT
;
import
static
emu
.
grasscutter
.
Configuration
.
SCRIPT
;
@ToString
@Setter
public
class
SceneMeta
{
public
class
SceneMeta
{
public
SceneConfig
config
;
public
SceneConfig
config
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneMonster.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneMonster
extends
SceneObject
{
public
class
SceneMonster
extends
SceneObject
{
public
int
monster_id
;
public
int
monster_id
;
}
}
\ No newline at end of file
src/main/java/emu/grasscutter/scripts/data/SceneObject.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
emu.grasscutter.utils.Position
;
import
emu.grasscutter.utils.Position
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneObject
{
public
class
SceneObject
{
public
int
level
;
public
int
level
;
public
int
config_id
;
public
int
config_id
;
...
@@ -11,5 +15,5 @@ public class SceneObject {
...
@@ -11,5 +15,5 @@ public class SceneObject {
/**
/**
* not set by lua
* not set by lua
*/
*/
public
int
groupId
;
public
transient
int
groupId
;
}
}
src/main/java/emu/grasscutter/scripts/data/SceneRegion.java
View file @
593de838
...
@@ -5,7 +5,12 @@ import emu.grasscutter.scripts.constants.ScriptRegionShape;
...
@@ -5,7 +5,12 @@ import emu.grasscutter.scripts.constants.ScriptRegionShape;
import
emu.grasscutter.utils.Position
;
import
emu.grasscutter.utils.Position
;
import
it.unimi.dsi.fastutil.ints.IntOpenHashSet
;
import
it.unimi.dsi.fastutil.ints.IntOpenHashSet
;
import
it.unimi.dsi.fastutil.ints.IntSet
;
import
it.unimi.dsi.fastutil.ints.IntSet
;
import
lombok.Data
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneRegion
{
public
class
SceneRegion
{
public
int
config_id
;
public
int
config_id
;
public
int
shape
;
public
int
shape
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneSuite.java
View file @
593de838
...
@@ -2,8 +2,11 @@ package emu.grasscutter.scripts.data;
...
@@ -2,8 +2,11 @@ package emu.grasscutter.scripts.data;
import
java.util.List
;
import
java.util.List
;
import
emu.grasscutter.utils.Position
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneSuite
{
public
class
SceneSuite
{
public
List
<
Integer
>
monsters
;
public
List
<
Integer
>
monsters
;
public
List
<
Integer
>
gadgets
;
public
List
<
Integer
>
gadgets
;
...
...
src/main/java/emu/grasscutter/scripts/data/SceneTrigger.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
lombok.Setter
;
@Setter
public
class
SceneTrigger
{
public
class
SceneTrigger
{
public
String
name
;
public
String
name
;
public
int
config_id
;
public
int
config_id
;
...
@@ -8,6 +11,7 @@ public class SceneTrigger {
...
@@ -8,6 +11,7 @@ public class SceneTrigger {
public
String
condition
;
public
String
condition
;
public
String
action
;
public
String
action
;
public
SceneGroup
currentGroup
;
@Override
@Override
public
boolean
equals
(
Object
obj
)
{
public
boolean
equals
(
Object
obj
)
{
if
(
obj
instanceof
SceneTrigger
sceneTrigger
){
if
(
obj
instanceof
SceneTrigger
sceneTrigger
){
...
...
src/main/java/emu/grasscutter/scripts/data/SceneVar.java
View file @
593de838
package
emu.grasscutter.scripts.data
;
package
emu.grasscutter.scripts.data
;
import
lombok.Setter
;
import
lombok.ToString
;
@ToString
@Setter
public
class
SceneVar
{
public
class
SceneVar
{
public
String
name
;
public
String
name
;
public
int
value
;
public
int
value
;
...
...
src/main/java/emu/grasscutter/scripts/serializer/LuaSerializer.java
View file @
593de838
package
emu.grasscutter.scripts.serializer
;
package
emu.grasscutter.scripts.serializer
;
import
java.lang.reflect.Field
;
import
com.esotericsoftware.reflectasm.ConstructorAccess
;
import
java.util.ArrayList
;
import
com.esotericsoftware.reflectasm.MethodAccess
;
import
java.util.List
;
import
lombok.AccessLevel
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.experimental.FieldDefaults
;
import
org.luaj.vm2.LuaTable
;
import
org.luaj.vm2.LuaTable
;
import
org.luaj.vm2.LuaValue
;
import
org.luaj.vm2.LuaValue
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
LuaSerializer
implements
Serializer
{
public
class
LuaSerializer
implements
Serializer
{
private
final
static
Map
<
Class
<?>,
MethodAccess
>
methodAccessCache
=
new
ConcurrentHashMap
<>();
private
final
static
Map
<
Class
<?>,
ConstructorAccess
<?>>
constructorCache
=
new
ConcurrentHashMap
<>();
private
final
static
Map
<
Class
<?>,
Map
<
String
,
FieldMeta
>>
fieldMetaCache
=
new
ConcurrentHashMap
<>();
@Override
@Override
public
<
T
>
List
<
T
>
toList
(
Class
<
T
>
type
,
Object
obj
)
{
public
<
T
>
List
<
T
>
toList
(
Class
<
T
>
type
,
Object
obj
)
{
return
serializeList
(
type
,
(
LuaTable
)
obj
);
return
serializeList
(
type
,
(
LuaTable
)
obj
);
...
@@ -20,7 +29,7 @@ public class LuaSerializer implements Serializer {
...
@@ -20,7 +29,7 @@ public class LuaSerializer implements Serializer {
}
}
public
<
T
>
List
<
T
>
serializeList
(
Class
<
T
>
type
,
LuaTable
table
)
{
public
<
T
>
List
<
T
>
serializeList
(
Class
<
T
>
type
,
LuaTable
table
)
{
List
<
T
>
list
=
new
ArrayList
();
List
<
T
>
list
=
new
ArrayList
<>
();
try
{
try
{
LuaValue
[]
keys
=
table
.
keys
();
LuaValue
[]
keys
=
table
.
keys
();
...
@@ -55,7 +64,7 @@ public class LuaSerializer implements Serializer {
...
@@ -55,7 +64,7 @@ public class LuaSerializer implements Serializer {
return
list
;
return
list
;
}
}
public
<
T
>
T
serialize
(
Class
<
T
>
type
,
LuaTable
table
)
{
public
<
T
>
T
serialize
(
Class
<
T
>
type
,
LuaTable
table
)
{
T
object
=
null
;
T
object
=
null
;
...
@@ -70,30 +79,34 @@ public class LuaSerializer implements Serializer {
...
@@ -70,30 +79,34 @@ public class LuaSerializer implements Serializer {
}
}
try
{
try
{
//noinspection ConfusingArgumentToVarargsMethod
if
(!
methodAccessCache
.
containsKey
(
type
)){
object
=
type
.
getDeclaredConstructor
().
newInstance
(
null
);
cacheType
(
type
);
}
var
methodAccess
=
methodAccessCache
.
get
(
type
);
var
fieldMetaMap
=
fieldMetaCache
.
get
(
type
);
object
=
(
T
)
constructorCache
.
get
(
type
).
newInstance
();
LuaValue
[]
keys
=
table
.
keys
();
LuaValue
[]
keys
=
table
.
keys
();
for
(
LuaValue
k
:
keys
)
{
for
(
LuaValue
k
:
keys
)
{
try
{
try
{
Field
field
=
getField
(
object
.
getClass
(),
k
.
checkjstring
()
)
;
var
keyName
=
k
.
checkjstring
();
if
(
field
==
null
)
{
if
(
!
field
MetaMap
.
containsKey
(
keyName
))
{
continue
;
continue
;
}
}
var
fieldMeta
=
fieldMetaMap
.
get
(
keyName
);
field
.
setAccessible
(
true
);
LuaValue
keyValue
=
table
.
get
(
k
);
LuaValue
keyValue
=
table
.
get
(
k
);
if
(
keyValue
.
istable
())
{
if
(
keyValue
.
istable
())
{
field
.
set
(
object
,
serialize
(
field
.
getType
(),
keyValue
.
checktable
()));
methodAccess
.
invoke
(
object
,
fieldMeta
.
index
,
serialize
(
field
Meta
.
getType
(),
keyValue
.
checktable
()));
}
else
if
(
field
.
getType
().
equals
(
float
.
class
))
{
}
else
if
(
field
Meta
.
getType
().
equals
(
float
.
class
))
{
field
.
setFloat
(
object
,
keyValue
.
tofloat
());
methodAccess
.
invoke
(
object
,
fieldMeta
.
index
,
keyValue
.
tofloat
());
}
else
if
(
field
.
getType
().
equals
(
int
.
class
))
{
}
else
if
(
field
Meta
.
getType
().
equals
(
int
.
class
))
{
field
.
setInt
(
object
,
keyValue
.
toint
());
methodAccess
.
invoke
(
object
,
fieldMeta
.
index
,
keyValue
.
toint
());
}
else
if
(
field
.
getType
().
equals
(
String
.
class
))
{
}
else
if
(
field
Meta
.
getType
().
equals
(
String
.
class
))
{
field
.
set
(
object
,
keyValue
.
tojstring
());
methodAccess
.
invoke
(
object
,
fieldMeta
.
index
,
keyValue
.
tojstring
());
}
else
{
}
else
{
field
.
set
(
object
,
keyValue
);
methodAccess
.
invoke
(
object
,
fieldMeta
.
index
,
keyValue
.
tojstring
()
);
}
}
}
catch
(
Exception
ex
)
{
}
catch
(
Exception
ex
)
{
//ex.printStackTrace();
//ex.printStackTrace();
...
@@ -107,16 +120,57 @@ public class LuaSerializer implements Serializer {
...
@@ -107,16 +120,57 @@ public class LuaSerializer implements Serializer {
return
object
;
return
object
;
}
}
public
<
T
>
Field
getField
(
Class
<
T
>
clazz
,
String
name
){
public
<
T
>
Map
<
String
,
FieldMeta
>
cacheType
(
Class
<
T
>
type
){
try
{
if
(
fieldMetaCache
.
containsKey
(
type
))
{
return
clazz
.
getField
(
name
);
return
fieldMetaCache
.
get
(
type
);
}
catch
(
NoSuchFieldException
ex
)
{
}
try
{
if
(!
constructorCache
.
containsKey
(
type
)){
return
clazz
.
getDeclaredField
(
name
);
constructorCache
.
putIfAbsent
(
type
,
ConstructorAccess
.
get
(
type
));
}
catch
(
NoSuchFieldException
e
)
{
}
// ignore
var
methodAccess
=
Optional
.
ofNullable
(
methodAccessCache
.
get
(
type
)).
orElse
(
MethodAccess
.
get
(
type
));
return
null
;
methodAccessCache
.
putIfAbsent
(
type
,
methodAccess
);
}
var
fieldMetaMap
=
new
HashMap
<
String
,
FieldMeta
>();
var
methodNameSet
=
new
HashSet
<>(
Arrays
.
stream
(
methodAccess
.
getMethodNames
()).
toList
());
Arrays
.
stream
(
type
.
getDeclaredFields
())
.
filter
(
field
->
methodNameSet
.
contains
(
getSetterName
(
field
.
getName
())))
.
forEach
(
field
->
{
var
setter
=
getSetterName
(
field
.
getName
());
var
index
=
methodAccess
.
getIndex
(
setter
);
fieldMetaMap
.
put
(
field
.
getName
(),
new
FieldMeta
(
field
.
getName
(),
setter
,
index
,
field
.
getType
()));
});
Arrays
.
stream
(
type
.
getFields
())
.
filter
(
field
->
!
fieldMetaMap
.
containsKey
(
field
.
getName
()))
.
filter
(
field
->
methodNameSet
.
contains
(
getSetterName
(
field
.
getName
())))
.
forEach
(
field
->
{
var
setter
=
getSetterName
(
field
.
getName
());
var
index
=
methodAccess
.
getIndex
(
setter
);
fieldMetaMap
.
put
(
field
.
getName
(),
new
FieldMeta
(
field
.
getName
(),
setter
,
index
,
field
.
getType
()));
});
fieldMetaCache
.
put
(
type
,
fieldMetaMap
);
return
fieldMetaMap
;
}
public
String
getSetterName
(
String
fieldName
){
if
(
fieldName
==
null
||
fieldName
.
length
()
==
0
){
return
null
;
}
}
if
(
fieldName
.
length
()
==
1
){
return
"set"
+
fieldName
.
toUpperCase
();
}
return
"set"
+
Character
.
toUpperCase
(
fieldName
.
charAt
(
0
))
+
fieldName
.
substring
(
1
);
}
@Data
@AllArgsConstructor
@FieldDefaults
(
level
=
AccessLevel
.
PRIVATE
)
static
class
FieldMeta
{
String
name
;
String
setter
;
int
index
;
Class
<?>
type
;
}
}
}
}
Write
Preview
Markdown
is supported
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