Seyed H. HAERI (Hossein | 17 Jun 2012 20:17
Picon
Gravatar

How to acquire the package of an object? How to acquire the exact runtime type of an object?

Dear all,

Consider the following configuration

package base {
  class B
}

package p1 {
  class D1 extends base.B
  def f(d: D1, i: Int) = i
}
package p2 {
  class D2 extends base.B
  def f(d: D2, i: Int) = i * 2
}

along with the following client package

package p3 {
  def g(b: B, i: Int): Int = b match {case _: D1 => p1.f(b.asInstanceOf[D1], i); case _: D2 => p2.f(b.asInstanceOf[D2], i)}
}

Is there anyway I can avoid the pattern matching on b? If I can acquire the package b is coming from, and, if I can acquire its exact runtime type, I seem to be able to do something like

def g(b: B, i: Int): Int = package(b).f(b.asInstanceOf[b.runtimeType], i)

I seem to think that reflections (perhaps with some help from ClassManifest) should make this possible. Any idea?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Naftoli Gugenheim | 18 Jun 2012 04:39
Picon
Gravatar

Re: How to acquire the package of an object? How to acquire the exact runtime type of an object?

whatever.getClass gives you a java.lang.Class, which has all the runtime information you need (see its Javadoc).


On Sun, Jun 17, 2012 at 2:17 PM, Seyed H. HAERI (Hossein) <hossein.haeri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Dear all,

Consider the following configuration

package base {
  class B
}

package p1 {
  class D1 extends base.B
  def f(d: D1, i: Int) = i
}
package p2 {
  class D2 extends base.B
  def f(d: D2, i: Int) = i * 2
}

along with the following client package

package p3 {
  def g(b: B, i: Int): Int = b match {case _: D1 => p1.f(b.asInstanceOf[D1], i); case _: D2 => p2.f(b.asInstanceOf[D2], i)}
}

Is there anyway I can avoid the pattern matching on b? If I can acquire the package b is coming from, and, if I can acquire its exact runtime type, I seem to be able to do something like

def g(b: B, i: Int): Int = package(b).f(b.asInstanceOf[b.runtimeType], i)

I seem to think that reflections (perhaps with some help from ClassManifest) should make this possible. Any idea?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------

Antoras | 18 Jun 2012 17:05
Picon
Gravatar

Re: How to acquire the package of an object? How to acquire the exact runtime type of an object?

By the way, you don't need the cast in this code:

def g(b: B, i: Int): Int = b match {case d: D1 => p1.f(d, i); case d: D2 => p2.f(d, i)}


On Sunday, June 17, 2012 8:17:33 PM UTC+2, Hossein wrote:

Dear all,

Consider the following configuration

package base {
  class B
}

package p1 {
  class D1 extends base.B
  def f(d: D1, i: Int) = i
}
package p2 {
  class D2 extends base.B
  def f(d: D2, i: Int) = i * 2
}

along with the following client package

package p3 {
  def g(b: B, i: Int): Int = b match {case _: D1 => p1.f(b.asInstanceOf[D1], i); case _: D2 => p2.f(b.asInstanceOf[D2], i)}
}

Is there anyway I can avoid the pattern matching on b? If I can acquire the package b is coming from, and, if I can acquire its exact runtime type, I seem to be able to do something like

def g(b: B, i: Int): Int = package(b).f(b.asInstanceOf[b.runtimeType], i)

I seem to think that reflections (perhaps with some help from ClassManifest) should make this possible. Any idea?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------
Som Snytt | 19 Jun 2012 06:09
Picon

Re: How to acquire the package of an object? How to acquire the exact runtime type of an object?

My first foray into the reflect api.

import reflect.api._
import reflect.runtime.universe

// look up a function in a package object
package pkgf {
  package base {
    class B
  }
  import base.B
  package p1 {
    class D1 extends B
  }
  package object p1 {
    def f(d: D1, i: Int) = i
  }
  package p2 {
    class D2 extends B
  }
  package object p2 {
    def f(d: D2, i: Int) = i * 2
  }
  object Test {
    def g(b: B, i: Int): Int = b match {
      case x: p1.D1 => p1.f(x, i)
      case x: p2.D2 => p2.f(x, i)
    }
    def g2(b: B, i: Int): Int = {
      val u: JavaUniverse = universe
      import u._
      val jm: JavaMirror = u.runtimeMirror(b.getClass.getClassLoader)
      val im: InstanceMirror = jm.reflect(b)
      val sym: ClassSymbol = im.symbol
      val psym: Symbol = sym.enclosingPackageClass
      val pos = psym.typeSignature.declaration(nme.PACKAGE).asModuleSymbol
      val po: InstanceMirror = jm.reflect(jm.reflectModule(pos).instance)
      val f = psym.typeSignature.declaration(newTermName("f")).asMethodSymbol
      val mm = po reflectMethod f
      mm(b, i).asInstanceOf[Int]
    }
    def main(args: Array[String]) {
      val b1 = new p1.D1
      val b2 = new p2.D2
      println(g(b1, 7)); println(g(b2, 7))
      println(g2(b1, 7)); println(g2(b2, 7))
    }
  }
}




On Sun, Jun 17, 2012 at 11:17 AM, Seyed H. HAERI (Hossein) <hossein.haeri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Dear all,

Consider the following configuration

package base {
  class B
}

package p1 {
  class D1 extends base.B
  def f(d: D1, i: Int) = i
}
package p2 {
  class D2 extends base.B
  def f(d: D2, i: Int) = i * 2
}

along with the following client package

package p3 {
  def g(b: B, i: Int): Int = b match {case _: D1 => p1.f(b.asInstanceOf[D1], i); case _: D2 => p2.f(b.asInstanceOf[D2], i)}
}

Is there anyway I can avoid the pattern matching on b? If I can acquire the package b is coming from, and, if I can acquire its exact runtime type, I seem to be able to do something like

def g(b: B, i: Int): Int = package(b).f(b.asInstanceOf[b.runtimeType], i)

I seem to think that reflections (perhaps with some help from ClassManifest) should make this possible. Any idea?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein | 19 Jun 2012 11:26
Picon
Gravatar

Re: How to acquire the package of an object? How to acquire the exact runtime type of an object?

OK, thank you guys. I'll revisit the matter on the list again if I happen to have more specific questions.

On 19 June 2012 06:09, Som Snytt <som.snytt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
My first foray into the reflect api.

import reflect.api._
import reflect.runtime.universe

// look up a function in a package object
package pkgf {
  package base {
    class B
  }
  import base.B
  package p1 {
    class D1 extends B
  }
  package object p1 {

    def f(d: D1, i: Int) = i
  }
  package p2 {
    class D2 extends B
  }
  package object p2 {

    def f(d: D2, i: Int) = i * 2
  }
  object Test {

    def g(b: B, i: Int): Int = b match {
      case x: p1.D1 => p1.f(x, i)
      case x: p2.D2 => p2.f(x, i)
    }
    def g2(b: B, i: Int): Int = {
      val u: JavaUniverse = universe
      import u._
      val jm: JavaMirror = u.runtimeMirror(b.getClass.getClassLoader)
      val im: InstanceMirror = jm.reflect(b)
      val sym: ClassSymbol = im.symbol
      val psym: Symbol = sym.enclosingPackageClass
      val pos = psym.typeSignature.declaration(nme.PACKAGE).asModuleSymbol
      val po: InstanceMirror = jm.reflect(jm.reflectModule(pos).instance)
      val f = psym.typeSignature.declaration(newTermName("f")).asMethodSymbol
      val mm = po reflectMethod f
      mm(b, i).asInstanceOf[Int]
    }
    def main(args: Array[String]) {
      val b1 = new p1.D1
      val b2 = new p2.D2
      println(g(b1, 7)); println(g(b2, 7))
      println(g2(b1, 7)); println(g2(b2, 7))

    }
  }
}




On Sun, Jun 17, 2012 at 11:17 AM, Seyed H. HAERI (Hossein) <hossein.haeri-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Dear all,

Consider the following configuration

package base {
  class B
}

package p1 {
  class D1 extends base.B
  def f(d: D1, i: Int) = i
}
package p2 {
  class D2 extends base.B
  def f(d: D2, i: Int) = i * 2
}

along with the following client package

package p3 {
  def g(b: B, i: Int): Int = b match {case _: D1 => p1.f(b.asInstanceOf[D1], i); case _: D2 => p2.f(b.asInstanceOf[D2], i)}
}

Is there anyway I can avoid the pattern matching on b? If I can acquire the package b is coming from, and, if I can acquire its exact runtime type, I seem to be able to do something like

def g(b: B, i: Int): Int = package(b).f(b.asInstanceOf[b.runtimeType], i)

I seem to think that reflections (perhaps with some help from ClassManifest) should make this possible. Any idea?

TIA,
--Hossein

--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------




--
--------------------------------------------------------------------------------------------------------------

Seyed H. HAERI (Hossein)

Research Assistant
Institute for Software Systems (STS)
Technical University of Hamburg (TUHH)
Hamburg, Germany

ACCU - Professionalism in programming - http://www.accu.org/
--------------------------------------------------------------------------------------------------------------

Gmane