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
35962542
Commit
35962542
authored
Nov 24, 2022
by
AnimeGitB
Browse files
Fix oversight on EnumTypeAdapterFactory
parent
0b532951
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/utils/JsonAdapters.java
View file @
35962542
...
@@ -71,37 +71,32 @@ public class JsonAdapters {
...
@@ -71,37 +71,32 @@ public class JsonAdapters {
static
class
EnumTypeAdapterFactory
implements
TypeAdapterFactory
{
static
class
EnumTypeAdapterFactory
implements
TypeAdapterFactory
{
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
<
T
>
TypeAdapter
<
T
>
create
(
Gson
gson
,
TypeToken
<
T
>
type
)
{
public
<
T
>
TypeAdapter
<
T
>
create
(
Gson
gson
,
TypeToken
<
T
>
type
)
{
Class
<
T
>
rawType
=
(
Class
<
T
>)
type
.
getRawType
();
Class
<
T
>
enumClass
=
(
Class
<
T
>)
type
.
getRawType
();
if
(!
rawType
.
isEnum
())
return
null
;
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");
// System.out.println("Looking for enum value field");
for
(
Field
f
:
rawType
.
getDeclaredFields
())
{
for
(
Field
f
:
enumClass
.
getDeclaredFields
())
{
id
=
switch
(
f
.
getName
())
{
if
(
switch
(
f
.
getName
())
{
case
"value"
,
"id"
->
true
;
default
->
false
;})
{
case
"value"
,
"id"
->
f
;
// System.out.println("Enum value field found - " + f.getName());
default
->
null
;
boolean
acc
=
f
.
isAccessible
();
};
f
.
setAccessible
(
true
);
if
(
id
!=
null
)
break
;
try
{
}
for
(
val
constant
:
enumConstants
)
if
(
id
==
null
)
{
map
.
put
(
String
.
valueOf
(
f
.
getInt
(
constant
)),
constant
);
// System.out.println("Not found");
}
catch
(
IllegalAccessException
e
)
{
return
null
;
// System.out.println("Failed to access enum id field.");
}
}
// System.out.println("Enum value field found - " + id.getName());
f
.
setAccessible
(
acc
);
break
;
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
);
}
}
}
catch
(
IllegalAccessException
e
)
{
// System.out.println("Failed to access enum id field.");
return
null
;
}
}
id
.
setAccessible
(
acc
);
return
new
TypeAdapter
<
T
>()
{
return
new
TypeAdapter
<
T
>()
{
public
T
read
(
JsonReader
reader
)
throws
IOException
{
public
T
read
(
JsonReader
reader
)
throws
IOException
{
...
...
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