Question: Q4: See the JAVA code on the next few pages. Use this code in a new JAVA project (name the Project/File as FindBugsGroupNumber.java), and perform

Q4: See the JAVA code on the next few pages. Use this code in a new JAVA project (name the Project/File as FindBugsGroupNumber.java), and perform a static code analysis using the SpotBugs tool. a) Identify the reported bugs from the tool, and give a brief overview of these bugs. b) Find a solution to fix each of these bugs, and submit the modified (working version) code/project. c) Use a table to answer a) and b)

//Codes for Q4

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.Collection;

import java.util.UnknownFormatConversionException;

public class Findbugs1 {

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

System.out.println("Findbugs Sample 001 for BC_IMPOSSIBLE_CAST");

try {

Findbugs1.bcImpossibleCastWRONG();

} catch (final ClassCastException e) {

System.out.println(" - ERROR:" + e.getMessage());

}

System.out.println("Findbugs Sample 002 for BC_IMPOSSIBLE_DOWNCAST");

try {

Findbugs1.bcImpossibleDowncastWRONG();

} catch (final ClassCastException e) {

System.out.println(" - ERROR:" + e.getMessage());

}

System.out.println("Findbugs Sample 003 for BC_IMPOSSIBLE_INSTANCEOF");

Findbugs1.bcImpossibleInstanceOfWRONG();

System.out.println("Findbugs Sample 004 for BC_IMPOSSIBLE_DOWNCAST_OF_TOARRAY");

try {

Findbugs1.bcImpossibleDowncastOfArrayWRONG();

} catch (final ClassCastException e) {

System.out.println(" - ERROR:" + e.getMessage());

}

System.out.println("Findbugs Sample 005 for DMI_BIGDECIMAL_CONSTRUCTED_FROM_DOUBLE");

Findbugs1.dmiBigDecimalConstructedFromDoubleWRONG();

System.out.println("Findbugs Sample 006 for ES_COMPARING_STRINGS_WITH_EQ");

Findbugs1.esComparingStringsWithEqWRONG();

System.out.println("Findbugs Sample 007 for VA_FORMAT_STRING_ILLEGAL");

try {

Findbugs1.vaFormatStringIllegalWRONG();

} catch (final UnknownFormatConversionException e) {

System.out.println(" - ERROR:" + e.getMessage());

}

System.out.println("Findbugs Sample 008 for RV_RETURN_VALUE_IGNORED");

Findbugs1.rvReturnValueIgnoredWRONG();

System.out.println("Findbugs Sample 009 for NP_ALWAYS_NULL");

try {

Findbugs1.npAlwaysNullWRONG();

} catch (final NullPointerException e) {

System.out.println(" - ERROR:" + e.getMessage());

}

System.out.println("Findbugs Sample 010 for QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT");

Findbugs1.qabQuestionableBooleanAssignmentWRONG();

}

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 stringVector = new ArrayList();

stringVector.add("abc");

stringVector.add("xyz");

final String[] stringArray = (String[]) stringVector.toArray();

System.out.println(" - " + stringArray.length);

}

private static void bcImpossibleDowncastOfArrayCORRECT() {

final Collection stringVector = new ArrayList();

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"));

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!