If you're like me, you spend a fair amount of time poking at objects of poorly documented classes in a REPL (Scala or otherwise...). This is great compared to writing whole test programs solely for the purpose of seeing what something does or what data it really contains, but it can be quite time consuming. So I've written a utility for use on the Scala REPL that will print out all of the "attributes" of an object. Here's some sample usage:
scala> import replutils._
import replutils._
scala> case class Test(a: CharSequence, b: Int)
defined class Test
scala> val t = Test("hello", 1)
t: Test = Test(hello,1)
scala> printAttrValues(t)
hashCode: int = -229308731
b: int = 1
a: CharSequence (String) = hello
productArity: int = 2
getClass: Class = class line0$object$$iw$$iw$Test
That looks fairly anti-climatic, but after spending hours typing objName.
Code and further usage instructions is available on BitBucket here.
So what exactly is an attribute?
I haven't quite figured that out yet. Right now it is any method that meets the following criteria:
- the method has zero arguments
- the method is public
- the method is not static
- the method's name is not on the exclude list (e.g. wait)
- the method's return type is not on the exclude list (e.g. Unit/void)
- the method is not a "default argument" method
0 comments:
Post a Comment