apache/dubbo · error · NoSuchMethodException

No method found with the specified name and parameter types

Error message

No method found with the specified name and parameter types

What it means

Thrown by ReflectionUtils.invoke(Object, String, Object...) when no method on the source class matches both the given name and the runtime parameter types. The NoSuchMethodException is then wrapped in ReflectionException. Matching uses getDeclaredMethods (not inherited) and compares param types via isAssignableFrom, so overloaded methods and superclass methods are easily missed.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java:76

     *
     * @param source     The object on which to invoke the method.
     * @param methodName The name of the method to invoke.
     * @param params     The parameters to pass to the method.
     * @return The result of invoking the specified method on the given object.
     */
    public static Object invoke(Object source, String methodName, Object... params) {
        try {
            Class<?>[] classes = Arrays.stream(params)
                    .map(param -> param != null ? param.getClass() : null)
                    .toArray(Class<?>[]::new);

            for (Method method : source.getClass().getDeclaredMethods()) {
                if (method.getName().equals(methodName) && matchParameters(method.getParameterTypes(), classes)) {
                    method.setAccessible(true);
                    return method.invoke(source, params);
                }
            }
            throw new NoSuchMethodException("No method found with the specified name and parameter types");
        } catch (Exception e) {
            throw new ReflectionException(e);
        }
    }

    private static boolean matchParameters(Class<?>[] methodParamTypes, Class<?>[] givenParamTypes) {
        if (methodParamTypes.length != givenParamTypes.length) {
            return false;
        }

        for (int i = 0; i < methodParamTypes.length; i++) {
            if (givenParamTypes[i] == null) {
                if (methodParamTypes[i].isPrimitive()) {
                    return false;
                }
            } else if (!methodParamTypes[i].isAssignableFrom(givenParamTypes[i])) {
                return false;
            }

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Verify the method name and parameter signature exist as a declared method on source.getClass() (not just a superclass).
  2. Ensure argument types are assignable to the method's parameter types; check for primitive vs. boxed-type mismatches.
  3. Catch ReflectionException and inspect getCause() to confirm it is NoSuchMethodException.

Example fix

// before
ReflectionUtils.invoke(obj, "process", 42);
// fails if process(long) is on a superclass

// after
// ensure method is declared on the concrete class, or call directly
obj.process(42);
Defensive patterns

Strategy: try-catch

Validate before calling

boolean methodExists = Arrays.stream(source.getClass().getDeclaredMethods())
    .anyMatch(m -> m.getName().equals(methodName));
if (!methodExists) throw new AssertionError("method not found: " + methodName);

Try / catch

try {
    Object result = ReflectionUtils.invoke(source, methodName, params);
} catch (ReflectionUtils.ReflectionException e) {
    if (e.getCause() instanceof NoSuchMethodException) {
        // no matching method — handle gracefully
    }
}

Prevention

When it happens

Trigger: Calling invoke(obj, "doSomething", args) where no declared method has that exact name with parameter types assignable from the runtime types of args; method exists on a superclass (not declared on obj.getClass()); passing boxed types (Integer) for a method expecting primitives.

Common situations: Testing a method that was renamed or overloaded; invoking a method defined in a parent class via a subclass instance; mismatch between autoboxed argument types and primitive parameter types in matchParameters.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/8cd849dcfb141455. Report an issue: GitHub.