java.lang.ArrayStoreException 分析

这个demo来说明怎样排查一个spring boot 1应用升级到spring boot 2时可能出现的java.lang.ArrayStoreException

demo地址:https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-ArrayStoreException

demo里有两个模块,springboot1-starterspringboot2-demo

springboot1-starter模块里,是一个简单的HealthIndicator实现

public class MyHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Builder builder) throws Exception {
builder.status(Status.UP);
builder.withDetail("hello", "world");
}
}
@Configuration
@AutoConfigureBefore(EndpointAutoConfiguration.class)
@AutoConfigureAfter(HealthIndicatorAutoConfiguration.class)
@ConditionalOnClass(value = { HealthIndicator.class })
public class MyHealthIndicatorAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MyHealthIndicator.class)
@ConditionalOnEnabledHealthIndicator("my")
public MyHealthIndicator myHealthIndicator() {
return new MyHealthIndicator();
}
}

springboot2-demo则是一个简单的spring boot2应用,引用了springboot1-starter模块。

把工程导入IDE,执行springboot2-demo里的ArrayStoreExceptionDemoApplication,抛出的异常是

Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724) ~[na:1.8.0_112]
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531) ~[na:1.8.0_112]
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355) ~[na:1.8.0_112]
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286) ~[na:1.8.0_112]
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_112]
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_112]
at java.lang.Class.createAnnotationData(Class.java:3521) ~[na:1.8.0_112]
at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_112]
at java.lang.Class.createAnnotationData(Class.java:3526) ~[na:1.8.0_112]
at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_112]
at java.lang.Class.getAnnotation(Class.java:3415) ~[na:1.8.0_112]
at java.lang.reflect.AnnotatedElement.isAnnotationPresent(AnnotatedElement.java:258) ~[na:1.8.0_112]
at java.lang.Class.isAnnotationPresent(Class.java:3425) ~[na:1.8.0_112]
at org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation(AnnotatedElementUtils.java:575) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.isHandler(RequestMappingHandlerMapping.java:177) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:217) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:188) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:129) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1769) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 16 common frames omitted

使用 Java Exception Breakpoint

下面来排查这个问题。

在IDE里,新建一个断点,类型是Java Exception Breakpoint(如果不清楚怎么添加,可以搜索对应IDE的使用文档),异常类是上面抛出来的java.lang.ArrayStoreException

当断点起效时,查看AnnotationUtils.findAnnotation(Class<?>, Class<A>, Set<Annotation>) line: 686 函数的参数。

可以发现

  • clazz是 class com.example.springboot1starter.MyHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$945c1f
  • annotationType是 interface org.springframework.boot.actuate.endpoint.annotation.Endpoint

说明是尝试从MyHealthIndicatorAutoConfiguration里查找@Endpoint信息时出错的。

MyHealthIndicatorAutoConfiguration上的确没有@Endpoint,但是为什么抛出java.lang.ArrayStoreException?

尝试以简单例子复现异常

首先尝试直接 new MyHealthIndicatorAutoConfiguration :

public static void main(String[] args) {
MyHealthIndicatorAutoConfiguration cc = new MyHealthIndicatorAutoConfiguration();
}

本以为会抛出异常来,但是发现执行正常。

再仔细看异常栈,可以发现是在at java.lang.Class.getDeclaredAnnotation(Class.java:3458)抛出的异常,则再尝试下面的代码:

public static void main(String[] args) {
MyHealthIndicatorAutoConfiguration.class.getDeclaredAnnotation(Endpoint.class);
}

发现可以复现异常了:

Exception in thread "main" java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:724)
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:531)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:355)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getDeclaredAnnotation(Class.java:3458)

为什么会是java.lang.ArrayStoreException

再仔细看异常信息:java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

ArrayStoreException是一个数组越界的异常,它只有一个String信息,并没有cause

那么我们尝试在 sun.reflect.annotation.TypeNotPresentExceptionProxy 的构造函数里打断点。

public class TypeNotPresentExceptionProxy extends ExceptionProxy {
private static final long serialVersionUID = 5565925172427947573L;
String typeName;
Throwable cause; public TypeNotPresentExceptionProxy(String typeName, Throwable cause) {
this.typeName = typeName;
this.cause = cause;
}

在断点里,我们可以发现:

  • typeName是 org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration
  • cause是 java.lang.ClassNotFoundException: org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration

终于真相大白了,是找不到org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration这个类。

那么它是怎么变成ArrayStoreException的呢?

仔细看下代码,可以发现AnnotationParser.parseClassValue把异常包装成为Object

//sun.reflect.annotation.AnnotationParser.parseClassValue(ByteBuffer, ConstantPool, Class<?>)
private static Object parseClassValue(ByteBuffer buf,
ConstantPool constPool,
Class<?> container) {
int classIndex = buf.getShort() & 0xFFFF;
try {
try {
String sig = constPool.getUTF8At(classIndex);
return parseSig(sig, container);
} catch (IllegalArgumentException ex) {
// support obsolete early jsr175 format class files
return constPool.getClassAt(classIndex);
}
} catch (NoClassDefFoundError e) {
return new TypeNotPresentExceptionProxy("[unknown]", e);
}
catch (TypeNotPresentException e) {
return new TypeNotPresentExceptionProxy(e.typeName(), e.getCause());
}
}

然后在sun.reflect.annotation.AnnotationParser.parseClassArray(int, ByteBuffer, ConstantPool, Class<?>)里尝试直接设置到数组里

// sun.reflect.annotation.AnnotationParser.parseClassArray(int, ByteBuffer, ConstantPool, Class<?>)
result[i] = parseClassValue(buf, constPool, container);

而这里数组越界了,ArrayStoreException只有越界的Object的类型信息,也就是上面的

java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

解决问题

发现是java.lang.ClassNotFoundException: org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,则加上@ConditionalOnClass的检查就可以了:

@Configuration
@AutoConfigureBefore(EndpointAutoConfiguration.class)
@AutoConfigureAfter(HealthIndicatorAutoConfiguration.class)
@ConditionalOnClass(value = {HealthIndicator.class, EndpointAutoConfiguration.class})
public class MyHealthIndicatorAutoConfiguration {

准确来说是spring boot2把一些类的package改了:

spring boot 1里类名是:

