1

Am I right that's it impossible to use dynamic proxy with java enums?

Thanks in advance.

dhblah
  • 9,751
  • 12
  • 56
  • 92
  • I would have the singleton implement an interface and use that interface where possible. An interface can have multiple enums and classes implement it. – Peter Lawrey Dec 07 '11 at 15:27

2 Answers2

2

Dynamic proxies in Java can be created only for interfaces.

However, enums can implement interfaces, so that you can make your enum implement an interface, and then create a dynamic proxy for that interface:

public interface A { ... }
public enum B implements A { ... }

A a = Proxy.newProxyInstance(cl, new Class[] { A.class }, ih);
axtavt
  • 239,438
  • 41
  • 511
  • 482
1

That sounds right to me - just like you can't use dynamic proxies for static members.

Why do you need to do that in the first place? enums should generally be value objects or constants that don't need to be, or shouldn't be, proxied in the first place.

wrschneider
  • 17,913
  • 16
  • 96
  • 176