Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor boxable value to conditional introduces unexpected side effect #446

Closed
hjohn opened this issue Mar 1, 2023 · 0 comments · Fixed by #447
Closed

Refactor boxable value to conditional introduces unexpected side effect #446

hjohn opened this issue Mar 1, 2023 · 0 comments · Fixed by #447
Assignees
Labels
bug Something isn't working

Comments

@hjohn
Copy link

hjohn commented Mar 1, 2023

Given the following program:

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.

@hjohn hjohn changed the title Refactor to conditional introduces unexpected side effect Refactor boxable value to conditional introduces unexpected side effect Mar 1, 2023
@jjohnstn jjohnstn transferred this issue from eclipse-jdt/eclipse.jdt.core Mar 1, 2023
@jjohnstn jjohnstn self-assigned this Mar 1, 2023
@jjohnstn jjohnstn added the bug Something isn't working label Mar 1, 2023
jjohnstn added a commit to jjohnstn/eclipse.jdt.ui-1 that referenced this issue Mar 1, 2023
- don't convert if boxing is involved but types of if/else are
  different
- fixes eclipse-jdt#446
github-actions bot pushed a commit to jjohnstn/eclipse.jdt.ui-1 that referenced this issue Mar 9, 2023
- don't convert if boxing is involved but types of if/else are
  different
- fixes eclipse-jdt#446
github-actions bot pushed a commit to jjohnstn/eclipse.jdt.ui-1 that referenced this issue Mar 9, 2023
- don't convert if boxing is involved but types of if/else are
  different
- fixes eclipse-jdt#446
jjohnstn added a commit that referenced this issue Mar 10, 2023
- don't convert if boxing is involved but types of if/else are
  different
- fixes #446
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants