<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FindBugs &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/findbugs/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:19:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>FindBugs &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>FindBug: Solving DM_DEFAULT_ENCODING Warning When Using FileWriter</title>
		<link>https://myshittycode.com/2014/03/26/findbug-solving-dm_default_encoding-warning-when-using-filewriter/</link>
					<comments>https://myshittycode.com/2014/03/26/findbug-solving-dm_default_encoding-warning-when-using-filewriter/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 26 Mar 2014 14:52:24 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[FindBugs]]></category>
		<category><![CDATA[Java]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=446</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following code to write some data into a file:- When running this code against a static code analysis tool, such as Findbugs, we get this high priority warning:- The Javadoc for FileWriter says:- Obviously, this class is too convenient and FindBugs is not happy about it. Further, there is [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/03/26/findbug-solving-dm_default_encoding-warning-when-using-filewriter/">FindBug: Solving DM_DEFAULT_ENCODING Warning When Using FileWriter</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following code to write some data into a file:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [2]; title: ; notranslate">
File file = new File(someFilePath);
Writer w = new FileWriter(file);
PrintWriter pw = new PrintWriter(w);
pw.println(someContent);
pw.close();
</pre></div>


<p>When running this code against a static code analysis tool, such as Findbugs, we get this high priority warning:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Found reliance on default encoding in com.choonchernlim.epicapp.CodeGeneratorService.createFile(String):
new java.io.FileWriter(File)
Found a call to a method which will perform a byte to String (or String
to byte) conversion, and will assume that the default platform encoding
is suitable. This will cause the application behaviour to vary between
platforms. Use an alternative API and specify a charset name or Charset
object explicitly.
</pre></div>


<p>The Javadoc for <a href="http://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html" target="_blank" rel="noopener">FileWriter</a> says:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Convenience class for writing character files. The constructors of this class
assume that the default character encoding and the default byte-buffer size are
acceptable. To specify these values yourself, construct an OutputStreamWriter
on a FileOutputStream.
</pre></div>


<p>Obviously, this class is too convenient and FindBugs is not happy about it. Further, there is no way to set a charset using <b>FileWriter</b>.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>To make FindBugs as happy as <a href="www.youtube.com/watch?v=y6Sxv-sUYtM" target="_blank" rel="noopener">Pharrell Williams</a>, we can use <b>OutputStreamWriter</b> instead because it allows us to specify a charset.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [2]; title: ; notranslate">
File file = new File(someFilePath);
Writer w = new OutputStreamWriter(new FileOutputStream(file), &quot;UTF-8&quot;);
PrintWriter pw = new PrintWriter(w);
pw.println(someContent);
pw.close();
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2014/03/26/findbug-solving-dm_default_encoding-warning-when-using-filewriter/">FindBug: Solving DM_DEFAULT_ENCODING Warning When Using FileWriter</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/03/26/findbug-solving-dm_default_encoding-warning-when-using-filewriter/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">446</post-id>	</item>
		<item>
		<title>Suppressing FindBugs Warnings</title>
		<link>https://myshittycode.com/2013/10/25/suppressing-findbugs-warnings/</link>
					<comments>https://myshittycode.com/2013/10/25/suppressing-findbugs-warnings/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 25 Oct 2013 14:31:53 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[FindBugs]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Static Code Analysis]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=184</guid>

					<description><![CDATA[<p>PROBLEM FindBugs is one of the many great static code analysis tools that I use everyday. However, the generated report may usually contain a few false positives that forces me to weave through them whenever I rerun my build on Jenkins. For example, I&#8217;m using Google Guava to construct my equals(...) and hashCode():- FindBugs will [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/10/25/suppressing-findbugs-warnings/">Suppressing FindBugs Warnings</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>FindBugs is one of the many great static code analysis tools that I use everyday. However, the generated report may usually contain a few false positives that forces me to weave through them whenever I rerun my build on Jenkins.</p>



<p>For example, I&#8217;m using <a href="http://code.google.com/p/guava-libraries/wiki/CommonObjectUtilitiesExplained" target="_blank" rel="noopener">Google Guava</a> to construct my <code>equals(...)</code> and <code>hashCode()</code>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public class Person {
    private String firstName;
    private String lastName;
    private Long age;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person other = (Person) o;

        return Objects.equal(firstName, other.firstName) &amp;amp;&amp;amp;
               Objects.equal(lastName, other.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(firstName, lastName);
    }

    // getters and setters
}
</pre></div>


<p>FindBugs will produce a <code>EQ_UNUSUAL</code> warning with the following description:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
myproject.Person.equals(Object) is unusual&amp;lt;/code&gt;
&amp;lt;code&gt;
&amp;lt;/code&gt;
&amp;lt;code&gt;This class doesn&#039;t do any of the patterns we recognize for checking
that the type of the argument is compatible with the type of the this
object. There might not be anything wrong with this code, but it is
worth reviewing.
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p>There are 2 known ways that I know to suppress these warnings. One way is to create FindBugs filter files, which I find very tedious. The other way is to use FindBug&#8217;s annotations to do so, which is what I&#8217;m going to show here.</p>



<p>First, we need to include the neccessary dependency:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupId&gt;net.sourceforge.findbugs&lt;/groupId&gt;
    &lt;artifactId&gt;annotations&lt;/artifactId&gt;
    &lt;version&gt;1.3.2&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p>Next, we annotation <code>equals(...)</code> to suppress that specific warning:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [6,7]; title: ; notranslate">
public class Person {
    private String firstName;
    private String lastName;
    private Long age;

    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = &quot;EQ_UNUSUAL&quot;,
                                                      justification=&quot;Implemented using Google Guava&quot;)
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person other = (Person) o;

        return Objects.equal(firstName, other.firstName) &amp;amp;&amp;amp;
               Objects.equal(lastName, other.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(firstName, lastName);
    }

    // getters and setters
}
</pre></div>


<p>That&#8217;s it&#8230; it&#8217;s that simple.</p>



<p>Yes, I know there&#8217;s this ugly FindBugs dependency in my code. However, I&#8217;ll take that any day so that I&#8217;m getting a cleaner report from FindBugs. I can also be absolutely sure that I have reviewed the generated warnings and decided that they are safe to be ignored.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/10/25/suppressing-findbugs-warnings/">Suppressing FindBugs Warnings</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/10/25/suppressing-findbugs-warnings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">184</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-18 18:38:57 by W3 Total Cache
-->