www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible using reflection or similar to extract only public

reply "Gary Willoughby" <dev nomad.so> writes:
Is it possible using reflection or similar to extract only public 
method names from classes? I'm thinking how i would go about 
writing a unit test/mocking framework, investigating how i can 
gather information about such things before i manipulate them.
Aug 06 2013
next sibling parent reply Marek Janukowicz <marek janukowicz.net> writes:
Gary Willoughby wrote:

 Is it possible using reflection or similar to extract only public
 method names from classes? I'm thinking how i would go about
 writing a unit test/mocking framework, investigating how i can
 gather information about such things before i manipulate them.
See traits: http://dlang.org/traits.html Look for: getProtection, getVirtualFunctions, getVirtualMethods. -- Marek Janukowicz
Aug 06 2013
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-08-07 00:57, Marek Janukowicz wrote:

 See traits: http://dlang.org/traits.html

 Look for: getProtection, getVirtualFunctions, getVirtualMethods.
And "allMembers" and "derivedMembers". -- /Jacob Carlborg
Aug 07 2013
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 6 August 2013 at 22:55:49 UTC, Marek Janukowicz wrote:
 Gary Willoughby wrote:

 Is it possible using reflection or similar to extract only 
 public
 method names from classes? I'm thinking how i would go about
 writing a unit test/mocking framework, investigating how i can
 gather information about such things before i manipulate them.
See traits: http://dlang.org/traits.html Look for: getProtection, getVirtualFunctions, getVirtualMethods.
I thought using __traits(...) was frowned upon?
Aug 07 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 7 August 2013 at 11:47:27 UTC, Gary Willoughby 
wrote:
 I thought using __traits(...) was frowned upon?
They want to give them phobos wrappers that might be a little prettier, but other than beauty/ugliness there's no reason not to use it.
Aug 07 2013
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Aug 06, 2013 at 11:41:35PM +0200, Gary Willoughby wrote:
 Is it possible using reflection or similar to extract only public
 method names from classes? I'm thinking how i would go about writing
 a unit test/mocking framework, investigating how i can gather
 information about such things before i manipulate them.
The following code demonstrates how you can do this: import std.stdio; class Base { private int x; public int y; this() {} private void privMethod() {} public void method() {} } class Derived : Base { public override void method() {} public void derivedMethod() {} private void privDerivedMethod() {} } void showAllMethods(C)(C obj) { writeln("All members:"); foreach (field; __traits(allMembers, C)) { static if (is(typeof(__traits(getMember, obj, field)) T == function)) { auto prot = __traits(getProtection, __traits(getMember, obj, field)); writefln("\t(%s) %s", prot, field); } } } void showDerivedMethods(C)(C obj) { writeln("\nDerived members:"); foreach (field; __traits(derivedMembers, C)) { static if (is(typeof(__traits(getMember, obj, field)) T == function)) { auto prot = __traits(getProtection, __traits(getMember, obj, field)); writefln("\t(%s) %s", prot, field); } } } string[] getPublicMethods(C)(C obj) { string[] methods; foreach (field; __traits(allMembers, C)) { static if (is(typeof(__traits(getMember, obj, field)) == function) && __traits(getProtection, __traits(getMember, obj, field)) == "public") { methods ~= field; } } return methods; } void main() { auto d = new Derived(); showAllMethods(d); showDerivedMethods(d); writeln("All public methods:"); writeln(getPublicMethods(d)); } The output is: All members: (public) method (public) derivedMethod (private) privDerivedMethod (public) __ctor (private) privMethod (public) toString (public) toHash (public) opCmp (public) opEquals (public) factory Derived members: (public) method (public) derivedMethod (private) privDerivedMethod (public) __ctor All public methods: ["method", "derivedMethod", "__ctor", "toString", "toHash", "opCmp", "opEquals", "factory"] Hope this helps! T -- Designer clothes: how to cover less by paying more.
Aug 06 2013