简体   繁体   English

为什么checkstyle抱怨这个?

[英]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 ? 为什么checkstyle会抱怨“写b的发生”这是什么意思? The doSomething1() is always called before doSomething() 在doSomething()之前总是调用doSomething1()

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. 这不是Checkstyle,而是Eclipse本身。

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. 这些是黄色的,就像Checkstyle标记一样。

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. 它将突出显示属性被读取的实例(“X的出现”)以及可以写入该属性的位置(“写出X的出现”),例如,如果将其作为参数传递给方法。

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 您可以看到消息的配置位置http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.5.2/org.eclipse.jdt/ui/3.5.2/org/eclipse/ JDT /内部/ UI /搜索/ SearchMessages.properties

I recently did some research into CheckStyle, FindBugs and others. 我最近对CheckStyle,FindBugs等进行了一些研究。

With FindBugs ( findbugs.sourceforge.net ), a common issue is what FindBugs calls "dead store", when a value is never read, but only written . 使用FindBugs(findbugs.sourceforge.net)时,一个常见的问题是FindBugs称之为“死存储”,当一个值永远不会被读取但只写入时 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'. 当您将光标上下文放在特定术语上时,就会出现这种现象,在您的情况下为'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. 试试这个,单击Exception一词的某个地方,你会看到只有浅灰色的空心框。 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. 看起来不像checkstyle http://checkstyle.sourceforge.net/availablechecks.html附带的那个。如果它是你自己的自定义规则,你需要问任何人写了那个自定义检查。

I can only think that the rule is for non mutable objects, which are much easier to maintain. 我只能认为该规则适用于非易变对象,这些对象更容易维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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