| 151 | * This class duplicates code in jakarta.el.Util. When making changes keep the code in sync. |
| 152 | */ |
| 153 | @SuppressWarnings("null") |
| 154 | public static Method getMethod(EvaluationContext ctx, Object base, Object property, Class<?>[] paramTypes, |
| 155 | Object[] paramValues) throws MethodNotFoundException { |
| 156 | |
| 157 | if (base == null || property == null) { |
| 158 | throw new MethodNotFoundException( |
| 159 | MessageFactory.get("error.method.notfound", base, property, paramString(paramTypes))); |
| 160 | } |
| 161 | |
| 162 | String methodName = (property instanceof String) ? (String) property : property.toString(); |
| 163 | |
| 164 | int paramCount; |
| 165 | if (paramTypes == null) { |
| 166 | paramCount = 0; |
| 167 | } else { |
| 168 | paramCount = paramTypes.length; |
| 169 | } |
| 170 | |
| 171 | Class<?> clazz = base.getClass(); |
| 172 | |
| 173 | // Fast path: when no arguments exist, there can only be one matching method and no need for coercion. |
| 174 | if (paramCount == 0) { |
| 175 | Method result = null; |
| 176 | Throwable t = null; |
| 177 | try { |
| 178 | Method method = clazz.getMethod(methodName, paramTypes); |
| 179 | result = getMethod(clazz, base, method); |
| 180 | } catch (NoSuchMethodException | SecurityException e) { |
| 181 | // Fall through |
| 182 | t = e; |
| 183 | } |
| 184 | if (result == null) { |
| 185 | throw new MethodNotFoundException( |
| 186 | MessageFactory.get("error.method.notfound", base, property, paramString(paramTypes)), t); |
| 187 | } |
| 188 | return result; |
| 189 | } |
| 190 | |
| 191 | Method[] methods = clazz.getMethods(); |
| 192 | Map<Method,MatchResult> candidates = new HashMap<>(); |
| 193 | |
| 194 | for (Method m : methods) { |
| 195 | if (!m.getName().equals(methodName)) { |
| 196 | // Method name doesn't match |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | Class<?>[] mParamTypes = m.getParameterTypes(); |
| 201 | int mParamCount = mParamTypes.length; |
| 202 | |
| 203 | // Check the number of parameters |
| 204 | // Multiple tests to improve readability |
| 205 | if (!m.isVarArgs() && paramCount != mParamCount) { |
| 206 | // Method has wrong number of parameters |
| 207 | continue; |
| 208 | } |
| 209 | if (m.isVarArgs() && paramCount < mParamCount - 1) { |
| 210 | // Method has wrong number of parameters |