Re: iterate through methods
From: xarax (xarax_at_email.com)
Date: 08/05/04
- Next message: xarax: "Re: Why traverse listener lists backwards?"
- Previous message: Ike: "Re: jEdit: compiles JDK 1.5.0 ok, but runs JDK 1.4.1 (why?)"
- In reply to: Christophe Vanfleteren: "Re: iterate through methods"
- Next in thread: JCary: "Re: iterate through methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 05 Aug 2004 00:24:52 GMT
"Christophe Vanfleteren" <c.v4nfl3t3r3n@pandora.be> wrote in message
news:3ycQc.203748$py4.10431543@phobos.telenet-ops.be...
> JCary wrote:
>
> > I would like to iterate through several methods and I cannot seem to find
> > a
> > way. Can someone please point me in the right direction. Can this even
> > be done.
> >
> > Thanks,
> >
> > Cary
> >
> > ** Idea of what I'm trying to accomplish
> >
> > private void methodIterator(){
> > String m;
> > for(int i = 0; i <= 10; i++){
> > m = "method_" + String.valueOf(i) + "()";
> > // Somehow execute method here
> > }
> > }
> <snip method>
>
> I'm sure a loop of less methods would have made your problem clear :)
>
> Anyway, most of the time it is pretty suspicous when you (think) you need
> something like this, but it can be done using reflection.
>
> Look at the java.lang.reflect package and
>
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getDeclaredMethods
()>
And if you think your problem requires reflection,
then think again, and think again, and think again
before using reflection.
For example, in your problem, is the string the
determining factor for which method is called or
is it the index? You problem looks like it is
index-driven, rather than name driven. Therefore,
an array of interface references indexed by the
loop variable would solve your problem and run
far more efficiently than reflection.
In C, an array of function pointers would be
indexed in the loop. In Java, the equivalent
approach is array an interface references indexed
in the loop. It's a little more complicated to
set up than C, but it's definitely very efficient.
- Next message: xarax: "Re: Why traverse listener lists backwards?"
- Previous message: Ike: "Re: jEdit: compiles JDK 1.5.0 ok, but runs JDK 1.4.1 (why?)"
- In reply to: Christophe Vanfleteren: "Re: iterate through methods"
- Next in thread: JCary: "Re: iterate through methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|