Thursday 15 August 2013

reflection - How to recognize scala constructor parameter 'fields' with no underlying java field? -


I am playing with reflection in Skel I have chosen

  class CloneSubject (Val One: int, b: string, private, c: boolean)   

type a theme and find more iterative typeof [cloneSubject] .members fields ( sym.isTerm & amp; (sym.asTerm.isVar || sym.asTerm.isVal) ), but when I try to create a FieldMirror, ( cm mirrorField sym. AsTerm ) I get this exception:

  exception in the formula "main" scala.ScalaReflectionException: Scala Zone B is not represented as a JAVA field, nor does the Java accessor method note that the private parameters of the class constructor are not mapped to fields and / or accessories, unless they are used outside of their declared constructs We do.   

Now, I understand why this happens, but I can not find any way to check it without throwing an exception. Of course, the use of Java reflection is just fine, but is it any point of view against the point?

Before avoiding error about filtering b For:

  Scala> Val m = reflect.runtime.universe.runtimeMirror (getClass.getClassLoader) m: Reflect. Runtime.universe.Mirror = with JavaMirror ... Scale & gt; Val im = m.reflect (New CloneSubject (5, "Hello", true)) im: CloneSubject @ 2a95e173 reflect.runtime.universe.InstanceMirror = Example mirror for Scala & gt; Val fields = typeOf [CloneSubject] Collecting Memory {case m: MethodSymbol if m.isGetter = & gt; M.accessed} field: iterable [reflect. Runtime.universe.Symbol] = List (variable C, value A) Scale & gt; Fields map (s = i.reflectField (s.asTerm) .Get) res45: Iterable [any] = list (true, 5)   

You know that every area It is possible that a gator is used, so that only existing things are to be presented by all the current gates.

Another option to use it to see if val / var has a heater: <> Scala & gt; Typeof [CloneSubject] .members.filter (! S => gt; s.isTerm & amp; amp; (s.asTerm.isVar || s.asTerm.isVal) & amp; amp; s.asTerm.getter = NoSymbol) res50: Iterable [Reflect.runtime.universe.Symbol] = Synchronized Ops (variable C, value A)

No comments:

Post a Comment