-
Notifications
You must be signed in to change notification settings - Fork 290
WELD-2812 Improve Invoker exception messages #3133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
impl/src/main/java/org/jboss/weld/invokable/InvokerValidationUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| package org.jboss.weld.invokable; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
||
| import jakarta.enterprise.invoke.Invoker; | ||
|
|
||
| import org.jboss.weld.exceptions.IllegalArgumentException; | ||
| import org.jboss.weld.logging.InvokerLogger; | ||
|
|
||
| /** | ||
| * Utility methods for checking the arguments and instances being used by an {@link Invoker}. | ||
| * <p> | ||
| * Handles to these methods are obtained via {@link MethodHandleUtils}. | ||
| */ | ||
| public class InvokerValidationUtils { | ||
|
|
||
| private InvokerValidationUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Validate that an instance being called by an {@link Invoker} has the expected type or is {@code null}. | ||
| * | ||
| * @param invokerMethod the method being invoked | ||
| * @param type the expected type of the instance | ||
| * @param instance the instance the method is being invoked on | ||
| * @throws ClassCastException if {@code instance} is not {@code null} and not of the expected type | ||
| */ | ||
| static void instanceHasType(Method invokerMethod, Class<?> type, Object instance) { | ||
| if (instance != null && !type.isInstance(instance)) { | ||
| throw InvokerLogger.LOG.wrongInstanceType(invokerMethod, instance.getClass(), type); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate that an instance being called by an {@link Invoker} is not {@code null}. | ||
| * | ||
| * @param invokerMethod the method being invoked | ||
| * @param instance the instance to check | ||
| * @throws NullPointerException if {@code instance} is {@code null} | ||
| */ | ||
| static void instanceNotNull(Method invokerMethod, Object instance) { | ||
| if (instance == null) { | ||
| throw InvokerLogger.LOG.nullInstance(invokerMethod); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate that if arguments are required then the arguments array is not {@code null} and any primitive arguments are not | ||
| * {@code null}. | ||
| * | ||
| * @param invokerMethod the method being invoked | ||
| * @param expectedTypes the expected type of each argument, a {@code null} entry indicates that the type of that parameter | ||
| * should not be checked | ||
| * @param args the array of arguments | ||
| * @throws NullPointerException if arguments are required and {@code args} is {@code null} or a primitive argument is | ||
| * {@code null} | ||
| */ | ||
| static void argumentsNotNull(Method invokerMethod, Class<?>[] expectedTypes, Object[] args) { | ||
| if (invokerMethod.getParameterCount() == 0) { | ||
| return; // If there are no arguments expected then there's nothing to check | ||
| } | ||
| if (args == null) { | ||
| throw InvokerLogger.LOG.nullArgumentArray(invokerMethod); | ||
| } | ||
| for (int i = 0; i < expectedTypes.length; i++) { | ||
| Class<?> expectedType = expectedTypes[i]; | ||
| Object arg = args[i]; | ||
| if (expectedType != null && arg == null && expectedType.isPrimitive()) { | ||
| throw InvokerLogger.LOG.nullPrimitiveArgument(invokerMethod, i + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate that an array of arguments for an {@link Invoker} has at least an expected number of elements | ||
| * | ||
| * @param invokerMethod the method being invoked | ||
| * @param requiredArgs the expected number of arguments | ||
| * @param args the array of arguments | ||
| * @return {@code args} | ||
| * @throws IllegalArgumentException if the length of {@code args} is less than {@code requiredArgs} | ||
| */ | ||
| static void argCountAtLeast(Method invokerMethod, int requiredArgs, Object[] args) { | ||
| int actualArgs = args == null ? 0 : args.length; | ||
| if (actualArgs < requiredArgs) { | ||
| throw InvokerLogger.LOG.notEnoughArguments(invokerMethod, requiredArgs, actualArgs); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate that each of the arguments being passed passed to a method by an {@link Invoker} has the correct type. | ||
| * <p> | ||
| * For each pair if type and argument from {@code expectedTypes} and {@code args}: | ||
| * <ul> | ||
| * <li>if the expected type is {@code null}, no validation is done | ||
| * <li>if the expected type is a primitive type, check that the argument is not {@code null} and can be converted to that | ||
| * primitive type using boxing and primitive widening conversions | ||
| * <li>otherwise, check that the argument is an instance of the expected type | ||
| * </ul> | ||
| * | ||
| * @param invokerMethod the method being invoked | ||
| * @param expectedTypes an array of the expected type of each argument. May contain {@code null} to indicate that that | ||
| * argument should not be validated. | ||
| * @param args the array of values being passed as arguments | ||
| * @throws ClassCastException if any of the arguments are not valid for their expected type | ||
| * @throws NullPointerException if an argument for a primitive-typed parameter is not null | ||
| */ | ||
| static void argumentsHaveCorrectType(Method invokerMethod, Class<?>[] expectedTypes, Object[] args) { | ||
| if (args == null) { | ||
| // Not expected since we're in the catch block of ClassCastException, but guarantees we can safely access args | ||
| throw InvokerLogger.LOG.nullArgumentArray(invokerMethod); | ||
| } | ||
| for (int i = 0; i < expectedTypes.length; i++) { | ||
| Class<?> expectedType = expectedTypes[i]; | ||
| Object arg = args[i]; | ||
| if (expectedType != null) { | ||
| int pos = i + 1; // 1-indexed argument position | ||
| if (expectedType.isPrimitive()) { | ||
| if (arg == null) { | ||
| // Not expected since we're in the catch block of ClassCastException, but guarantees we can safely call methods on arg | ||
| throw InvokerLogger.LOG.nullPrimitiveArgument(invokerMethod, pos); | ||
| } | ||
| if (!primitiveConversionPermitted(expectedType, arg.getClass())) { | ||
| throw InvokerLogger.LOG.wrongArgumentType(invokerMethod, pos, arg.getClass(), expectedType); | ||
| } | ||
| } else { | ||
| if (arg != null && !expectedType.isInstance(arg)) { | ||
| throw InvokerLogger.LOG.wrongArgumentType(invokerMethod, pos, arg.getClass(), expectedType); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate whether a reference type can be converted to a primitive type via an unboxing and primitive widening conversion. | ||
| * | ||
| * @param primitive the target primitive type | ||
| * @param actual the reference type to test | ||
| * @return {@code true} if {@code actual} can be converted to {@code primitive} via an unboxing and primitive widening | ||
| * conversion, otherwise {@code false} | ||
| */ | ||
| private static boolean primitiveConversionPermitted(Class<?> primitive, Class<? extends Object> actual) { | ||
| if (primitive == Integer.TYPE) { | ||
| return actual == Integer.class | ||
| || actual == Character.class | ||
| || actual == Short.class | ||
| || actual == Byte.class; | ||
| } else if (primitive == Long.TYPE) { | ||
| return actual == Long.class | ||
| || actual == Integer.class | ||
| || actual == Character.class | ||
| || actual == Short.class | ||
| || actual == Byte.class; | ||
| } else if (primitive == Boolean.TYPE) { | ||
| return actual == Boolean.class; | ||
| } else if (primitive == Double.TYPE) { | ||
| return actual == Double.class | ||
| || actual == Float.class | ||
| || actual == Long.class | ||
| || actual == Integer.class | ||
| || actual == Character.class | ||
| || actual == Short.class | ||
| || actual == Byte.class; | ||
| } else if (primitive == Float.TYPE) { | ||
| return actual == Float.class | ||
| || actual == Long.class | ||
| || actual == Integer.class | ||
| || actual == Character.class | ||
| || actual == Short.class | ||
| || actual == Byte.class; | ||
| } else if (primitive == Short.TYPE) { | ||
| return actual == Short.class | ||
| || actual == Byte.class; | ||
| } else if (primitive == Character.TYPE) { | ||
| return actual == Character.class; | ||
| } else if (primitive == Byte.TYPE) { | ||
| return actual == Byte.class; | ||
| } | ||
| throw new RuntimeException("Unhandled primitive type: " + primitive); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...arquillian/src/test/java/org/jboss/weld/tests/invokable/exceptions/ExceptionTestBean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.jboss.weld.tests.invokable.exceptions; | ||
|
|
||
| import jakarta.enterprise.context.Dependent; | ||
|
|
||
| @Dependent | ||
| public class ExceptionTestBean { | ||
|
|
||
| public String ping(String s, int i) { | ||
| return s + i; | ||
| } | ||
|
|
||
| public static String staticPing(String s, int i) { | ||
| return s + i; | ||
| } | ||
|
|
||
| public void voidPing(String s, int i) { | ||
| String result = s + i; | ||
| } | ||
|
|
||
| public String noargPing() { | ||
| return "42"; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I understand it correctly that this is needed strictly because the adapter method handle (
cceCatch) must have the exact same type as the target method handle? I am a bit surprised this doesn't cause issues when invoking the method handle for the original (user defined) method.Also, a nitpicker note - the comment has a superflous
thein it :)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a requirement of
MethodHandles.catchException.I think this works because
MethodHandle.invokewill attempt to adapt the arguments to the types expected by the method handle, in the same way thatMethodHandle.asTypewould.Without this line which changes the parameter type, the final
MethodHandlewill have:Objectfor static methods)Object[]This will cast the instance to the expected type when
invokeis called, so when we're trying to catch and handle theClassCastExceptionourselves, we need to ensure that the first parameter has typeObjectand that the cast to the expected type is done within our catch block.Otherwise, it would also be possible satisfy the requirement that parameter types match exactly by adapting the
cceCatchmethod handle to match the user's method:However, this results in the
invokecall doing the cast which means we can't catch the exception.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right, that's it.
I keep thinking along the lines of
invokeExactwhich is what probably wouldn't work.Thanks for detailed explanation.