Skip to content

Commit aeb64ad

Browse files
committed
start to make UseVarArgs more usable
1 parent bf816f7 commit aeb64ad

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

etc/messages.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,9 +1173,13 @@ a[0] = new A(); // results in ArrayStoreException (Runtime)
11731173
<Detector class="com.mebigfatguy.fbcontrib.detect.UseVarArgs">
11741174
<Details>
11751175
<![CDATA[
1176-
<p>This detector looks for definitions of methods that have an array as the last parameter.
1176+
<p>This detector looks for problems and possibilities to use var args better. Specially looks for
1177+
<ul>
1178+
<li>definitions of methods that have an array as the last parameter.
11771179
Since this class is compiled with Java 1.5 or better, it would be more flexible for clients of this
1178-
method to define this parameter as a vararg parameter.</p>
1180+
method to define this parameter as a vararg parameter</li>
1181+
<li>classes that have samed named vararg and non vararg methods not clearly differentiated.</li>
1182+
<li>Explicitly passing null to a var arg parameter</li></ul>.</p>
11791183
<p>It is a fast detector.</p>
11801184
]]>
11811185
</Details>

src/main/java/com/mebigfatguy/fbcontrib/detect/UseVarArgs.java

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,49 +85,55 @@ public void visitMethod(Method obj) {
8585
return;
8686
}
8787

88-
if (Values.CONSTRUCTOR.equals(getMethodName()) && javaClass.getClassName().contains("$")) {
89-
return;
90-
}
91-
92-
List<String> types = SignatureUtils.getParameterSignatures(obj.getSignature());
93-
if ((types.isEmpty()) || (types.size() > 2)) {
94-
return;
95-
}
88+
boolean isVarMethod = (obj.getAccessFlags() & Constants.ACC_VARARGS) != 0;
9689

97-
if ((obj.getAccessFlags() & Constants.ACC_VARARGS) != 0) {
98-
return;
99-
}
90+
boolean isConvertable = !isVarMethod && methodHasConvertableLastParam(obj);
10091

101-
String lastParmSig = types.get(types.size() - 1);
102-
if (!lastParmSig.startsWith(Values.SIG_ARRAY_PREFIX)
103-
|| lastParmSig.startsWith(Values.SIG_ARRAY_OF_ARRAYS_PREFIX)) {
104-
return;
92+
super.visitMethod(obj);
93+
94+
if (isConvertable) {
95+
bugReporter.reportBug(new BugInstance(this, BugType.UVA_USE_VAR_ARGS.name(), LOW_PRIORITY).addClass(this)
96+
.addMethod(this));
10597
}
10698

107-
if (SignatureBuilder.SIG_BYTE_ARRAY.equals(lastParmSig)
108-
|| SignatureBuilder.SIG_CHAR_ARRAY.equals(lastParmSig)) {
109-
return;
110-
}
99+
} catch (ClassNotFoundException cnfe) {
100+
bugReporter.reportMissingClass(cnfe);
101+
}
102+
}
103+
104+
public boolean methodHasConvertableLastParam(Method method) throws ClassNotFoundException {
105+
if (Values.CONSTRUCTOR.equals(getMethodName()) && javaClass.getClassName().contains("$")) {
106+
return false;
107+
}
108+
List<String> types = SignatureUtils.getParameterSignatures(method.getSignature());
109+
if ((types.isEmpty()) || (types.size() > 2)) {
110+
return false;
111+
}
111112

112-
if (hasSimilarParms(types)) {
113-
return;
114-
}
113+
String lastParmSig = types.get(types.size() - 1);
114+
if (!lastParmSig.startsWith(Values.SIG_ARRAY_PREFIX)
115+
|| lastParmSig.startsWith(Values.SIG_ARRAY_OF_ARRAYS_PREFIX)) {
116+
return false;
117+
}
115118

116-
if (obj.isStatic() && "main".equals(obj.getName()) && SIG_STRING_ARRAY_TO_VOID.equals(obj.getSignature())) {
117-
return;
118-
}
119+
if (SignatureBuilder.SIG_BYTE_ARRAY.equals(lastParmSig)
120+
|| SignatureBuilder.SIG_CHAR_ARRAY.equals(lastParmSig)) {
121+
return false;
122+
}
119123

120-
if (!obj.isPrivate() && !obj.isStatic() && isInherited(obj)) {
121-
return;
122-
}
124+
if (hasSimilarParms(types)) {
125+
return false;
126+
}
123127

124-
super.visitMethod(obj);
125-
bugReporter.reportBug(new BugInstance(this, BugType.UVA_USE_VAR_ARGS.name(), LOW_PRIORITY).addClass(this)
126-
.addMethod(this));
128+
if (method.isStatic() && "main".equals(method.getName()) && SIG_STRING_ARRAY_TO_VOID.equals(method.getSignature())) {
129+
return false;
130+
}
127131

128-
} catch (ClassNotFoundException cnfe) {
129-
bugReporter.reportMissingClass(cnfe);
132+
if (!method.isPrivate() && !method.isStatic() && isInherited(method)) {
133+
return false;
130134
}
135+
136+
return true;
131137
}
132138

133139
/**

0 commit comments

Comments
 (0)