diff --git a/Dockerfile b/Dockerfile index 726cc8e..00e9425 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,7 +39,6 @@ RUN jar -xvf ${ARTIFACT_JAR_PATTERN} RUN native-image \ --initialize-at-build-time=`cat ./InitializeAtBuildTime | tr '\n' ','` \ --initialize-at-run-time=`cat ./InitializeAtRunTime | tr '\n' ','` \ --H:ReflectionConfigurationResources=META-INF/native-image/github-api-native-test-custom-definitions/reflect-config.json \ --no-fallback \ --enable-https \ --install-exit-handlers \ diff --git a/pom.xml b/pom.xml index 3272e1c..f7c7c63 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,7 @@ bridge-method-injector ${bridge-method-injector.version} + diff --git a/src/main/java/github/api/nat/test/aot/GitHubRuntimeHints.java b/src/main/java/github/api/nat/test/aot/GitHubRuntimeHints.java new file mode 100644 index 0000000..69e4212 --- /dev/null +++ b/src/main/java/github/api/nat/test/aot/GitHubRuntimeHints.java @@ -0,0 +1,45 @@ +package github.api.nat.test.aot; + +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.TypeReference; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.jar.JarEntry; +import java.util.jar.JarInputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class GitHubRuntimeHints implements RuntimeHintsRegistrar { + + private static final Logger LOGGER = Logger.getLogger(GitHubRuntimeHints.class.getName()); + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator)).forEach(classpathEntry -> { + // If the classpathEntry is no jar skip it + if (!classpathEntry.endsWith(".jar")) { + return; + } + + try (JarInputStream is = new JarInputStream(new FileInputStream(classpathEntry))) { + JarEntry entry; + while ((entry = is.getNextJarEntry()) != null) { + if (entry.getName().endsWith(".class") && entry.getName().startsWith("org/kohsuke/github")) { + String githubApiClassName = entry.getName().replace("/", "."); + String githubApiClassNameWithoutClass = githubApiClassName.substring(0, githubApiClassName.length() - 6); + LOGGER.log(Level.INFO, "Registered class " + githubApiClassNameWithoutClass + " for reflections and serialization."); + hints.reflection().registerType(TypeReference.of(githubApiClassNameWithoutClass), MemberCategory.values()); + hints.serialization().registerType(TypeReference.of(githubApiClassName)); + } + } + } catch (IOException e) { + LOGGER.log(Level.INFO, "Error while reading jars", e); + } + }); + } +} diff --git a/src/main/resources/META-INF/native-image/github-api-native-test-custom-definitions/reflect-config.json b/src/main/resources/META-INF/native-image/github-api-native-test-custom-definitions/reflect-config.json deleted file mode 100644 index 91781d0..0000000 --- a/src/main/resources/META-INF/native-image/github-api-native-test-custom-definitions/reflect-config.json +++ /dev/null @@ -1,42 +0,0 @@ -[ - { - "name" : "org.kohsuke.github.GHRepository", - "allDeclaredClasses" : true, - "allDeclaredMethods": true, - "allDeclaredFields": true, - "allDeclaredConstructors": true, - "allPublicClasses" : true, - "allPublicMethods": true, - "allPublicFields": true, - "allPublicConstructors": true, - "allRecordComponents": true, - "allNestedMembers": true, - "allSigners": true, - "allPermittedSubclasses": true, - "queryAllDeclaredMethods" : true, - "queryAllDeclaredConstructors" : true, - "queryAllPublicMethods" : true, - "queryAllPublicConstructors" : true, - "unsafeAllocated": true - }, - { - "name" : "org.kohsuke.github.GHUser", - "allDeclaredClasses" : true, - "allDeclaredMethods": true, - "allDeclaredFields": true, - "allDeclaredConstructors": true, - "allPublicClasses" : true, - "allPublicMethods": true, - "allPublicFields": true, - "allPublicConstructors": true, - "allRecordComponents": true, - "allNestedMembers": true, - "allSigners": true, - "allPermittedSubclasses": true, - "queryAllDeclaredMethods" : true, - "queryAllDeclaredConstructors" : true, - "queryAllPublicMethods" : true, - "queryAllPublicConstructors" : true, - "unsafeAllocated": true - } -] \ No newline at end of file diff --git a/src/main/resources/META-INF/spring/aot.factories b/src/main/resources/META-INF/spring/aot.factories new file mode 100644 index 0000000..6690d0f --- /dev/null +++ b/src/main/resources/META-INF/spring/aot.factories @@ -0,0 +1 @@ +org.springframework.aot.hint.RuntimeHintsRegistrar=github.api.nat.test.aot.GitHubRuntimeHints \ No newline at end of file