Re: Introspect whether setCharacterEncoding is present..

Charles Forsythe <[email protected]> Thu, 14 Oct 2004 06:44:33 -0500
Newsgroups gmane.comp.java.advanced-servlets
Message-ID <[email protected]>
Endre Stølsvik wrote:

>| If an API "level test" is appropriate, it would be a lot cleaner (and
>| faster) to test for the existence of a 2.3 class, rather than looking
>| for the setCharacterEncoding method via reflection.
>
>Why?
>
Determining the existence of a method via introspection is tantamount to 
making assumptions about an Object's implementation, which is 
particularly naughty when the entity in question is an interface 
implementation.

Since HttpRequest is an interface, the implementing class may have 
public methods that it doesn't expect "unfamiliar" code to call, 
including (possibly) setCharacterEncoding. Maybe the HttpRequest you are 
introspecitng is an RMI stub that happens to feature 
setCharacterEncoding so that it can serve APIv2.3 stubs when presented 
to APIv2.3 clients.  Maybe you're running on a container that has a "2.2 
API build" option for people who don't have a Java2 VM or don't need 
APIv2.3 features and setCharacterEncoding is an inoperative artifact of 
the "2.3 API build."  If there is a possibility that you running in a 
APIv2.2-compliant container, then an extant setCharacterEncoding might 
just throw an UnsupportedOperationException.  Who knows?

APIv2.3 objects suchs as javax.servlet.http.HttpRequestWrapper shouldn't 
exist unless the APIv2.3 is supported -- why would they?  It just seems 
like a more robust test.

>What it seems like we'll end up on, is to ask the servlet API for its
>version, then if (major >= 3 || (major == 2 && minor >= 3)), then
>introspect the method and invoke it (so that we still can compile on
>servlet2.2.jar).
>  What would be the benefit of your check, as opposed to introspection?
>
My mechanism doesn't require reflection, so it can run on J2ME-based 
servers.  The reflection method does allow the code to be compiled 
against the APIv2.2 classes, so that could be considered an advantage.

-- Charles



------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

Before posting a question, try to find your answer here: 
<http://www.egroups.com/links/advanced-servlets>
Announcements should go to: [email protected]
To Post a message, send it to: [email protected]
To Unsubscribe, send a blank message to: [email protected] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/advanced-servlets/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/