package com.oracle.svm.jni.access;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import org.graalvm.nativeimage.Platform.HOSTED_ONLY;
import org.graalvm.nativeimage.Platforms;
import jdk.vm.ci.meta.MetaUtil;
public final class JNIAccessibleClass {
private final Class<?> classObject;
private Map<JNIAccessibleMethodDescriptor, JNIAccessibleMethod> methods;
private Map<String, JNIAccessibleField> fields;
JNIAccessibleClass(Class<?> clazz) {
this.classObject = clazz;
}
public Class<?> getClassObject() {
return classObject;
}
public Collection<JNIAccessibleField> getFields() {
return (fields != null) ? fields.values() : Collections.emptySet();
}
Map<String, JNIAccessibleField> getFieldsByName() {
return (fields != null) ? fields : Collections.emptyMap();
}
public JNIAccessibleField getField(String name) {
return (fields != null) ? fields.get(name) : null;
}
@Platforms(HOSTED_ONLY.class)
void addFieldIfAbsent(String name, Function<String, JNIAccessibleField> mappingFunction) {
if (fields == null) {
fields = new HashMap<>();
}
fields.computeIfAbsent(name, mappingFunction);
}
@Platforms(HOSTED_ONLY.class)
void addMethodIfAbsent(JNIAccessibleMethodDescriptor descriptor, Function<JNIAccessibleMethodDescriptor, JNIAccessibleMethod> mappingFunction) {
if (methods == null) {
methods = new HashMap<>();
}
methods.computeIfAbsent(descriptor, mappingFunction);
}
public Collection<JNIAccessibleMethod> getMethods() {
return (methods != null) ? methods.values() : Collections.emptySet();
}
public Map<JNIAccessibleMethodDescriptor, JNIAccessibleMethod> getMethodsByDescriptor() {
return (methods != null) ? methods : Collections.emptyMap();
}
public JNIAccessibleMethod getMethod(JNIAccessibleMethodDescriptor descriptor) {
return (methods != null) ? methods.get(descriptor) : null;
}
String getInternalName() {
return MetaUtil.toInternalName(classObject.getName());
}
}