Skip to content

NullPointerException in BindingReflectionHintsRegistrar for anonymous classes #29657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
christophstrobl opened this issue Dec 7, 2022 · 1 comment
Assignees
Labels
theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Milestone

Comments

@christophstrobl
Copy link
Member

The shouldSkipMembers method uses type.getCanonicalName(), which may return null for anonymous, local and other types leading to NPE when calling startsWith.

The above happened when reading Java8 compiled code of com.mongodb.connection.ServerType.
Retrieving constructor arguments and registering them for binding failed because a synthetic constructor uses an anonymous class (com.mongodb.connection.ServerType$1).

synthetic ServerType(java.lang.String arg0, int arg1, com.mongodb.connection.ServerType$1 arg2) { // <init> //(Ljava/lang/String;ILcom/mongodb/connection/ServerType$1;)V
    <localVar:index=0 , name=this , desc=Lcom/mongodb/connection/ServerType;, sig=null, start=L0, end=L1>
    <localVar:index=1 , name=x0 , desc=Ljava/lang/String;, sig=null, start=L0, end=L1>
    <localVar:index=2 , name=x1 , desc=I, sig=null, start=L0, end=L1>
    <localVar:index=3 , name=x2 , desc=Lcom/mongodb/connection/ServerType$1;, sig=null, start=L0, end=L1>

    L0 {
        aload 0 // reference to self
        aload 1 // reference to arg0
        iload 2 // reference to arg1
        invokespecial com/mongodb/connection/ServerType.<init>(Ljava/lang/String;I)V
        return
    }
    L1 {
    }
}
ReflectionHints hints = new ReflectionHints();
BindingReflectionHintsRegistrar reflectionHintsRegistrar = new BindingReflectionHintsRegistrar();
Arrays.stream(com.mongodb.connection.ServerType.class.getDeclaredConstructors()).forEach(ctor -> {
	Arrays.stream(ctor.getParameterTypes()).forEach(type -> {
		reflectionHintsRegistrar.registerReflectionHints(hints, type);
	});
});
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.startsWith(String)" because the return value of "java.lang.Class.getCanonicalName()" is null
	at org.springframework.aot.hint.BindingReflectionHintsRegistrar.shouldSkipMembers(BindingReflectionHintsRegistrar.java:74)
	at org.springframework.aot.hint.BindingReflectionHintsRegistrar.lambda$registerReflectionHints$0(BindingReflectionHintsRegistrar.java:87)
	at org.springframework.aot.hint.ReflectionHints.registerType(ReflectionHints.java:86)
	at org.springframework.aot.hint.ReflectionHints.registerType(ReflectionHints.java:109)
	at org.springframework.aot.hint.BindingReflectionHintsRegistrar.registerReflectionHints(BindingReflectionHintsRegistrar.java:86)
	at org.springframework.aot.hint.BindingReflectionHintsRegistrar.registerReflectionHints(BindingReflectionHintsRegistrar.java:65)

related: spring-projects/spring-data-mongodb#4248

@bclozel bclozel self-assigned this Dec 7, 2022
@bclozel bclozel added type: bug A general bug theme: aot An issue related to Ahead-of-time processing labels Dec 7, 2022
@bclozel bclozel added this to the 6.0.3 milestone Dec 7, 2022
@bclozel bclozel changed the title Potential NPE in BindingReflectionHintsRegistrar#shouldSkipMembers(...) NullPointerException in BindingReflectionHintsRegistrar for anonymous classes Dec 7, 2022
@bclozel bclozel closed this as completed in d601f31 Dec 7, 2022
@bclozel
Copy link
Member

bclozel commented Dec 7, 2022

Thanks @christophstrobl for the analysis!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants