diff --git a/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java b/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java index 7b2881d0..b102edf0 100644 --- a/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java +++ b/src/main/java/com/comphenix/protocol/reflect/StructureModifier.java @@ -143,8 +143,7 @@ public class StructureModifier { for (FieldAccessor accessor : fields) { Field field = accessor.getField(); if (!field.getType().isPrimitive() && !Modifier.isFinal(field.getModifiers())) { - Object defaultInstance = DEFAULT_GENERATOR.getDefault(field.getType()); - if (defaultInstance != null) { + if (DEFAULT_GENERATOR.hasDefault(field.getType())) { requireDefaults.put(accessor, currentFieldIndex); } } diff --git a/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java b/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java index 6a50dcd9..523e4f1e 100644 --- a/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java +++ b/src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java @@ -26,6 +26,8 @@ import javax.annotation.Nullable; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; import java.util.logging.Level; /** @@ -164,7 +166,19 @@ public class DefaultInstances implements InstanceProvider { public T getDefault(Class type) { return getDefaultInternal(type, registered, 0); } - + + private final ThreadLocal, Boolean>> cache = ThreadLocal.withInitial(WeakHashMap::new); + + /** + * Determines if a given class has a default value. + * + * @param type - the class to check + * @return true if the class has a default value, false otherwise + */ + public boolean hasDefault(Class type) { + return cache.get().computeIfAbsent(type, aClass -> getDefaultInternal(aClass, registered, 0) != null); + } + /** * Retrieve the constructor with the fewest number of parameters. * @param Type