Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Struts - A MVC web framework
Tomcat - JSP/Servlet container
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
JSP - A mailing list about Java Server Pages specification and reference
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Oracle Connection Pooling in 3 2 2
Servlet : Session invalidate
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Tomcat and webapplication specific java library path
Running a Simple JMS Example
Mapping in workers2 properties
org apache jasper JasperException
Cannot find message resources under key org apache struts action
   MESSAGE
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
url string for connecting jboss to oracle
Value attribute of <html:checkbox
javax servlet ServletException: BeanUtils populate
HTTP Status 404 The requested resource is not available
5 0 18: Windows XP Pro vs Windows 2000
 
Urgent - Alternatives for Using Reflection

Urgent - Alternatives for Using Reflection

2004-08-04       - By Bruce K. Haddon

 Back
Reply:     1     2     3     4     5     6     7     8  

Folks,

I believe that the wording of the original question really did not
characterize the problem that was to be solved. Almost obviously, if
the class ABC is known and can be instantiated via "new," and an
"if-else" can decide which method to call, then reflection is not
needed to probe the properties of the class ABC.

Thus, I think that the question that was being asked was how to
capture the result of some evaluation of whether getA(), getB(), or
getC() is to be called, and to pass that decision to another class, so
that that other class does not have to repeat the evaluation (or even
know that there was an evaluation to be performed).

In short, how should a functor be created, that "points" to the
correct method, and which can be used by another class to call the
correct method, without that class having to know (or care) which
method it is calling.

Obviously, using reflection to create a Method object is one way. So,
in this view, the question becomes, "what other way of doing this
would be possible without using reflection?" This is not to say that
reflection is a bad way; in fact, it is the only truly general way
(TTBOMK) of doing this in Java.

Another, more limited way, would be to created essentially a ABC_get
class that is local to the code that makes the decision, and is then
passed to the objects that need to call these get methods; it would
look something like:

public class ABC_get
{
private char which_selection;
private ABC target_selection;

public ABC_get(char which, ABC target )
{
    which_selection = which;
    target_selection = target;
}

public string getABC()
{
    switch( which_selection )
    {
    case 'A':
        return target_selection.getA();
    case 'B':
        return target_selection.getB();
    case 'C':
        return target_selection.getC();
    default:
           throw ....
    }
}
}

(Of course, this can be prettied--using enumerations, and so on, and
made safe, and various checks and protections may be added, but this
version will do for the idea ... )

The code that makes the decision on behalf of the other object(s) can
record that decision by creating an ABC_get object with the correct
values for the constructor, and the other object(s) can use the
ABC_call object as a proxy to call the correct method without knowing
how the decision was made, or even which method of an ABC instance it
is calling.

So, the answer is "yes," it can be done without reflection, but in all
in all, it is not so elegant. But if there is some good reason for not
using reflection, then something along these lines can be used.

Regards, Bruce



> Jaise George wrote:

> Hi Shiva,
>
> If you do not know what methods are there in the class, then I think
> reflection is the only way. Reflection is meant for that only. As I said
> in my previous mail, reflection is not a costly issue now. Methods in
> java.lang.Class Source code of java.lang.Class uses reflection.
>
> If you use an interface or any other implementation like that, you MUST
> KNOW the method names in the class.
>
> If decided to use an interface, you can go for a factory pattern here.
>
> Class ABC implements InterfaceA, InterfaceB, InterfaceC
> {
> }
>
> Interface InterfaceA{
>         public string GetA();
> }
>
> Interface InterfaceB{
>         public string GetB();
> }
>
> Now use a factory class to return the appropriate interface downcasted
> from Class ABC. Call the method from the object.
> This alleviates the discomfort you might have in using if-else
> statements.
>
> Hope this helps.
>
> Regards
> Jaise
>
> -- --Original Message-- --
> From: A mailing list for Java(tm) 2 Platform, Enterprise Edition
> [mailto:J2EE-INTEREST@(protected)] On Behalf Of Shivprasad_Bade
> Sent: Tuesday, August 03, 2004 6:48 PM
> To: J2EE-INTEREST@(protected)
> Subject: Re: Urgent - Alternatives for Using Reflection
>
>
> Thanx Jaise for quick reply,
>
> Here is the details.
>
> I have a class.
>
> class ABC
> {
>         private string a;
>         private string b;
>         private string c;
>         public string getA(){}
>         public string getB(){}
>         public string getC(){}
>       ...
>       ...
> }
>
> Now in other class
>
> someMethod(String condition)
> {
>     instance=new ABC();
>     //now depending upon the condition I have to call either getA()
> getB() or getC() and I don't
>     // know what methods are there in the class and I don't want to use
> the reflection.
>
> }
>
>
> Hope it gives some idea to you.
> And abt the interface solution I have given a thought to it but before
> that just wanted to make sure there is no other way for doin this.
>
> -
>
> Regards,
>
> Have a nice day..!!!
>
> Shivprasad Bade
>
>
> -- --Original Message-- --
> From: Jaise George [mailto:jaiseg@(protected)]
> Sent: Tuesday, August 03, 2004 6:42 PM
> To: J2EE-INTEREST@(protected)
> Subject: Re: Urgent - Alternatives for Using Reflection
>
>
> Hi Shiva,
>
> Your description is not clear enough to give a solution. If the methods
> of that class is absolutely unknown, you can go for reflection.
> Reflection is not that dangerous these days. I guess application servers
> use that extensively.
>
> Alternatively an option I can you is to use an interface, if the class
> is developed by your own team.
>
> Jaise
>
> -- --Original Message-- --
> From: A mailing list for Java(tm) 2 Platform, Enterprise Edition
> [mailto:J2EE-INTEREST@(protected)] On Behalf Of Shivprasad_Bade
> Sent: Tuesday, August 03, 2004 6:32 PM
> To: J2EE-INTEREST@(protected)
> Subject: Urgent - Alternatives for Using Reflection
>
>
> Hi,
>
> Sorry for posting it on J2EE list ...
>
> Is there any alternative for using the Reflection.
>
> I have a class instance with me but want to call a method depending upon
> some condition and I dont want to use the if-else etc.
>
> Thanx in advance
>
>
> -
>
> Regards,
>
> Have a nice day..!!!
>
> Shivprasad Bade
>
> Logic will get you from A to B - imagination will take you anywhere.
> ************************************************************************

--

======================================================================
Dr. Bruce K. Haddon
Principal Engineer/Master Java Architect
Sun Software Professional Services
Sun Microsystems, Inc., UBRM01-241
500 Eldorado Blvd                              (78418) +1 303/272 8418
Broomfield, CO 80021-3400, U.S.A.                 Bruce.Haddon@(protected)

"Advances  are  made  by  asking  questions; discoveries  are  made by
 questioning answers."  Bernhard Haisch, Astrophysicist

===========================================================================
To unsubscribe, send email to listserv@(protected) and include in the body
of the message "signoff J2EE-INTEREST".  For general help, send email to
listserv@(protected) and include in the body of the message "help".

©2008 junlu.com - Jax Systems, LLC, U.S.A.