mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2025-12-06 22:10:54 +03:00
Native Libraries: added API version to:
- test whether native library matches the JAR (bad builds could e.g. ship a newer JAR with an older incompatible native library) - invoke a method (to get API version) to check whether native library works correctly if API version do not match, or method could not invoked correctly, disable usage of FlatLaf native library Windows and macOS binaries built and signed locally in clean workspace Linux binary built by GitHub Actions
This commit is contained in:
@@ -15,6 +15,11 @@ FlatLaf Change Log
|
|||||||
- `FlatSVGIcon` color filters now support linear gradients. (PR #817)
|
- `FlatSVGIcon` color filters now support linear gradients. (PR #817)
|
||||||
- Added support for `JSplitPane.expandableSide` client property to
|
- Added support for `JSplitPane.expandableSide` client property to
|
||||||
`FlatSplitPane`.
|
`FlatSplitPane`.
|
||||||
|
- Native libraries: Added API version check to test whether native library
|
||||||
|
matches the JAR (bad builds could e.g. ship a newer JAR with an older
|
||||||
|
incompatible native library) and to test whether native methods can be invoked
|
||||||
|
(some security software allows loading native library but blocks method
|
||||||
|
invocation).
|
||||||
|
|
||||||
|
|
||||||
## 3.4
|
## 3.4
|
||||||
|
|||||||
@@ -37,16 +37,18 @@ class FlatNativeLibrary
|
|||||||
private static boolean initialized;
|
private static boolean initialized;
|
||||||
private static NativeLibrary nativeLibrary;
|
private static NativeLibrary nativeLibrary;
|
||||||
|
|
||||||
|
private native static int getApiVersion();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads native library (if available) and returns whether loaded successfully.
|
* Loads native library (if available) and returns whether loaded successfully.
|
||||||
* Returns {@code false} if no native library is available.
|
* Returns {@code false} if no native library is available.
|
||||||
*/
|
*/
|
||||||
static synchronized boolean isLoaded() {
|
static synchronized boolean isLoaded( int apiVersion ) {
|
||||||
initialize();
|
initialize( apiVersion );
|
||||||
return (nativeLibrary != null) ? nativeLibrary.isLoaded() : false;
|
return (nativeLibrary != null) ? nativeLibrary.isLoaded() : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initialize() {
|
private static void initialize( int apiVersion ) {
|
||||||
if( initialized )
|
if( initialized )
|
||||||
return;
|
return;
|
||||||
initialized = true;
|
initialized = true;
|
||||||
@@ -104,7 +106,26 @@ class FlatNativeLibrary
|
|||||||
return; // no native library available for current OS or CPU architecture
|
return; // no native library available for current OS or CPU architecture
|
||||||
|
|
||||||
// load native library
|
// load native library
|
||||||
nativeLibrary = createNativeLibrary( classifier, ext );
|
NativeLibrary nativeLibrary = createNativeLibrary( classifier, ext );
|
||||||
|
if( !nativeLibrary.isLoaded() )
|
||||||
|
return;
|
||||||
|
|
||||||
|
// check API version (and check whether library works)
|
||||||
|
try {
|
||||||
|
int actualApiVersion = getApiVersion();
|
||||||
|
if( actualApiVersion != apiVersion ) {
|
||||||
|
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Wrong API version in native library (expected "
|
||||||
|
+ apiVersion + ", actual " + actualApiVersion + "). Ignoring native library.", null );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch( Throwable ex ) {
|
||||||
|
// could be a UnsatisfiedLinkError in case that loading native library
|
||||||
|
// from temp directory was blocked by some OS security mechanism
|
||||||
|
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Failed to get API version of native library. Ignoring native library.", ex );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FlatNativeLibrary.nativeLibrary = nativeLibrary;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NativeLibrary createNativeLibrary( String classifier, String ext ) {
|
private static NativeLibrary createNativeLibrary( String classifier, String ext ) {
|
||||||
@@ -118,9 +139,9 @@ class FlatNativeLibrary
|
|||||||
if( library.isLoaded() )
|
if( library.isLoaded() )
|
||||||
return library;
|
return library;
|
||||||
|
|
||||||
LoggingFacade.INSTANCE.logSevere( "Did not find library '" + System.mapLibraryName( libraryName )
|
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '" + System.mapLibraryName( libraryName )
|
||||||
+ "' in java.library.path '" + System.getProperty( "java.library.path" )
|
+ "' not found in java.library.path '" + System.getProperty( "java.library.path" )
|
||||||
+ "', using extracted library instead", null );
|
+ "'. Using extracted native library instead.", null );
|
||||||
} else {
|
} else {
|
||||||
// try standard library naming scheme
|
// try standard library naming scheme
|
||||||
// (same as in flatlaf.jar in package 'com/formdev/flatlaf/natives')
|
// (same as in flatlaf.jar in package 'com/formdev/flatlaf/natives')
|
||||||
@@ -139,11 +160,11 @@ class FlatNativeLibrary
|
|||||||
return new NativeLibrary( libraryFile2, true );
|
return new NativeLibrary( libraryFile2, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
LoggingFacade.INSTANCE.logSevere( "Did not find library '"
|
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '"
|
||||||
+ libraryFile.getName()
|
+ libraryFile.getName()
|
||||||
+ (libraryName2 != null ? ("' or '" + libraryName2) : "")
|
+ (libraryName2 != null ? ("' or '" + libraryName2) : "")
|
||||||
+ "' in '" + libraryFile.getParentFile().getAbsolutePath()
|
+ "' not found in '" + libraryFile.getParentFile().getAbsolutePath()
|
||||||
+ "', using extracted library instead", null );
|
+ "'. Using extracted native library instead.", null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ import com.formdev.flatlaf.util.SystemInfo;
|
|||||||
*/
|
*/
|
||||||
class FlatNativeLinuxLibrary
|
class FlatNativeLinuxLibrary
|
||||||
{
|
{
|
||||||
|
private static int API_VERSION_LINUX = 3001;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether native library is loaded/available.
|
* Checks whether native library is loaded/available.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -42,7 +44,7 @@ class FlatNativeLinuxLibrary
|
|||||||
* method of this class. Otherwise, the native library may not be loaded.
|
* method of this class. Otherwise, the native library may not be loaded.
|
||||||
*/
|
*/
|
||||||
static boolean isLoaded() {
|
static boolean isLoaded() {
|
||||||
return SystemInfo.isLinux && FlatNativeLibrary.isLoaded();
|
return SystemInfo.isLinux && FlatNativeLibrary.isLoaded( API_VERSION_LINUX );
|
||||||
}
|
}
|
||||||
|
|
||||||
// direction for _NET_WM_MOVERESIZE message
|
// direction for _NET_WM_MOVERESIZE message
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ import com.formdev.flatlaf.util.SystemInfo;
|
|||||||
*/
|
*/
|
||||||
public class FlatNativeMacLibrary
|
public class FlatNativeMacLibrary
|
||||||
{
|
{
|
||||||
|
private static int API_VERSION_MACOS = 2001;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether native library is loaded/available.
|
* Checks whether native library is loaded/available.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -51,7 +53,7 @@ public class FlatNativeMacLibrary
|
|||||||
* method of this class. Otherwise, the native library may not be loaded.
|
* method of this class. Otherwise, the native library may not be loaded.
|
||||||
*/
|
*/
|
||||||
public static boolean isLoaded() {
|
public static boolean isLoaded() {
|
||||||
return SystemInfo.isMacOS && FlatNativeLibrary.isLoaded();
|
return SystemInfo.isMacOS && FlatNativeLibrary.isLoaded( API_VERSION_MACOS );
|
||||||
}
|
}
|
||||||
|
|
||||||
public native static boolean setWindowRoundedBorder( Window window, float radius, float borderWidth, int borderColor );
|
public native static boolean setWindowRoundedBorder( Window window, float radius, float borderWidth, int borderColor );
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ import com.formdev.flatlaf.util.SystemInfo;
|
|||||||
*/
|
*/
|
||||||
public class FlatNativeWindowsLibrary
|
public class FlatNativeWindowsLibrary
|
||||||
{
|
{
|
||||||
|
private static int API_VERSION_WINDOWS = 1001;
|
||||||
|
|
||||||
private static long osBuildNumber = Long.MIN_VALUE;
|
private static long osBuildNumber = Long.MIN_VALUE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +41,7 @@ public class FlatNativeWindowsLibrary
|
|||||||
* method of this class. Otherwise, the native library may not be loaded.
|
* method of this class. Otherwise, the native library may not be loaded.
|
||||||
*/
|
*/
|
||||||
public static boolean isLoaded() {
|
public static boolean isLoaded() {
|
||||||
return SystemInfo.isWindows && FlatNativeLibrary.isLoaded();
|
return SystemInfo.isWindows && FlatNativeLibrary.isLoaded( API_VERSION_WINDOWS );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ class FlatWindowsNativeWindowBorder
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
// check whether native library was successfully loaded
|
// check whether native library was successfully loaded
|
||||||
if( !FlatNativeLibrary.isLoaded() )
|
if( !FlatNativeWindowsLibrary.isLoaded() )
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// create new instance
|
// create new instance
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -21,7 +21,10 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flatlafJniHeaders {
|
flatlafJniHeaders {
|
||||||
headers = listOf( "com_formdev_flatlaf_ui_FlatNativeLinuxLibrary.h" )
|
headers = listOf(
|
||||||
|
"com_formdev_flatlaf_ui_FlatNativeLibrary.h",
|
||||||
|
"com_formdev_flatlaf_ui_FlatNativeLinuxLibrary.h"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
library {
|
library {
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 FormDev Software GmbH
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include "com_formdev_flatlaf_ui_FlatNativeLibrary.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Karl Tauber
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// increase this version if changing API or functionality of native library
|
||||||
|
// also update version in Java class com.formdev.flatlaf.ui.FlatNativeLinuxLibrary
|
||||||
|
#define API_VERSION_LINUX 3001
|
||||||
|
|
||||||
|
|
||||||
|
//---- JNI methods ------------------------------------------------------------
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
|
||||||
|
( JNIEnv* env, jclass cls )
|
||||||
|
{
|
||||||
|
return API_VERSION_LINUX;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||||
|
#include <jni.h>
|
||||||
|
/* Header for class com_formdev_flatlaf_ui_FlatNativeLibrary */
|
||||||
|
|
||||||
|
#ifndef _Included_com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
#define _Included_com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
/*
|
||||||
|
* Class: com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
* Method: getApiVersion
|
||||||
|
* Signature: ()I
|
||||||
|
*/
|
||||||
|
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
|
||||||
|
(JNIEnv *, jclass);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
@@ -26,7 +26,10 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
flatlafJniHeaders {
|
flatlafJniHeaders {
|
||||||
headers = listOf( "com_formdev_flatlaf_ui_FlatNativeMacLibrary.h" )
|
headers = listOf(
|
||||||
|
"com_formdev_flatlaf_ui_FlatNativeLibrary.h",
|
||||||
|
"com_formdev_flatlaf_ui_FlatNativeMacLibrary.h"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
library {
|
library {
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||||
|
#include <jni.h>
|
||||||
|
/* Header for class com_formdev_flatlaf_ui_FlatNativeLibrary */
|
||||||
|
|
||||||
|
#ifndef _Included_com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
#define _Included_com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
/*
|
||||||
|
* Class: com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
* Method: getApiVersion
|
||||||
|
* Signature: ()I
|
||||||
|
*/
|
||||||
|
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
|
||||||
|
(JNIEnv *, jclass);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 FormDev Software GmbH
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include "com_formdev_flatlaf_ui_FlatNativeLibrary.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Karl Tauber
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// increase this version if changing API or functionality of native library
|
||||||
|
// also update version in Java class com.formdev.flatlaf.ui.FlatNativeMacLibrary
|
||||||
|
#define API_VERSION_MACOS 2001
|
||||||
|
|
||||||
|
|
||||||
|
//---- JNI methods ------------------------------------------------------------
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
|
||||||
|
( JNIEnv* env, jclass cls )
|
||||||
|
{
|
||||||
|
return API_VERSION_MACOS;
|
||||||
|
}
|
||||||
@@ -22,6 +22,7 @@ plugins {
|
|||||||
|
|
||||||
flatlafJniHeaders {
|
flatlafJniHeaders {
|
||||||
headers = listOf(
|
headers = listOf(
|
||||||
|
"com_formdev_flatlaf_ui_FlatNativeLibrary.h",
|
||||||
"com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h",
|
"com_formdev_flatlaf_ui_FlatNativeWindowsLibrary.h",
|
||||||
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder.h",
|
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder.h",
|
||||||
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h"
|
"com_formdev_flatlaf_ui_FlatWindowsNativeWindowBorder_WndProc.h"
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2024 FormDev Software GmbH
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include "com_formdev_flatlaf_ui_FlatNativeLibrary.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Karl Tauber
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
// increase this version if changing API or functionality of native library
|
||||||
|
// also update version in Java class com.formdev.flatlaf.ui.FlatNativeWindowsLibrary
|
||||||
|
#define API_VERSION_WINDOWS 1001
|
||||||
|
|
||||||
|
|
||||||
|
//---- JNI methods ------------------------------------------------------------
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
|
||||||
|
( JNIEnv* env, jclass cls )
|
||||||
|
{
|
||||||
|
return API_VERSION_WINDOWS;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||||
|
#include <jni.h>
|
||||||
|
/* Header for class com_formdev_flatlaf_ui_FlatNativeLibrary */
|
||||||
|
|
||||||
|
#ifndef _Included_com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
#define _Included_com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
/*
|
||||||
|
* Class: com_formdev_flatlaf_ui_FlatNativeLibrary
|
||||||
|
* Method: getApiVersion
|
||||||
|
* Signature: ()I
|
||||||
|
*/
|
||||||
|
JNIEXPORT jint JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLibrary_getApiVersion
|
||||||
|
(JNIEnv *, jclass);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user