timowest / scalagen

Java to Scala transformation
Apache License 2.0
216 stars 32 forks source link

Bad conversion - doesn't wrap args in parenthesis #60

Closed dnadolny closed 10 years ago

dnadolny commented 10 years ago

I have the Java code:

class TryMe {
  private void someMethod(String str) {}

  private void useIt() {
    int n = 5;
    someMethod(Integer.toString(n + 1).length() * 5);
  }
}

When converted, you get:

class TryMe {
  private def someMethod(str: String) {
  }

  private def useIt() {
    val n = 5
    someMethod(java.lang.Integer toString n + 1.length * 5)
  }
}

This is incorrect, and in this case gives a compiler error on 1.length (although you can't rely on having an error). The n + 1 needs to be wrapped in brackets since that is the argument to toString.

I believe the problem is in ScalaDumpVisitor.scala line 744. If the method is one in the SHORT_FORM list and it has less than 2 arguments, the resulting scala will use a space rather than a dot and parenthesis. I think the condition needs to be strengthened: if there is an argument, the code for that argument must not contain a space.

timowest commented 10 years ago

Released in 0.3.1