Skip to content

Commit 20dda2b

Browse files
author
Lyor Goldstein
committed
Fix return type of Native#loadLibrary to match unconstrained generic type
1 parent 4e0388c commit 20dda2b

20 files changed

+652
-358
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Bug Fixes
3333
* [#549](https://github.com/java-native-access/jna/pull/549): Fixed bug in types derived from XID - [@twall](https://github.com/twall).
3434
* [#536](https://github.com/java-native-access/jna/pull/536): Fixed bug in determining the Library and options associated with types defined outside of a Library - [@twall](https://github.com/twall).
3535
* [#531](https://github.com/java-native-access/jna/pull/531): Ensure direct-mapped callbacks use the right calling convention - [@twall](https://github.com/twall).
36+
* [#565](https://github.com/java-native-access/jna/pull/565): Fix return type of Native#loadLibrary to match unconstrained generic [@lgoldstein](https://github.com/lgoldstein)
3637

3738
Release 4.2.1
3839
=============

contrib/platform/src/com/sun/jna/platform/mac/MacFileUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class MacFileUtils extends FileUtils {
3232

3333
public interface FileManager extends Library {
3434

35-
public FileManager INSTANCE = (FileManager)Native.loadLibrary("CoreServices", FileManager.class);
35+
FileManager INSTANCE = Native.loadLibrary("CoreServices", FileManager.class);
3636

3737
int kFSFileOperationDefaultOptions = 0;
3838
int kFSFileOperationsOverwrite = 0x01;

contrib/platform/src/com/sun/jna/platform/unix/X11.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class XImage extends PointerType { }
278278

279279
/** Definition (incomplete) of the Xext library. */
280280
interface Xext extends Library {
281-
Xext INSTANCE = (Xext)Native.loadLibrary("Xext", Xext.class);
281+
Xext INSTANCE = Native.loadLibrary("Xext", Xext.class);
282282
// Shape Kinds
283283
int ShapeBounding = 0;
284284
int ShapeClip = 1;
@@ -296,7 +296,7 @@ void XShapeCombineMask(Display display, Window window, int dest_kind,
296296

297297
/** Definition (incomplete) of the Xrender library. */
298298
interface Xrender extends Library {
299-
Xrender INSTANCE = (Xrender)Native.loadLibrary("Xrender", Xrender.class);
299+
Xrender INSTANCE = Native.loadLibrary("Xrender", Xrender.class);
300300
class XRenderDirectFormat extends Structure {
301301
public short red, redMask;
302302
public short green, greenMask;
@@ -338,7 +338,7 @@ protected List getFieldOrder() {
338338
/** Definition of the Xevie library. */
339339
interface Xevie extends Library {
340340
/** Instance of Xevie. Note: This extension has been removed from xorg/xserver on Oct 22, 2008 because it is broken and maintainerless. */
341-
Xevie INSTANCE = (Xevie)Native.loadLibrary("Xevie", Xevie.class);
341+
Xevie INSTANCE = Native.loadLibrary("Xevie", Xevie.class);
342342
int XEVIE_UNMODIFIED = 0;
343343
int XEVIE_MODIFIED = 1;
344344
// Bool XevieQueryVersion (Display* display, int* major_version, int* minor_version);
@@ -355,7 +355,7 @@ interface Xevie extends Library {
355355

356356
/** Definition of the XTest library. */
357357
interface XTest extends Library {
358-
XTest INSTANCE = (XTest)Native.loadLibrary("Xtst", XTest.class);///usr/lib/libxcb-xtest.so.0
358+
XTest INSTANCE = Native.loadLibrary("Xtst", XTest.class);///usr/lib/libxcb-xtest.so.0
359359
boolean XTestQueryExtension(Display display, IntByReference event_basep, IntByReference error_basep, IntByReference majorp, IntByReference minorp);
360360
boolean XTestCompareCursorWithWindow(Display display, Window window, Cursor cursor);
361361
boolean XTestCompareCurrentCursorWithWindow(Display display, Window window);
@@ -393,7 +393,7 @@ protected List getFieldOrder() {
393393
}
394394
}
395395

396-
X11 INSTANCE = (X11)Native.loadLibrary("X11", X11.class);
396+
X11 INSTANCE = Native.loadLibrary("X11", X11.class);
397397

398398
/*
399399
typedef struct {

src/com/sun/jna/Native.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public static List<String> toStringList(char[] buf, int offset, int len) {
446446
* @throws UnsatisfiedLinkError if the library cannot be found or
447447
* dependent libraries are missing.
448448
*/
449-
public static <T extends Library> T loadLibrary(Class<T> interfaceClass) {
449+
public static <T> T loadLibrary(Class<T> interfaceClass) {
450450
return loadLibrary(null, interfaceClass);
451451
}
452452

@@ -465,7 +465,7 @@ public static <T extends Library> T loadLibrary(Class<T> interfaceClass) {
465465
* dependent libraries are missing.
466466
* @see #loadLibrary(String, Class, Map)
467467
*/
468-
public static <T extends Library> T loadLibrary(Class<T> interfaceClass, Map options) {
468+
public static <T> T loadLibrary(Class<T> interfaceClass, Map options) {
469469
return loadLibrary(null, interfaceClass, options);
470470
}
471471

@@ -483,7 +483,7 @@ public static <T extends Library> T loadLibrary(Class<T> interfaceClass, Map opt
483483
* dependent libraries are missing.
484484
* @see #loadLibrary(String, Class, Map)
485485
*/
486-
public static <T extends Library> T loadLibrary(String name, Class<T> interfaceClass) {
486+
public static <T> T loadLibrary(String name, Class<T> interfaceClass) {
487487
return loadLibrary(name, interfaceClass, Collections.emptyMap());
488488
}
489489

@@ -503,7 +503,12 @@ public static <T extends Library> T loadLibrary(String name, Class<T> interfaceC
503503
* @throws UnsatisfiedLinkError if the library cannot be found or
504504
* dependent libraries are missing.
505505
*/
506-
public static <T extends Library> T loadLibrary(String name, Class<T> interfaceClass, Map options) {
506+
public static <T> T loadLibrary(String name, Class<T> interfaceClass, Map options) {
507+
if (!Library.class.isAssignableFrom(interfaceClass)) {
508+
throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")"
509+
+ " of library=" + name + " does not extend " + Library.class.getSimpleName());
510+
}
511+
507512
Library.Handler handler = new Library.Handler(name, interfaceClass, options);
508513
ClassLoader loader = interfaceClass.getClassLoader();
509514
Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler);
@@ -1396,7 +1401,7 @@ public static boolean registered(Class<?> cls) {
13961401
/** Unregister the native methods for the given class. */
13971402
private static native void unregister(Class<?> cls, long[] handles);
13981403

1399-
private static String getSignature(Class<?> cls) {
1404+
static String getSignature(Class<?> cls) {
14001405
if (cls.isArray()) {
14011406
return "[" + getSignature(cls.getComponentType());
14021407
}

0 commit comments

Comments
 (0)