我正在研究jibx以将xml文件转换为java对象,一切正常,直到遇到IllegalAccessError异常。我花了很多时间,最后我发现它是由适当的最终财产引起的。一个简单的演示如下所示:
//xml
<type name="test"></type>
//binding
<binding>
<mapping name="type" class="demo.MyType" value-style="attribute">
<value name="name" field="name" usage="required" />
</mapping>
</binding>
//java class
public class MyType{
// only protected final will cause the error, private final works well.
protected final name;
public MyType(){
name = "demo";
}
}
// convert method
IBindingFactory bindingFactory = BindingDirectory.getFactory(MyType.class);
IUnmarshallingContext uctx =
bindingFactory.createUnmarshallingContext();
Manifest manifest = (Manifest) uctx.unmarshalDocument(new
FileInputStream("demo.xml"), null);
我知道上面的演示中受保护的final属性是没有意义的,但是在我的实际情况下,代码是由其他人编写的,并且受保护的final属性在超类中。
我只想知道为什么jibx仅对受保护的最终属性抛出IllegalAccessError?
详细错误堆栈如下所示(我的环境中的错误,而不是上述演示的错误):
java.lang.IllegalAccessError: null
at self.jason.study.cartridge.api.JiBX_cartridge_demoMungeAdapter.JiBX_cartridge_demo_unmarshalAttr_1_0() ~[classes/:na]
at self.jason.study.cartridge.api.JiBX_cartridge_demoManifest_access13.unmarshal() ~[classes/:na]
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement(UnmarshallingContext.java:2757) ~[jibx-run-1.3.1.jar:na]
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument(UnmarshallingContext.java:2900) ~[jibx-run-1.3.1.jar:na]
at self.jason.study.cartridge.CartridgeApplication.lambda$0(CartridgeApplication.java:24) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at self.jason.study.cartridge.CartridgeApplication.main(CartridgeApplication.java:31) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_73]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_73]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_73]
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_73]
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
at java.lang.reflect.Method.invoke (Unknown Source)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
at java.lang.Thread.run (Unknown Source)
Caused by: java.lang.IllegalAccessError
at self.jason.study.cartridge.api.JiBX_cartridge_demoMungeAdapter.JiBX_cartridge_demo_unmarshalAttr_1_0 ()
at self.jason.study.cartridge.api.JiBX_cartridge_demoManifest_access13.unmarshal ()
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalElement (UnmarshallingContext.java:2757)
at org.jibx.runtime.impl.UnmarshallingContext.unmarshalDocument (UnmarshallingContext.java:2900)
at self.jason.study.cartridge.CartridgeApplication.lambda$0 (CartridgeApplication.java:24)
at org.springframework.boot.SpringApplication.callRunner (SpringApplication.java:797)
at org.springframework.boot.SpringApplication.callRunners (SpringApplication.java:781)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:335)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1255)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1243)
at self.jason.study.cartridge.CartridgeApplication.main (CartridgeApplication.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
at java.lang.reflect.Method.invoke (Unknown Source)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:496)
at java.lang.Thread.run (Unknown Source)