Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Plain View
Flume >> mail # dev >> Why does ComponentConfigurationFactory.create pass classname as part of init

David Capwell 2012-09-20, 05:20
Copy link to this message
Re: Why does ComponentConfigurationFactory.create pass classname as part of init
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.

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.

David Capwell 2012-09-20, 06:02
Hari Shreedharan 2012-09-20, 18:56
David Capwell 2012-09-24, 20:30