Skip to content

Commit

Permalink
Move native Proxy configuration from Feature to json
Browse files Browse the repository at this point in the history
Follow up to quarkusio#29886
  • Loading branch information
zakkak authored and Sanne committed Feb 16, 2023
1 parent 6f9c072 commit 058e24e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.JPMSExportBuildItem;
import io.quarkus.deployment.builditem.nativeimage.JniRuntimeAccessBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem;
Expand Down Expand Up @@ -81,7 +80,6 @@ public class NativeImageFeatureStep {
static final String LEGACY_JNI_RUNTIME_ACCESS = "com.oracle.svm.core.jni.JNIRuntimeAccess";
static final String JNI_RUNTIME_ACCESS = "org.graalvm.nativeimage.hosted.RuntimeJNIAccess";
static final String BEFORE_ANALYSIS_ACCESS = Feature.BeforeAnalysisAccess.class.getName();
static final String DYNAMIC_PROXY_REGISTRY = "com.oracle.svm.core.jdk.proxy.DynamicProxyRegistry";
static final String LOCALIZATION_FEATURE = "com.oracle.svm.core.jdk.localization.LocalizationFeature";
static final String RUNTIME_RESOURCE_SUPPORT = "org.graalvm.nativeimage.impl.RuntimeResourceSupport";

Expand Down Expand Up @@ -120,7 +118,6 @@ void generateFeature(BuildProducer<GeneratedNativeImageClassBuildItem> nativeIma
List<RuntimeInitializedClassBuildItem> runtimeInitializedClassBuildItems,
List<RuntimeInitializedPackageBuildItem> runtimeInitializedPackageBuildItems,
List<RuntimeReinitializedClassBuildItem> runtimeReinitializedClassBuildItems,
List<NativeImageProxyDefinitionBuildItem> proxies,
List<NativeImageResourcePatternsBuildItem> resourcePatterns,
List<NativeImageResourceBundleBuildItem> resourceBundles,
List<ServiceProviderBuildItem> serviceProviderBuildItems,
Expand Down Expand Up @@ -242,53 +239,6 @@ public void write(String s, byte[] bytes) {
overallCatch.invokeStaticMethod(runtimeReinitializedClasses.getMethodDescriptor());
}

if (!proxies.isEmpty()) {
MethodCreator registerProxies = file
.getMethodCreator("registerProxies", void.class)
.setModifiers(Modifier.PRIVATE | Modifier.STATIC);

// Needed to access DYNAMIC_PROXY_REGISTRY in GraalVM 22.2
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk.proxy",
null, GraalVM.Version.VERSION_22_3_0));

ResultHandle versionCompareto22_3Result = registerProxies.invokeVirtualMethod(VERSION_COMPARE_TO,
registerProxies.invokeStaticMethod(VERSION_CURRENT),
registerProxies.marshalAsArray(int.class, registerProxies.load(22), registerProxies.load(3)));

for (NativeImageProxyDefinitionBuildItem proxy : proxies) {
ResultHandle array = registerProxies.newArray(Class.class, registerProxies.load(proxy.getClasses().size()));
int i = 0;
for (String p : proxy.getClasses()) {
ResultHandle clazz = registerProxies.invokeStaticMethod(
ofMethod(Class.class, "forName", Class.class, String.class), registerProxies.load(p));
registerProxies.writeArrayValue(array, i++, clazz);

}

BranchResult graalVm22_3Test = registerProxies.ifGreaterEqualZero(versionCompareto22_3Result);
/* GraalVM >= 22.3 */
try (BytecodeCreator greaterThan22_2 = graalVm22_3Test.trueBranch()) {
MethodDescriptor registerMethod = ofMethod("org.graalvm.nativeimage.hosted.RuntimeProxyCreation",
"register", void.class, Class[].class);
greaterThan22_2.invokeStaticMethod(
registerMethod,
array);
}
/* GraalVM < 22.3 */
try (BytecodeCreator smallerThan22_3 = graalVm22_3Test.falseBranch()) {
ResultHandle proxySupportClass = smallerThan22_3.loadClassFromTCCL(DYNAMIC_PROXY_REGISTRY);
ResultHandle proxySupport = smallerThan22_3.invokeStaticMethod(
IMAGE_SINGLETONS_LOOKUP,
proxySupportClass);
smallerThan22_3.invokeInterfaceMethod(ofMethod(DYNAMIC_PROXY_REGISTRY,
"addProxyClass", void.class, Class[].class), proxySupport, array);
}

}
registerProxies.returnVoid();
overallCatch.invokeStaticMethod(registerProxies.getMethodDescriptor());
}

/* Resource includes and excludes */
if (!resourcePatterns.isEmpty()) {
MethodCreator resourceIncludesExcludes = file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.deployment.steps;

import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

import io.quarkus.builder.Json;
import io.quarkus.builder.Json.JsonArrayBuilder;
import io.quarkus.builder.Json.JsonObjectBuilder;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;

public class NativeImageProxyConfigStep {

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
void generateProxyConfig(BuildProducer<GeneratedResourceBuildItem> proxyConfig,
List<NativeImageProxyDefinitionBuildItem> proxies) {
JsonArrayBuilder root = Json.array();

for (NativeImageProxyDefinitionBuildItem proxy : proxies) {
JsonArrayBuilder interfaces = Json.array();
for (String cl : proxy.getClasses()) {
interfaces.add(cl);
}
JsonObjectBuilder proxyJson = Json.object();
proxyJson.put("interfaces", interfaces);
root.add(proxyJson);
}

try (StringWriter writer = new StringWriter()) {
root.appendTo(writer);
proxyConfig.produce(new GeneratedResourceBuildItem("META-INF/native-image/proxy-config.json",
writer.toString().getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 058e24e

Please sign in to comment.