由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - anonymous innerclass reflection question
相关主题
问题征解The right way to create new Exception
有熟悉Java Reflection的吗is it legal for the constructor of a REST resource class to throw exceptions?
Netbean duplicate class error跪求大牛指点Java,看不懂什么意思。
Answer to "Is this a Bug or not? "Re: java.lang.reflect.InvocationTargetEx
一道java题问个基本问题
感觉inner class挺好用的invoke a function dynamically in Java?
请问一个语法问题return null or empty list/set/...
how to copy an Object?对 spring 的 exception 处理方式真是不适应
相关话题的讨论汇总
话题: reflection话题: anonymous话题: innerclass话题: exception话题: java
进入Java版参与讨论
1 (共1页)
c********k
发帖数: 74
1
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 have dynamic classes? Is it possible?
Thanks in advance.
Gary
import
n*****k
发帖数: 123
2

Does this have anything to do the famous reflection error, you must have a
default public constructor, in order to use class.newInstance() method.

【在 c********k 的大作中提到】
: 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 have dynamic classes? Is it possible?
: Thanks in advance.
: Gary

e***g
发帖数: 158
3
it does'nt make sense. anonym. inner class needs access to parent object, even
local variables, how could it be created outside the scope it is defined.
use static public nested class.

【在 c********k 的大作中提到】
: 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 have dynamic classes? Is it possible?
: Thanks in advance.
: Gary

m******t
发帖数: 2416
4

IMO,this problem is caused by that any non-static inner class does
not have a default constructor(i.e. an argumentless one).
They always have a constructor that takes a reference to the
enclosing instance.
There is no way you can do the thing you are trying to
do in the way you are doing it now. 8-) I think you are doing things
in Java with some COM mentality. Dynamic Proxy is probably what
you are looking for...

【在 c********k 的大作中提到】
: 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 have dynamic classes? Is it possible?
: Thanks in advance.
: Gary

1 (共1页)
进入Java版参与讨论
相关主题
对 spring 的 exception 处理方式真是不适应一道java题
问一个 java generic问题感觉inner class挺好用的
2 Questions about Constructor请问一个语法问题
继续问土问题how to copy an Object?
问题征解The right way to create new Exception
有熟悉Java Reflection的吗is it legal for the constructor of a REST resource class to throw exceptions?
Netbean duplicate class error跪求大牛指点Java,看不懂什么意思。
Answer to "Is this a Bug or not? "Re: java.lang.reflect.InvocationTargetEx
相关话题的讨论汇总
话题: reflection话题: anonymous话题: innerclass话题: exception话题: java