Dynamic object method invocation using reflection in scala -


I'm looking to make a way of dynamic call logic based on the template id within the scale Then log on to template id 1, logic id 2 call logic b, etc. The logic will be varied but there will be only one input / output, in addition the number of different template IDs will be found in thousands and will not be known before time, hence a loose coupling seems to be the way to go.

I have started to see the reflection to do this 2.11.1 And when I know the logic used for time, but it does not seem to be correctly understood to use the reflection For example, logging argument for example, entering template id 2 would be arguments.

Below is a cut example that shows how the static version works and I'm still using the skeleton for the dynamic version.

  Import the package to scala.reflect.runtime. {Universe = & gt; RU} attribute {ext theMethod (x: string) = {println ("a" + x)}} {def theMethod (X: string): unit} // different logon organized in different object object objects Object object2 TheTrait {def theMethod (x: string) = {println ("c" + x)}} // expanded stat {def theMethod (x: string) = {println ("b" + x)}} object3 expanded Does. / Dynamic reflection methods reflect object {// "static" greeting object object1.the method method static frequency () = {val m = ru.runtimeMirror (getClass.getClassLoader) val im = m.reflect (thePackage.object1) val method = Ru. TypeOf [thePackage.object1.type] .decl (ru.TermName ("the method"). AsMethod Well Methodod = IM. Refract method (method "method") static invocation // "dynamic" methods def dynamicInvocation ( Y: integer) = {val m = ru.runtimeMirror (getClass.getClassLoader) val module = m.staticModule ("the package.object" + y) val im = m.reflect module (module) // stuck ... static The approach does not work here) Dynamic Transfer (1) Dynamic Transfer (2) Dynamic Inclusion (3)}   

What to add / change in the dynamic exploration method Yk, or what I should use a different approach?

You need to get an example mirror for your module, on which you reflect the method You can. Def dynamic invocation (y: integer) = {val m = ru.runtime mirror (getClass.getClassLoader) val module = m.staticModule ("the package.object" + y) val im = M.reflect module (module) val method = im.symbol.info.decl (ru.TermName ("themethod")). AsMethod Val objMirror = m.reflect (im.instance) objMirror.reflectMethod (method) ("test")}

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -