阿里巴巴 fastjson-1.2.12.jar json解析异常java.lang.ClassFormatError: Invalid method Code length 66865 in class file com/alibaba/fastjson/serializer/ASMSerializer_6_UserKdlb

承接上篇:fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text ‘2019-05-24 13:52:11‘ could not be parsed at index 10

之前在线上用的版本是fastjson-1.2.7.jar 一切正常,更换以后时间解析看似一切正常。

因为在系统中设计json反序列化的地方比较多,刚刚放到生产环境,app那边的接口报错了

 java.lang.ClassFormatError: Invalid method Code length 66865 in class file com/alibaba/fastjson/serializer/ASMSerializer_6_UserKdlb
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at com.alibaba.fastjson.util.ASMClassLoader.defineClassPublic(ASMClassLoader.java:174)
    at com.alibaba.fastjson.serializer.ASMSerializerFactory.createJavaBeanSerializer(ASMSerializerFactory.java:396)
    at com.alibaba.fastjson.serializer.SerializeConfig.createASMSerializer(SerializeConfig.java:95)
    at com.alibaba.fastjson.serializer.SerializeConfig.createJavaBeanSerializer(SerializeConfig.java:163)
    at com.alibaba.fastjson.serializer.SerializeConfig.createJavaBeanSerializer(SerializeConfig.java:117)
    at com.alibaba.fastjson.serializer.SerializeConfig.getObjectWriter(SerializeConfig.java:504)
    at com.alibaba.fastjson.serializer.SerializeConfig.getObjectWriter(SerializeConfig.java:320)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:884)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:812)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:853)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:812)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:840)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:812)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:892)
    at com.alibaba.fastjson.JSON.toJSON(JSON.java:812)
    at com.netmarch.pointlocationapp.controller.AppPointLocationControllerNew.getList(AppPointLocationControllerNew.java:204)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

找到相应的方法,签名如下

@RequestMapping("/list")
    @ResponseBody
    public JSONObject getList(@RequestBody String body, UserKdlb userKdlb, Pager<UserKdlb> pagers,
            HttpServletRequest request, HttpServletResponse response) {
        AppUtils.setHeader(request, response);
        JSONObject obj = null;
        ReturnParam rp = new ReturnParam();
...
               obj = (JSONObject) JSON.toJSON(rp);
return obj;
}

这是一直运行的代码,按常理来说如果有bug早就反应出来了。

codereview发现,这个json对象之前的步骤中插入一个list,相关代码如下

List<UserKdlb> list = null;

public class UserKdlb implements Serializable{

    private Byte pre_distribution_work_for_outsourcing_companies;//建设外包公司预分配工作
    private String company_employees_must_complete_the_commissioning_time;//公司员工调试必须完成时间

    //室外周报关联查询列表添加页面显示字段
    private Integer number_of_points_entered_into_outdoor_construction_sites;

    //室外点位资料字表对应字段;
    private List<PointLocationFaultRecord> ltPLFR;

    //管理合同表查询此字段进而在页面显示样式,点位表f_htbh 存的可能是合同关联字段,也可能是项目关联字段,所以要查询两次;
    private String whxfxq;

    //关联点位子记录最新一条添加记录显示列
    private String estimated_date_of_repair;//预计修复日期
    private String dname;//同一故障点位名称
    private String application_stop_instruction;//申请停用说明
    private String son_estimated_date_of_repair;//子记录最新预计修复日期
    private String son_application_stop_instruction;//子记录申请停用说明
    //最新连接时间
    private String ping_time;

    private int prediction_and_debugging_amount;//(预-调试完)总量
    private int debugging_completion_difficulty;//调试完成难度
    private int count;//项目数量
    private int sum;//项目和

    private int count_percent;
    private float total_commissioning_percent;
    private float debugging_completion_difficulty_percent;
    private float prediction_and_debugging_amount_percent;
    private float prediction_and_debugging_difficulty_percent;

