|
Deprecated
Override both public boolean Object.equals(Object other), and public int Object.hashCode(), or override neither. Even if you are inheriting a hashCode() from a parent class, consider implementing hashCode and explicitly delegating to your superclass. Example :
// this is bad
public class Bar {
public boolean equals(Object o) {
// do some comparison
}
}
// and so is this
public class Baz {
public int hashCode() {
// return some hash value
}
}
// this is OK
public class Foo {
public boolean equals(Object other) {
// do some comparison
}
public int hashCode() {
// return some hash value
}
}
This rule is deprecated, use squid:S1206 instead.
Repository: pmd
Key: OverrideBothEqualsAndHashcode
Available since 07 Jan 2014
|
|
Overriding a method just to call the same method from the super class without performing any other actions is useless and misleading.
The following code snippet illustrates this rule:
public void doSomething() { // Non-Compliant
super.doSomething();
}
@Override
public boolean isLegal(Action action) { // Non-Compliant
return super.isLegal(action);
}
@Override
public boolean isLegal(Action action) { // Compliant - not simply forwarding the call
return super.isLegal(new Action(/* ... */));
}
@Id
@Override
public int getId() { // Compliant - there is annotation different from @Override
return super.getId();
}
Repository: squid
Key: S1185
Available since 07 Jan 2014
|
|
Sharing some naming conventions is a key point to make it possible for a team to efficiently collaborate. This rule allows to check that all package names match a provided regular expression.
The following code snippet illustrates this rule when the regular expression value is "^[a-z]+(\\.[a-z][a-z0-9]*)*$":
package org.Example; // Non-Compliant
package org.example; // Compliant
Repository: squid
Key: S00120
Available since 07 Jan 2014
|
|
This class is an inner class, but does not use its embedded reference
to the object which created it. This reference makes the instances
of the class larger, and may keep the reference to the creator object
alive longer than necessary. If possible, the class should be
made into a static inner class. Since anonymous inner
classes cannot be marked as static, doing this will require refactoring
the inner class so that it is a named inner class.
Repository: findbugs
Key: SIC_INNER_SHOULD_BE_STATIC_ANON
Available since 07 Jan 2014
|
|
This class is an inner class, but does not use its embedded reference
to the object which created it except during construction of the
inner object. This reference makes the instances
of the class larger, and may keep the reference to the creator object
alive longer than necessary. If possible, the class should be
made into a static inner class. Since the reference to the
outer object is required during construction of the inner instance,
the inner class will need to be refactored so as to
pass a reference to the outer instance to the constructor
for the inner class.
Repository: findbugs
Key: SIC_INNER_SHOULD_BE_STATIC_NEEDS_THIS
Available since 07 Jan 2014
|
|
A large String constant is duplicated across multiple class files.
This is likely because a final field is initialized to a String constant, and the Java language
mandates that all references to a final field from other classes be inlined into
that classfile. See JDK bug 6447475
for a description of an occurrence of this bug in the JDK and how resolving it reduced
the size of the JDK by 1 megabyte.
Repository: findbugs
Key: HSC_HUGE_SHARED_STRING_CONSTANT
Available since 07 Jan 2014
|
|
This method accesses the value of a Map entry, using a key that was retrieved from
a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the
Map.get(key) lookup.
Repository: findbugs
Key: WMI_WRONG_MAP_ITERATOR
Available since 07 Jan 2014
|
|
Repository: findbugs
Key: DMI_COLLECTION_OF_URLS
Available since 07 Jan 2014
|
|
A boxed primitive is allocated just to call toString(). It is more effective to just use the static
form of toString which takes the primitive value. So,
Replace... | With this... |
new Integer(1).toString() | Integer.toString(1) |
new Long(1).toString() | Long.toString(1) |
new Float(1.0).toString() | Float.toString(1.0) |
new Double(1.0).toString() | Double.toString(1.0) |
new Byte(1).toString() | Byte.toString(1) |
new Short(1).toString() | Short.toString(1) |
new Boolean(true).toString() | Boolean.toString(true) |
Repository: findbugs
Key: DM_BOXED_PRIMITIVE_TOSTRING
Available since 07 Jan 2014
|
|
This method allocates an object just to call getClass() on it, in order to
retrieve the Class object for it. It is simpler to just access the .class property of the class.
Repository: findbugs
Key: DM_NEW_FOR_GETCLASS
Available since 07 Jan 2014
|
|
This method uses a static method from java.lang.Math on a constant value. This method's
result in this case, can be determined statically, and is faster and sometimes more accurate to
just use the constant. Methods detected are:
Method | Parameter |
abs | -any- |
acos | 0.0 or 1.0 |
asin | 0.0 or 1.0 |
atan | 0.0 or 1.0 |
atan2 | 0.0 |
cbrt | 0.0 or 1.0 |
ceil | -any- |
cos | 0.0 |
cosh | 0.0 |
exp | 0.0 or 1.0 |
expm1 | 0.0 |
floor | -any- |
log | 0.0 or 1.0 |
log10 | 0.0 or 1.0 |
rint | -any- |
round | -any- |
sin | 0.0 |
sinh | 0.0 |
sqrt | 0.0 or 1.0 |
tan | 0.0 |
tanh | 0.0 |
toDegrees | 0.0 or 1.0 |
toRadians | 0.0 |
Repository: findbugs
Key: UM_UNNECESSARY_MATH
Available since 07 Jan 2014
|
|
The method seems to be building a String using concatenation in a loop.
In each iteration, the String is converted to a StringBuffer/StringBuilder,
appended to, and converted back to a String.
This can lead to a cost quadratic in the number of iterations,
as the growing string is recopied in each iteration.
Better performance can be obtained by using
a StringBuffer (or StringBuilder in Java 1.5) explicitly.
For example:
// This is bad
String s = "";
for (int i = 0; i < field.length; ++i) {
s = s + field[i];
}
// This is better
StringBuffer buf = new StringBuffer();
for (int i = 0; i < field.length; ++i) {
buf.append(field[i]);
}
String s = buf.toString();
Repository: findbugs
Key: SBSC_USE_STRINGBUFFER_CONCATENATION
Available since 07 Jan 2014
|
|
Creating new instances of java.lang.Boolean wastes
memory, since Boolean objects are immutable and there are
only two useful values of this type. Use the Boolean.valueOf()
method (or Java 1.5 autoboxing) to create Boolean objects instead.
Repository: findbugs
Key: DM_BOOLEAN_CTOR
Available since 07 Jan 2014
|
|
Using new Double(double) is guaranteed to always result in a new object whereas
Double.valueOf(double) allows caching of values to be done by the compiler, class library, or JVM.
Using of cached values avoids object allocation and the code will be faster.
Unless the class must be compatible with JVMs predating Java 1.5,
use either autoboxing or the valueOf() method when creating instances of Double and Float .
Repository: findbugs
Key: DM_FP_NUMBER_CTOR
Available since 07 Jan 2014
|
|
Creating a new java.lang.String object using the
no-argument constructor wastes memory because the object so created will
be functionally indistinguishable from the empty string constant
"" . Java guarantees that identical string constants
will be represented by the same String object. Therefore,
you should just use the empty string constant directly.
Repository: findbugs
Key: DM_STRING_VOID_CTOR
Available since 07 Jan 2014
|
|
Using the java.lang.String(String) constructor wastes memory
because the object so constructed will be functionally indistinguishable
from the String passed as a parameter. Just use the
argument String directly.
Repository: findbugs
Key: DM_STRING_CTOR
Available since 07 Jan 2014
|
|
Using new Integer(int) is guaranteed to always result in a new object whereas
Integer.valueOf(int) allows caching of values to be done by the compiler, class library, or JVM.
Using of cached values avoids object allocation and the code will be faster.
Values between -128 and 127 are guaranteed to have corresponding cached instances
and using valueOf is approximately 3.5 times faster than using constructor.
For values outside the constant range the performance of both styles is the same.
Unless the class must be compatible with JVMs predating Java 1.5,
use either autoboxing or the valueOf() method when creating instances of
Long , Integer , Short , Character , and Byte .
Repository: findbugs
Key: DM_NUMBER_CTOR
Available since 07 Jan 2014
|
|
Calling String.toString() is just a redundant operation.
Just use the String.
Repository: findbugs
Key: DM_STRING_TOSTRING
Available since 07 Jan 2014
|
|
This method uses the toArray() method of a collection derived class, and passes
in a zero-length prototype array argument. It is more efficient to use
myCollection.toArray(new Foo[myCollection.size()])
If the array passed in is big enough to store all of the
elements of the collection, then it is populated and returned
directly. This avoids the need to create a second array
(by reflection) to return as the result.
Repository: findbugs
Key: ITA_INEFFICIENT_TO_ARRAY
Available since 07 Jan 2014
|
|
A primitive is boxed, and then immediately unboxed. This probably is due to a manual
boxing in a place where an unboxed value is required, thus forcing the compiler
to immediately undo the work of the boxing.
Repository: findbugs
Key: BX_BOXING_IMMEDIATELY_UNBOXED
Available since 07 Jan 2014
|
|
A primitive boxed value constructed and then immediately converted into a different primitive type
(e.g., new Double(d).intValue() ). Just perform direct primitive coercion (e.g., (int) d ).
Repository: findbugs
Key: BX_BOXING_IMMEDIATELY_UNBOXED_TO_PERFORM_COERCION
Available since 07 Jan 2014
|
|
This private method is never called. Although it is
possible that the method will be invoked through reflection,
it is more likely that the method is never used, and should be
removed.
Repository: findbugs
Key: UPM_UNCALLED_PRIVATE_METHOD
Available since 07 Jan 2014
|
|
This class is an inner class, but does not use its embedded reference
to the object which created it. This reference makes the instances
of the class larger, and may keep the reference to the creator object
alive longer than necessary. If possible, the class should be
made static.
Repository: findbugs
Key: SIC_INNER_SHOULD_BE_STATIC
Available since 07 Jan 2014
|
|
Repository: findbugs
Key: DMI_BLOCKING_METHODS_ON_URL
Available since 07 Jan 2014
|
|
This field is never read. Consider removing it from the class.
Repository: findbugs
Key: URF_UNREAD_FIELD
Available since 07 Jan 2014
|