Magic constant inspection

This commit is contained in:
Alexey Kudravtsev
2012-01-12 16:41:32 +04:00
parent d966eef1fa
commit a87146dc5b
15 changed files with 1670 additions and 49 deletions
@@ -0,0 +1,435 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<!--<problem>-->
<!--<file>X.java</file>-->
<!--<line>5</line>-->
<!--<problem_class>Duplicate throws</problem_class>-->
<!--<description>Duplicate throws</description>-->
<!--</problem>-->
<problem>
<file>X.java</file>
<line>29</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>30</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>31</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>33</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>34</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>35</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>36</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>55</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>56</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>57</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>59</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>60</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>61</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>62</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>81</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>82</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>83</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>85</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>86</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>87</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>88</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>118</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>119</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>122</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>123</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>124</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>125</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>173</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>174</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>175</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>177</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>178</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>179</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>180</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y, Const.Z</description>
</problem>
<problem>
<file>X.java</file>
<line>193</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>195</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>227</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>228</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>229</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>230</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
<problem>
<file>X.java</file>
<line>231</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">magic constant</problem_class>
<description>Must be one of the: Const.X, Const.Y</description>
</problem>
</problems>
@@ -0,0 +1,235 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.intellij.lang.annotations.MagicConstant;
import java.io.*;
class Const {
public static final int X = 0;
public static final int Y = 2;
public static final int Z = 3;
}
public class X {
void f(@MagicConstant(intValues={Const.X, Const.Y, Const.Z}) int x) {
/////////// BAD
f(0);
f(1);
f(Const.X | Const.Y);
int i = Const.X | Const.Y;
f(i);
if (x == 3) {
x = 2;
assert x != 1;
}
////////////// GOOD
f(Const.X);
f(Const.Y);
f(Const.Z);
int i2 = this == null ? Const.X : Const.Y;
f(i2);
if (x == Const.X) {
x = Const.Y;
assert x != Const.Z;
}
f2(x);
}
void f2(@MagicConstant(valuesFromClass =Const.class) int x) {
/////////// BAD
f2(0);
f2(1);
f2(Const.X | Const.Y);
int i = Const.X | Const.Y;
f2(i);
if (x == 3) {
x = 2;
assert x != 1;
}
////////////// GOOD
f2(Const.X);
f2(Const.Y);
f2(Const.Z);
int i2 = this == null ? Const.X : Const.Y;
f2(i2);
if (x == Const.X) {
x = Const.Y;
assert x != Const.Z;
}
f(x);
}
void f3(@MagicConstant(flags ={Const.X, Const.Y, Const.Z}) int x) {
/////////// BAD
f3(2);
f3(1);
f(Const.X | Const.Y);
int i = Const.X | 4;
f3(i);
if (x == 3) {
x = 2;
assert x != 1;
}
////////////// GOOD
f3(Const.X);
f3(Const.Y);
f3(Const.Z);
int i2 = this == null ? Const.X : Const.Y;
f3(i2);
int ix = Const.X | Const.Y;
f3(ix);
f3(0);
f3(-1);
int f = 0;
if (x == Const.X) {
x = Const.Y;
assert x != Const.Z;
f |= Const.Y;
}
else {
f |= Const.X;
}
f3(f);
f4(x);
}
void f4(@MagicConstant(flagsFromClass =Const.class) int x) {
/////////// BAD
f4(-3);
f4(1);
f4(Const.X | Const.Y);
int i = Const.X | 4;
f4(i);
if (x == 3) {
x = 2;
assert x != 1;
}
////////////// GOOD
f4(Const.X);
f4(Const.Y);
f4(Const.Z);
int i2 = this == null ? Const.X : Const.Y;
f4(i2);
int ix = Const.X | Const.Y;
f4(ix);
f4(0);
f4(-1);
int f = 0;
if (x == Const.X) {
x = Const.Y;
assert x != Const.Z;
f |= Const.Y;
}
else {
f |= Const.X;
}
f4(f);
f3(x);
}
class Alias {
@MagicConstant(intValues={Const.X, Const.Y, Const.Z})
@interface IntEnum{}
void f(@IntEnum int x) {
////////////// GOOD
f(Const.X);
f(Const.Y);
f(Const.Z);
int i2 = this == null ? Const.X : Const.Y;
f(i2);
if (x == Const.X) {
x = Const.Y;
assert x != Const.Z;
}
f2(x);
/////////// BAD
f(0);
f(1);
f(Const.X | Const.Y);
int i = Const.X | Const.Y;
f(i);
if (x == 3 || getClass().isInterface()) {
x = 2;
assert x != 1;
}
f2(x);
}
}
class MagicAnnoInsideAnnotationUsage {
@interface III {
@MagicConstant(intValues = {Const.X, Const.Y}) int val();
}
// bad
@III(val = 2)
int h;
@III(val = Const.X | Const.Y)
void f(){}
// good
@III(val = Const.X)
int h2;
}
abstract class BeanInfoParsing {
/**
* @see java.lang.Runtime#exit(int)
*
* @beaninfo
* preferred: true
* bound: true
* enum: DO_NOTHING_ON_CLOSE Const.X
* HIDE_ON_CLOSE Const.Y
* description: The frame's default close operation.
*/
public void setX(int operation) {
}
public abstract int getX();
{
// good
setX(Const.X);
setX(Const.Y);
if (getX() == Const.X || getX() == Const.Y) {}
// bad
setX(0);
setX(-1);
setX(Const.Z);
if (getX() == 1) {}
if (getX() == Const.Z) {}
}
}
}