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

8257853: Remove dependencies on JNF's JNI utility functions in AWT and 2D code #1679

Closed
wants to merge 10 commits into from
Closed

Large diffs are not rendered by default.

Large diffs are not rendered by default.

@@ -38,6 +38,7 @@
#import "CMenuBar.h"
#import "ThreadUtilities.h"
#import "NSApplicationAWT.h"
#import "JNIUtilities.h"

#pragma mark App Menu helpers

@@ -243,9 +244,11 @@ - (id) init {
BOOL prefsEnabled = (prefsAvailable && [self.fPreferencesMenu isEnabled] && ([self.fPreferencesMenu target] != nil));

JNIEnv *env = [ThreadUtilities getJNIEnv];
static JNF_CLASS_CACHE(sjc_AppMenuBarHandler, "com/apple/eawt/_AppMenuBarHandler");
static JNF_STATIC_MEMBER_CACHE(sjm_initMenuStates, sjc_AppMenuBarHandler, "initMenuStates", "(ZZZZ)V");
JNFCallStaticVoidMethod(env, sjm_initMenuStates, aboutAvailable, aboutEnabled, prefsAvailable, prefsEnabled);
DECLARE_CLASS_RETURN(sjc_AppMenuBarHandler, "com/apple/eawt/_AppMenuBarHandler", NULL);
DECLARE_STATIC_METHOD_RETURN(sjm_initMenuStates, sjc_AppMenuBarHandler, "initMenuStates", "(ZZZZ)V", NULL);
(*env)->CallStaticVoidMethod(env, sjc_AppMenuBarHandler, sjm_initMenuStates,
aboutAvailable, aboutEnabled, prefsAvailable, prefsEnabled);
CHECK_EXCEPTION();

// register for the finish launching and system power off notifications by default
NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];
@@ -272,7 +275,12 @@ - (void)dealloc {

#pragma mark Callbacks from AppKit

static JNF_CLASS_CACHE(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler");
static jclass sjc_AppEventHandler = NULL;
#define GET_APPEVENTHANDLER_CLASS() \
GET_CLASS(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler");

#define GET_APPEVENTHANDLER_CLASS_RETURN(ret) \
GET_CLASS_RETURN(sjc_AppEventHandler, "com/apple/eawt/_AppEventHandler", ret);

- (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
AWT_ASSERT_APPKIT_THREAD;
@@ -283,8 +291,10 @@ - (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEven
//fprintf(stderr,"jm_handleOpenURL\n");
JNIEnv *env = [ThreadUtilities getJNIEnv];
jstring jURL = JNFNSToJavaString(env, url);
This conversation was marked as resolved by prrace

This comment has been minimized.

@prsadhuk

prsadhuk Dec 17, 2020
Contributor

Don't we need to remove this JNFNSToJavaString dependancy?

This comment has been minimized.

@prrace

prrace Dec 17, 2020
Author Contributor

Not for these changes. There will be a subsequent patch to update all of the string conversions

static JNF_STATIC_MEMBER_CACHE(jm_handleOpenURI, sjc_AppEventHandler, "handleOpenURI", "(Ljava/lang/String;)V");
JNFCallStaticVoidMethod(env, jm_handleOpenURI, jURL); // AWT_THREADING Safe (event)
GET_APPEVENTHANDLER_CLASS();
DECLARE_STATIC_METHOD(jm_handleOpenURI, sjc_AppEventHandler, "handleOpenURI", "(Ljava/lang/String;)V");
(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handleOpenURI, jURL); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, jURL);

[replyEvent insertDescriptor:[NSAppleEventDescriptor nullDescriptor] atIndex:0];
@@ -293,14 +303,22 @@ - (void)_handleOpenURLEvent:(NSAppleEventDescriptor *)openURLEvent withReplyEven
// Helper for both open file and print file methods
// Creates a Java list of files from a native list of files
- (jobject)_createFilePathArrayFrom:(NSArray *)filenames withEnv:(JNIEnv *)env {
static JNF_CLASS_CACHE(sjc_ArrayList, "java/util/ArrayList");
static JNF_CTOR_CACHE(jm_ArrayList_ctor, sjc_ArrayList, "(I)V");
static JNF_MEMBER_CACHE(jm_ArrayList_add, sjc_ArrayList, "add", "(Ljava/lang/Object;)Z");
static jclass sjc_ArrayList = NULL;
if (sjc_ArrayList == NULL) {
sjc_ArrayList = (*env)->FindClass(env, "java/util/ArrayList");
if (sjc_ArrayList != NULL) sjc_ArrayList = (*env)->NewGlobalRef(env, sjc_ArrayList); \
}
CHECK_NULL_RETURN(sjc_ArrayList, NULL);
DECLARE_METHOD_RETURN(jm_ArrayList_ctor, sjc_ArrayList, "<init>", "(I)V", NULL);
DECLARE_METHOD_RETURN(jm_ArrayList_add, sjc_ArrayList, "add", "(Ljava/lang/Object;)Z", NULL);

jobject jFileNamesArray = (*env)->NewObject(env, sjc_ArrayList, jm_ArrayList_ctor, (jint)[filenames count]); // AWT_THREADING Safe (known object)
CHECK_EXCEPTION_NULL_RETURN(jFileNamesArray, NULL);

jobject jFileNamesArray = JNFNewObject(env, jm_ArrayList_ctor, (jint)[filenames count]); // AWT_THREADING Safe (known object)
for (NSString *filename in filenames) {
jstring jFileName = JNFNormalizedJavaStringForPath(env, filename);
This conversation was marked as resolved by prrace

This comment has been minimized.

@prsadhuk

prsadhuk Dec 17, 2020
Contributor

JNFNormalizedJavaStringForPath?

This comment has been minimized.

@prrace

prrace Dec 17, 2020
Author Contributor

Another one for a later patch

JNFCallVoidMethod(env, jFileNamesArray, jm_ArrayList_add, jFileName);
(*env)->CallVoidMethod(env, jFileNamesArray, jm_ArrayList_add, jFileName);
CHECK_EXCEPTION();
}

return jFileNamesArray;
@@ -325,8 +343,11 @@ - (void)application:(NSApplication *)theApplication openFiles:(NSArray *)fileNam
// convert the file names array
jobject jFileNamesArray = [self _createFilePathArrayFrom:fileNames withEnv:env];

static JNF_STATIC_MEMBER_CACHE(jm_handleOpenFiles, sjc_AppEventHandler, "handleOpenFiles", "(Ljava/util/List;Ljava/lang/String;)V");
JNFCallStaticVoidMethod(env, jm_handleOpenFiles, jFileNamesArray, jSearchString);
GET_APPEVENTHANDLER_CLASS();
DECLARE_STATIC_METHOD(jm_handleOpenFiles, sjc_AppEventHandler,
"handleOpenFiles", "(Ljava/util/List;Ljava/lang/String;)V");
(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handleOpenFiles, jFileNamesArray, jSearchString);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, jFileNamesArray);
(*env)->DeleteLocalRef(env, jSearchString);

@@ -341,8 +362,11 @@ - (NSApplicationPrintReply)application:(NSApplication *)application printFiles:(
//fprintf(stderr,"jm_handlePrintFile\n");
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject jFileNamesArray = [self _createFilePathArrayFrom:fileNames withEnv:env];
static JNF_STATIC_MEMBER_CACHE(jm_handlePrintFile, sjc_AppEventHandler, "handlePrintFiles", "(Ljava/util/List;)V");
JNFCallStaticVoidMethod(env, jm_handlePrintFile, jFileNamesArray); // AWT_THREADING Safe (event)
GET_APPEVENTHANDLER_CLASS_RETURN(NSPrintingCancelled);
DECLARE_STATIC_METHOD_RETURN(jm_handlePrintFile, sjc_AppEventHandler,
"handlePrintFiles", "(Ljava/util/List;)V", NSPrintingCancelled);
(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handlePrintFile, jFileNamesArray); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, jFileNamesArray);

return NSPrintingSuccess;
@@ -354,8 +378,10 @@ + (void)_notifyJava:(jint)notificationType {

//fprintf(stderr,"jm_handleOpenApplication\n");
JNIEnv *env = [ThreadUtilities getJNIEnv];
static JNF_STATIC_MEMBER_CACHE(jm_handleNativeNotification, sjc_AppEventHandler, "handleNativeNotification", "(I)V");
JNFCallStaticVoidMethod(env, jm_handleNativeNotification, notificationType); // AWT_THREADING Safe (event)
GET_APPEVENTHANDLER_CLASS();
DECLARE_STATIC_METHOD(jm_handleNativeNotification, sjc_AppEventHandler, "handleNativeNotification", "(I)V");
(*env)->CallStaticVoidMethod(env, sjc_AppEventHandler, jm_handleNativeNotification, notificationType); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
}

// About menu handler
@@ -25,7 +25,7 @@

#import "CDataTransferer.h"
#import "ThreadUtilities.h"
#import "jni_util.h"
#import "JNIUtilities.h"
#import <Cocoa/Cocoa.h>
#import <JavaNativeFoundation/JavaNativeFoundation.h>

@@ -88,18 +88,19 @@ - (void)checkPasteboard:(id)sender {
if (self.changeCount != newChangeCount) {
self.changeCount = newChangeCount;

// Notify that the content might be changed
static JNF_CLASS_CACHE(jc_CClipboard, "sun/lwawt/macosx/CClipboard");
static JNF_STATIC_MEMBER_CACHE(jm_contentChanged, jc_CClipboard, "notifyChanged", "()V");
JNIEnv *env = [ThreadUtilities getJNIEnv];
JNFCallStaticVoidMethod(env, jm_contentChanged);
// Notify that the content might be changed
DECLARE_CLASS(jc_CClipboard, "sun/lwawt/macosx/CClipboard");
DECLARE_STATIC_METHOD(jm_contentChanged, jc_CClipboard, "notifyChanged", "()V");
(*env)->CallStaticVoidMethod(env, jc_CClipboard, jm_contentChanged);
CHECK_EXCEPTION();

// If we have a Java pasteboard owner, tell it that it doesn't own the pasteboard anymore.
static JNF_MEMBER_CACHE(jm_lostOwnership, jc_CClipboard, "notifyLostOwnership", "()V");
DECLARE_METHOD(jm_lostOwnership, jc_CClipboard, "notifyLostOwnership", "()V");
@synchronized(self) {
if (self.clipboardOwner) {
JNIEnv *env = [ThreadUtilities getJNIEnv];
JNFCallVoidMethod(env, self.clipboardOwner, jm_lostOwnership); // AWT_THREADING Safe (event)
(*env)->CallVoidMethod(env, self.clipboardOwner, jm_lostOwnership); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
JNFDeleteGlobalRef(env, self.clipboardOwner);
self.clipboardOwner = NULL;
}
@@ -38,6 +38,7 @@
#import "DnDUtilities.h"
#import "ThreadUtilities.h"
#import "LWCToolkit.h"
#import "JNIUtilities.h"


// When sIsJavaDragging is true Java drag gesture has been recognized and a drag is/has been initialized.
@@ -69,9 +70,17 @@ + (void)javaDraggingEnd
}
@end

JNF_CLASS_CACHE(DataTransfererClass, "sun/awt/datatransfer/DataTransferer");
JNF_CLASS_CACHE(CDragSourceContextPeerClass, "sun/lwawt/macosx/CDragSourceContextPeer");
JNF_CLASS_CACHE(CImageClass, "sun/lwawt/macosx/CImage");
static jclass DataTransfererClass = NULL;
static jclass CDragSourceContextPeerClass = NULL;

#define GET_DT_CLASS() \
GET_CLASS(DataTransfererClass, "sun/awt/datatransfer/DataTransferer");

#define GET_DT_CLASS_RETURN(ret) \
GET_CLASS_RETURN(DataTransfererClass, "sun/awt/datatransfer/DataTransferer", ret);

#define GET_DSCP_CLASS() \
GET_CLASS(CDragSourceContextPeerClass, "sun/lwawt/macosx/CDragSourceContextPeer");

static NSDragOperation sDragOperation;
static NSPoint sDraggingLocation;
@@ -212,8 +221,11 @@ - (void)dealloc
//
- (jobject)dataTransferer:(JNIEnv*)env
{
JNF_STATIC_MEMBER_CACHE(getInstanceMethod, DataTransfererClass, "getInstance", "()Lsun/awt/datatransfer/DataTransferer;");
return JNFCallStaticObjectMethod(env, getInstanceMethod);
GET_DT_CLASS_RETURN(NULL);
DECLARE_STATIC_METHOD_RETURN(getInstanceMethod, DataTransfererClass, "getInstance", "()Lsun/awt/datatransfer/DataTransferer;", NULL);
jobject o = (*env)->CallStaticObjectMethod(env, DataTransfererClass, getInstanceMethod);
CHECK_EXCEPTION();
return o;
}

// Appropriated from Windows' awt_DataTransferer.cpp:
@@ -227,9 +239,11 @@ - (jbyteArray)convertData:(jlong)format
jbyteArray data = nil;

if (transferer != NULL) {
JNF_MEMBER_CACHE(convertDataMethod, DataTransfererClass, "convertData", "(Ljava/lang/Object;Ljava/awt/datatransfer/Transferable;JLjava/util/Map;Z)[B");
data = JNFCallObjectMethod(env, transferer, convertDataMethod, fComponent, fTransferable, format, fFormatMap, (jboolean) TRUE);
GET_DT_CLASS_RETURN(NULL);
DECLARE_METHOD_RETURN(convertDataMethod, DataTransfererClass, "convertData", "(Ljava/lang/Object;Ljava/awt/datatransfer/Transferable;JLjava/util/Map;Z)[B", NULL);
data = (*env)->CallObjectMethod(env, transferer, convertDataMethod, fComponent, fTransferable, format, fFormatMap, (jboolean) TRUE);
}
CHECK_EXCEPTION();

return data;
}
@@ -555,11 +569,14 @@ - (void)doDrag
}

// DragSourceContextPeer.dragDropFinished() should be called even if there was an error:
JNF_MEMBER_CACHE(dragDropFinishedMethod, CDragSourceContextPeerClass, "dragDropFinished", "(ZIII)V");
GET_DSCP_CLASS();
DECLARE_METHOD(dragDropFinishedMethod, CDragSourceContextPeerClass, "dragDropFinished", "(ZIII)V");
DLog3(@" -> posting dragDropFinished, point %f, %f", point.x, point.y);
JNFCallVoidMethod(env, fDragSourceContextPeer, dragDropFinishedMethod, success, dragOp, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
JNF_MEMBER_CACHE(resetHoveringMethod, CDragSourceContextPeerClass, "resetHovering", "()V");
JNFCallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable
(*env)->CallVoidMethod(env, fDragSourceContextPeer, dragDropFinishedMethod, success, dragOp, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
This conversation was marked as resolved by prrace

This comment has been minimized.

@prsadhuk

prsadhuk Dec 18, 2020
Contributor

CHECK_EXCEPTION missed?

CHECK_EXCEPTION();
DECLARE_METHOD(resetHoveringMethod, CDragSourceContextPeerClass, "resetHovering", "()V");
(*env)->CallVoidMethod(env, fDragSourceContextPeer, resetHoveringMethod); // Hust reset static variable
CHECK_EXCEPTION();
} @finally {
sNeedsEnter = NO;
AWTToolkit.inDoDragDropLoop = NO;
@@ -594,8 +611,10 @@ - (void)draggingOperationChanged:(NSDragOperation)dragOp {
DLog3(@" -> posting operationChanged, point %f, %f", point.x, point.y);
jint modifiedModifiers = fDragKeyModifiers | fDragMouseModifiers | [DnDUtilities javaKeyModifiersForNSDragOperation:dragOp];

JNF_MEMBER_CACHE(operationChangedMethod, CDragSourceContextPeerClass, "operationChanged", "(IIII)V");
JNFCallVoidMethod(env, fDragSourceContextPeer, operationChangedMethod, targetActions, modifiedModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
GET_DSCP_CLASS();
DECLARE_METHOD(operationChangedMethod, CDragSourceContextPeerClass, "operationChanged", "(IIII)V");
(*env)->CallVoidMethod(env, fDragSourceContextPeer, operationChangedMethod, targetActions, modifiedModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
}

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)localDrag {
@@ -667,12 +686,14 @@ - (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint {
DLog4(@"[CDragSource draggedImage moved]: (%f, %f) %@\n", screenPoint.x, screenPoint.y, self);

DLog3(@" -> posting dragMotion, point %f, %f", point.x, point.y);
JNF_MEMBER_CACHE(dragMotionMethod, CDragSourceContextPeerClass, "dragMotion", "(IIII)V");
JNFCallVoidMethod(env, fDragSourceContextPeer, dragMotionMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)

GET_DSCP_CLASS();
DECLARE_METHOD(dragMotionMethod, CDragSourceContextPeerClass, "dragMotion", "(IIII)V");
(*env)->CallVoidMethod(env, fDragSourceContextPeer, dragMotionMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
DLog3(@" -> posting dragMouseMoved, point %f, %f", point.x, point.y);
JNF_MEMBER_CACHE(dragMouseMovedMethod, CDragSourceContextPeerClass, "dragMouseMoved", "(IIII)V");
JNFCallVoidMethod(env, fDragSourceContextPeer, dragMouseMovedMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
DECLARE_METHOD(dragMouseMovedMethod, CDragSourceContextPeerClass, "dragMouseMoved", "(IIII)V");
(*env)->CallVoidMethod(env, fDragSourceContextPeer, dragMouseMovedMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
}
JNF_COCOA_EXIT(env);
}
@@ -696,8 +717,10 @@ - (void) postDragEnter {

NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
DLog3(@" -> posting dragEnter, point %f, %f", point.x, point.y);
JNF_MEMBER_CACHE(dragEnterMethod, CDragSourceContextPeerClass, "dragEnter", "(IIII)V");
JNFCallVoidMethod(env, fDragSourceContextPeer, dragEnterMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
GET_DSCP_CLASS();
DECLARE_METHOD(dragEnterMethod, CDragSourceContextPeerClass, "dragEnter", "(IIII)V");
(*env)->CallVoidMethod(env, fDragSourceContextPeer, dragEnterMethod, targetActions, (jint) fModifiers, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();
}

- (void) postDragExit {
@@ -706,8 +729,11 @@ - (void) postDragExit {

NSPoint point = [self mapNSScreenPointToJavaWithOffset:sDraggingLocation];
DLog3(@" -> posting dragExit, point %f, %f", point.x, point.y);
JNF_MEMBER_CACHE(dragExitMethod, CDragSourceContextPeerClass, "dragExit", "(II)V");
JNFCallVoidMethod(env, fDragSourceContextPeer, dragExitMethod, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
GET_DSCP_CLASS();
DECLARE_METHOD(dragExitMethod, CDragSourceContextPeerClass, "dragExit", "(II)V");
(*env)->CallVoidMethod(env, fDragSourceContextPeer, dragExitMethod, (jint) point.x, (jint) point.y); // AWT_THREADING Safe (event)
CHECK_EXCEPTION();

}