【问题标题】:Get methods names in an interface(without implementing) using dynamic proxy使用动态代理获取接口中的方法名称(不实现)
【发布时间】:2014-02-05 11:59:04
【问题描述】:

我有一个像这样的简单界面

public interface Arithmatic {

    public long add(int num1, int num2) ;

    public long multiply(int num1, int num2) ;

  }

我需要调用接口的方法名称而不实现它,使用 java 动态代理。我该如何实现 Invocation Handlernew Proxy instance 呢?

我必须在我的项目中使用 scala 来完成这项任务。

我的代理实例是这样的

val impl = Proxy.newProxyInstance(

      classOf[ Arithmatic].getClassLoader,
      classOf[ Arithmatic].getClasses,
      handler

    ).asInstanceOf[ Arithmatic]

就像在 java 中一样,我不能将它转换为算术。它给了我

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to Arithmatic

我该如何解决这个问题?

【问题讨论】:

  • 您永远不需要实现 interface调用它。调用者不是实现者。

标签: java scala dynamic-proxy


【解决方案1】:
猜你喜欢
  • 2016-03-21
  • 2013-01-15
  • 1970-01-01
  • 2023-03-18
  • 2020-08-10
  • 2015-11-21
  • 2017-08-21
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多