Skip to content

Commit

Permalink
fix: github-api in native image
Browse files Browse the repository at this point in the history
  • Loading branch information
klopfdreh committed Aug 19, 2024
1 parent d957af9 commit 8c50f6c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 43 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<artifactId>bridge-method-injector</artifactId>
<version>${bridge-method-injector.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/github/api/nat/test/aot/GitHubRuntimeHints.java
Original file line number Diff line number Diff line change
@@ -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);
}
});
}
}

This file was deleted.

1 change: 1 addition & 0 deletions src/main/resources/META-INF/spring/aot.factories
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=github.api.nat.test.aot.GitHubRuntimeHints

0 comments on commit 8c50f6c

Please sign in to comment.