Redundant nullcheck in SWITCH_TABLE

View: New views
1 Messages — Rating Filter:   Alert me  

Redundant nullcheck in SWITCH_TABLE

by James Gula :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am frequently troubled by reports similar to the following which I assume is a false positive triggered by the code that the compiler generates for a switch table. In all the problem cases, I am switching on an enum value but unfortunately, I can't get it to recreate in a simple example. I have attached an example with the offending source file and the resulting class file but unfortunately these can't be compiled standalone without pulling in a bunch of additional stuff.

This problem is particularly annoying because the only way to turn off the warning is to suppress RCN for the whole class.

I am using Findbugs 1.3.4 20080506 with JSDK 1.6.0 u6 under Eclipse 3.3.3 and OpenSuse 10.3 although I've seen the problem through multiple versions of everything.

L D RCN: Redundant nullcheck of org.dablr.lib.gui.swt.SwtMessageBox.$SWITCH_TABLE$org$dablr$lib$gui$dialog$MessageBoxIcon, which is known to be non-null in org.dablr.lib.gui.swt.SwtMessageBox.$SWITCH_TABLE$org$dablr$lib$gui$dialog$MessageBoxIcon() dablr-0.0.3/src/main/java/org/dablr/lib/gui/swt SwtMessageBox.java line 26 1210717722362 58619

This is triggered by the following code.

public static int convertIconToSwt(final MessageBoxIcon icon) {
switch(icon) {
case ERROR:
return SWT.ICON_ERROR;
case WARNING:
return SWT.ICON_WARNING;
case QUESTION:
return SWT.ICON_QUESTION;
case WORKING:
return SWT.ICON_WORKING;
default:
throw new EnhancedIllegalArgumentException("icon", "unexpected icon value: " + icon.toString());

}
}

Can anyone shed some light on the issue?

Thanks,
Jim


James Gula
2018 Galaxy
Newport Beach, CA 92660
949-646-6753

[SwtMessageBox.java]

/**
 *
 */
package org.dablr.lib.gui.swt;

import org.dablr.lib.gui.GuiService;
import org.dablr.lib.gui.Site;
import org.dablr.lib.gui.dialog.DialogId;
import org.dablr.lib.gui.dialog.DialogStatus;
import org.dablr.lib.gui.dialog.MessageBoxButtonStyle;
import org.dablr.lib.gui.dialog.MessageBoxIcon;
import org.dablr.lib.gui.dialog.AbstractMessageBox;
import org.dablr.lib.util.EnhancedIllegalArgumentException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;



/**
 * An SWT implementation of a message box.
 *
 * @author James Gula
 *
 */
public class SwtMessageBox extends AbstractMessageBox {
        /**
         * The value for the SWT close button.
         */
        private static final int SWT_CLOSE_BUTTON = -4;

        /**
         * The SWT widget that implements the message box.
         */
        private final org.eclipse.swt.widgets.MessageBox swtMessageBox;

        /**
         * Initializes a new instance of the class.
         *
         * @param id the unique dialog identifier
         * @param site the site on which the message box will be displayed
         * @param guiService the <code>GuiService</code> used for resources
         * @param title the text that will appear as the title of the message box
         * @param message the string text of the message box
         * @param icon the standard icon to display
         * @param buttonStyle the button configuration to use
         */
        public SwtMessageBox(
                        final DialogId id,
                        final Site site,
                        final GuiService guiService,
                        final String title,
                        final String message,
                        final MessageBoxIcon icon,
                        final MessageBoxButtonStyle buttonStyle) {
                super(id, site, guiService, title, message, icon, buttonStyle);
                if (!(site instanceof SwtSite)) {
                        throw new EnhancedIllegalArgumentException("site", "Site is not an instance of SwtSite");
                }
                if (!(guiService instanceof SwtGuiService)) {
                        throw new EnhancedIllegalArgumentException("guiService", "guiService is not an instance of SwtGuiService");
                }
                Composite parent = ((SwtSite) site).getComposite();
                if (!(parent instanceof Shell)) {
                        throw new EnhancedIllegalArgumentException("site", "the composite of the site is not a shell");
                }
                int swtStyle = convertIconToSwt(icon) | convertButtonStyleToSwt(buttonStyle);
                this.swtMessageBox = new org.eclipse.swt.widgets.MessageBox((Shell) parent, swtStyle);
                this.swtMessageBox.setMessage(message);
                this.swtMessageBox.setText(title);
        }

        /* @see org.dablr.lib.gui.dialog.Dialog#show() */
        /** {@inheritDoc} */
        public DialogStatus show() {
                int result = swtMessageBox.open();
                switch(result) {
                case SWT.OK:
                case SWT.YES:
                case SWT.RETRY:
                        return DialogStatus.OK;
                case SWT.CANCEL:
                case SWT.ABORT:
                case SWT_CLOSE_BUTTON: // When user clicks the window close button
                        return DialogStatus.CANCEL;
                case SWT.NO:
                case SWT.IGNORE:
                        return DialogStatus.OTHER;
                       
                default:
                    String text = Integer.toHexString(result) + " " + Integer.toString(result);
                        throw new IllegalStateException("Unexpected SWT MessageBox result 0x" + text);
                }
        }
       
        /**
         * Converts an icon enumeration value to an SWT value.
         *
         * @param icon the icon to convert
         * @return the SWT value
         */
        public static int convertIconToSwt(final MessageBoxIcon icon) {
                switch(icon) {
                case ERROR:
                        return SWT.ICON_ERROR;
                case WARNING:
                        return SWT.ICON_WARNING;
                case QUESTION:
                        return SWT.ICON_QUESTION;
                case WORKING:
                        return SWT.ICON_WORKING;
                default:
                        throw new EnhancedIllegalArgumentException("icon", "unexpected icon value: " + icon.toString());

                }
        }
       
        /**
         * Converts a button style enumeration value to an SWT value.
         *
         * @param buttonStyle the button style to convert
         * @return the SWT value
         */
        public static int convertButtonStyleToSwt(final MessageBoxButtonStyle buttonStyle) {
                switch(buttonStyle) {
                case OK:
                        return SWT.OK;
                case OK_CANCEL:
                        return SWT.OK | SWT.CANCEL;
                case YES_NO:
                        return SWT.YES | SWT.NO;
                case YES_NO_CANCEL:
                        return SWT.YES | SWT.NO | SWT.CANCEL;
                case RETRY_CANCEL:
                        return SWT.RETRY | SWT.CANCEL;
                case ABORT_RETRY_IGNORE:
                        return SWT.ABORT | SWT.RETRY | SWT.IGNORE;
                default:
                        throw new EnhancedIllegalArgumentException(
                                "buttonStyle", "unexpected buttonStyle value: " + buttonStyle.toString());
                }
        }

}




_______________________________________________
Findbugs-discuss mailing list
Findbugs-discuss@...
https://mailman.cs.umd.edu/mailman/listinfo/findbugs-discuss

SwtMessageBox.class (6K) Download Attachment
smime.p7s (4K) Download Attachment
LightInTheBox - Buy quality products at wholesale price