Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ce38d2a
feat: add fory-graalvm-feature
mengnankkkk Oct 9, 2025
c8be5a3
fix: ci bug
mengnankkkk Oct 9, 2025
8d10234
fix: ci bugs
mengnankkkk Oct 9, 2025
9caacb7
fix: checkstyle
mengnankkkk Oct 9, 2025
2e78cdf
Update pom.xml
mengnankkkk Oct 9, 2025
de00781
fix: ci bugs
mengnankkkk Oct 9, 2025
ed2b97a
fix: ci bug
mengnankkkk Oct 9, 2025
6960c25
Merge branch 'main' into fix-mengnankk
mengnankkkk Oct 9, 2025
191d2a8
Merge branch 'main' into fix-mengnankk
mengnankkkk Oct 9, 2025
3a261fc
fix: ci bugs
mengnankkkk Oct 9, 2025
0c5e82b
feat: fix Timing issue in GraalVM Feature
mengnankkkk Oct 10, 2025
1592b3a
feat: remove static fields
mengnankkkk Oct 11, 2025
ab25f6d
fix: chectstyle
mengnankkkk Oct 11, 2025
e9f8ef4
fix: Api bug
mengnankkkk Oct 13, 2025
21d7de8
fix: adapt jdk17
mengnankkkk Oct 13, 2025
56e00e7
fix: adapt jdk17
mengnankkkk Oct 13, 2025
95abbde
Merge branch 'main' into fix-mengnankk
mengnankkkk Oct 13, 2025
021b1c5
fix:APi moved to GraalvmSupport
mengnankkkk Oct 13, 2025
25d3203
Update java/fory-core/src/main/java/org/apache/fory/util/GraalvmSuppo…
mengnankkkk Oct 14, 2025
f896fef
fix: replace name
mengnankkkk Oct 14, 2025
e4a9852
fix: checkstyle
mengnankkkk Oct 14, 2025
39ad1b7
fix: move registerClassForGraalvm
mengnankkkk Oct 15, 2025
d5c62e5
fix: ci test bug
mengnankkkk Oct 15, 2025
fa7c904
Update integration_tests/graalvm_tests/src/main/java/org/apache/fory/…
mengnankkkk Oct 16, 2025
9db3ce4
fix: move something to GraalvmSupport
mengnankkkk Oct 21, 2025
4ef6f86
Merge branch 'main' into fix-mengnankk
mengnankkkk Oct 21, 2025
e39fff8
fix: only init in Graalvm
mengnankkkk Oct 21, 2025
cf7f88d
fix: test up
mengnankkkk Oct 21, 2025
761dc74
fix: modules bug
mengnankkkk Oct 21, 2025
f079030
fix: pom test
mengnankkkk Oct 22, 2025
9984a8f
fix: pom bug
mengnankkkk Oct 22, 2025
5992a1c
Update pom.xml
mengnankkkk Oct 22, 2025
545bb13
fix: pom.xml
mengnankkkk Oct 22, 2025
d3d0e2b
fix: test bugs
mengnankkkk Oct 22, 2025
11e4d95
Update native-image.properties
mengnankkkk Oct 22, 2025
cb789a5
fix: move someting
mengnankkkk Oct 23, 2025
69f3a25
Update pom.xml
mengnankkkk Oct 23, 2025
d276915
feat: replace junit
mengnankkkk Oct 23, 2025
23c81c9
fix: something error
mengnankkkk Oct 23, 2025
332c960
fix: test in graalvm
mengnankkkk Oct 24, 2025
e787e3f
fix: test
mengnankkkk Oct 24, 2025
287df20
fix: use unsafe to solve
mengnankkkk Oct 24, 2025
b1cb3c8
fix: add ObjectCreator and ObjectCreators imports and use interface i…
mengnankkkk Oct 24, 2025
7bb71eb
style: apply code formatting with spotless
mengnankkkk Oct 24, 2025
21cd616
docs: add missing Javadoc comments for inner classes
mengnankkkk Oct 24, 2025
fe8aaa7
fix: configure javadoc plugin to handle incubating modules in GraalVM
mengnankkkk Oct 24, 2025
7643cec
fix: ci bug
mengnankkkk Oct 25, 2025
bb3c396
fix: ci bug from pom
mengnankkkk Oct 25, 2025
4114145
fix: ci bug use unsafe
mengnankkkk Oct 25, 2025
7bc5d40
fix: ci bug?
mengnankkkk Oct 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions integration_tests/graalvm_tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>fory-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.fory</groupId>
<artifactId>fory-graalvm-feature</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fory.graalvm;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.fory.Fory;
import org.apache.fory.config.Language;

