ToolBox, OSGi and classloading

已查看 24 次
跳至第一个未读帖子

Simon Schäfer

未读,
2014年7月7日 19:30:032014/7/7
收件人 scala-...@googlegroups.com
I have some very simple code that uses the toolbox to execute some code provided as string:

object Test extends App {

  import java.io.File
  import java.net.URLClassLoader

  import scala.reflect.runtime.universe.runtimeMirror
  import scala.tools.reflect.ToolBox

  val path = Seq(
    "/home/antoras/dev/scala/ToolboxTest/bin" // this path contains my project
  )
  val cl = new URLClassLoader(
      path.map(p => new File(p).toURI().toURL()).toArray,
      getClass.getClassLoader())

  val tb = runtimeMirror(cl).mkToolBox()
  import tb._, u._

  val tree = typecheck(parse("object M {import simple._; class X extends Interface}"))

  new Traverser {
    override def traverse(t: Tree) {
      t match {
        case ClassDef(_, _, _, Template(parents, _, _)) =>
          parents foreach {
            case tt: TypeTree =>
              Option(tt.original) foreach { o =>
                val sit = typeOf[simple.Interface]
                val sym = o.symbol
                println(sit.typeSymbol == sym)
                println(sym.asType.toType <:< sit)
              }
            case _ =>
          }
        case _ =>
      }
      super.traverse(t)
    }
  }.traverse(tree)
}
// and in another file
package simple

class Obj
trait Interface {
  val obj: Obj
}


This code works as expected, it prints two times `true`. The problem is that I tried to use this code inside of the IDE and therefore in an OSGi environment but there it prints two times `false`. Looking inside with the debugger shows that the symbols look identical but they aren't.

All types come from the ToolBox, whose classpath can access OSGi packages, therefore I'm not sure if this may be a problem about different classpaths. Any ideas how different classpaths could happen or what else makes my live hard here?

Eugene Burmako

未读,
2014年7月8日 04:59:262014/7/8
收件人 scala-...@googlegroups.com
What if you compare cl.loadClass("simple.Interface"), getClass.getClassLoader().loadClass("simple.Interface") and classOf[simple.Interface]? Which ones will be equal and which ones will not?


--
You received this message because you are subscribed to the Google Groups "Scala IDE Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-ide-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-ide-dev/53BB2D79.9060806%40antoras.de.
For more options, visit https://groups.google.com/d/optout.

Simon Schäfer

未读,
2014年7月8日 05:21:352014/7/8
收件人 scala-...@googlegroups.com
println((sit.getClass().getClassLoader(), sym.getClass().getClassLoader()))
val c1 = cl.loadClass("simple.Interface")
val c2 = getClass().getClassLoader().loadClass("simple.Interface")
val c3 = classOf[simple.Interface]
println((c1 == c2, c2 == c3))

The first println shows that the classloaders are the same in the OSGi environment and therefore the second one prints only true. Probably not even a classloader issue.

iulian dragos

未读,
2014年7月8日 05:23:062014/7/8
收件人 scala-ide-dev
On Tue, Jul 8, 2014 at 11:21 AM, Simon Schäfer <ma...@antoras.de> wrote:
println((sit.getClass().getClassLoader(), sym.getClass().getClassLoader()))
val c1 = cl.loadClass("simple.Interface")
val c2 = getClass().getClassLoader().loadClass("simple.Interface")
val c3 = classOf[simple.Interface]
println((c1 == c2, c2 == c3))

The first println shows that the classloaders are the same in the OSGi environment and therefore the second one prints only true. Probably not even a classloader issue.

I didn't get it :( What is the output?
 

For more options, visit https://groups.google.com/d/optout.



--
« Je déteste la montagne, ça cache le paysage »
Alphonse Allais

Simon Schäfer

未读,
2014年7月8日 05:32:352014/7/8
收件人 scala-...@googlegroups.com

On 07/08/2014 11:22 AM, iulian dragos wrote:



On Tue, Jul 8, 2014 at 11:21 AM, Simon Schäfer <ma...@antoras.de> wrote:
println((sit.getClass().getClassLoader(), sym.getClass().getClassLoader()))
val c1 = cl.loadClass("simple.Interface")
val c2 = getClass().getClassLoader().loadClass("simple.Interface")
val c3 = classOf[simple.Interface]
println((c1 == c2, c2 == c3))

The first println shows that the classloaders are the same in the OSGi environment and therefore the second one prints only true. Probably not even a classloader issue.

I didn't get it :( What is the output?

It prints

2014-07-08 11:30:52,407 DEBUG [main] - System.out - (org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@269d4d64[org.scala-lang.scala-reflect:2.11.2.v20140627-160437-fbfce33cb0(id=2018)],org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader@269d4d64[org.scala-lang.scala-reflect:2.11.2.v20140627-160437-fbfce33cb0(id=2018)])
2014-07-08 11:30:52,407 DEBUG [main] - System.out - (true,true)
回复全部
回复作者
转发
0 个新帖子