|
| 1 | +package github.api.nat.test.aot; |
| 2 | + |
| 3 | +import org.springframework.aot.hint.MemberCategory; |
| 4 | +import org.springframework.aot.hint.RuntimeHints; |
| 5 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 6 | +import org.springframework.aot.hint.TypeReference; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.jar.JarEntry; |
| 13 | +import java.util.jar.JarInputStream; |
| 14 | +import java.util.logging.Level; |
| 15 | +import java.util.logging.Logger; |
| 16 | + |
| 17 | +public class GitHubRuntimeHints implements RuntimeHintsRegistrar { |
| 18 | + |
| 19 | + private static final Logger LOGGER = Logger.getLogger(GitHubRuntimeHints.class.getName()); |
| 20 | + |
| 21 | + @Override |
| 22 | + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { |
| 23 | + Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator)).forEach(classpathEntry -> { |
| 24 | + // If the classpathEntry is no jar skip it |
| 25 | + if (!classpathEntry.endsWith(".jar")) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + try (JarInputStream is = new JarInputStream(new FileInputStream(classpathEntry))) { |
| 30 | + JarEntry entry; |
| 31 | + while ((entry = is.getNextJarEntry()) != null) { |
| 32 | + if (entry.getName().endsWith(".class") && entry.getName().startsWith("org/kohsuke/github")) { |
| 33 | + String githubApiClassName = entry.getName().replace("/", "."); |
| 34 | + String githubApiClassNameWithoutClass = githubApiClassName.substring(0, githubApiClassName.length() - 6); |
| 35 | + LOGGER.log(Level.INFO, "Registered class " + githubApiClassNameWithoutClass + " for reflections and serialization."); |
| 36 | + hints.reflection().registerType(TypeReference.of(githubApiClassNameWithoutClass), MemberCategory.values()); |
| 37 | + hints.serialization().registerType(TypeReference.of(githubApiClassName)); |
| 38 | + } |
| 39 | + } |
| 40 | + } catch (IOException e) { |
| 41 | + LOGGER.log(Level.INFO, "Error while reading jars", e); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | +} |
0 commit comments