中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

深入Spring Boot :怎樣排查 java.lang.ArrayStoreException

2018-07-02    來源:importnew

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

java.lang.ArrayStoreException 分析

這個(gè)demo來說明怎樣排查一個(gè)spring boot 1應(yīng)用升級(jí)到spring boot 2時(shí)可能出現(xiàn)的java.lang.ArrayStoreException

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

demo里有兩個(gè)模塊,springboot1-starterspringboot2-demo。

springboot1-starter模塊里,是一個(gè)簡(jiǎn)單的HealthIndicator實(shí)現(xiàn)

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則是一個(gè)簡(jiǎn)單的spring boot2應(yīng)用,引用了springboot1-starter模塊。

把工程導(dǎo)入IDE,執(zhí)行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

下面來排查這個(gè)問題。

在IDE里,新建一個(gè)斷點(diǎn),類型是Java Exception Breakpoint(如果不清楚怎么添加,可以搜索對(duì)應(yīng)IDE的使用文檔),異常類是上面拋出來的java.lang.ArrayStoreException。

當(dāng)斷點(diǎn)起效時(shí),查看AnnotationUtils.findAnnotation(Class<?>, Class<A>, Set<Annotation>) line: 686函數(shù)的參數(shù)。

可以發(fā)現(xiàn)

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

說明是嘗試從MyHealthIndicatorAutoConfiguration里查找@Endpoint信息時(shí)出錯(cuò)的。

MyHealthIndicatorAutoConfiguration上的確沒有@Endpoint,但是為什么拋出java.lang.ArrayStoreException?

嘗試以簡(jiǎn)單例子復(fù)現(xiàn)異常

首先嘗試直接 new MyHealthIndicatorAutoConfiguration :

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

本以為會(huì)拋出異常來,但是發(fā)現(xiàn)執(zhí)行正常。

再仔細(xì)看異常棧,可以發(fā)現(xiàn)是在at java.lang.Class.getDeclaredAnnotation(Class.java:3458)拋出的異常,則再嘗試下面的代碼:

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

發(fā)現(xiàn)可以復(fù)現(xiàn)異常了:

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)

為什么會(huì)是java.lang.ArrayStoreException

再仔細(xì)看異常信息:java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy

ArrayStoreException是一個(gè)數(shù)組越界的異常,它只有一個(gè)String信息,并沒有cause

那么我們嘗試在?sun.reflect.annotation.TypeNotPresentExceptionProxy?的構(gòu)造函數(shù)里打斷點(diǎn)。

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;
    }

在斷點(diǎn)里,我們可以發(fā)現(xiàn):

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

終于真相大白了,是找不到org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration這個(gè)類。

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

仔細(xì)看下代碼,可以發(fā)現(xiàn)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<?>)里嘗試直接設(shè)置到數(shù)組里

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

而這里數(shù)組越界了,ArrayStoreException只有越界的Object的類型信息,也就是上面的

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

解決問題

發(fā)現(xiàn)是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 {

準(zhǔn)確來說是spring boot2把一些類的package改了:

spring boot 1里類名是:

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

spring boot 2里類名是:

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

總結(jié)

  • 當(dāng)類加載時(shí),并不會(huì)加載它的annotation的field所引用的Class<?>,當(dāng)調(diào)用Class.getDeclaredAnnotation(Class<A>)里才會(huì)加載

    以上面的例子來說,就是@AutoConfigureBefore(EndpointAutoConfiguration.class)里的EndpointAutoConfiguration并不會(huì)和MyHealthIndicatorAutoConfiguration一起被加載。

  • jdk內(nèi)部的解析字節(jié)碼的代碼不合理,把ClassNotFoundException異常吃掉了
  • 排查問題需要一步步深入調(diào)試

標(biāo)簽: ssd 代碼 搜索

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:JDK 源碼閱讀 : FileDescriptor

下一篇:RocketMQ 源碼學(xué)習(xí) 4 : 消息發(fā)送