문제

Because scala class name may not match the source file name.

How Can I find which .class file in the jar a scala type belongs to.

In scala.reflect.api.Symbols.Symbol type, there is a value called associatedFile, but it always returns null

scala> typeOf[List[_]].typeSymbol.associatedFile
res4: scala.reflect.io.AbstractFile = null

How can I get the class file name programmatically ?

More detailed : If I get the binary/source jar , for example scala-library.jar ,scala-library-src.jar. How Can I tell which source file in source jar comes from the class in binary jar.

Can Scala presentation compiler tell this info ?

도움이 되었습니까?

해결책

associatedFile returns the source file that the symbol is defined in - this is used in macros, runtime reflection doesn't have such information and that's why you are getting nulls.

If you are using Scala runtime reflection, you can get the corresponding java.lang.Class object:

import scala.reflect.runtime.universe._
val mirror = runtimeMirror(getClass.getClassLoader)
val clazz = mirror.runtimeClass(typeOf[List[_]])

Fully qualified class name should map to relative classfile path in rather straightforward way:

clazz.getName.replace('.', '/') + ".class"

but you should be aware that runtime classes aren't always loaded from classfiles, various class loaders may fetch them from many different sources in many ways (for example, generate them on the fly in runtime). Also, the exact location of the classfile is a ClassLoader's implementation detail that you can't know in general case.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top