You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
public static void main(String[] args) {
Object s = "Hi";
Number number;
if(s instanceof String) {
number = Long.parseLong("1");
}
else {
number = Double.parseDouble("1");
}
System.out.println(number);
}
This prints:
class java.lang.Long = 1
When applying the refactoring to conditional the program becomes:
public static void main(String[] args) {
Object s = "Hi";
Number number;
number = s instanceof String ? Long.parseLong("1") : Double.parseDouble("1");
System.out.println(number.getClass() + " = " + number);
}
This prints:
class java.lang.Double = 1.0
Expected
I would expect this refactoring to not be available when it changes the results.
Cause
The refactoring doesn't take into account that unboxed values can be implicitly cast (long to double in this case). The conditional s instanceof String ? 1 : 1.0 is always a double, and after boxing would always be Double instead of Long or Double.
The text was updated successfully, but these errors were encountered:
hjohn
changed the title
Refactor to conditional introduces unexpected side effect
Refactor boxable value to conditional introduces unexpected side effect
Mar 1, 2023
jjohnstn
transferred this issue from eclipse-jdt/eclipse.jdt.core
Mar 1, 2023
Given the following program:
This prints:
When applying the refactoring to conditional the program becomes:
This prints:
Expected
I would expect this refactoring to not be available when it changes the results.
Cause
The refactoring doesn't take into account that unboxed values can be implicitly cast (
longtodoublein this case). The conditionals instanceof String ? 1 : 1.0is always adouble, and after boxing would always beDoubleinstead ofLongorDouble.The text was updated successfully, but these errors were encountered: