API Reflection Interface

Discussion in 'Java' started by franck.dubois1970, Nov 23, 2007.

  1. Hello,

    i would to specify interface constants with xml or property file
    i think for that using api reflection but i don't see how to use
    interface with api reflection ?

    someone has idea ?

    thanks in advance
    Franck
     
    franck.dubois1970, Nov 23, 2007
    #1
    1. Advertisements

  2. If I understand you correctly, what you want to do is to be able to
    specify the constants of an interface using another file. If so, then
    the only way to do this is to generate class files dynamically; (most)
    interface constants are compile-time constant expressions that, for all
    practical purposes, cease to exist in the compiled code. Therefore,
    these values need to be known at compile time, so the using classes need
    to have a ready class file available at compile time.

    In short: Can't be done with reflection at runtime.
     
    Joshua Cranmer, Nov 23, 2007
    #2
    1. Advertisements

  3. Hello Frank,

    If you really absolutely need to do that there are ways and you may be
    interested to look at the following two Reflection APIs:

    - Class.forName
    - Proxy.newProxyInstance

    BUT I would seriously object the need to use this mechanism at
    application level outside of a framework: there are cleaner alternatives for
    an application e.g. how about a map of Factory instances keyed by some
    ID that is read from your XML? so your application create instances that
    that specified through configuration hmmm sounds like IoC to me.

    Well anyway you can find an example of the Proxy.newProxyInstance here:
    http://perfectjpattern.svn.sourcefo...ructural/composite/Composite.java?view=markup

    The idea is: you give me an interface Class type that you may create
    from a String loaded from an XML file see Class.forName, and I
    give you a dynamic instance (implementation) of such interface.

    I managed to componentize (or make a component out of) the Composite
    Design Pattern that way. It is almost exactly the same mechanism as the
    Mock objects use for testing.

    HTH,
    Giovanni
     
    Giovanni Azua, Nov 24, 2007
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.