Bug 204352 - [quick fix] convert autoboxing to explicit conversion
Summary: [quick fix] convert autoboxing to explicit conversion
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3   Edit
Hardware: PC All
: P3 enhancement with 4 votes (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 278518 328991 (view as bug list)
Depends on:
Blocks: 128883
  Show dependency tree
 
Reported: 2007-09-22 07:24 EDT by Claudio Nieder CLA
Modified: 2014-02-06 12:58 EST (History)
10 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Claudio Nieder CLA 2007-09-22 07:24:14 EDT
Build ID: I20070621-1340

With

p(Integer i) { } { int j; p(j); }

I get an autoboxing warning at p(j) and the quick fix allows me to "add @SurpressWarning boxing'

It would be nice if quick fix would also offer "explicit conversion to Integer" which would change j into Integer.valueOf(j), i.e. in the above example after choosing that quick fix I would get

p(Integer i) { } { int j; p(Integer.valueOf(j)); }


Dependency on Bug #128883:

If you really implement the clean up action asked for in Bug #128883 then my request becomes void as it would handle this case too.
Comment 1 Benno Baumgartner CLA 2008-03-11 13:14:26 EDT
For compiler compliance <=1.4 this could be offered as a quick assist?
Comment 2 Markus Keller CLA 2008-03-11 13:44:28 EDT
> For compiler compliance <=1.4 this could be offered as a quick assist?

With Java <= 1.4, "p(Integer i) { } { int j; p(j); }" has a compile error, so you could only offer a quick *fix* to convert the call to "p(new Integer(j));"

A quick *assist* would be interesting for Java >= 1.5 with the autoboxing warning disabled.
Comment 3 Jean Hominal CLA 2009-05-30 13:14:21 EDT
*** Bug 278518 has been marked as a duplicate of this bug. ***
Comment 4 Dani Megert CLA 2010-10-29 02:20:14 EDT
*** Bug 328991 has been marked as a duplicate of this bug. ***
Comment 5 Scott Johnson CLA 2010-11-19 11:55:16 EST
Is there any way to increase the priority of this?  Just wondering, since it seems to have been around for a while, and I would think it's a pretty easy fix.

Thanks, 

~Scott
Comment 6 Dani Megert CLA 2010-11-22 06:13:11 EST
(In reply to comment #5)
> Is there any way to increase the priority of this?
The easiest way to get something is to provide a patch, especially since you think it's easy to do ;-)
Comment 7 Scott Johnson CLA 2010-11-22 10:26:05 EST
Fair enough.  I'll look at this.  However, given that I'm not experienced in eclipse's internal workings, my assessment could be incorrect that it's easy to do.  Did you have insight in this?
Comment 8 Dani Megert CLA 2010-11-22 10:34:15 EST
(In reply to comment #7)
> Fair enough.  I'll look at this.  However, given that I'm not experienced in
> eclipse's internal workings, my assessment could be incorrect that it's easy to
> do.  Did you have insight in this?
No, sorry. Maybe Markus can give some.
Comment 9 Markus Keller CLA 2010-11-24 10:34:07 EST
An entry point to see how a similar quick fix / clean up is implemented:

org.eclipse.jdt.internal.ui.text.correction.LocalCorrectionsSubProcessor.addUnnecessaryCastProposal(..).

The same implementation should also be used to implement the multi-fix (so that you can fix several problems of the same category at once) and the clean up (bug 128883). See also references to
org.eclipse.jdt.internal.corext.fix.CleanUpConstants.REMOVE_UNNECESSARY_CASTS to see how this is hooked up into the clean up infrastructure.

The new clean up should go to the Code Style tab.
Comment 10 Scott Johnson CLA 2010-12-15 23:38:51 EST
Ok, I think I have this working fairly well.  I want to polish it up a bit before I post a patch.  Specifically, I was wondering about strings.  I have a string "valueOf" that's defined in my quick fix operation, and I'm not sure if I should externalize it.  It seems like something that probably shouldn't be changed by a user without recompiling, so is it acceptable to leave it as a constant declaration, or should I place it in a preferences file somewhere?
Comment 11 Dani Megert CLA 2010-12-16 02:57:43 EST
(In reply to comment #10)
> I have a string "valueOf"
Can you give more details why/where you need it?


> It seems like something that probably shouldn't be
> changed by a user without recompiling, so is it acceptable to leave it as a
> constant declaration
Correct. Only translatable strings must be externalized.
Comment 12 Scott Johnson CLA 2010-12-16 11:54:40 EST
> Can you give more details why/where you need it?

Sure.  Perhaps I'm doing this a bit incorrectly.  I have the following in the AutoboxToExplicitConversionOperation.rewriteAST() method:

Expression expr = (Expression)(placeholder);
MethodInvocation me = fSelectedNode.getAST().newMethodInvocation();
SimpleName qualifier = fSelectedNode.getAST().newSimpleName(
                                                fWrapper.getSimpleName());
SimpleName name = fSelectedNode.getAST().newSimpleName("valueOf");

So, the last SimpleName call seems like it's kind of hard-coded.  I'm really just wondering if this is acceptable, or if there is a better way to do it.
Comment 13 Scott Johnson CLA 2010-12-17 11:41:50 EST
Also, I'm having a bit of trouble with the clean up section.  I have it wired up in the code style tab, and can choose the option to automatically convert.  When I hit 'Next', though, it doesn't actually register the IProblem.  The code I am using to test is:

  public static void main(String args[]) {
    Integer i = 32;
    
    System.out.println("Integer: " + i);
  }

What it finds for an IProblem when I run the CleanUp menu and select my new option and set a breakpoint in PotentialProgrammingProblemsCleanUp.createFix (I accidentally put it in here before I read what you said about putting it in the CodeStyle tab.  I'm trying to get it to work first, then I'll move it over to the CodeStyle tab) is simply the problem about the non-externalized strings.  

I feel like I haven't quite hooked it up correctly, as the compiler doesn't seem to be returning the desired problems to the CleanUp logic.  Can you give me some advice about how to get the compiler to return a selection of problems that I am desiring checking for?
Comment 14 Dani Megert CLA 2011-01-03 05:29:12 EST
(In reply to comment #12)
> > Can you give more details why/where you need it?
> 
> Sure.  Perhaps I'm doing this a bit incorrectly.  I have the following in the
> AutoboxToExplicitConversionOperation.rewriteAST() method:
> 
> Expression expr = (Expression)(placeholder);
> MethodInvocation me = fSelectedNode.getAST().newMethodInvocation();
> SimpleName qualifier = fSelectedNode.getAST().newSimpleName(
>                                                 fWrapper.getSimpleName());
> SimpleName name = fSelectedNode.getAST().newSimpleName("valueOf");
> 
> So, the last SimpleName call seems like it's kind of hard-coded.  I'm really
> just wondering if this is acceptable, or if there is a better way to do it.

Yes, that's OK. Just make sure it's marked with //$NON-NLS-1$
Comment 15 Markus Keller CLA 2011-01-03 12:18:10 EST
(In reply to comment #13)
> Can you give
> me some advice about how to get the compiler to return a selection of problems
> that I am desiring checking for?

Sounds like your implementation of AbstractCleanUp#getRequirements() doesn't include the compiler option. See e.g. how UnnecessaryCodeCleanUp#getRequirements() builds a map with the JavaCore.COMPILER_PB_* option.