|
| 1 | +package org.jboss.weld.invokable; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | + |
| 5 | +import jakarta.enterprise.invoke.Invoker; |
| 6 | + |
| 7 | +import org.jboss.weld.exceptions.IllegalArgumentException; |
| 8 | +import org.jboss.weld.logging.InvokerLogger; |
| 9 | + |
| 10 | +/** |
| 11 | + * Utility methods for checking the arguments and instances being used by an {@link Invoker}. |
| 12 | + * <p> |
| 13 | + * Handles to these methods are obtained via {@link MethodHandleUtils}. |
| 14 | + */ |
| 15 | +public class InvokerValidationUtils { |
| 16 | + |
| 17 | + private InvokerValidationUtils() { |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Validate that an instance being called by an {@link Invoker} has the expected type or is {@code null}. |
| 22 | + * |
| 23 | + * @param invokerMethod the method being invoked |
| 24 | + * @param type the expected type of the instance |
| 25 | + * @param instance the instance the method is being invoked on |
| 26 | + * @throws ClassCastException if {@code instance} is not {@code null} and not of the expected type |
| 27 | + */ |
| 28 | + static void instanceHasType(Method invokerMethod, Class<?> type, Object instance) { |
| 29 | + if (instance != null && !type.isInstance(instance)) { |
| 30 | + throw InvokerLogger.LOG.wrongInstanceType(invokerMethod, instance.getClass(), type); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Validate that an instance being called by an {@link Invoker} is not {@code null}. |
| 36 | + * |
| 37 | + * @param invokerMethod the method being invoked |
| 38 | + * @param instance the instance to check |
| 39 | + * @throws NullPointerException if {@code instance} is {@code null} |
| 40 | + */ |
| 41 | + static void instanceNotNull(Method invokerMethod, Object instance) { |
| 42 | + if (instance == null) { |
| 43 | + throw InvokerLogger.LOG.nullInstance(invokerMethod); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Validate that if arguments are required then the arguments array is not {@code null} and any primitive arguments are not |
| 49 | + * {@code null}. |
| 50 | + * |
| 51 | + * @param invokerMethod the method being invoked |
| 52 | + * @param expectedTypes the expected type of each argument, a {@code null} entry indicates that the type of that parameter |
| 53 | + * should not be checked |
| 54 | + * @param args the array of arguments |
| 55 | + * @throws NullPointerException if arguments are required and {@code args} is {@code null} or a primitive argument is |
| 56 | + * {@code null} |
| 57 | + */ |
| 58 | + static void argumentsNotNull(Method invokerMethod, Class<?>[] expectedTypes, Object[] args) { |
| 59 | + if (invokerMethod.getParameterCount() == 0) { |
| 60 | + return; // If there are no arguments expected then there's nothing to check |
| 61 | + } |
| 62 | + if (args == null) { |
| 63 | + throw InvokerLogger.LOG.nullArgumentArray(invokerMethod); |
| 64 | + } |
| 65 | + for (int i = 0; i < expectedTypes.length; i++) { |
| 66 | + Class<?> expectedType = expectedTypes[i]; |
| 67 | + Object arg = args[i]; |
| 68 | + if (expectedType != null && arg == null && expectedType.isPrimitive()) { |
| 69 | + throw InvokerLogger.LOG.nullPrimitiveArgument(invokerMethod, i + 1); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Validate that an array of arguments for an {@link Invoker} has at least an expected number of elements |
| 76 | + * |
| 77 | + * @param invokerMethod the method being invoked |
| 78 | + * @param requiredArgs the expected number of arguments |
| 79 | + * @param args the array of arguments |
| 80 | + * @return {@code args} |
| 81 | + * @throws IllegalArgumentException if the length of {@code args} is less than {@code requiredArgs} |
| 82 | + */ |
| 83 | + static void argCountAtLeast(Method invokerMethod, int requiredArgs, Object[] args) { |
| 84 | + int actualArgs = args == null ? 0 : args.length; |
| 85 | + if (actualArgs < requiredArgs) { |
| 86 | + throw InvokerLogger.LOG.notEnoughArguments(invokerMethod, requiredArgs, actualArgs); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Validate that each of the arguments being passed passed to a method by an {@link Invoker} has the correct type. |
| 92 | + * <p> |
| 93 | + * For each pair if type and argument from {@code expectedTypes} and {@code args}: |
| 94 | + * <ul> |
| 95 | + * <li>if the expected type is {@code null}, no validation is done |
| 96 | + * <li>if the expected type is a primitive type, check that the argument is not {@code null} and can be converted to that |
| 97 | + * primitive type using boxing and primitive widening conversions |
| 98 | + * <li>otherwise, check that the argument is an instance of the expected type |
| 99 | + * </ul> |
| 100 | + * |
| 101 | + * @param invokerMethod the method being invoked |
| 102 | + * @param expectedTypes an array of the expected type of each argument. May contain {@code null} to indicate that that |
| 103 | + * argument should not be validated. |
| 104 | + * @param args the array of values being passed as arguments |
| 105 | + * @throws ClassCastException if any of the arguments are not valid for their expected type |
| 106 | + * @throws NullPointerException if an argument for a primitive-typed parameter is not null |
| 107 | + */ |
| 108 | + static void argumentsHaveCorrectType(Method invokerMethod, Class<?>[] expectedTypes, Object[] args) { |
| 109 | + if (args == null) { |
| 110 | + // Not expected since we're in the catch block of ClassCastException, but guarantees we can safely access args |
| 111 | + throw InvokerLogger.LOG.nullArgumentArray(invokerMethod); |
| 112 | + } |
| 113 | + for (int i = 0; i < expectedTypes.length; i++) { |
| 114 | + Class<?> expectedType = expectedTypes[i]; |
| 115 | + Object arg = args[i]; |
| 116 | + if (expectedType != null) { |
| 117 | + int pos = i + 1; // 1-indexed argument position |
| 118 | + if (expectedType.isPrimitive()) { |
| 119 | + if (arg == null) { |
| 120 | + // Not expected since we're in the catch block of ClassCastException, but guarantees we can safely call methods on arg |
| 121 | + throw InvokerLogger.LOG.nullPrimitiveArgument(invokerMethod, pos); |
| 122 | + } |
| 123 | + if (!primitiveConversionPermitted(expectedType, arg.getClass())) { |
| 124 | + throw InvokerLogger.LOG.wrongArgumentType(invokerMethod, pos, arg.getClass(), expectedType); |
| 125 | + } |
| 126 | + } else { |
| 127 | + if (arg != null && !expectedType.isInstance(arg)) { |
| 128 | + throw InvokerLogger.LOG.wrongArgumentType(invokerMethod, pos, arg.getClass(), expectedType); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Validate whether a reference type can be converted to a primitive type via an unboxing and primitive widening conversion. |
| 137 | + * |
| 138 | + * @param primitive the target primitive type |
| 139 | + * @param actual the reference type to test |
| 140 | + * @return {@code true} if {@code actual} can be converted to {@code primitive} via an unboxing and primitive widening |
| 141 | + * conversion, otherwise {@code false} |
| 142 | + */ |
| 143 | + private static boolean primitiveConversionPermitted(Class<?> primitive, Class<? extends Object> actual) { |
| 144 | + if (primitive == Integer.TYPE) { |
| 145 | + return actual == Integer.class |
| 146 | + || actual == Character.class |
| 147 | + || actual == Short.class |
| 148 | + || actual == Byte.class; |
| 149 | + } else if (primitive == Long.TYPE) { |
| 150 | + return actual == Long.class |
| 151 | + || actual == Integer.class |
| 152 | + || actual == Character.class |
| 153 | + || actual == Short.class |
| 154 | + || actual == Byte.class; |
| 155 | + } else if (primitive == Boolean.TYPE) { |
| 156 | + return actual == Boolean.class; |
| 157 | + } else if (primitive == Double.TYPE) { |
| 158 | + return actual == Double.class |
| 159 | + || actual == Float.class |
| 160 | + || actual == Long.class |
| 161 | + || actual == Integer.class |
| 162 | + || actual == Character.class |
| 163 | + || actual == Short.class |
| 164 | + || actual == Byte.class; |
| 165 | + } else if (primitive == Float.TYPE) { |
| 166 | + return actual == Float.class |
| 167 | + || actual == Long.class |
| 168 | + || actual == Integer.class |
| 169 | + || actual == Character.class |
| 170 | + || actual == Short.class |
| 171 | + || actual == Byte.class; |
| 172 | + } else if (primitive == Short.TYPE) { |
| 173 | + return actual == Short.class |
| 174 | + || actual == Byte.class; |
| 175 | + } else if (primitive == Character.TYPE) { |
| 176 | + return actual == Character.class; |
| 177 | + } else if (primitive == Byte.TYPE) { |
| 178 | + return actual == Byte.class; |
| 179 | + } |
| 180 | + throw new RuntimeException("Unhandled primitive type: " + primitive); |
| 181 | + } |
| 182 | +} |
0 commit comments