Skip to content
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

Cannot construct instance of java.nio.file.Path with GraalVM Native Image #3725

Closed
tisonkun opened this issue Jan 9, 2023 · 8 comments
Closed
Labels
to-evaluate Issue that has been received but not yet evaluated

Comments

@tisonkun
Copy link

tisonkun commented Jan 9, 2023

Reproduce

https://github.com/tisonkun/hawkeye/tree/repro-jackson-native

# Use GraalVM 22.3.0
./mvnw clean package -DskipTests -Pnative
./distribution/native/target/hawkeye-native check

... throws:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.nio.file.Path` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: UNKNOWN; byte offset: #UNKNOWN] (through reference chain: io.korandoru.hawkeye.core.HawkEyeConfig$HawkEyeConfigBuilder["baseDir"])
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1909)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:408)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1354)
	at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:274)
	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeSetAndReturn(MethodProperty.java:158)
	at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.vanillaDeserialize(BuilderBasedDeserializer.java:293)
	at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserialize(BuilderBasedDeserializer.java:217)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3542)
	at io.korandoru.hawkeye.core.HawkEyeConfig.of(HawkEyeConfig.java:65)
	at io.korandoru.hawkeye.command.HawkEyeCommandCheck.call(HawkEyeCommandCheck.java:42)
	at io.korandoru.hawkeye.command.HawkEyeCommandCheck.call(HawkEyeCommandCheck.java:28)
	at picocli.CommandLine.executeUserObject(CommandLine.java:1953)
	at picocli.CommandLine.access$1300(CommandLine.java:145)
	at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2358)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2352)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2314)
	at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179)
	at picocli.CommandLine$RunLast.execute(CommandLine.java:2316)
	at picocli.CommandLine.execute(CommandLine.java:2078)
	at io.korandoru.hawkeye.command.HawkEyeCommandMain.main(HawkEyeCommandMain.java:35)

picocli provides picocli-codegen to generate Native Image configs file. I wonder whether jackson ecosystem has a similar solution or how I can properly config it.

It seems that NioPathDeserializer isn't contained in the final image due to inaccurate dead code analysis.

Ref: korandoru/hawkeye#18

@tisonkun tisonkun added the to-evaluate Issue that has been received but not yet evaluated label Jan 9, 2023
@tisonkun
Copy link
Author

Manually add Java7HandlersImpl in reflect-config fix the issue: korandoru/hawkeye#37

But still I expect an automatic tool like picocli-codegen to analyze and generate such a config file for me.

@cowtowncoder
Copy link
Member

I think this is due to dynamic loading of support for java.nio.file.Path, originally to allow running Jackson on Java 6 (so Java 7 types were dynamically loaded), but later to keep Android compatibility.

Potentially #3708 merged in 2.15 might help here, possibly solving the problem.
@tisonkun Would it be possible for you to try 2.15.0-SNAPSHOT to see if it might work better?

@tisonkun

This comment was marked as outdated.

@tisonkun

This comment was marked as outdated.

@tisonkun
Copy link
Author

@cowtowncoder It helps.

I try with a commit:

diff --git a/hawkeye-core/pom.xml b/hawkeye-core/pom.xml
index 79c1c3d..f2337a9 100644
--- a/hawkeye-core/pom.xml
+++ b/hawkeye-core/pom.xml
@@ -32,6 +32,16 @@
         <dependency>
             <groupId>com.fasterxml.jackson.dataformat</groupId>
             <artifactId>jackson-dataformat-toml</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>com.fasterxml.jackson.core</groupId>
+                    <artifactId>jackson-databind</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
diff --git a/hawkeye-core/src/main/resources/META-INF/native-image/hawkeye-core/reflect-config.json b/hawkeye-core/src/main/resources/META-INF/native-imag
e/hawkeye-core/reflect-config.json
index 0347aee..3139dc3 100644
--- a/hawkeye-core/src/main/resources/META-INF/native-image/hawkeye-core/reflect-config.json
+++ b/hawkeye-core/src/main/resources/META-INF/native-image/hawkeye-core/reflect-config.json
@@ -6,13 +6,5 @@
     "allDeclaredMethods": true,
     "allPublicMethods": true,
     "allDeclaredFields": true
-  },
-  {
-    "name": "com.fasterxml.jackson.databind.ext.Java7HandlersImpl",
-    "allDeclaredConstructors": true,
-    "allPublicConstructors": true,
-    "allDeclaredMethods": true,
-    "allPublicMethods": true,
-    "allDeclaredFields": true
   }
 ]
diff --git a/pom.xml b/pom.xml
index bcd8758..b014cf2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,6 +31,15 @@
 
     <url>https://github.com/korandoru/hawkeye</url>
 
+    <repositories>
+        <repository>
+            <id>snapshots-repo</id>
+            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
+            <releases><enabled>false</enabled></releases>
+            <snapshots><enabled>true</enabled></snapshots>
+        </repository>
+    </repositories>
+
     <licenses>
         <license>
             <name>Apache 2.0 License</name>
@@ -107,6 +116,11 @@
                 <artifactId>jackson-dataformat-toml</artifactId>
                 <version>${jackson-dataformat-toml.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.fasterxml.jackson.core</groupId>
+                <artifactId>jackson-databind</artifactId>
+                <version>2.15.0-SNAPSHOT</version>
+            </dependency>
             <dependency>
                 <groupId>org.slf4j</groupId>
                 <artifactId>slf4j-api</artifactId>

@tisonkun
Copy link
Author

How can I get notified when 2.15.0 released?

@cowtowncoder
Copy link
Member

One way is to follow @FasterXML on Twitter; another is

https://groups.google.com/g/jackson-announce

and of course there is Release page itself:

https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.15

where announcement is sent. It will take a while for 2.15 to get released; I hope we can get release candidates out in maybe March 2023.

@tisonkun
Copy link
Author

FYI - verified and upgraded korandoru/hawkeye#83

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants