timowest / scalagen

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

Conversion drops a constructor #53

Open dnadolny opened 11 years ago

dnadolny commented 11 years ago

Starting from the Java:

class A {
    public A(String str) {}
}
class B extends A {
    public B() {
        super("some value");
    }
    public B(String str) {
        super(str);
    }
}

Converting the class B gives:

  class B extends A {
    def this(str: String) {
      super(str)
    }
  }

The default constructor that called super("some value") is silently dropped.

dnadolny commented 11 years ago

Another case of a dropped constructor:

public class A extends B<String, Map<String, Double>> {
   private static final long serialVersionUID = 1L;

   public A() {
      super(new C<Map<String, Double>>() {
         public D<String, Double> z() {
            return new E<String, Double>(0.0);
         }
      });
   }
}

becomes:

@SerialVersionUID(1L)
class A extends B[String, Map[String, Double]]
timowest commented 11 years ago

The constructor usage optimization is not yet perfect, the sources are here https://github.com/mysema/scalagen/blob/master/scalagen/src/main/scala/com/mysema/scalagen/Constructors.scala

I don't think there is a valid way to convert the first example to Scala, but the second example should work

dnadolny commented 11 years ago

Technically the first one could be (class B(str: String) extends A(str) { def this() = this("some value") }), although to do that would require knowing that the literal "some value" is a String, and that the B(String str) constructor doesn't do anything not done by B() (which, in this case, is just nothing). It's probably not worth it to attempt to fix it, but how about putting a comment /* Unconverted constructor: (original Java here, or at least the signature) */, or something to that effect?