(點選上方公眾號,可快速關註)
來源:hengyunabc,
blog.csdn.net/hengyunabc/article/details/79475505
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-starter和springboot2-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, Set
可以發現
-
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)裡才會載入
以上面的例子來說,就是@AutoConfigureBefore(EndpointAutoConfiguration.class)裡的EndpointAutoConfiguration並不會和MyHealthIndicatorAutoConfiguration一起被載入。
-
jdk內部的解析位元組碼的程式碼不合理,把ClassNotFoundException異常吃掉了
-
排查問題需要一步步深入除錯
看完本文有收穫?請轉發分享給更多人
關註「ImportNew」,提升Java技能