Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

reflection - Getting public fields (and their respective values) of an Instance in Scala/Java

PHP introduces a method that allows you to pick out all public values of an instance. Is there any way to do this in Scala? That is to fetch all values of all public fields of an instantiated class (not object).

Let's us assume I have this class

class TestElement( datatype: Datatype, var subject: String, var day: Int, var time: Int )
  extends DataElement( datatype: Datatype ) {    
   def to( group: Group ) = group.add( this );
}

var element = new TestElement( datatype, "subject", 1, 1 );

What I need from the method in question, is to get a Map or two Collections of values.

var element.method                                       // the function I need
ret: ( ("subject", "subject"), ("day", 1), ("time", 1) ) // its output
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's time for bed, so I don't have time for a full answer, but look at the results of element.getClass.getFields (or getDeclaredFields for private fields) - you can call getValue(element) on the Field objects to fetch their values.


Awake now, and still no better answer, so:

First, note that in Java terms, your class doesn't have a public field subject, what it has is a private field subject and accessor methods subject() and subject_$eq(String).

You can iterate over the private field objects as described above, populating a Map from the pairs:

def getFields(o: Any): Map[String, Any] = {
  val fieldsAsPairs = for (field <- o.getClass.getDeclaredFields) yield {
    field.setAccessible(true)
    (field.getName, field.get(o)) 
  }
  Map(fieldsAsPairs :_*)
}

Now you can either define this method on TestElement (replacing o with this), or more generally usefully define a conversion so that you can call getFields on any reference

implicit def any2FieldValues[A](o: A) = new AnyRef {
  def fieldValues = getFields(o)
}

So that

element.fieldValues 

will give the result you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...