Thread overview
Is it possible using reflection or similar to extract only public method names from classes?
Aug 06, 2013
Gary Willoughby
Aug 06, 2013
Marek Janukowicz
Aug 07, 2013
Jacob Carlborg
Aug 07, 2013
Gary Willoughby
Aug 07, 2013
Adam D. Ruppe
Aug 10, 2013
H. S. Teoh
August 06, 2013
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.
August 06, 2013
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
August 07, 2013
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
August 07, 2013
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?
August 07, 2013
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.
August 10, 2013
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.