简体   繁体   English

Java在公共抽象超类上反映了非法访问异常

[英]Java reflect illegalAccessException on public abstract super class

I have been given an abstract class: 我得到了一个抽象类:

public abstract FooPACImpl {
    ...
}

And two autogenerated subclasses: 和两个自动生成的子类:

public replyTypeFooPAC extends FooPACImpl {
    ...
}
public requestTypeFooPAC extends FooPACImpl {
    ...
}

And an API for creating a network pattern (request/reply) that uses a request Type and a reply Type (A and B are expected to be auto-generated sub-classes of FooPACImpl like the above): 还有一个用于创建使用请求类型和答复类型(期望A和B是FooPACImpl的自动生成的子类,如上所述)的网络模式(请求/答复)的API:

Replier<A, B> get_simple_replier (
    String topic, 
    Object<A> requestType, 
    Object<B> replyType) {...}

Requester<A, B> get_simple_requester (
    String topic, 
    Object<A> requestType, 
    Object<B> replyType) {...}

I pull the topic string, and the string names of the two types A and B from an XML stub: 我从XML存根中提取主题字符串以及A和B这两种类型的字符串名称:

<rr>
    <topic>MyReqRepTopic</topic>
    <reqtype>foo.bar.baz.myReqTypeFooPAC</reqtype>
    <replytype>foo.bar.baz.myReplyTypeFooPAC</replytype>
</rr>

Apparently, reflection in Java can't handle directly a "public abstract" super class? 显然,Java中的反射不能直接处理“公共抽象”超类?

FooPACImpl foopa = (FooPACImpl) Class.forName(reqtypeName).newInstance();

results in 结果是

java.lang.IllegalAccessException: Class foo.bar.fnorb.ServiceSupport \
    can not access a member of class foo.bar.baz.myReqTypeFooPAC with \
    modifiers "protected"

1) Why "with modifiers 'protected'"? 1)为什么“用修饰语'保护'”? FooPACImpl.getClass().getModifiers returns "public abstract" FooPACImpl.getClass()。getModifiers返回“公共摘要”

2) Is this exception thrown because the class is abstract? 2)是否因为类是抽象的而抛出此异常? or does it have to do with a possible constructor 'protected FooPACImpl () { };'? 还是与可能的构造函数“ protected FooPACImpl(){};”有关?

3) And, is this even achievable? 3)而且,这甚至可以实现吗? My understanding is that if you subclass AA with BB, you can use AA where a BB is expected, since BB will have anything that AA is expected to have (methods, fields, etc). 我的理解是,如果您将BB与AA一起使用,则可以在需要BB的地方使用AA,因为BB将具有AA希望具有的任何东西(方法,字段等)。 Although, it's been awhile. 虽然,已经有一段时间了。 I may be a bit hazy on the particulars of inheritance in java. 我可能对Java继承的细节有些困惑。

Thanks for any points/pointers, and a yes/no answer for #3 is sufficient. 感谢您提出的任何要点/指标,对#3的是/否答案就足够了。 In the case of a 'no', hints to how it could be implemented without reflection. 在“否”的情况下,提示如何无需反思即可实现。 I do have access to the code generator templates for the autogenerated stuff, if that helps. 如果有帮助,我可以访问自动生成的东西的代码生成器模板。

In the auto-generated sub-classes of FooPACImpl, there were two protected constructors: 在FooPACImpl的自动生成的子类中,有两个受保护的构造函数:

protected myReqTypeFooPAC() {
    super(...);
}

protected myReqTypeFooPAC(boolean b) {
    super(..., b, ...); 
}

I was looking for the problem in the parent class, but it turned out to be the constructor thing. 我在父类中寻找问题,但事实证明这是构造函数。 Changing those to public allowed the code to run as expected, which I can do either using reflection? 将这些更改为公开状态可使代码按预期运行,我可以使用反射来做到这一点吗? or by changing the codegen templates... 或通过更改代码生成模板...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM