flume-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Capwell <dcapw...@gmail.com>
Subject Re: Why does ComponentConfigurationFactory.create pass classname as part of init
Date Thu, 20 Sep 2012 06:02:58 GMT
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 <
hshreedharan@cloudera.com> 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.
>
>
>

Mime
View raw message