4

For example, I saw a Scala expression like this:

objX.methodY

There seems no way to see where methodY came from, it may came from:

(1) the class of objX, let's call it ClassX

(2) the super class of objX (ClassX), let's call it SuperClassX

(3) a class called ClassZ, ClassZ is irrelevant to objX, but there is an implicit conversion from ClassX to ClassZ.

So there may be three possibilities for the source of methodY, does anyone have ideas about how to find out where the methodY is defined?

In other words, in Scala, how to inspect the information of a method (especially the method came from implicit conversion) at Runtime?

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
  • I don't know about scala; but in Java, you can think this way - the method is always "on" the runtime class of the object; if that class does not define the method, it "inherits" the definition of the method from the super class. – ZhongYu May 12 '15 at 05:13
  • @bayou.io Yes, and Scala introduced the `implicit conversion`, which makes the searching more difficult.. – Hanfei Sun May 12 '15 at 05:18
  • I suspect that implicit conversion is resolved at compile time, not runtime. - but again I don't know scala. – ZhongYu May 12 '15 at 05:19
  • The implicit is resolved at compile time, but hard to say from where it comes w/o trying to remove `import` one by one. That's why I would not use implicit conversion to pimp. – cchantep May 12 '15 at 07:51

2 Answers2

0

There is the possibility to get Class which has the method declared by reflection. With your namings:

objX.getClass().getMethod("methodY").getDeclaringClass();
Flown
  • 11,480
  • 3
  • 45
  • 62
  • You are using Java reflection, Java reflection doesn't know anything about Scala semantics. You'll probably have to use Scala reflection. – Jörg W Mittag May 12 '15 at 06:00
0

At runtime the code would be replaced with something like convertToY(objX).methodY, so at that point there's no connection to objX to inspect, it's just a function parameter. What are you trying to accomplish?

Daenyth
  • 35,856
  • 13
  • 85
  • 124