|
1 | 1 | package org.simplejavamail.internal.clisupport;
|
2 | 2 |
|
| 3 | +import org.jetbrains.annotations.NotNull; |
3 | 4 | import org.simplejavamail.api.email.EmailPopulatingBuilder;
|
4 | 5 | import org.simplejavamail.api.internal.clisupport.model.CliBuilderApiType;
|
5 | 6 | import org.simplejavamail.api.internal.clisupport.model.CliReceivedCommand;
|
|
10 | 11 | import org.slf4j.Logger;
|
11 | 12 |
|
12 | 13 | import java.lang.reflect.InvocationTargetException;
|
| 14 | +import java.lang.reflect.Method; |
13 | 15 | import java.util.List;
|
14 | 16 |
|
| 17 | +import static java.lang.String.format; |
15 | 18 | import static org.slf4j.LoggerFactory.getLogger;
|
16 | 19 |
|
17 | 20 | class CliCommandLineConsumerResultHandler {
|
@@ -58,14 +61,30 @@ private static <T> T invokeBuilderApi(List<CliReceivedOptionData> cliReceivedOpt
|
58 | 61 | if (option.determineTargetBuilderApi() == builderApiType) {
|
59 | 62 | try {
|
60 | 63 | LOGGER.debug("\t\t.{}({})", option.getDeclaredOptionSpec().getSourceMethod().getName(), option.getProvidedOptionValues());
|
61 |
| - currentBuilder = option.getDeclaredOptionSpec().getSourceMethod().invoke(currentBuilder, option.getProvidedOptionValues().toArray()); |
| 64 | + |
| 65 | + Method sourceMethod = determineTrueSourceMethod(option.getDeclaredOptionSpec().getSourceMethod()); |
| 66 | + |
| 67 | + currentBuilder = sourceMethod.invoke(currentBuilder, option.getProvidedOptionValues().toArray()); |
62 | 68 | } catch (IllegalArgumentException e) {
|
63 |
| - throw new CliExecutionException(CliExecutionException.WRONG_CURRENT_BUILDER, e); |
| 69 | + throw new CliExecutionException(formatCliInvocationError(CliExecutionException.WRONG_CURRENT_BUILDER, option), e); |
64 | 70 | } catch (IllegalAccessException | InvocationTargetException e) {
|
65 |
| - throw new CliExecutionException(CliExecutionException.ERROR_INVOKING_BUILDER_API, e); |
| 71 | + throw new CliExecutionException(formatCliInvocationError(CliExecutionException.ERROR_INVOKING_BUILDER_API, option), e); |
| 72 | + } catch (NoSuchMethodException e) { |
| 73 | + throw new CliExecutionException("This should never happen", e); |
66 | 74 | }
|
67 | 75 | }
|
68 | 76 | }
|
69 | 77 | return (T) currentBuilder;
|
70 | 78 | }
|
| 79 | + |
| 80 | + @NotNull |
| 81 | + // yeah, so after deserializing a java.lang.Method, it's actually a fake, so let's find it's real counter version |
| 82 | + private static Method determineTrueSourceMethod(@NotNull Method sourceMethod) |
| 83 | + throws NoSuchMethodException { |
| 84 | + return sourceMethod.getDeclaringClass().getDeclaredMethod(sourceMethod.getName(), sourceMethod.getParameterTypes()); |
| 85 | + } |
| 86 | + |
| 87 | + private static String formatCliInvocationError(final String exceptionTemplate, final CliReceivedOptionData option) { |
| 88 | + return format(exceptionTemplate, option.getProvidedOptionValues(), option.getDeclaredOptionSpec().getName()); |
| 89 | + } |
71 | 90 | }
|
0 commit comments