package com.oracle.svm.jni.access;
import java.lang.reflect.Modifier;
import org.graalvm.nativeimage.Platform.HOSTED_ONLY;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.function.CFunctionPointer;
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.FeatureImpl.CompilationAccessImpl;
import com.oracle.svm.hosted.meta.HostedUniverse;
import com.oracle.svm.hosted.meta.MethodPointer;
import com.oracle.svm.jni.hosted.JNIJavaCallWrapperMethod;
import com.oracle.svm.jni.hosted.JNIJavaCallWrapperMethod.CallVariant;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;
public final class JNIAccessibleMethod extends JNIAccessibleMember {
public static ResolvedJavaField getCallWrapperField(MetaAccessProvider metaAccess, CallVariant variant, boolean nonVirtual) {
StringBuilder name = new StringBuilder(32);
if (variant == CallVariant.VARARGS) {
name.append("varargs");
} else if (variant == CallVariant.ARRAY) {
name.append("array");
} else if (variant == CallVariant.VA_LIST) {
name.append("valist");
} else {
throw VMError.shouldNotReachHere();
}
if (nonVirtual) {
name.append("Nonvirtual");
}
name.append("CallWrapper");
try {
return metaAccess.lookupJavaField(JNIAccessibleMethod.class.getDeclaredField(name.toString()));
} catch (NoSuchFieldException e) {
throw VMError.shouldNotReachHere(e);
}
}
@Platforms(HOSTED_ONLY.class) private final JNIAccessibleMethodDescriptor descriptor;
private final int modifiers;
@SuppressWarnings("unused") private CFunctionPointer varargsCallWrapper;
@SuppressWarnings("unused") private CFunctionPointer arrayCallWrapper;
@SuppressWarnings("unused") private CFunctionPointer valistCallWrapper;
@SuppressWarnings("unused") private CFunctionPointer varargsNonvirtualCallWrapper;
@SuppressWarnings("unused") private CFunctionPointer arrayNonvirtualCallWrapper;
@SuppressWarnings("unused") private CFunctionPointer valistNonvirtualCallWrapper;
@Platforms(HOSTED_ONLY.class) private final JNIJavaCallWrapperMethod varargsCallWrapperMethod;
@Platforms(HOSTED_ONLY.class) private final JNIJavaCallWrapperMethod arrayCallWrapperMethod;
@Platforms(HOSTED_ONLY.class) private final JNIJavaCallWrapperMethod valistCallWrapperMethod;
@Platforms(HOSTED_ONLY.class) private final JNIJavaCallWrapperMethod varargsNonvirtualCallWrapperMethod;
@Platforms(HOSTED_ONLY.class) private final JNIJavaCallWrapperMethod arrayNonvirtualCallWrapperMethod;
@Platforms(HOSTED_ONLY.class) private final JNIJavaCallWrapperMethod valistNonvirtualCallWrapperMethod;
JNIAccessibleMethod(JNIAccessibleMethodDescriptor descriptor,
int modifiers,
JNIAccessibleClass declaringClass,
JNIJavaCallWrapperMethod varargsCallWrapper,
JNIJavaCallWrapperMethod arrayCallWrapper,
JNIJavaCallWrapperMethod valistCallWrapper,
JNIJavaCallWrapperMethod varargsNonvirtualCallWrapperMethod,
JNIJavaCallWrapperMethod arrayNonvirtualCallWrapperMethod,
JNIJavaCallWrapperMethod valistNonvirtualCallWrapperMethod) {
super(declaringClass);
assert varargsCallWrapper != null && arrayCallWrapper != null && valistCallWrapper != null;
assert (Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers))
? (varargsNonvirtualCallWrapperMethod == null && arrayNonvirtualCallWrapperMethod == null && valistNonvirtualCallWrapperMethod == null)
: (varargsNonvirtualCallWrapperMethod != null & arrayNonvirtualCallWrapperMethod != null && valistNonvirtualCallWrapperMethod != null);
this.descriptor = descriptor;
this.modifiers = modifiers;
this.varargsCallWrapperMethod = varargsCallWrapper;
this.arrayCallWrapperMethod = arrayCallWrapper;
this.valistCallWrapperMethod = valistCallWrapper;
this.varargsNonvirtualCallWrapperMethod = varargsNonvirtualCallWrapperMethod;
this.arrayNonvirtualCallWrapperMethod = arrayNonvirtualCallWrapperMethod;
this.valistNonvirtualCallWrapperMethod = valistNonvirtualCallWrapperMethod;
}
public boolean isPublic() {
return Modifier.isPublic(modifiers);
}
public boolean isStatic() {
return Modifier.isStatic(modifiers);
}
@Platforms(HOSTED_ONLY.class)
void finishBeforeCompilation(CompilationAccessImpl access) {
HostedUniverse hUniverse = access.getUniverse();
AnalysisUniverse aUniverse = access.getUniverse().getBigBang().getUniverse();
varargsCallWrapper = MethodPointer.factory(hUniverse.lookup(aUniverse.lookup(varargsCallWrapperMethod)));
arrayCallWrapper = MethodPointer.factory(hUniverse.lookup(aUniverse.lookup(arrayCallWrapperMethod)));
valistCallWrapper = MethodPointer.factory(hUniverse.lookup(aUniverse.lookup(valistCallWrapperMethod)));
if (!Modifier.isStatic(modifiers) && !Modifier.isAbstract(modifiers)) {
varargsNonvirtualCallWrapper = MethodPointer.factory(hUniverse.lookup(aUniverse.lookup(varargsNonvirtualCallWrapperMethod)));
arrayNonvirtualCallWrapper = MethodPointer.factory(hUniverse.lookup(aUniverse.lookup(arrayNonvirtualCallWrapperMethod)));
valistNonvirtualCallWrapper = MethodPointer.factory(hUniverse.lookup(aUniverse.lookup(valistNonvirtualCallWrapperMethod)));
}
setHidingSubclasses(access.getMetaAccess(), this::anyMatchIgnoreReturnType);
}
private boolean anyMatchIgnoreReturnType(ResolvedJavaType sub) {
for (ResolvedJavaMethod method : sub.getDeclaredMethods()) {
if (descriptor.matchesIgnoreReturnType(method)) {
return true;
}
}
return false;
}
}