Skip to content

Commit

Permalink
Allow to pass GeneratorOptions to MatchPlaceholder Method
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpartsch committed Jun 27, 2022
1 parent ce551f1 commit 4946476
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '1.4.2'
version = '1.4.3'

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ private Optional<PlaceholderData> matchPattern(String placeholderName, Locale lo
if (method.getParameterCount() == 1) {
return evaluateSingleParameterFunction(placeholderName, method);
} else if (method.getParameterCount() == 2) {
return evaluateTwoParameterFunction(placeholderName, locale, method);
var additionalParamType = method.getParameterTypes()[1];
if(additionalParamType.isAssignableFrom(Locale.class))
return evaluateTwoParameterFunction(placeholderName, locale, Locale.class, method);
return evaluateTwoParameterFunction(placeholderName, options, GenerationOptions.class, method);
} else {
logger.error("@MatchPlaceholder-annotated method {} must take exactly one parameter (String) or two (String, Locale). It takes {}.",
method, method.getParameterCount());
Expand Down Expand Up @@ -237,20 +240,20 @@ private Optional<String> evaluateSingleParameterFunction(String placeholderName,
}
}

private Optional<String> evaluateTwoParameterFunction(String placeholderName, Locale locale, Method method)
private <T> Optional<String> evaluateTwoParameterFunction(String placeholderName, T additionalOption, Class<T> additionalOptionClass, Method method)
throws IllegalAccessException, InvocationTargetException {
var parameterTypes = method.getParameterTypes();
if (!parameterTypes[0].equals(String.class)) {
logger.warn("@MatchPlaceholder-annotated method {} must take a String (placeholderName) as first parameter, but takes {}.", method,
parameterTypes[0]);
return Optional.empty();
}
if (!parameterTypes[1].equals(Locale.class)) {
logger.warn("@MatchPlaceholder-annotated method {} can only take a java.util.Locale as second parameter, but takes {}.", method,
parameterTypes[1]);
if (!parameterTypes[1].equals(additionalOptionClass)) {
logger.warn("@MatchPlaceholder-annotated method {} can only take a {} as second parameter, but takes {}.", method,
additionalOptionClass, parameterTypes[1]);
return Optional.empty();
}
var returnValue = method.invoke(bean, placeholderName, locale);
var returnValue = method.invoke(bean, placeholderName, additionalOption);
if (returnValue instanceof Optional<?> optionalReturnValue) {
return optionalReturnValue.map(Object::toString);
} else {
Expand Down

0 comments on commit 4946476

Please sign in to comment.