Consider using the builder pattern instead of static factory methods:
http://en.wikipedia.org/wiki/Builder_pattern

On Sat, Jan 2, 2010 at 2:58 PM, jotobjects <jotobje...@gmail.com> wrote:
> Here is a start...
>
> Class barClass = com.example.Bar.class;
> Method m = barClass.getMethod("foo", ...) // add parameter types to
> call
> Object val = m.invoke(null, ...) // add parameter values to call
>
> On Jan 2, 12:10 pm, Philip <philip.dese...@gmail.com> wrote:
>>
>> 2. I am trying to replicate the easy functionality of the
>> NSClassFromString method. Basically, if I have a string for a full
>> qualified class name, what is the best way to invoke a static method
>> with a known signature for that class.
>>
>> In objective c:
>>
>> Class foo()
>> {
>>         NSString *r = @"Bar";
>>         Class c = NSClassFromString(r);
>>         return c;
>>
>> }
>>
>> then you can call method sna() on class Bar with [foo() sna:d]
>>
>> So far in Java using reflection, I came up with this convoluted code
>> that doesn't replicate the behavior for the parameters for the
>> function (I pass back the object reference, not the method back,
>> therefore I also have to pass the parameter to call the method):
>>
>> static SuperClassOfBar foo(float d) {
>>         try {
>>             String r = "Bar";
>>             Class c = Class.forName("com.example."+r); // Fully
>> qualified name for class
>>             Class partypes[] = new Class[1];
>>             partypes[0] = Float.TYPE;
>>             Method m = c.getDeclaredMethod("sna", partypes);
>>             Object arglist[] = new Object[1];
>>             arglist[0] = d;
>>             Object o = m.invoke(null, arglist);
>>             return SuperClassOfBar.class.cast(o);
>>         } catch (Exception e) {
>>             return null;
>>         }
>>
>> It works but it's heavy and not pretty. I understand that leaving a
>> dynamic language like objective c , I have to make some compromises,
>> but I may miss something about Java for not having found something
>> more lightweight for such a generic programming functionality. Any
>> suggestions to simplify this implementation are welcome.
>>
>> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to