Skip to content

Commit

Permalink
Also ignore transient fields and add optional BlueNBT argument to Typ…
Browse files Browse the repository at this point in the history
…e(De)Serializer constructors
  • Loading branch information
TBlueF committed Mar 15, 2024
1 parent d60cb8b commit c00a564
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ public DefaultAdapter(TypeToken<T> type, ObjectConstructor<T> constructor, BlueN
Class<?> raw;
while (typeToken != null && (raw = typeToken.getRawType()) != Object.class) {
for (Field field : raw.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()))
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))
continue;

field.setAccessible(true);

String[] names = new String[]{ field.getName() };
Expand All @@ -102,6 +104,12 @@ public DefaultAdapter(TypeToken<T> type, ObjectConstructor<T> constructor, BlueN
if (deserializerType != null) {
typeDeserializer = typeDeserializerCache.computeIfAbsent(deserializerType, t -> {
try {
// try BlueNBT constructor
try {
return t.getDeclaredConstructor(BlueNBT.class).newInstance(blueNBT);
} catch (NoSuchMethodException ignore) {}

// use no-args constructor
return t.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new RuntimeException("Failed to create Instance of TypeDeserializer!", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ public DefaultAdapter(TypeToken<T> type, BlueNBT blueNBT) {
Class<?> raw;
while (typeToken != null && (raw = typeToken.getRawType()) != Object.class) {
for (Field field : raw.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers()))
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))
continue;

field.setAccessible(true);

String[] names = new String[]{ field.getName() };
Expand All @@ -93,6 +95,12 @@ public DefaultAdapter(TypeToken<T> type, BlueNBT blueNBT) {
if (serializerType != null) {
typeSerializer = typeSerializerCache.computeIfAbsent(serializerType, t -> {
try {
// try BlueNBT constructor
try {
return t.getDeclaredConstructor(BlueNBT.class).newInstance(blueNBT);
} catch (NoSuchMethodException ignore) {}

// use no-args constructor
return t.getDeclaredConstructor().newInstance();
} catch (Exception ex) {
throw new RuntimeException("Failed to create Instance of TypeSerializer!", ex);
Expand Down

0 comments on commit c00a564

Please sign in to comment.