question about reflection: extendsclass()

Monkey Forums/Monkey Programming/question about reflection: extendsclass()

wiebow(Posted 2012) [#1]
I have a question about the following code:
#REFLECTION_FILTER="untitled*"
Import reflection

Function Main()
	Local baseClass:= GetClass("a")
	
	Local classes:= GetClasses()
	For Local c:Int = 0 to classes.Length()-1
	
		If classes[c].ExtendsClass(baseClass)	
			Print( "Class: " + classes[c].Name() )
			
			Local methods:= classes[c].GetMethods()
			For Local m:Int = 0 to methods.Length()-1
				Print( "  Method: " + methods[m].Name() )
			Next
			
			Local fields:= classes[c].GetFields()
			For Local f:Int = 0 to fields.Length()-1
				Print( "  Field: " + fields[f].Name() )
			Next			
		End	
	Next
End

Class a
	Field fa
	
	Method ma()
	End
End

Class b extends a
	Field fb
	
	Method mb()
	End
End


When you run this code, you will see it will print information about class a AND class b.

My questions:

1- Why is it printing the information about class a? I am checking if the current class EXTENDS class a, which to me means I am not interested in class a, but only in classes which extends class a.
2- When querying class b: why am I not seeing the methods and fields defined in class a as well? They are a part of class b as well.


slenkar(Posted 2012) [#2]
That's just the way it works :) you. Just have to loop through super classes to get all the fields


Samah(Posted 2012) [#3]
This is why Java has getFields/getDeclaredFields, and getMethods/getDeclaredMethods. The former returns all public fields/methods (including inherited), while the latter will only return fields/methods declared in the specified class (including private).

Perhaps Monkey should have something similar?


slenkar(Posted 2012) [#4]
It's not a big deal just keep invoking superclass until it returns null


wiebow(Posted 2012) [#5]
I know this is the way things work, but I was hoping that I was missing something obvious. =]


wiebow(Posted 2012) [#6]
Mark obviously fixed this in the v155. Thanks Mark.