Java interoperability: Prompt for scala.collection.mutable.<RelevantCollection> when using quickfix
case class JavaPerson(@BeanProperty var name:String,@BeanProperty var addresses:java.lang.Object)
case class ScalaPerson (@BeanProperty var name:String,@BeanProperty var addresses:Map[String,String])
import java.util.{Map=>JMap,HashMap=>JHashMap}
import scala.collection.JavaConverters._
//Mutable map not imported yet
//import scala.collection.mutable.Map
import scala.reflect.BeanProperty
object TestImport {
def main(args : Array[String]) : Unit = {
//note type here is java.lang.Object
val addresses:java.lang.Object = new JHashMap[String,String]()
val p1 = JavaPerson("Harry Potter",addresses)
//error here type mistmatch found scala.collection.mutable.Map[String,String] required scala.collection.immutable.Map[String,String]
//Quick fix should present option to import mutable collection since we know we are converting from Java.
val p2 = ScalaPerson(p1.name,p1.addresses.asInstanceOf[JMap[String,String]].asScala)
}
}
After manually import the mutable Map, this works fine.
Leave a comment
on 2011-05-24 14:52 *
By Mirko Stocker
Ok, I now understand what you're proposing. So the quickfix should effectly change your case class' member from a immutable map to a mutable one (In this particular case changing the import would be enough. But if you were using the immutable map as well, then you would need to change the case class).
Uh no - although I didn't consider this possibility.
My Scala case class are defined in their own files and thus have their own import of Scala mutable collections (I now realise that in the example, they look to be in the same file, sorry for the confusion). The JavaPerson example here is just to show what I'm obtaining or sending when integrating with Java libs.
So, I get JavaPerson object, one of its members is a map. Using asScala on that map returns a mutable scala map. Since, immutable is available by default, the compiler complains, hence the
"type mistmatch found scala.collection.mutable.Map[String,String] required scala.collection.immutable.Map[String,String]"
Importing the mutable map in TestImport fixes the problem without having to modify the case class.
What I'm proposing is this:
When converting a Java Collection to a Scala Collection and I try an import to fix the resulting error, I should be prompted for the scala mutable Collection.
My Scala case class are defined in their own files and thus have their own import of Scala mutable collections (I now realise that in the example, they look to be in the same file, sorry for the confusion). The JavaPerson example here is just to show what I'm obtaining or sending when integrating with Java libs.
So, I get JavaPerson object, one of its members is a map. Using asScala on that map returns a mutable scala map. Since, immutable is available by default, the compiler complains, hence the
"type mistmatch found scala.collection.mutable.Map[String,String] required scala.collection.immutable.Map[String,String]"
Importing the mutable map in TestImport fixes the problem without having to modify the case class.
What I'm proposing is this:
When converting a Java Collection to a Scala Collection and I try an import to fix the resulting error, I should be prompted for the scala mutable Collection.
on 2011-05-25 09:29 *
By Mirko Stocker
Sorry, I still don't fully understand your issue. So if I put the case classes into their own files and define the addresses parameter of ScalaPerson as a mutable Map, then everything is fine.
Let's make another example, if you have something like this, without an import of Map:
Then the compiler complains that "type mismatch; found : scala.collection.mutable.Map[String,String] required: scala.collection.immutable.Map[String,String]", and then a quick fix could fix this problem by importing the mutable map. Is this what you're looking for?
Let's make another example, if you have something like this, without an import of Map:
val myMap: Map[String, String] = p1.addresses.asInstanceOf[JMap[String, String]].asScala
Then the compiler complains that "type mismatch; found : scala.collection.mutable.Map[String,String] required: scala.collection.immutable.Map[String,String]", and then a quick fix could fix this problem by importing the mutable map. Is this what you're looking for?
on 2011-05-25 12:43 *
By Mirko Stocker
Ok, now I get it. I think this shouldn't be too hard to implement, if you want to give it a try :-)
on 2011-08-15 14:56 *
By Mirko Stocker
Version changed from 2.0.0-beta4 to 2.0.0-beta09-29
Eclipse version changed from Helios to All
on 2014-07-19 00:40 *
By Simon Schäfer
Component changed from Refactoring to Quick Fix
Version changed from 2.0.0-final-29 to 3.0.4-211
on 2014-07-19 00:41 *
By Simon Schäfer
Description changed from case class JavaPerson(@Bean... to case class JavaP...
on 2015-03-13 23:15 *
By Simon Schäfer
Eclipse version changed from All to Luna - Eclipse 4.4
Version changed from 3.0.4-211 to 4.0.0
Milestone changed from Enhancements to -none-