简体   繁体   中英

Scala macros: generate code based on file contents

I would like to generate scala classes based on some files. Placement of the files doesn't matter (it can be resources folder or files can be placed just near my scala source files).

How can I read them when scala macro is executed? (I use StaticAnnotation's inline def apply(defn: Any): Any method)

When I try to use resources, they can't be found (I suppose those resources can't be found because it's compile time and project is not compiled yet) I would like to read files placed right near my scala sources, but how can I get source file path when macro is executed?

your project dir

val projectDir = System.getProperty("user.dir")

now you can access source file

val file = new File(s"$projectDir/src/resources/<SOURCES FILE>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM