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

Commit

Permalink
Merge pull request #295 from scala/topic/java-fields
Browse files Browse the repository at this point in the history
(wip) stop compiling when pickler fields are empty. Fixes #60, #263
  • Loading branch information
jsuereth committed Feb 25, 2015
2 parents 4562538 + d0d6c91 commit d668829
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lazy val root: Project = (project in file(".")).

/** Scala Pickling code */
lazy val core: Project = (project in file("core")).
dependsOn(testUtil % "test->test").
settings(commonSettings: _*).
settings(
name := "scala-pickling",
Expand All @@ -73,6 +74,9 @@ lazy val core: Project = (project in file("core")).
}
)

lazy val testUtil: Project = (project in file("test-util")).
settings(commonSettings ++ noPublish: _*)

lazy val sandbox: Project = (project in file("sandbox")).
dependsOn(core).
settings(commonSettings ++ noPublish: _*).
Expand Down
13 changes: 11 additions & 2 deletions core/src/main/scala/pickling/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import ir._
trait TypeAnalysis extends Macro {
import c.universe._

def isStaticOnly: Boolean =
c.inferImplicitValue(typeOf[IsStaticOnly]) != EmptyTree

def isCaseClass(sym: TypeSymbol): Boolean =
sym.isClass && sym.asClass.isCaseClass

Expand Down Expand Up @@ -188,7 +191,8 @@ trait PicklerMacros extends Macro with PickleMacros with FastTypeTagMacros {
scala.pickling.PickleOps(out).pickleInto(b)
)
""")
} else (nonLoopyFields ++ loopyFields).flatMap(fir => {
}
else (nonLoopyFields ++ loopyFields).flatMap(fir => {
// for each field, compute a tree for pickling it
// (or empty list, if impossible)

Expand Down Expand Up @@ -225,11 +229,16 @@ trait PicklerMacros extends Macro with PickleMacros with FastTypeTagMacros {
else reflectively("picklee", fir)(fm => putField(q"$fm.get.asInstanceOf[${fir.tpe}]"))
} else if (fir.javaSetter.isDefined) {
List(putField(getField(fir)))
} else if (!fir.isParam && sym.isJava) {
Nil
} else {
reflectivelyWithoutGetter("picklee", fir)(fvalue =>
tryPutField(q"$fvalue.asInstanceOf[scala.util.Try[${fir.tpe}]]"))
}
})
if (cir.fields.nonEmpty && putFields.isEmpty) {
throw PicklingException("No fields are captured. You need a custom pickler to handle this.")
}
val endEntry = q"builder.endEntry()"
if (shouldBotherAboutSharing(tpe)) {
q"""
Expand Down Expand Up @@ -298,7 +307,7 @@ trait PicklerMacros extends Macro with PickleMacros with FastTypeTagMacros {

case tpe1 if sym.isClass =>
def pickleAfterDispatch(excludeSelf: Boolean) = {
val dispatchTree = if (c.inferImplicitValue(typeOf[IsStaticOnly]) != EmptyTree) { // StaticOnly *imported*
val dispatchTree = if (isStaticOnly) { // StaticOnly *imported*
val notClosedReasons = whyNotClosed(sym.asType)
if (notClosedReasons.nonEmpty)
c.abort(c.enclosingPosition, s"cannot generate fully static pickler because: ${notClosedReasons.mkString(", ")}")
Expand Down
9 changes: 6 additions & 3 deletions core/src/test/scala/pickling/NegativeCompilation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ object NegativeCompilation {
}

def toolboxClasspath = {
val f = new java.io.File(s"core/target/scala-${scalaBinaryVersion}/classes")
if (!f.exists) sys.error(s"output directory ${f.getAbsolutePath} does not exist.")
f.getAbsolutePath
val f0 = new java.io.File(s"core/target/scala-${scalaBinaryVersion}/classes")
val f1 = new java.io.File(s"test-util/target/scala-${scalaBinaryVersion}/test-classes")
val fs = Vector(f0, f1)
fs foreach { f => if (!f.exists) sys.error(s"output directory ${f.getAbsolutePath} does not exist.") }
val sep = sys.props("file.separator")
fs.map(_.getAbsolutePath).mkString(sep)
}

def quasiquotesJar: String = {
Expand Down
20 changes: 20 additions & 0 deletions core/src/test/scala/pickling/neg/java-field-fail.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package scala.pickling.javafieldfail

import scala.pickling._
import NegativeCompilation._
import org.scalatest.FunSuite

class JavaFieldFailTest extends FunSuite {
test("x.pickle does not compile for FakeByte") {
expectError("Cannot generate") {
"""import _root_.scala.pickling._
|import _root_.scala.pickling.Defaults._
|import _root_.scala.pickling.json._
|import _root_.scala.pickling.static._
|import scala.pickling.javafieldfail.FakeByte
|
|val x: FakeByte = new FakeByte(10)
|val pkl = x.pickle""".stripMargin
}
}
}
14 changes: 14 additions & 0 deletions test-util/src/test/java/pickling/FakeByte.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scala.pickling.javafieldfail;

public final class FakeByte {
private static final long serialVersionUID = 1L;
private final byte value;

public FakeByte(byte value) {
this.value = value;
}

public byte byteValue() {
return value;
}
}

0 comments on commit d668829

Please sign in to comment.