|
|
-
Re: Why does ComponentConfigurationFactory.create pass classname as part of initHari Shreedharan 2012-09-20, 18:56
I don't remember why I did this at that time. I will take a look at this when I get some time and get back to you. FWIW, this is part of a yet to be completed component-wise configuration system which I plan to get back to some time soon. Hopefully, this is not a blocker for you as of now.
Thanks, Hari -- Hari Shreedharan On Wednesday, September 19, 2012 at 11:02 PM, David Capwell wrote: > I guess my real question would be why my custom ComponentConfiguration has to have this constructor. Here is quick sample of what i mean: > > public void createInstance() { > final ComponentConfiguration config = ComponentConfigurationFactory.create( > "name", > RandomConfig.class.getName(), > ComponentConfiguration.ComponentType.SOURCE); > } > > public static class RandomConfig extends ComponentConfiguration { > > public RandomConfig() { > super("my awesome name"); > } > } > > > This fails since the constructor doesn't exist. If i add a param "String ignored" then it works just fine. > On Wed, Sep 19, 2012 at 10:47 PM, Hari Shreedharan <[EMAIL PROTECTED] (mailto:[EMAIL PROTECTED])> wrote: > > From: http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html > > There are two reflective methods for creating instances of classes: java.lang.reflect.Constructor.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29) and Class.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29). The former is preferred and is thus used in these examples because: > > Class.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29) can only invoke the zero-argument constructor, while Constructor.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29) may invoke any constructor, regardless of the number of parameters. > > Class.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29) throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. InvocationTargetException (http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/InvocationTargetException.html). > > Class.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#newInstance%28%29) requires that the constructor be visible; Constructor.newInstance() (http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29) may invoke private constructors under certain circumstances. > > > > > > > > > > Thanks, > > Hari > > > > -- > > Hari Shreedharan > > > > > > On Wednesday, September 19, 2012 at 10:20 PM, David Capwell wrote: > > > > > I was going over the flume 1.2.0 code and i was wondering why > > > the ComponentConfigurationFactory.create class has the following: > > > > > > confType = (Class<? extends ComponentConfiguration>) Class.forName(type); > > > return confType.getConstructor(String.class).newInstance(type); > > > > > > Since type is the class, then why does the the class need a constructor > > > that puts in the class name? > > > > > > thanks for your time reading this email. > > > |