Can I use reflection from SBT?

I'm trying to create some kind of template with SBT (a tool that is completely insignificant for me). I use shapeless sbt files as the main link for the task. I saw that this project uses code generation from scratch, but my case is slightly different since I would like to generate some classes from others. I pretend to use the new Scala 2.10.0-M4 reflection capabilities for this. What basic configuration is needed to be reflected in the SBT assembly?

Currently, sbt cannot find the scala.reflect.runtime.universe package, and I do not know if this problem arose either from the new Scala jar section or from a bad configuration. In addition, my sbt says:

[info] This is sbt 0.13.0-20120530-052139 [info] The current project is {file:/home/jlg/sandbox/abc/}abc [info] The current project is built against Scala 2.10.0-SNAPSHOT [info] [info] sbt, sbt plugins, and build definitions are using Scala 2.9.2 

By the way, does anyone know of other projects using SBT to generate source code?

+7
source share
2 answers

Current SBT releases are based on Scala 2.9, and source code generation works with SBT with the same libraries. There are two options:

  • Be extremely bleeding: get an SBT release running on Scala 2.10 (not even branch 0.13), or waiting for it. The biggest problem is not only that you have to recompile SBT yourself, recompiling each individual SBT plugin that you will need for Scala 2.10. In the long run, this may be the best strategy to do what you ask, but at the moment it can be a lot. However, be careful that you cannot use reflection in the summary code without evil tricks, since code generation must occur before compilation. If you need to do this, try generating code at compile time in the program instead using macros. This excludes SBT and is much more standard, but I'm not sure that you can generate full classes in this version (this, I think, is planned for the future).
  • go with the old one: stick with Scala 2.9 and use the features of the scan (ScalaSigParser) to reflect in compile time. The problem is that the API is different (not sure how deep) and is not supported mainly for public use, although various people have been using it for ages. For the project in which I work, the colleague's approach was taken, and I integrated it into SBT for my project (https://github.com/ps-mr/LinqOnSteroids/); In addition, I use Scalate to write patterns to generate code, which is pretty effective. See, In particular, build.sbt , which calls project / Generator.scala and project / src / main / scala / ivm / generation / ScalaSigHelpers.scala (some not fully-generic wrappers for ScalaSigParser ). Pattern templates for the generated code are located in src / main / resources , the most relevant here is src / main / resources / WrappedClassInlined.ssp . Even more interesting, I'm afraid that you will need to order a check and play with it to see what it is does, but feel free to ask questions.

Please note that the code is protected by the BSD license, so you need to keep the original copyright if you copy the code.

Note: all links (with the exception of the license) refer to the current HEAD for stability, so they will not disappear so easily, even if the files are moved / deleted in future versions.

+3
source

If you use 2.10.0-SNAPSHOT, you need to go for scala.reflect.runtime.universe . See http://dcsobral.blogspot.ch/2012/07/json-serialization-with-reflection-in.html for more information.

+1
source

All Articles