yegor256
yegor256

Reputation: 105193

How to teach findbugs to understand IoC fields properly?

This is my class (JAX-RS annotated):

@Path("/")
public class Foo {
  @Context
  private UriInfo uriInfo;
  // ...
}

This is what findbugs says:

Unwritten field: com.XXX.Foo.uriInfo

It's true, the field is unwritten, but it is injected by JAX-RS servlet. I think that I'm doing something wrong here, but how to solve the problem?

Upvotes: 6

Views: 1854

Answers (1)

yegor256
yegor256

Reputation: 105193

What I've understand so far is that findbugs is right. It tells me that this variable is not accessible from outside of the class, and my annotation is not valid in terms of OOP. The JAX-RS servlet will have to break field access restrictions in order to inject UriInfo. I have to give him a legal way to this field:

@Path("/")
public class Foo {
  private UriInfo uriInfo;
  @Context
  public void setUriInfo(UriInfo info) {
    this.uriInfo = info;
  }
  // ...
}

Now it's correct for findbugs and for OOP design paradigm :)

Upvotes: 3

Related Questions