commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Juozas Baliuka" <bali...@centras.lt>
Subject Re: [VOTE] (3b) XxxUtilsConstructors last chance
Date Thu, 22 Aug 2002 06:35:27 GMT
<snip>
>   protected StringUtils loadStringUtility(String className) throws
>       Exception
>   {
>       // Get the Class object for the fully qualified class name.
>       Class c = Class.forName(className);
>       // Now get an instance of that class using its public no args ctor.
>       return (StringUtils) c.newInstance();
>   }
>
> The className parameter might come from a configuration file, or user
> specified option.  The StringUtils instance returned by this example
> method might be used only via introspection, meaning that access to
> any methods defined by a sub-class would be allowed without casting.

You do not need "public exdendable" in this case :


   protected Object loadAnyUtility(String className) throws
       Exception /*, SecurityException */
   {
       // Get the Class object for the fully qualified class name.
       Class c = Class.forName(className);
       // Now get an instance of that class using its any no args ctor.
      Constructor constructor =c.getDeclaredConstructor( new Class[]{});
       constructor.setAccessible(true);
       // do not need to cast for introspection
       return  constructor.newInstance( new Object[]{} );
   }




>
> Making the class final or removing the no arguments constructor
> effectively removes the ability to use the class in this fashion.  So
> far, the majority of arguments for taking this path revolve around "a
> book told me to" or "it breaks my idea of how static classes are
> used", while arguments against this path point out real world use
> cases (like this one) that, while not necessarily common, are
> completely valid.
> --
>
> Daniel Rall <dlr@finemaltcoding.com>
>
> --
> To unsubscribe, e-mail:
<mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:commons-dev-help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message