Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
Grasscutter
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
ziqian zhang
Grasscutter
Commits
35962542
Commit
35962542
authored
2 years ago
by
AnimeGitB
Browse files
Options
Downloads
Patches
Plain Diff
Fix oversight on EnumTypeAdapterFactory
parent
0b532951
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/emu/grasscutter/utils/JsonAdapters.java
+22
-27
22 additions, 27 deletions
src/main/java/emu/grasscutter/utils/JsonAdapters.java
with
22 additions
and
27 deletions
src/main/java/emu/grasscutter/utils/JsonAdapters.java
+
22
−
27
View file @
35962542
...
...
@@ -71,37 +71,32 @@ public class JsonAdapters {
static
class
EnumTypeAdapterFactory
implements
TypeAdapterFactory
{
@SuppressWarnings
(
"unchecked"
)
public
<
T
>
TypeAdapter
<
T
>
create
(
Gson
gson
,
TypeToken
<
T
>
type
)
{
Class
<
T
>
rawType
=
(
Class
<
T
>)
type
.
getRawType
();
if
(!
rawType
.
isEnum
())
return
null
;
Class
<
T
>
enumClass
=
(
Class
<
T
>)
type
.
getRawType
();
if
(!
enumClass
.
isEnum
())
return
null
;
// Make mappings of (string) names to enum constants
val
map
=
new
HashMap
<
String
,
T
>();
val
enumConstants
=
enumClass
.
getEnumConstants
();
for
(
val
constant
:
enumConstants
)
map
.
put
(
constant
.
toString
(),
constant
);
Field
id
=
null
;
// If the enum also has a numeric value, map those to the constants too
// System.out.println("Looking for enum value field");
for
(
Field
f
:
rawType
.
getDeclaredFields
())
{
id
=
switch
(
f
.
getName
())
{
case
"value"
,
"id"
->
f
;
default
->
null
;
};
if
(
id
!=
null
)
break
;
}
if
(
id
==
null
)
{
// System.out.println("Not found");
return
null
;
}
// System.out.println("Enum value field found - " + id.getName());
val
map
=
new
HashMap
<
String
,
T
>();
boolean
acc
=
id
.
isAccessible
();
id
.
setAccessible
(
true
);
try
{
for
(
T
constant
:
rawType
.
getEnumConstants
())
{
map
.
put
(
constant
.
toString
(),
constant
);
map
.
put
(
String
.
valueOf
(
id
.
getInt
(
constant
)),
constant
);
for
(
Field
f
:
enumClass
.
getDeclaredFields
())
{
if
(
switch
(
f
.
getName
())
{
case
"value"
,
"id"
->
true
;
default
->
false
;})
{
// System.out.println("Enum value field found - " + f.getName());
boolean
acc
=
f
.
isAccessible
();
f
.
setAccessible
(
true
);
try
{
for
(
val
constant
:
enumConstants
)
map
.
put
(
String
.
valueOf
(
f
.
getInt
(
constant
)),
constant
);
}
catch
(
IllegalAccessException
e
)
{
// System.out.println("Failed to access enum id field.");
}
f
.
setAccessible
(
acc
);
break
;
}
}
catch
(
IllegalAccessException
e
)
{
// System.out.println("Failed to access enum id field.");
return
null
;
}
id
.
setAccessible
(
acc
);
return
new
TypeAdapter
<
T
>()
{
public
T
read
(
JsonReader
reader
)
throws
IOException
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment