The FindBugs Blog

Tuesday, May 01, 2007

Round and Round we go; Netbeans WTF

I found the following code in a recent snapshot of netbeans. If anyone has a clue as to what the developer was thinking, let me know. The code is in org.openide.awt.HtmlLabelUI:


private static int textWidth(String text, Graphics g, Font f, boolean html) {
if (text != null) {
if (html) {
return Math.round(
Math.round(
Math.ceil(
HtmlRenderer.renderHTML(
text, g, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, f, Color.BLACK,
HtmlRenderer.STYLE_CLIP, false
)
)
)
);
} else {
return Math.round(
Math.round(
Math.ceil(
HtmlRenderer.renderPlainString(
text, g, 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE, f, Color.BLACK,
HtmlRenderer.STYLE_CLIP, false
)
)
)
);
}
} else {
return 0;
}
}

Labels:

7 Comments:

At 11:42 AM, Blogger shot said...

Franky speaking I do not understand it from the very beginning: I have found the method in google:

public static double renderPlainString

Render a string to a graphics instance, using the same API as renderHTML... Returns the width in pixels successfully painted.

Why the successful pixels amount is a double?

 
At 6:46 PM, Blogger Nico said...

Maybe the programmer is just a huge fan of Math.round() ^^

I can imagine how this one developed, he just used
Math.ceil() first, but then:

possible loss of precision
found : double
required: int


ok, so he was looking for a method to convert it to int, he had the javadoc for the class Math still open (he just looked up Math.ceil()) and found the method
static long round(double a)
"That's gonna work - long is also an integer right?"
Math.round(Math.ceil())
of course...

possible loss of precision
found : long
required: int


it didn't. But there is another round() method:
static int round(float a)
And being the clever programmer he obviously is ^^, he knew Java would convert his long to float. So, using round a second time, the result would now be int!
"Hooray!"

 
At 3:49 AM, Blogger Abiya Carol said...

I just see the post i am so happy the post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be subscribing to your feed and I hope you post again soon.

Branding Services in Chennai

 
At 1:49 PM, Blogger Philips Huges said...



Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much

Personal Installment Loans
Title Car loan
Cash Advance Loan

 
At 2:17 AM, Blogger CIIT Noida said...

Really something Grate in this article Thanks for sharing this. We are providing Online Training Classes. After reading this slightly I am changed my way of introduction about my training to people.

Best Linux training in Noida
Linux Training Institute in Noida

 
At 2:19 AM, Blogger CIIT Noida said...

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.

Best bca college in noida
Top bca colleges in noida

 
At 1:23 AM, Blogger cyber news said...

Thanks for such important information.keep up the good work.Ethical Hacking training is based on current industry standards that helps attendees to secure placements in their dream jobs at MNCs. Indian Cyber Army Provides Best Ethical Hacking Training in India.Indian Cyber Army credibility in Ethical hacking training & Cybercrime investigation training is acknowledged across nation as we offer hands on practical knowledge and full assistance with basic as well as advanced level ethical hacking & cybercrime investigation courses

 

Post a Comment

<< Home