History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEA-12818
Type: Bug Bug
Status: Open Open
Assignee: Alexey Kudravtsev
Reporter: Surkhab Niazi
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
IDEA: Feedback

"Split into 2 if's" generates incorrect code

Created: 19 May 07 05:22   Updated: 01 Jun 07 18:35
Component/s: Editor. Intention Actions

File Attachments: 1. Java Source File Test.java (3 kb)

Environment: Windows XP Professional

Build: 6,827
Severity: High


 Description  « Hide
I saw some incorrect code get generated when using the "Split into 2 if's" intention.

Test.java (below) is an artificial example of this, where NullnessComparator represents the a simple case of this problem. NullnessComparatorSplit is what the intention action results in when invoked on (x == null && y != null) and is incorrect. (See output of main for simple confirmation of this)

NullnessComparatorCorrectSplit is what I think the correct code should be after the split, and NullnessComparatorAlt in an equivalent case to NullnessComparator that is processed correctly by the Intention Action , resulting in NullnessComparatorAltSplit

Test.java
import java.util.Comparator;
import static java.text.MessageFormat.format;

/**
 * Created by IntelliJ IDEA.
 * User: niazis
 * Date: Jan 29, 2007
 * Time: 3:31:17 PM
 */
public class Test
{
  public static void main(String[] args)
  {

    Object o = new Object();

    testComparatorx(new NullnessComparator<Object>(),o);
    testComparatorx(new NullnessComparatorSplit<Object>(),o);
    testComparatorx(new NullnessComparatorCorrectSplit<Object>(), o);
    testComparatorx(new NullnessComparatorAlt<Object>(),o);
    testComparatorx(new NullnessComparatorAltSplit<Object>(),o);


  }

  private static <X> void testComparatorx(Comparator<X> comparator, X o)
  {
    System.out.println(format("{0}\t{1}\t{2}\t{3}\t[{4}]",
                              comparator.compare(null, o),
                              comparator.compare(null, null),
                              comparator.compare(o, o),
                              comparator.compare(o, null),
                              comparator.getClass().getSimpleName()
    )
    );
  }

  private static class NullnessComparator<T> implements Comparator<T>
  {
    public int compare(Object x, Object y)
    {
      if (x == null && y != null)
        return -1;
      else if (x != null && y == null)
        return 1;
      return 0;
    }
  }
  private static class NullnessComparatorSplit<T> implements Comparator<T>
  {
    public int compare(Object x, Object y)
    {
      if (x == null)
        if (y != null)
          return -1;
        else if (x != null && y == null)
          return 1;
      else if (x != null && y == null)
        return 1;
      return 0;
    }
  }
  private static class NullnessComparatorCorrectSplit<T> implements Comparator<T>
  {
    public int compare(Object x, Object y)
    {
      if (x == null)
      {
        if (y != null)
          return -1;
        else if (x != null && y == null)
          return 1;
      }
      else if (x != null && y == null)
        return 1;
      return 0;
    }
  }
  private static class NullnessComparatorAlt<T> implements Comparator<T>
  {
    public int compare(Object x, Object y)
    {
      if (x == null && y != null)
        return -1;
      else if (x != null && y == null)
        return 1;
      else
        return 0;
    }
  }

  private static class NullnessComparatorAltSplit<T> implements Comparator<T>
  {
    public int compare(Object x, Object y)
    {
      if (x == null)
        if (y != null)
          return -1;
        else if (x != null && y == null)
          return 1;
        else
          return 0;
      else if (x != null && y == null)
        return 1;
      else
        return 0;
    }
  }
}

Found in 6827, also confirmed existed in 6180



 All   Comments   Work Log   Change History      Sort Order:
Surkhab Niazi - 19 May 07 05:25
Don't ask me why I would want to split that particular condition this is just a simplified example..

Bas Leijdekkers - 28 May 07 20:38
"Split If" is an internal IDEA intention, is it not?