PHP __call equivalent for java

后端 未结 5 967
醉梦人生
醉梦人生 2021-02-07 10:11

Is there a Java equivalent for the __call of PHP?

It would make sense to me if that\'s not the case, because it would probably result in compiler errors.

From th

5条回答
  •  星月不相逢
    2021-02-07 11:04

    as other said, java doesn't support this.

    it does have something called a proxy class which can intercept calls to known methods (rather than undefined methods as in php's __call()). a proxy can be created dynamically as a wrapper around any interface:

    http://tutorials.jenkov.com/java-reflection/dynamic-proxies.html#proxy

    http://java.sun.com/j2se/1.4.2/docs/guide/reflection/proxy.html#examples

    Foo foo = (Foo) DebugProxy.newInstance(new FooImpl());
    foo.bar(null);
    

    foo looks like a Foo, but all the calls are intercepted by FooImpl's invoke() method.

    to create a truly de novo class at runtime with dynamic methods in its interface, you can essentially compile a class definition and use java's class loader to import it at runtime. a tool like apache's JCI or Arch4J can handle this for you. still, the class will only have those methods you specify.

提交回复
热议问题