Generate warnings for autoboxing use
Asked Answered
F

1

10

I would like to generate warnings for ALL autoboxing and unboxing.

Has anyone found an effective way?

Eclipse catches basic autoboxing errors: eg. Integer i = null; i++. But fails on anything complex, and isn't really what I'm after.

I've looked at PMD and Checkstyle but couldn't find anything there.

I know it's a controversial subject, but I think it's a dangerous feature. There's no way to disable it, so I would at least like a report to check during code reviews.

I see this NullPointerException far too often in code reviews and bugs:

class X {
    Integer i = null;

    public int doSomething() {
        return i + 2;
    }
}

I want to stop it from getting that far.

Funiculate answered 22/2, 2013 at 9:16 Comment(3)
From your query I understand the following setting is already set in Eclipse? Am i Right? "Preferences->Java->Compiler->Errors/Warnings->Potential Programming Problems->Boxing and unboxing conversions"Youthful
Ah, that's perfect! Thanks. I missed that one. I only had the "Null pointer access" enabled. Please post this as an Answer :)Funiculate
It would still be nice to know how to check for autoboxing using static analysis tools. Then it could be integrated into the nightly build.Daylong
Y
15

Set this setting in your Eclipse,

Preferences -> Java -> Compiler -> Errors/Warnings -> Potential Programming Problems -> Boxing and unboxing conversions.

Youthful answered 22/2, 2013 at 9:52 Comment(1)
Hopefully they separate boxing from unboxing. Boxing I don't mind unboxing is more dangerous: bugs.eclipse.org/bugs/show_bug.cgi?id=163065Emblem

© 2022 - 2024 — McMap. All rights reserved.