  • org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration

spring boot 2里类名是:

  • org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration

总结

  • 当类加载时,并不会加载它的annotation的field所引用的Class<?>,当调用Class.getDeclaredAnnotation(Class<A>)里才会加载

    以上面的例子来说,就是@AutoConfigureBefore(EndpointAutoConfiguration.class)里的EndpointAutoConfiguration并不会和MyHealthIndicatorAutoConfiguration一起被加载。

  • jdk内部的解析字节码的代码不合理,把ClassNotFoundException异常吃掉了
  • 排查问题需要一步步深入调试

深入Spring Boot: 怎样排查 java.lang.ArrayStoreException的更多相关文章

  1. eclipse spring boot 项目出现java.lang.ClassCastException 解决方法

    问题 eclipse spring boot 项目出现java.lang.ClassCastException 解决方法: 重新生成项目

  2. 深入分析Spring Boot2,解决 java.lang.ArrayStoreException异常

    将某个项目从Spring Boot1升级Spring Boot2之后出现如下报错,查了很多不同的解决方法都没有解决: Spring boot2项目启动时遇到了异常: java.lang.ArraySt ...

  3. spring boot 启动报 java.lang.NoClassDefFoundError: ch/qos/logback/core/spi/LifeCycle 错误

    Failed to instantiate SLF4J LoggerFactory Reported exception: java.lang.NoClassDefFoundError: ch/qos ...

  4. Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long错误

    Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be ...

  5. 11.java.lang.ArrayStoreException

    java.lang.ArrayStoreException 数组存储异常 当试图将类型不兼容类型的对象存入一个Object[]数组时将引发异常 Object[] obj = new String[3] ...

  6. java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

    java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.an ...

  7. 排查java.lang.OutOfMemoryError: GC overhead limit exceeded

    帮助客户排查java.lang.OutOfMemoryError: GC overhead limit exceeded错误记录: 具体网址: https://support.oracle.com/e ...

  8. Spring Boot JPA中java 8 的应用

    文章目录 Optional Stream API CompletableFuture Spring Boot JPA中java 8 的应用 上篇文章中我们讲到了如何在Spring Boot中使用JPA ...

  9. 使用Spring Boot来加速Java web项目的开发

    我想,现在企业级的Java web项目应该或多或少都会使用到Spring框架的. 回首我们以前使用Spring框架的时候,我们需要首先在(如果你使用Maven的话)pom文件中增加对相关的的依赖(使用 ...

随机推荐

  1. Paper Reviews and Presentations

    Copied from Advanced Computer Networks, Johns Hopkins University. Paper Reviews and Presentations Ea ...

  2. HDU 4302 Holedox Eating(multiset)

    http://acm.hdu.edu.cn/showproblem.php?pid=4302 题意: 在一条直线上,会有多条命令,如果是0,那么就会在x位置处出现一个蛋糕,如果是1,某人就会找到最近的 ...

  3. Java中泛型Class<T>、T与Class<?>、 Object类和Class类、 object.getClass()和Object.class

    一.区别 单独的T 代表一个类型(表现形式是一个类名而已) ,而 Class<T>代表这个类型所对应的类(又可以称做类实例.类类型.字节码文件), Class<?>表示类型不确 ...

  4. _itemmod_stat

    制作几种基础模板,用模板快速生成装备 `comment` 备注 `entry`目标装备 entry `src_entry` 模板装备entry `stat_muilt` 属性倍率 `mindmg_mu ...

  5. Codeforces 746 G. New Roads

    题目链接:http://codeforces.com/contest/746/problem/G mamaya,不知道YY了一个什么做法就这样过去了啊 2333 首先我显然可以随便构造出一棵树满足他所 ...

  6. Missing artifact com.oracle:ojdbc6:jar:10.2.0.4.0问题解决 ojdbc包pom.xml出错

    遇到的问题:ojdbc.jar包出错 原因:因为oracle的ojdbc.jar是收费的,所以maven的中央仓库中没有这个资源,只能通过配置本地库才能加载到项目中去. 解决办法: (前提是安装好了m ...

  7. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

  8. x1c2017 8G版 win linux的取舍纠结记录

    x1c 2017 的 i5 7200U 8G ram 的丐版.换了1T SSD.其实一般使用没啥问题. 1 外出携带的轻便性太满意(mac13寸相比之下都太重了): 2 coding时候的安静性,比原 ...

  9. 从flask视角理解angular(二)Blueprint VS Component

    Component类似flask app下面的每个blueprint. import 'rxjs/add/operator/switchMap'; import { Component, OnInit ...

  10. jquery将表单序列化json对象

    $.fn.serializeObject = function () { var obj = {}; var count = 0; $.each(this.serializeArray(), func ...