简体   繁体   中英

Why does checkstyle complain about this?

I have a class field which is a data object. Why does checkstyle complain about "write occurrence of b" and what does it mean ? The doSomething1() is always called before doSomething()

public class A{
  private B b;
  public void doSomething() {
    if(b!=null) {
      b.setYear(2012);
      b.setDay("Tuesday");
   } 
  }
  public void doSomething1(){
    b = new B();
    b.setDate(new Date());
  }
}

It is not Checkstyle that this is coming from, but Eclipse itself.

If you highlight a property of your class, it will show you occurrences of that property in the class, with markers in the right-hand scroll bar. These are yellow, like the Checkstyle markers.

It will highlight instances where the property is read ("Occurrence of X") and where that property could be written to ("Write occurence of X"), for instance if you pass it into a method as a parameter.

You can see where the message is configured http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.5.2/org.eclipse.jdt/ui/3.5.2/org/eclipse/jdt/internal/ui/search/SearchMessages.properties

I recently did some research into CheckStyle, FindBugs and others.

With FindBugs ( findbugs.sourceforge.net ), a common issue is what FindBugs calls "dead store", when a value is never read, but only written . Could it be this issue?

Your example seems to be very simplified - can you provide more details?

It's not an error. This phenomenon occurs when you put the cursor context on a particular term, in your case 'b'. When you do so, you can see all occurrences of that term demarcated by empty gray boxes to the right of your code window. You will also see in pale yellow the places where that term, if it's a variable, has data written to it.

Try this out, click somewhere on the word Exception and you will see there are only pale gray, hollow boxes. Now try a real variable in your code, you should see gray boxes and one or more pale yellow boxes appear.

I've never used this feature myself, but maybe someone finds it valuable.

Are you sure this isn't your own custom rule? Doesn't look like one of the ones that come with checkstyle http://checkstyle.sourceforge.net/availablechecks.html If it's your own custom rule, you need to ask whomever wrote that custom check.

I can only think that the rule is for non mutable objects, which are much easier to maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM