Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > anonymous inner class reflection question

Reply
Thread Tools

anonymous inner class reflection question

 
 
Gary
Guest
Posts: n/a
 
      08-13-2003
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");
}
}
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-13-2003
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
 
Reply With Quote
 
 
 
 
_nuhb_
Guest
Posts: n/a
 
      08-14-2003
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_

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access private inner class with reflection? Ulrich Scholz Java 2 07-05-2010 10:42 AM
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
All classes from pkg name & inner class reflection Jeffy Java 2 09-10-2003 01:49 PM



Advertisments