You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java中获取作为Lambda参数传入的方法名称

获取方法引用对应的方法名称

要在invokeRpc方法中拿到传入的方法引用对应的方法名,Java本身没有直接的标准API支持——因为方法引用被编译成函数式接口的实例后,不会主动保留原方法的元数据。可以通过以下几种方式实现:

方式一:利用Oracle/OpenJDK特定反射实现(非标准)

Lambda表达式和方法引用在Oracle/OpenJDK中会被编译成包含writeReplace方法的类,通过反射可以提取其中的方法信息。示例代码如下:

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;

@FunctionalInterface
public interface MyRPC {
    Object execute(Object input);
}

public class MyImpl {
    public Object rpc1(Object input) {
        System.out.println(input);
        return 100;
    }

    public static Object invokeRpc(MyRPC rpc, Object input) throws Exception {
        // 获取Lambda的SerializedLambda实例
        Method writeReplace = rpc.getClass().getDeclaredMethod("writeReplace");
        writeReplace.setAccessible(true);
        SerializedLambda serializedLambda = (SerializedLambda) writeReplace.invoke(rpc);
        
        // 提取方法名和所属类名
        String methodName = serializedLambda.getImplMethodName();
        String className = serializedLambda.getImplClass().replace('/', '.');
        String fullMethodName = className + "::" + methodName;
        
        System.out.println("方法名:" + methodName);
        System.out.println("完整方法标识:" + fullMethodName);
        
        return rpc.execute(input);
    }

    public static void main(String[] args) throws Exception {
        final MyImpl myc = new MyImpl();
        invokeRpc(myc::rpc1, 1);
    }
}

注意:这种方式依赖Oracle/OpenJDK的私有实现逻辑,在GraalVM、IBM J9等其他JVM环境下可能失效,仅适合测试或特定场景使用。

方式二:手动传递方法元信息(推荐生产环境)

如果追求兼容性和稳定性,最可靠的方式是调用时主动传递方法名或方法对象,修改invokeRpc方法的参数即可:

传递方法名字符串

@FunctionalInterface
public interface MyRPC {
    Object execute(Object input);
}

public class MyImpl {
    public Object rpc1(Object input) {
        System.out.println(input);
        return 100;
    }

    public static Object invokeRpc(MyRPC rpc, String methodName, Object input) {
        System.out.println("方法名:" + methodName);
        return rpc.execute(input);
    }

    public static void main(String[] args) {
        final MyImpl myc = new MyImpl();
        invokeRpc(myc::rpc1, "rpc1", 1);
        // 也可以直接传递完整标识
        invokeRpc(myc::rpc1, "MyImpl::rpc1", 1);
    }
}

传递Method对象(获取更完整信息)

import java.lang.reflect.Method;

@FunctionalInterface
public interface MyRPC {
    Object execute(Object input);
}

public class MyImpl {
    public Object rpc1(Object input) {
        System.out.println(input);
        return 100;
    }

    public static Object invokeRpc(MyRPC rpc, Method method, Object input) {
        String fullMethodName = method.getDeclaringClass().getName() + "::" + method.getName();
        System.out.println("完整方法标识:" + fullMethodName);
        return rpc.execute(input);
    }

    public static void main(String[] args) throws NoSuchMethodException {
        final MyImpl myc = new MyImpl();
        Method rpc1Method = MyImpl.class.getMethod("rpc1", Object.class);
        invokeRpc(myc::rpc1, rpc1Method, 1);
    }
}

这种方式完全基于Java标准API,兼容性拉满,适合生产环境使用。

方式三:注解标记结合反射扫描

如果需要批量处理RPC方法,可以自定义注解标记目标方法,再通过反射扫描注解信息。但这种方式需要提前标记方法,无法直接从方法引用中动态获取,适合初始化阶段的元信息收集场景。


内容的提问来源于stack exchange,提问作者Long Quanzheng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 09:07:31