Read More‎ > ‎

Findbugs™ Warnings By Example - Basic Rules



This article describes selected warnings of Findbugs with Java sample code. The following 10 warnings are covered: 

- BC_IMPOSSIBLE_CAST 
- BC_IMPOSSIBLE_DOWNCAST 
- BC_IMPOSSIBLE_INSTANCEOF 
- BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY 
- DMI_BIGDECIMAL_CONSTRUCTED_FROM_DOUBLE 
- ES_COMPARING_STRINGS_WITH_EQ 
- VA_FORMAT_STRING_ILLEGAL 
- RV_RETURN_VALUE_IGNORED 
- NP_ALWAYS_NULL 
- QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT

Motivation for the 'Findbugs™ Warnings By Example' Series











The FindBugs™ bug descriptions of the online documentation are concise and well written. 

Unfortunately, some parts of the descriptions are not easy to understand (even for experienced Java developers). It can be difficult to understand the exact root cause for a warning and/or to find the correct way of fixing. 

To be honest - at least I had problems with some warnings in the last years. Quite often, I found no helping sample code in the web. The main weakness of the bug descriptions is, that it uses seldom sample code to demonstrate the wrong and correct situation. 

Example Code

The following code has been compiled with JDK 1.6.0_24 and Findbugs™ (Version 2.0.1-rc2) will show all warnings with the default settings of the Findbugs™ Eclipse Plugin (Version 2.0.1.20120511).

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.sw_engineering_candies;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UnknownFormatConversionException;

@SuppressWarnings(value = { "null", "unused" })
public class FindbugsWarningsByExample1 {

	public static void main(final String[] args) {

		System.out.println("Findbugs Sample 001 for BC_IMPOSSIBLE_CAST");
		// WRONG
		try {
			FindbugsWarningsByExample1.bcImpossibleCastWRONG();
		} catch (final ClassCastException e) {
			System.out.println("   - ERROR:" + e.getMessage());
		}
		// CORRECT
		FindbugsWarningsByExample1.bcImpossibleCastCORRECT();

		System.out.println("Findbugs Sample 002 for BC_IMPOSSIBLE_DOWNCAST");
		// WRONG
		try {
			FindbugsWarningsByExample1.bcImpossibleDowncastWRONG();
		} catch (final ClassCastException e) {
			System.out.println("   - ERROR:" + e.getMessage());
		}
		// CORRECT
		FindbugsWarningsByExample1.bcImpossibleDowncastCORRECT();

		System.out.println("Findbugs Sample 003 for BC_IMPOSSIBLE_INSTANCEOF");
		// WRONG
		FindbugsWarningsByExample1.bcImpossibleInstanceOfWRONG();
		// CORRECT
		FindbugsWarningsByExample1.bcImpossibleInstanceOfCORRECT();

		System.out.println("Findbugs Sample 004 for BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY");
		// WRONG
		try {
			FindbugsWarningsByExample1.bcImpossibleDowncastOfArrayWRONG();
		} catch (final ClassCastException e) {
			System.out.println("   - ERROR:" + e.getMessage());
		}
		// CORRECT
		FindbugsWarningsByExample1.bcImpossibleDowncastOfArrayCORRECT();

		System.out.println("Findbugs Sample 005 for DMI_BIGDECIMAL_CONSTRUCTED_FROM_DOUBLE");
		// WRONG
		FindbugsWarningsByExample1.dmiBigDecimalConstructedFromDoubleWRONG();
		// CORRECT
		FindbugsWarningsByExample1.dmiBigDecimalConstructedFromDoubleCORRECT();

		System.out.println("Findbugs Sample 006 for ES_COMPARING_STRINGS_WITH_EQ");
		// WRONG
		FindbugsWarningsByExample1.esComparingStringsWithEqWRONG();
		// CORRECT
		FindbugsWarningsByExample1.esComparingStringsWithEqCORRECT();

		System.out.println("Findbugs Sample 007 for VA_FORMAT_STRING_ILLEGAL");
		// WRONG
		try {
			FindbugsWarningsByExample1.vaFormatStringIllegalWRONG();
		} catch (final UnknownFormatConversionException e) {
			System.out.println("   - ERROR:" + e.getMessage());
		}
		// CORRECT
		FindbugsWarningsByExample1.vaFormatStringIllegalCORRECT();

		System.out.println("Findbugs Sample 008 for RV_RETURN_VALUE_IGNORED");
		// WRONG
		FindbugsWarningsByExample1.rvReturnValueIgnoredWRONG();
		// CORRECT
		FindbugsWarningsByExample1.rvReturnValueIgnoredCORRECT();

		System.out.println("Findbugs Sample 009 for NP_ALWAYS_NULL");
		// WRONG
		try {
			FindbugsWarningsByExample1.npAlwaysNullWRONG();
		} catch (final NullPointerException e) {
			System.out.println("   - ERROR:" + e.getMessage());
		}
		// CORRECT
		FindbugsWarningsByExample1.npAlwaysNullCORRECT();

		System.out.println("Findbugs Sample 010 for QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT");
		// WRONG
		FindbugsWarningsByExample1.qabQuestionableBooleanAssignmentWRONG();
		// CORRECT
		FindbugsWarningsByExample1.qabQuestionableBooleanAssignmentCORRECT();

	}

