anonymous inner class reflection question

Discussion in 'Java' started by Gary, Aug 13, 2003.

  1. Gary

    Gary Guest

    Hi, I got the following exception when running the codes attached
    below:
    Exception in thread "main" java.lang.InstantiationException:
    IFactory$1
    at java.lang.Class.newInstance0(Class.java:281)
    at java.lang.Class.newInstance(Class.java:249)
    at IFactory.getI(IFactory.java:20)
    at IFactory.main(IFactory.java:35)

    Is it because Java doesn't support reflection for anonymous inner
    class? If so, how can I add dynamic classes? Is it possible?

    Thanks in advance.

    Gary

    import java.util.*;

    public class IFactory {
    private static HashMap map = new HashMap();
    private static IFactory factory = new IFactory();

    static {
    map.put("A", A.class);
    map.put("B", B.class);
    }

    private IFactory() {}

    public static IFactory getInstance() {
    return factory;
    }

    public I getI(String name) throws Exception {
    Class c = (Class) map.get(name);
    return (I) c.newInstance();
    }

    public void addI(String name) throws Exception {
    I i = new I() {public void print(){System.out.println("C");}};
    map.put(name, i.getClass());
    }

    public static void main(String[] args) throws Exception {
    IFactory factory = IFactory.getInstance();

    factory.getI("A").print();

    factory.addI("C");

    factory.getI("C").print();
    }
    }

    abstract class I {
    abstract public void print();
    }

    class A extends I {
    public void print() {
    System.out.println("A");
    }
    }

    class B extends I {
    public void print() {
    System.out.println("B");
    }
    }
    Gary, Aug 13, 2003
    #1
    1. Advertising

  2. On 12 Aug 2003 18:19:28 -0700, Gary wrote:
    > Hi, I got the following exception when running the codes attached
    > below:
    > Exception in thread "main" java.lang.InstantiationException:
    > IFactory$1
    > at java.lang.Class.newInstance0(Class.java:281)
    > at java.lang.Class.newInstance(Class.java:249)
    > at IFactory.getI(IFactory.java:20)
    > at IFactory.main(IFactory.java:35)
    >
    > Is it because Java doesn't support reflection for anonymous inner
    > class? If so, how can I add dynamic classes? Is it possible?


    Constructors to inner classes have a hidden first argument that you
    need to provide when you use reflection. Pass an instance of the outer
    class.

    Use "javap -s" to confirm this.

    /gordon

    --
    [ do not send me private copies of your followups ]
    g o r d o n . b e a t o n @ e r i c s s o n . c o m
    Gordon Beaton, Aug 13, 2003
    #2
    1. Advertising

  3. Gary

    _nuhb_ Guest

    Hey Gordon

    Gordon Beaton wrote:

    >>Is it because Java doesn't support reflection for anonymous inner
    >>class? If so, how can I add dynamic classes? Is it possible?

    > Constructors to inner classes have a hidden first argument that you
    > need to provide when you use reflection. Pass an instance of the outer
    > class.
    > Use "javap -s" to confirm this.

    If you need an example take a look at <a
    href="http://forum.java.sun.com/thread.jsp?forum=31&thread=428588">this
    thread</a> at the jdc.

    greets
    _nuhb_
    _nuhb_, Aug 14, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffy
    Replies:
    2
    Views:
    2,655
    Thomas Weidenfeller
    Sep 10, 2003
  2. E11
    Replies:
    1
    Views:
    4,711
    Thomas Weidenfeller
    Oct 12, 2005
  3. Ulrich Scholz
    Replies:
    2
    Views:
    12,853
    EugenZ
    Jul 5, 2010
  4. Pyenos
    Replies:
    2
    Views:
    377
    Pyenos
    Dec 27, 2006
  5. Reporter
    Replies:
    3
    Views:
    464
    Mike Schilling
    May 12, 2007
Loading...

Share This Page