diff --git a/owner/src/main/java/org/aeonbits/owner/Converters.java b/owner/src/main/java/org/aeonbits/owner/Converters.java index ab422884..919486bd 100644 --- a/owner/src/main/java/org/aeonbits/owner/Converters.java +++ b/owner/src/main/java/org/aeonbits/owner/Converters.java @@ -142,7 +142,7 @@ Object tryConvert(Method targetMethod, Class targetType, String text) { if (!canUsePropertyEditors()) return SKIP; - PropertyEditor editor = PropertyEditorManager.findEditor(targetType); + PropertyEditor editor = findEditor(targetType); if (editor == null) return SKIP; try { editor.setAsText(text); @@ -151,17 +151,19 @@ Object tryConvert(Method targetMethod, Class targetType, String text) { throw unsupportedConversion(e, targetType, text); } } - - private boolean canUsePropertyEditors() { - return isPropertyEditorAvailable() && !isPropertyEditorDisabled(); + + private PropertyEditor findEditor(Class targetType) { + PropertyEditor editor = PROPERTY_BY_CLASS.get(targetType); + if (editor == null) { + editor = PropertyEditorManager.findEditor(targetType); + if (editor != null) + PROPERTY_BY_CLASS.put(targetType, editor); + } + return editor; } - private boolean isPropertyEditorAvailable() { - return isClassAvailable("java.beans.PropertyEditorManager"); - } - - private boolean isPropertyEditorDisabled() { - return Boolean.getBoolean("org.aeonbits.owner.property.editor.disabled"); + private boolean canUsePropertyEditors() { + return isPropertyEditorAvailable && !isPropertyEditorDisabled; } }, @@ -265,6 +267,12 @@ private static Object convertWithConverterClass(Method targetMethod, String text } private static final Map, Class>> converterRegistry = new ConcurrentHashMap, Class>>(); + + private static final boolean isPropertyEditorAvailable = isClassAvailable("java.beans.PropertyEditorManager"); + + private static final boolean isPropertyEditorDisabled = Boolean.getBoolean("org.aeonbits.owner.property.editor.disabled"); + + private static final Map, PropertyEditor> PROPERTY_BY_CLASS = new WeakHashMap, PropertyEditor>(); abstract Object tryConvert(Method targetMethod, Class targetType, String text);