How reflection filter works?

Monkey Forums/Monkey Programming/How reflection filter works?

Tibit(Posted 2013) [#1]
I would like to learn how the reflection filter works.

Sometimes it works, most time it doesn't.

I'd love if someone who knows could explain how to use it or what I need to know to understand it, how the compiler uses it.

I'd like to see if I can use it for events. I know Ziggy does so I assume it is viable even in a larger context.

Right now I only get things working with:

#REFLECTION_FILTER += "*"


Tibit(Posted 2013) [#2]
Anyone knows if there is an event module based on reflection?


Rone(Posted 2013) [#3]
Did you try EventHandler<T> from junglegui?

Class MyEventArgs
   Field X,Y
End 

Class MyClass 
   Field Event:= new EventHandler<MyEventArgs>
   
   Method OnEvent()
   		Event.RaiseEvent(Self, new MyEventArgs)
   End
End 

Class TestApp extends App 

	Field bla:= new MyClass
	
	Method OnCreate()
		bla.Event.Add(Self,"MyEventHandler")
	End 

	
	Method MyEventHandler(s:Object, e:MyEventArgs)
	End 
End



marksibly(Posted 2013) [#4]
> I would like to learn how the reflection filter works.

Have a look at banas/mak/reflectiontest for a demo (I haven't yet updated docs to deal with += and app config settings).

Also the '|' separator is probably not necessary in the "|mojo*" bit. Am going to have a cleanup of += soon...


Tibit(Posted 2013) [#5]
Rone, awseome! Yes I did but did not get the eventArgs thing, but I get it now thanks to your example.

However even with the reflectiontest example I'm not getting how the reflectionFilter works or maybe I'm trying to do something it cannot?

So I assume it parses my code pre-compile based on imports recursively?

So:
#REFLECTION_FILTER+="reflectiontest.monkey"

Should add that file and nothing else if that file exists in main-file-folder, current-code-file-folder,any-modpath-folder.

It should not matter what this file imports right? Unless one of the imported files contain their own #REFLECTION_FILTER+="thisFile.monkey", then I assume the "add" rules above are followed as if inside that file, right?

@Mark, changing the reflection filter to #REFLECTION_FILTER+="reflectiontest.monkey" results in:
Monkey Runtime Error : TypeError: Cannot call method 'p_NewInstance' of null <91>

Two Use Cases I would like explained:

1. I have 10 files that is called some1Test.monkey or that I have 10 random files in a folder called /testfolder/

How to create a reflection filter so a library using reflection can get only the files ending with Test, is * for this? Or get only the files in the /testfolder/ ? I need to import these files individually? Can I add something like += in each file?

2. Assume I'm using Ziggy's event system. I have 12 classes in my game that use these reflection-events. What do I need to do to make that work? Because adding #REFLECTION_FILTER+="thisFile.monkey" to each file seems to not work.


Rone(Posted 2013) [#6]
2.
#REFLECTION_FILTER+="file0*|file1*|"

junglequi is pretty big, if you're only use eventhandler.
Here's the delegate<t> implementation without junglequi dependency:



Tibit(Posted 2013) [#7]
Am I correct that you are saying I need in my [Mainfile?] add the reflection code for each class that use reflection?

The file can't do it itself?

So I need to keep track and list all my event-using files in a long list that I copy each time I switch main file and for each main-test I run?

How do I mange that? Seems to be quite a lot of work!

And thanks for the code snippet! :)


Rone(Posted 2013) [#8]
#REFLECTION_FILTER+="thisfile*|" works too


Tibit(Posted 2013) [#9]
Thanks! I actually got it to work this time. I have no idea what I did wrong before, looks the same to me...

Here is an event-demo if anyone later on visit this thread.

reflectThis.monkey


reflectTest.monkey - Note that this file knows nothing about reflection.



Evil Roy Ferguson(Posted 2013) [#10]
For me this is still not working on any submodules. If I have screens.monkey and import that, it works, but screens/screen1.monkey is not exposed to reflection with anything I've tried - is anything special needed for submodules?


Tibit(Posted 2013) [#11]
What do you mean by sub-module?

I have been having problems with this a lot but now that I did an example from scratch to find the cause, it ofc works. Maybe the situation is more complex.

Can you make an example that fails that you expect to work?


Evil Roy Ferguson(Posted 2013) [#12]
Actually feeling pretty dumb now - the issue was that my class implemented an interface that wasn't in the reflection filter, which was preventing it from showing up in the list of exposed classes.


Tibit(Posted 2013) [#13]
I'm using a lot of interface myself, but how can an interface hide anything from the reflection filter? That sounds mystic :)


wiebow(Posted 2013) [#14]
I use reflection in this module, which can include other modules for reflection. Maybe a gander at the code will make some things more clear to you.

https://code.google.com/p/mutated-monkey/wiki/unittest