	private static void bcImpossibleCastWRONG() {
		final Object doubleValue = Double.valueOf(1.0);
		final Long value = (Long) doubleValue;
		System.out.println("   - " + value);
	}

	private static void bcImpossibleCastCORRECT() {
		final Object doubleValue = Double.valueOf(1.0);
		final Double value = (Double) doubleValue;
		System.out.println("   - " + value);
	}

	private static void bcImpossibleDowncastWRONG() {
		final Object exception = new RuntimeException("abc");
		final SecurityException value = (SecurityException) exception;
		System.out.println("   - " + value.getMessage());
	}

	private static void bcImpossibleDowncastCORRECT() {
		final Object exception = new RuntimeException("abc");
		final RuntimeException value = (RuntimeException) exception;
		System.out.println("   - " + value.getMessage());
	}

	private static void bcImpossibleInstanceOfWRONG() {
		final Object value = Double.valueOf(1.0);
		System.out.println("   - " + (value instanceof Long));
	}

	private static void bcImpossibleInstanceOfCORRECT() {
		final Object value = Double.valueOf(1.0);
		System.out.println("   - " + (value instanceof Double));
	}

	private static void bcImpossibleDowncastOfArrayWRONG() {
		final Collection<String> stringVector = new ArrayList<String>();
		stringVector.add("abc");
		stringVector.add("xyz");
		final String[] stringArray = (String[]) stringVector.toArray();
		System.out.println("   - " + stringArray.length);
	}

	private static void bcImpossibleDowncastOfArrayCORRECT() {
		final Collection<String> stringVector = new ArrayList<String>();
		stringVector.add("abc");
		stringVector.add("xyz");
		final String[] stringArray = stringVector.toArray(new String[stringVector.size()]);
		System.out.println("   - " + stringArray.length);
	}

	private static void dmiBigDecimalConstructedFromDoubleWRONG() {
		final BigDecimal bigDecimal = new BigDecimal(3.1);
		System.out.println("   - " + bigDecimal.toString());
	}

	private static void dmiBigDecimalConstructedFromDoubleCORRECT() {
		final BigDecimal bigDecimal = new BigDecimal("3.1");
		System.out.println("   - " + bigDecimal.toString());
	}

	private static void esComparingStringsWithEqWRONG() {
		final StringBuilder sb1 = new StringBuilder("1234");
		final StringBuilder sb2 = new StringBuilder("1234");
		final String string1 = sb1.toString();
		final String string2 = sb2.toString();
		System.out.println("   - " + (string1 == string2));
	}

	private static void esComparingStringsWithEqCORRECT() {
		final StringBuilder sb1 = new StringBuilder("1234");
		final StringBuilder sb2 = new StringBuilder("1234");
		final String string1 = sb1.toString();
		final String string2 = sb2.toString();
		System.out.println("   - " + string1.equals(string2));
	}

	private static void vaFormatStringIllegalWRONG() {
		System.out.println(String.format("   - %>s  %s", "10", "9"));
	}

	private static void vaFormatStringIllegalCORRECT() {
		System.out.println(String.format("   - %s > %s", "10", "9"));
	}

	private static void rvReturnValueIgnoredWRONG() {
		final BigDecimal bigDecimal = BigDecimal.ONE;
		bigDecimal.add(BigDecimal.ONE);
		System.out.println(String.format("   - " + bigDecimal));
	}

	private static void rvReturnValueIgnoredCORRECT() {
		final BigDecimal bigDecimal = BigDecimal.ONE;
		final BigDecimal newValue = bigDecimal.add(BigDecimal.ONE);
		System.out.println(String.format("   - " + newValue));
	}

	private static void npAlwaysNullWRONG() {
		final String value = null;
		if (null != value & value.length() > 2) {
			System.out.println(String.format("   - " + value));
		} else {
			System.out.println(String.format("   - value is invalid"));
		}
	}

	private static void npAlwaysNullCORRECT() {
		final String value = null;
		if (null != value && value.length() > 2) {
			System.out.println(String.format("   - " + value));
		} else {
			System.out.println(String.format("   - value is invalid"));
		}
	}

	private static void qabQuestionableBooleanAssignmentWRONG() {
		boolean value = false;
		if (value = true) {
			System.out.println(String.format("   - value is true"));
		} else {
			System.out.println(String.format("   - value is false"));
		}
	}

	private static void qabQuestionableBooleanAssignmentCORRECT() {
		final boolean value = false;
		if (value == true) {
			System.out.println(String.format("   - value is true"));
		} else {
			System.out.println(String.format("   - value is false"));
		}
	}

}

Find Code on GitHub

Change History

Revision  Name  Author  Description
 1.0  Jun 23, 2012  Markus Sprunck   first version
 1.1  Aug 19, 2012  Markus Sprunck  improved layout for tablets
 1.2  Jan 19, 2013  Markus Sprunck      smaller content improvements
 1.3  Apr 25, 2013  Markus Sprunck  source code now on GitHub

Sponsored Link