    private Float projectPoints;//项目金额
    private Integer projectCount;//项目个数

网上的说法是方法名太长,超过了65535(2^16-1,64K),里面有很多参数名比较长,可能在生成Getter/Setter时候过长,报错了。

由于记录这篇博文的时间比较仓促,笔者没有时间去深究java中方法(method)相关的结构信息,各位看官可以去了解一下

https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.6

https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.3

经过在网上一番搜索,说是非java编译生成的类,在运行时被进行检测,防止有恶意代码被注入。

Start-up time, I‘d say. Verification that classes are correct takes some time when the class is loaded. Since classes might be loaded in a lazy fashion (not on app start, but when being used for the first time), this might cause unexpected and undesired runtime delays.

Actually the class does not need to be checked in general. The compiler will not emit any invalid bytecode or class construct. The reason for verification is that the class may be build on one system, get hosted online and is transmitted to you through the unprotected internet. On this path, a malicious attacker might modify the bytecode and create something the compiler might never create; something that can crash the JVM or possibly circumvents security restrictions. Thus the class is verified before it is used. If this is a local application, there is usually no need to check the bytecode again.

JVM加载class文件时会做字节码校验(bytecode verification)。这篇文档非常详细地讲了JVM会做哪些校验。 如果你的class文件是由java源文件通过javac编译出来的,那么基本上不用担心bytecode verification。 如果class文件是由asm、cglib等动态生成出来的或者由其它编译器生成的,那么JVM在校验它的bytecode时就有可能失败。 失败的原因可能是你生成的bytecode有bug,也可能是由于新版本的JVM加入了新的验证条件后导致原来可以通过验证的bytecode现在不能通过了。

很多Java框架都会动态生成class文件,再加上JVM版本也会时不时地修改它的bytecode verification行为。 所以,运行代码时偶尔会遇到java.lang.VerifyError错误。 在不能修改框架代码或者切换JVM实现的情况下,JVM提供了一些选项可以让你改变或者绕过bytecode verification。

-XX:-UseSplitVerifier

-XX:-UseSplitVerifier可以让JVM不开启“type-checking verifier”。 这样就不强制要求class文件含有StackMapTable(所有Java 7 version 51之后的class文件默认要求含有StackMapTable)。

-noverify

-noverify选项可以关闭bytecode verification。

有的观点认为某些bytecode verification除了给动态生成bytecode增加麻烦之外,并没有什么大用。 但是这篇文章强烈建议不要关闭bytecode verification,特别是在生产环境里。 因为bytecode verification可以检测到恶意代码或者代码中的bug。 特别是代码中的bug,因为没有人可以保证(动态产生的)字节码是百分百bug free的。

回到我的问题,由于PactProviderRule类来自于外部依赖,CI上的JVM也不能替换 (CI上用的是非oracle的JVM实现,该测试代码在本地的官方JVM上是可以运行通过,所以CI上的失败有一定可能是其用的JVM实现的原因), 所以一个简单的方法是在maven跑测试代码时,给JVM加上-noverify选项。

按照以上方法给jvm加了参数 -noverify  -XX:-UseSplitVerifier启动后并未凑效也没解决问题。

最终放弃添加jvm参数,按照官方的bug修复跟踪,替换成了1.2.13版本了,修复代码在167行,至此,线上正常接口运行了。

相关参考:

Ensuring JVM method size limit is never exceeded #661

一个fastjson转换JSON字符串的报错排查

Use of -noverify when launching java apps

原文地址:https://www.cnblogs.com/passedbylove/p/11044611.html

时间: 2024-08-07 17:12:52

阿里巴巴 fastjson-1.2.12.jar json解析异常java.lang.ClassFormatError: Invalid method Code length 66865 in class file com/alibaba/fastjson/serializer/ASMSerializer_6_UserKdlb的相关文章

【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)

原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添加进工作的build path.3.(关键的一步)将lib设为源文件夹.如果不设置,则程序编译可以通过,但运行的时候,会报: java.lang.NoClassDefFoundError # re: Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundErro

Intellij打包jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attrib

开始使用如下代码进行打包 <build> <!-- mvn assembly:assembly -Dmaven.test.skip=true --> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <appendAsse

json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不给我们报错,很郁闷, 特别是ie, 有些问题, 得借助FireFox的返回结果分析. 当然, FireFox有时也没报错. 异常栈: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException at net.sf.js

使用json报错java.lang.NoClassDefFoundError: nu/xom/ParentNode

解决方案: 引入xom包; eg: <dependency> <groupId>xom</groupId> <artifactId>xom</artifactId> <version>1.2.5</version></dependency>

用 org.json 解析JSON

1.下载 java-json.jar 包 介绍:http://www.json.org/ 下载: https://github.com/stleary/JSON-java/releases https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22 附件:java-json.jar.zip 附件:json-20160810-javadoc.jar 2.解析 JSON  代码如下: 1

Spring Boot默认的JSON解析框架设置

方案一:启动类继承WebMvcConfigurerAdapter,覆盖方法configureMessageConverters ... @SpringBootApplication public class UserApplication extends WebMvcConfigurerAdapter{ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters)

【Android开发经验】比Gson解析速度快10倍!——Json解析神器Jackson使用介绍

转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在前面的两篇文章中,我们介绍了Json数据格式和系统自带Json以及Google的Gson项目,如果能学会这些东西,基本能满足工作需求了.但是,程序员都有追求极致效率的嗜好,在满足了基本需求之后,我们会考虑能不能再优化一下效率呢?当然!今天这篇文章要介绍的,就是在数据量比较大的时候,比Gson的解析效率高近10倍的Json数据解析框架- -Jackson! 下面是一个大神关于几个常见的Json数据的解析速

安卓 json解析如何使用与讲解

JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换.JSON采用兼容性很高的文本格式,同时也具备类似于C语言体系的行为– Json.org JSON Vs XML 1.JSON和XML的数据可读性基本相同 2.JSON和XML同样拥有丰富的解析手段 3.JSON相对于XML来讲,数据的体积小 4.JSON与JavaScript的交互更加方便 5

java lang IllegalArgumentException MALFORMED jar解析中文报错问题

二月 13, 2015 11:16:51 上午 org.apache.catalina.startup.TldConfig tldScanJarSEVERE: Exception processing JAR at resource path E:\Program Files\apache-tomcat-6.0.20\webapps\jp-center\WEB-INF\lib\jp-page.jar in context /jp-centerjava.lang.IllegalArgumentEx