
|
If you were logged in you would be able to see more operations.
|
|
|
|
File Attachments:
|
1.
Test.java (3 kb)
|
|
Environment:
|
Windows XP Professional
|
|
| Build: |
6,827
|
| Severity: |
High
|
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
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
|
|
Description
|
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
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 |
Show » |
|