public class FeatureTestExample {

// Test class with private constructor (problematic for creation)
public static class ProblematicClass {
private String value;

private ProblematicClass() {
// Private constructor
}

public ProblematicClass(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

// Test interface for proxy
public interface TestInterface {
String getValue();

void setValue(String value);
}

// Simple invocation handler
public static class TestInvocationHandler implements InvocationHandler {
private String value;

public TestInvocationHandler(String value) {
this.value = value;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ("getValue".equals(method.getName())) {
return value;
} else if ("setValue".equals(method.getName())) {
value = (String) args[0];
return null;
}
return null;
}
}

public static void main(String[] args) {
System.out.println("Testing Fory GraalVM Feature...");

Fory fory =
Fory.builder().withLanguage(Language.JAVA).withRefTracking(true).withCodegen(false).build();

fory.register(ProblematicClass.class);
fory.register(TestInvocationHandler.class);

// Register proxy interface
Fory.addProxyInterface(TestInterface.class);

try {
// Test 1: Serialize/deserialize problematic class
ProblematicClass original = new ProblematicClass("test-value");
byte[] serialized = fory.serialize(original);
ProblematicClass deserialized = (ProblematicClass) fory.deserialize(serialized);

if (!"test-value".equals(deserialized.getValue())) {
throw new RuntimeException("Problematic class test failed");
}
System.out.println("✓ Problematic class serialization test passed");

// Test 2: Serialize/deserialize proxy object
TestInterface proxy =
(TestInterface)
Proxy.newProxyInstance(
TestInterface.class.getClassLoader(),
new Class[] {TestInterface.class},
new TestInvocationHandler("proxy-value"));

byte[] proxySerialised = fory.serialize(proxy);
TestInterface deserializedProxy = (TestInterface) fory.deserialize(proxySerialised);

if (!"proxy-value".equals(deserializedProxy.getValue())) {
throw new RuntimeException("Proxy test failed");
}
System.out.println("✓ Proxy serialization test passed");

System.out.println("All GraalVM Feature tests passed!");

} catch (Exception e) {
System.err.println("GraalVM Feature test failed: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public static void main(String[] args) throws Throwable {
Benchmark.main(args);
CollectionExample.main(args);
ArrayExample.main(args);
FeatureTestExample.main(args);
}
}
47 changes: 47 additions & 0 deletions java/fory-core/src/main/java/org/apache/fory/Fory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.concurrent.NotThreadSafe;
Expand Down Expand Up @@ -177,11 +178,13 @@ public Fory(ForyBuilder builder, ClassLoader classLoader) {
@Override
public void register(Class<?> cls) {
_getTypeResolver().register(cls);
TypeResolver.registerClassForGraalvm(cls, config.getConfigHash());
}

@Override
public void register(Class<?> cls, int id) {
_getTypeResolver().register(cls, id);
TypeResolver.registerClassForGraalvm(cls, config.getConfigHash());
}

@Deprecated
Expand Down Expand Up @@ -213,6 +216,7 @@ public void register(Class<?> cls, String typeName) {

public void register(Class<?> cls, String namespace, String typeName) {
_getTypeResolver().register(cls, namespace, typeName);
TypeResolver.registerClassForGraalvm(cls, config.getConfigHash());
}

@Override
Expand Down Expand Up @@ -1764,4 +1768,47 @@ public MetaCompressor getMetaCompressor() {
public static ForyBuilder builder() {
return new ForyBuilder();
}

/**
* Get all registered classes for GraalVM native image compilation. This is a convenience method
* that delegates to {@link TypeResolver#getAllRegisteredClasses()}.
*
* @return unmodifiable set of all registered classes
*/
public static Set<Class<?>> getRegisteredClasses() {
return TypeResolver.getAllRegisteredClasses();
}

/**
* Get all registered proxy interfaces for GraalVM native image compilation. This is a convenience
* method that delegates to {@link TypeResolver#getAllProxyInterfaces()}.
*
* @return unmodifiable set of all registered proxy interfaces
*/
public static Set<Class<?>> getProxyInterfaces() {
return TypeResolver.getAllProxyInterfaces();
}

/**
* Register a proxy interface for GraalVM native image compilation. This is a convenience method
* that delegates to {@link TypeResolver#registerProxyInterfaceForGraalvm}.
*
* <p>Note: This registers the proxy interface globally across all Fory configurations. If you
* need per-configuration registration, use the TypeResolver methods directly.
*
* @param proxyInterface the proxy interface to register
* @throws NullPointerException if proxyInterface is null
* @throws IllegalArgumentException if proxyInterface is not an interface
*/
public static void addProxyInterface(Class<?> proxyInterface) {
TypeResolver.registerProxyInterfaceForGraalvm(proxyInterface, 0);
}

/**
* Clear all GraalVM registrations. This is primarily for testing purposes. This is a convenience
* method that delegates to {@link TypeResolver#clearGraalvmRegistrations()}.
*/
public static void clearRegistrations() {
TypeResolver.clearGraalvmRegistrations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,47 @@ public static <T> ObjectCreator<T> getObjectCreator(Class<T> type) {
return (ObjectCreator<T>) cache.get(type, () -> creategetObjectCreator(type));
}

/**
* Checks if a class is problematic for object creation and requires special handling in GraalVM.
*
* <p>A class is considered problematic if it lacks a public no-arg constructor and would
* typically require ReflectionFactory or unsafe allocation for instantiation.
*
* @param type the class to check
* @return true if the class is problematic for creation, false otherwise
*/
public static boolean isProblematicForCreation(Class<?> type) {
if (type.isInterface()
|| java.lang.reflect.Modifier.isAbstract(type.getModifiers())
|| type.isArray()
|| type.isEnum()
|| type.isAnonymousClass()
|| type.isLocalClass()
|| RecordUtils.isRecord(type)) {
return false;
}
Constructor<?>[] constructors = type.getDeclaredConstructors();
if (constructors.length == 0) {
return true;
}
for (Constructor<?> constructor : constructors) {
if (constructor.getParameterCount() == 0) {
return false;
}
}
return true;
}

private static <T> ObjectCreator<T> creategetObjectCreator(Class<T> type) {
if (RecordUtils.isRecord(type)) {
return new RecordObjectCreator<>(type);
}
Constructor<T> noArgConstructor = ReflectionUtils.getNoArgConstructor(type);
if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE && Platform.JAVA_VERSION >= 25) {
if (GraalvmSupport.IN_GRAALVM_NATIVE_IMAGE) {
if (noArgConstructor != null) {
return new DeclaredNoArgCtrObjectCreator<>(type);
} else {
return new ParentNoArgCtrObjectCreator<>(type);
return new UnsafeObjectCreator<>(type);
}
}
if (noArgConstructor == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,15 @@ static class GraalvmClassRegistry {
final List<ClassResolver> resolvers;
final Map<Class<?>, Class<? extends Serializer>> serializerClassMap;
final Map<Long, Class<? extends Serializer>> deserializerClassMap;
final Set<Class<?>> registeredClasses;
final Set<Class<?>> proxyInterfaces;

private GraalvmClassRegistry() {
resolvers = Collections.synchronizedList(new ArrayList<>());
serializerClassMap = new ConcurrentHashMap<>();
deserializerClassMap = new ConcurrentHashMap<>();
registeredClasses = ConcurrentHashMap.newKeySet();
proxyInterfaces = ConcurrentHashMap.newKeySet();
}
}

Expand Down Expand Up @@ -670,6 +674,72 @@ public final MetaStringResolver getMetaStringResolver() {
return metaStringResolver;
}

/**
* Get all registered classes across all GraalVM registries for native image compilation.
*
* @return unmodifiable set of all registered classes
*/
public static Set<Class<?>> getAllRegisteredClasses() {
Set<Class<?>> allClasses = ConcurrentHashMap.newKeySet();
for (GraalvmClassRegistry registry : GRAALVM_REGISTRY.values()) {
allClasses.addAll(registry.registeredClasses);
}
return Collections.unmodifiableSet(allClasses);
}

/**
* Get all registered proxy interfaces across all GraalVM registries for native image compilation.
*
* @return unmodifiable set of all registered proxy interfaces
*/
public static Set<Class<?>> getAllProxyInterfaces() {
Set<Class<?>> allInterfaces = ConcurrentHashMap.newKeySet();
for (GraalvmClassRegistry registry : GRAALVM_REGISTRY.values()) {
allInterfaces.addAll(registry.proxyInterfaces);
}
return Collections.unmodifiableSet(allInterfaces);
}

/**
* Register a class in the GraalVM registry for native image compilation. This should be called
* from {@link org.apache.fory.Fory#register} methods.
*
* @param cls the class to register
* @param configHash the configuration hash for the Fory instance
*/
public static void registerClassForGraalvm(Class<?> cls, int configHash) {
GraalvmClassRegistry registry =
GRAALVM_REGISTRY.computeIfAbsent(configHash, k -> new GraalvmClassRegistry());
registry.registeredClasses.add(cls);
}

/**
* Register a proxy interface in the GraalVM registry for native image compilation.
*
* @param proxyInterface the proxy interface to register
* @param configHash the configuration hash for the Fory instance
*/
public static void registerProxyInterfaceForGraalvm(Class<?> proxyInterface, int configHash) {
if (proxyInterface == null) {
throw new NullPointerException("Proxy interface must not be null");
}
if (!proxyInterface.isInterface()) {
throw new IllegalArgumentException(
"Proxy type must be an interface: " + proxyInterface.getName());
}
GraalvmClassRegistry registry =
GRAALVM_REGISTRY.computeIfAbsent(configHash, k -> new GraalvmClassRegistry());
registry.proxyInterfaces.add(proxyInterface);
}

/** Clear all GraalVM registrations. This is primarily for testing purposes. */
public static void clearGraalvmRegistrations() {
for (GraalvmClassRegistry registry : GRAALVM_REGISTRY.values()) {
registry.registeredClasses.clear();
registry.proxyInterfaces.clear();
}
}

static class ExtRegistry {
// Here we set it to 1 because `NO_CLASS_ID` is 0 to avoid calculating it again in
// `register(Class<?> cls)`.
Expand Down
Loading
Loading