Is there a Checkstyle rule available to restrict non-static access to static variables and methods?
This should raise a warning:
instance.staticField = value;
Eclipse has a setting for this, but I want to enforce it on the build.
I guess using javac -Xlint:static -Werror toto.java
is what you're looking for.
From documentation :
-Xlint:name
: Enable warning name. See the section Warnings That Can Be Enabled or Disabled with -Xlint Option for a list of warnings you can enable with this option.
-Werror
: Terminate compilation if warnings occur.
I tried with this example :
public class StaticTest {
public static String toto = "toto";
public static void main(String s[]) {
StaticTest st = new StaticTest();
st.toto="dfd";
}
}
and the output is:
StaticTest.java:16: warning: [static] static variable should be qualified by type name,
StaticTest, instead of by an expression
st.toto="dfd"; ^
error: warnings found and -Werror specified 1 error 1 warning
As it name indicates it, Checkstyle only check the form of your code. If you search bug patterns, you should take a look at Findbugs:
不,Checkstyle的所有检查都具有局限性/范围-它仅检查一个文件的结构,无法获取其他类/文件的结构。
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.