Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Avro substitutions for Java 17 #21464

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.function.Function;

import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
Expand All @@ -15,6 +16,7 @@
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.jdk.JDK17OrLater;

@TargetClass(className = "org.apache.avro.generic.GenericDatumReader")
final class Target_org_apache_avro_generic_GenericDatumReader {
Expand Down Expand Up @@ -56,5 +58,30 @@ protected Target_org_apache_avro_generic_GenericDatumReader(GenericData data) {

}

@TargetClass(className = "org.apache.avro.reflect.ReflectionUtil", onlyWith = JDK17OrLater.class)
final class Target_org_apache_avro_reflect_ReflectionUtil {

@Substitute
public static <V, R> Function<V, R> getConstructorAsFunction(Class<V> parameterClass, Class<R> clazz) {
// Cannot use the method handle approach as it uses ProtectionDomain which got removed.
try {
Constructor<R> constructor = clazz.getConstructor(parameterClass);
return new Function<V, R>() {
@Override
public R apply(V v) {
try {
return constructor.newInstance(v);
} catch (Exception e) {
throw new IllegalStateException("Unable to create new instance for " + clazz, e);
}
}
};
} catch (Throwable t) {
// if something goes wrong, do not provide a Function instance
return null;
}
}
}

class AvroSubstitutions {
}