Bug 37643 - [quick assist] convert == to equals() and vice versa
Summary: [quick assist] convert == to equals() and vice versa
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Deepak Azad CLA
QA Contact:
URL:
Whiteboard:
Keywords: bugday
Depends on:
Blocks:
 
Reported: 2003-05-15 08:12 EDT by Adam Kiezun CLA
Modified: 2021-04-08 11:42 EDT (History)
3 users (show)

See Also:


Attachments
fix (6.62 KB, patch)
2004-01-12 04:57 EST, Sebastian Davids CLA
no flags Details | Diff
fix (6.62 KB, patch)
2004-01-12 05:00 EST, Sebastian Davids CLA
daniel_megert: review-
Details | Diff
tests (11.35 KB, patch)
2004-01-12 05:00 EST, Sebastian Davids CLA
daniel_megert: review-
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Kiezun CLA 2003-05-15 08:12:43 EDT
quick assist could help me change my == expressions to equals() ands vice versa
(this is only possible when both sides of == are reference types and left side 
is not the null literal, but that should be trivially checkable)
Comment 1 Sebastian Davids CLA 2004-01-12 04:57:36 EST
Created attachment 7404 [details]
fix

@@ implementation notes @@

<object A>.equals(<object B>)

offer only quick assist if <fully qualified name of A>.equals(<fully qualified
name)
Comment 2 Sebastian Davids CLA 2004-01-12 05:00:17 EST
Created attachment 7405 [details]
fix

forgot to remove debug string :/
Comment 3 Sebastian Davids CLA 2004-01-12 05:00:36 EST
Created attachment 7406 [details]
tests
Comment 4 Martin Aeschlimann CLA 2004-01-12 05:36:05 EST
Thanks, Sebastian!
Can you look at the tests again, as they collide with the tests you wrote for 
bug 37432.
You transform a.equals(b); to a == b; which is not a valid statement. I think 
it would be better to not return a quick assist in that case.
I try to keep the offered quick fixes as precice as possible to avoid big lists.

I think it would make sense to have all the equals tests together. The idea 
would be to somthing like the test I attached.
For the tests please look that you are not depended on the order of the 
proposals. 

public void testChangeEqualsToSame() throws Exception {
    IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, 
null);
    StringBuffer buf= new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        boolean b= \"a\".equals(\"b\");\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), 
false, null);
    
    String str= "equals";
    AssistContext context= getCorrectionContext(cu, buf.toString().indexOf
(str), 0);
    List proposals= collectAssists(context, FILTER_EQ);
    
    assertNumberOf("proposals", proposals.size(), 2);
    assertCorrectLabels(proposals);
    
    CUCorrectionProposal proposal= (CUCorrectionProposal) proposals.get(0);
    String preview1= proposal.getCompilationUnitChange().getPreviewContent();

    buf= new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        boolean b= \"a\" == \"b\";\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1= buf.toString();
    
    proposal= (CUCorrectionProposal) proposals.get(1);
    String preview2= proposal.getCompilationUnitChange().getPreviewContent();

    buf= new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        boolean b= \"b\".equals(\"a\");\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2= buf.toString();
    
assertEqualStringsIgnoreOrder(new String[] { preview1, preview2 }, new String[] 
{ expected1, expected2 });	
}
Comment 5 Martin Aeschlimann CLA 2004-01-12 05:54:22 EST
BTW, there's still a System.out in the code.
Some other remarks:
- 'resultingCollections' can be null. That means that this is only a requst if 
there is a quick assist, but no proposal should be created (yet). This is used 
to show the quick assist light bulb.
- Can you merge the code for bug 37432 and 'equals to =='? They have more or 
less the same prereq testing, so it would make sense to have single method. 
(But do as you prefer)
- Use 'ASTRewriteCorrectionProposal' instead of 'LinkedNamesAssistProposal' as 
your proposals don't enter the linked mode.
- right.resolveTypeBinding(); can return null
- you can compare bindings by idendity (if (binding1 == binding2)) {..
- you miss the case:
		int[] a = null, b = null;
		if (a.equals(b)) {}
(also applies to bug 37432)
-> I think you could change your code to
 if (binding.isNullType() || binding.isPrimitive()) {
   //overloaded equals w/ non-class/interface arg
   return false;
 }
Comment 6 Martin Aeschlimann CLA 2004-01-20 09:19:37 EST
sebastian? do you prefer I take over?
Comment 7 Sebastian Davids CLA 2004-01-21 04:47:08 EST
I have a bandwidth restriction of 1 GB download per month.

Unfortunately, I have already used it up.

I could continue work on this PR at the beginning of next month.

If you don't want to wait that long, please go ahead.

Sorry for the inconvenience :/