Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to teach findbugs to understand IoC fields properly?

Tags:

java

findbugs

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?

like image 911
yegor256 Avatar asked Nov 18 '10 09:11

yegor256


1 Answers

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 :)

like image 198
yegor256 Avatar answered Oct 23 '22 09:10

yegor256