Skip to content
This repository has been archived by the owner on Feb 20, 2019. It is now read-only.

Pickling Java classes yields empty objects #60

Closed
andrewwilson opened this issue Oct 21, 2013 · 2 comments
Closed

Pickling Java classes yields empty objects #60

andrewwilson opened this issue Oct 21, 2013 · 2 comments

Comments

@andrewwilson
Copy link

Hi,
It seems that java classes aren't currently well handled, when attempting to either pickle directly, or say embedded in fields of pure scala classes.
The examples below compile successfully, and generate pickled representations.

However in each case:
a) unpickling to the specific type, successfully returns an object, however it's incorrectly initialised. (i.e. it's fields don't match those of the original object)

b) unpickling to [Any] fails at runtime.

This is rather unfortunate, since particularly case (a) could lead to hard to detect errors.
It would be great if you could take a look.

package picklingtests
import scala.pickling._
import scala.pickling.json._

import org.joda.time.LocalDate

import org.junit._
import org.junit.Assert._

case class X(date: org.joda.time.LocalDate)

class PickleTest {

  @Test def testPickleCaseClassJavaField_Specific = {
    val dt = LocalDate.now().plusDays(3)
    val x = X(dt)
    val pickled = x.pickle
    println(pickled)
    val restored = pickled.unpickle[X]

    assertEquals(x, restored)
  }

    @Test def testPickleCaseClassJavaField_Any = {
    val dt = LocalDate.now().plusDays(3)
    val x = X(dt)
    val pickled = x.pickle
    println(pickled)
    val restored = pickled.unpickle[Any]

    assertEquals(x, restored)
  }


  @Test def testPickleJavaClass_Any = {
    val x = LocalDate.now().plusDays(3)
    val pickled = x.pickle
    println(pickled)
    val restored = pickled.unpickle[Any]

    assertEquals(x, restored)
  }

  @Test def testPickleJavaClass_Specific = {
    val x = LocalDate.now().plusDays(3)
    val pickled = x.pickle
    println(pickled)
    val restored = pickled.unpickle[LocalDate]

    assertEquals(x, restored)
  }

  @Test def testPickleJavaClass2_Any = {
    val x = new java.awt.Rectangle(1, 2, 3, 4)
    val pickled = x.pickle
    println(pickled)
    val restored = pickled.unpickle[Any]

    assertEquals(x, restored)
  }

  @Test def testPickleJavaClass2_Specific = {
    val x = new java.awt.Rectangle(1, 2, 3, 4)
    val pickled = x.pickle
    println(pickled)
    val restored = pickled.unpickle[java.awt.Rectangle]

    assertEquals(x, restored)
  }

}

Output:

JSONPickle({
  "tpe": "picklingtests.X",
  "date": {

  }
})
JSONPickle({
  "tpe": "picklingtests.X",
  "date": {

  }
})
JSONPickle({
  "tpe": "java.awt.Rectangle"
})
JSONPickle({
  "tpe": "org.joda.time.LocalDate"
})
JSONPickle({
  "tpe": "org.joda.time.LocalDate"
})
JSONPickle({
  "tpe": "java.awt.Rectangle"
})

picklingtests.PickleTest

testPickleCaseClassJavaField_Specific(picklingtests.PickleTest)
java.lang.AssertionError: expected:<X(2013-10-24)> but was:<X(2013-10-21)>


testPickleCaseClassJavaField_Any(picklingtests.PickleTest)
java.util.NoSuchElementException: key not found: x$1
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:235)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:159)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2$$anonfun$fieldVals$1$1.apply(Runtime.scala:174)



testPickleJavaClass2_Specific(picklingtests.PickleTest)
java.lang.AssertionError: expected:<java.awt.Rectangle[x=1,y=2,width=3,height=4]> but was:<java.awt.Rectangle[x=0,y=0,width=0,height=0]>


testPickleJavaClass_Any(picklingtests.PickleTest)
java.util.NoSuchElementException: key not found: x$1
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:235)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:159)

testPickleJavaClass_Specific(picklingtests.PickleTest)
java.lang.AssertionError: expected:<2013-10-24> but was:<2013-10-21>


testPickleJavaClass2_Any(picklingtests.PickleTest)
java.util.NoSuchElementException: key not found: x$1
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:58)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:58)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:235)
    at scala.pickling.json.JSONPickleReader.readField(JSONPickleFormat.scala:159)
    at scala.pickling.InterpretedUnpicklerRuntime$$anon$2$$anonfun$fieldVals$1$1.apply(Runtime.scala:174)



@debop
Copy link

debop commented Jul 23, 2014

I have same problem.

@zen0wu
Copy link

zen0wu commented Aug 12, 2014

Same here, unpickle a pickled java object yields an empty object.

@eed3si9n eed3si9n added the bug label Feb 8, 2015
@eed3si9n eed3si9n changed the title Pickling java classes Pickling java classes yields empty objects Feb 8, 2015
@eed3si9n eed3si9n changed the title Pickling java classes yields empty objects Pickling Java classes yields empty objects Feb 8, 2015
eed3si9n added a commit that referenced this issue Feb 9, 2015
Current implementation uses reflectivelyWithoutGetter to try accessing
primary constructor params by name, and is allowed to fail silently.
This ends up generating empty objects for Java classes like
java.lang.Byte.

- This does not attempt reflection unless we have a known param.
- When the list of fields are non-empty, but the pickler generates zero
fields, it will raise exception to fail the macro.
jsuereth added a commit that referenced this issue Feb 25, 2015
(wip) stop compiling when pickler fields are empty. Fixes #60, #263
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants