Skip to content

CAN_NOT_PROXY_CLASS

Googler edited this page Aug 13, 2020 · 1 revision

CAN_NOT_PROXY_CLASS

Summary

When Guice detects that there is a circular dependency cycle while creating objects, it will try to break the cycle by generating a proxy at runtime. However, this circular proxy feature only works when the the type in the cycle is an interface because Guice can not proxy non-interface types. So you will get a CAN_NOT_PROXY_CLASS error when Guice failed to generate a proxy at runtime.

Example:

final class Foo {
  @Inject
  Foo(Bar bar) {
    ...
  }
}

final class Bar {
  @Inject
  Bar(Foo foo) {
    ...
  }
}

Both Foo and Bar are concrete class and Guice can't break this circular dependency cycle so CAN_NOT_PROXY_CLASS error is thrown.

NOTE:: If circular proxy feature is disabled, the above example will cause a CIRCULAR_PROXY_DISABLED error instead.

Common Causes

Unintended circular dependency cycle

As a best practice, avoid introducing circular dependency cycles in your application. If there are no cycles, there is no need for the proxy. See ways to avoid circular dependencies.

Using concrete types

If you can't break the cycle then you can try switching to use an interface in the cycle so Guice can use the proxy to break the chain:

interface Foo {}

final class FooImpl implements Foo {
  @Inject
  FooImpl(Bar bar) {
    ...
  }
}

interface Bar {}

final class BarImpl implements Bar {
  @Inject
  BarImpl(Foo foo) {
    ...
  }
}
Clone this wiki locally