Skip to content

Commit

Permalink
Handle ClassCastException when resolving placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonOellerer committed May 6, 2024
1 parent 4e2d70c commit 335402c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.6.9'
version = '1.6.10'

java {
toolchain {
Expand Down
Expand Up @@ -7,7 +7,7 @@ public interface CustomPlaceholderRegistry {
void addHandler(String placeholder, Class<? extends PlaceholderData> customWordPlaceholderDataClass);

Optional<PlaceholderData> resolve(String placeholder, Object object)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException;
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassCastException;

boolean governs(String placeholderName, Object object);
}
Expand Up @@ -144,7 +144,7 @@ private static NumberFormat toNumberFormat(Numeric numeric, Locale locale) {
if (numeric.minIntDigits() != -1) {
format.setMinimumIntegerDigits(numeric.minIntDigits());
}
if (!numeric.currencyCode().equals("")) {
if (!numeric.currencyCode().isEmpty()) {
format.setCurrency(Currency.getInstance(numeric.currencyCode()));
}
format.setGroupingUsed(numeric.groupingUsed());
Expand Down Expand Up @@ -381,6 +381,9 @@ private Optional<PlaceholderData> doReflectiveResolve(String placeholderName, Lo
} catch (EmptyOptionalException e) {
logger.warn("Placeholder {} property is an empty optional", e.getMessage());
return Optional.empty();
} catch (ClassCastException e) {
logger.warn("ClassCastException when resolving custom placeholder %s".formatted(placeholderName), e);
return Optional.empty();
}
}

Expand Down Expand Up @@ -486,7 +489,7 @@ private NumberFormat findNumberFormat(String fieldName, Locale locale) {
.or(() -> ReflectionUtils.findFieldAnnotation(bean.getClass(), fieldName, Numeric.class)
.map(numeric -> toNumberFormat(numeric, locale)))
.orElseGet(() -> {
logger.info("Did not find formatting directive for {}, formatting according to locale {}", fieldName, locale);
logger.debug("Did not find formatting directive for {}, formatting according to locale {}", fieldName, locale);
return NumberFormat.getInstance(locale);
});
}
Expand Down Expand Up @@ -515,6 +518,6 @@ private Object resolveNonFinalValue(Object property, String placeholderName)

@Override
public String toString() {
return bean != null? bean.toString() : "";
return bean != null ? bean.toString() : "";
}
}

0 comments on commit 335402c

Please sign in to comment.