Skip to content

Commit

Permalink
Merge pull request #566 from lgoldstein/load-library-fix
Browse files Browse the repository at this point in the history
Fix return type of Native#loadLibrary to match unconstrained generic …
  • Loading branch information
Lyor Goldstein committed Dec 17, 2015
2 parents 38614fd + 93ca701 commit 27e40c2
Show file tree
Hide file tree
Showing 28 changed files with 719 additions and 437 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Bug Fixes
* [#549](https://github.com/java-native-access/jna/pull/549): Fixed bug in types derived from XID - [@twall](https://github.com/twall).
* [#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).
* [#531](https://github.com/java-native-access/jna/pull/531): Ensure direct-mapped callbacks use the right calling convention - [@twall](https://github.com/twall).
* [#566](https://github.com/java-native-access/jna/pull/566): Fix return type of Native#loadLibrary to match unconstrained generic [@lgoldstein](https://github.com/lgoldstein)

Release 4.2.1
=============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class MacFileUtils extends FileUtils {

public interface FileManager extends Library {

public FileManager INSTANCE = (FileManager)Native.loadLibrary("CoreServices", FileManager.class);
FileManager INSTANCE = Native.loadLibrary("CoreServices", FileManager.class);

int kFSFileOperationDefaultOptions = 0;
int kFSFileOperationsOverwrite = 0x01;
Expand Down
10 changes: 5 additions & 5 deletions contrib/platform/src/com/sun/jna/platform/unix/X11.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class XImage extends PointerType { }

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

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

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

X11 INSTANCE = (X11)Native.loadLibrary("X11", X11.class);
X11 INSTANCE = Native.loadLibrary("X11", X11.class);

/*
typedef struct {
Expand Down
31 changes: 15 additions & 16 deletions contrib/platform/src/com/sun/jna/platform/win32/Msi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,78 +20,77 @@
*/
public interface Msi extends StdCallLibrary {

Msi INSTANCE = (Msi)
Native.loadLibrary("msi", Msi.class, W32APIOptions.DEFAULT_OPTIONS);
Msi INSTANCE = Native.loadLibrary("msi", Msi.class, W32APIOptions.DEFAULT_OPTIONS);

/**
* The component being requested is disabled on the computer.
*/
static int INSTALLSTATE_NOTUSED = -7;
int INSTALLSTATE_NOTUSED = -7;

/**
* The configuration data is corrupt.
*/
static int INSTALLSTATE_BADCONFIG = -6;
int INSTALLSTATE_BADCONFIG = -6;

/**
* The installation is suspended or in progress.
*/
static int INSTALLSTATE_INCOMPLETE = -5;
int INSTALLSTATE_INCOMPLETE = -5;

/**
* The feature must run from the source, and the source is unavailable.
*/
static int INSTALLSTATE_SOURCEABSENT = -4;
int INSTALLSTATE_SOURCEABSENT = -4;

/**
* The return buffer is full.
*/
static int INSTALLSTATE_MOREDATA = -3;
int INSTALLSTATE_MOREDATA = -3;

/**
* An invalid parameter was passed to the function.
*/
static int INSTALLSTATE_INVALIDARG = -2;
int INSTALLSTATE_INVALIDARG = -2;

/**
* An unrecognized product or feature was specified.
*/
static int INSTALLSTATE_UNKNOWN = -1;
int INSTALLSTATE_UNKNOWN = -1;

/**
* The feature is broken.
*/
static int INSTALLSTATE_BROKEN = 0;
int INSTALLSTATE_BROKEN = 0;

/**
* The advertised feature.
*/
static int INSTALLSTATE_ADVERTISED = 1;
int INSTALLSTATE_ADVERTISED = 1;

/**
* The component is being removed.
*/
static int INSTALLSTATE_REMOVED = 1;
int INSTALLSTATE_REMOVED = 1;

/**
* The feature was uninstalled.
*/
static int INSTALLSTATE_ABSENT = 2;
int INSTALLSTATE_ABSENT = 2;

/**
* The feature was installed on the local drive.
*/
static int INSTALLSTATE_LOCAL = 3;
int INSTALLSTATE_LOCAL = 3;

/**
* The feature must run from the source, CD-ROM, or network.
*/
static int INSTALLSTATE_SOURCE = 4;
int INSTALLSTATE_SOURCE = 4;

/**
* The feature is installed in the default location: local or source.
*/
static int INSTALLSTATE_DEFAULT = 5;
int INSTALLSTATE_DEFAULT = 5;

/**
* The MsiGetComponentPath function returns the full path to an installed component. If the key path for the
Expand Down
16 changes: 6 additions & 10 deletions contrib/platform/src/com/sun/jna/platform/win32/SetupApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
*/
public interface SetupApi extends StdCallLibrary {

SetupApi INSTANCE = (SetupApi)
Native.loadLibrary("setupapi", SetupApi.class, W32APIOptions.DEFAULT_OPTIONS);
SetupApi INSTANCE = Native.loadLibrary("setupapi", SetupApi.class, W32APIOptions.DEFAULT_OPTIONS);

/**
* The GUID_DEVINTERFACE_DISK device interface class is defined for hard disk storage devices.
Expand Down Expand Up @@ -82,7 +81,6 @@ public interface SetupApi extends StdCallLibrary {
* Removable.
*/
int CM_DEVCAP_REMOVABLE = 0x00000004;


/** make change in all hardware profiles */
int DICS_FLAG_GLOBAL = 0x00000001;
Expand All @@ -94,23 +92,22 @@ public interface SetupApi extends StdCallLibrary {
/**
* Open/Create/Delete device key.
*
* @see #SetupDiOpenDevRegKey
* @see #SetupDiOpenDevRegKey
*/

int DIREG_DEV = 0x00000001;

/**
* Open/Create/Delete driver key
*
* @see #SetupDiOpenDevRegKey
* @see #SetupDiOpenDevRegKey
*/

int DIREG_DRV = 0x00000002;

/**
* Delete both driver and Device key
*
* @see #SetupDiOpenDevRegKey
* @see #SetupDiOpenDevRegKey
*/

int DIREG_BOTH = 0x00000004;

/**
Expand All @@ -124,7 +121,6 @@ public interface SetupApi extends StdCallLibrary {
*/
int SPDRP_DEVICEDESC = 0x00000000;


/**
* The SetupDiGetClassDevs function returns a handle to a device information set that contains requested device
* information elements for a local computer.
Expand Down
3 changes: 1 addition & 2 deletions contrib/platform/src/com/sun/jna/platform/win32/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
*/
public interface Version extends StdCallLibrary {

Version INSTANCE = (Version)
Native.loadLibrary("version", Version.class, W32APIOptions.DEFAULT_OPTIONS);
Version INSTANCE = Native.loadLibrary("version", Version.class, W32APIOptions.DEFAULT_OPTIONS);

/**
* Determines whether the operating system can retrieve version information for a specified file. If version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ public static void main(String[] args) {
}

public void testGetFileVersionNumbers() {
String testFileName = "regedit.exe";
File file = new File(System.getenv("SystemRoot"), testFileName);
assertTrue("Test file with version info in it should exist.", file.exists());
File file = new File(System.getenv("SystemRoot"), "regedit.exe");
assertTrue("Test file with version info in it should exist: " + file, file.exists());

VS_FIXEDFILEINFO version = VersionUtil.getFileVersionInfo(file.getAbsolutePath());
assertNotNull("Version info should have been returned.", version);

assertTrue("The major file version number should be greater than 0 when pulling version from \"" + testFileName + "\"", version.getFileVersionMajor() > 0);
assertTrue("The minor file version number should be greater than or equal to 0 when pulling version from \"" + testFileName + "\"", version.getFileVersionMinor() >= 0);
assertTrue("The revision file version number should be greater than or equal to 0 when pulling version from \"" + testFileName + "\"", version.getFileVersionRevision() >= 0);
assertTrue("The build file version number should be greater than or equal to 0 when pulling version from \"" + testFileName + "\"", version.getFileVersionBuild() > 0);
assertTrue("The major product version number should be greater than 0 when pulling version from \"" + testFileName + "\"", version.getProductVersionMajor() > 0);
assertTrue("The minor product version number should be greater than or equal to 0 when pulling version from \"" + testFileName + "\"", version.getProductVersionMinor() >= 0);
assertTrue("The revision product version number should be greater than or equal to 0 when pulling version from \"" + testFileName + "\"", version.getProductVersionRevision() >= 0);
assertTrue("The build product version number should be greater than or equal to 0 when pulling version from \"" + testFileName + "\"", version.getProductVersionBuild() > 0);
assertTrue("The major file version number should be greater than 0 when pulling version from \"" + file + "\"", version.getFileVersionMajor() > 0);
assertTrue("The minor file version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getFileVersionMinor() >= 0);
assertTrue("The revision file version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getFileVersionRevision() >= 0);
assertTrue("The build file version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getFileVersionBuild() > 0);

assertTrue("The major product version number should be greater than 0 when pulling version from \"" + file + "\"", version.getProductVersionMajor() > 0);
assertTrue("The minor product version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getProductVersionMinor() >= 0);
assertTrue("The revision product version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getProductVersionRevision() >= 0);
assertTrue("The build product version number should be greater than or equal to 0 when pulling version from \"" + file + "\"", version.getProductVersionBuild() > 0);
}
}
15 changes: 10 additions & 5 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public static List<String> toStringList(char[] buf, int offset, int len) {
* @throws UnsatisfiedLinkError if the library cannot be found or
* dependent libraries are missing.
*/
public static <T extends Library> T loadLibrary(Class<T> interfaceClass) {
public static <T> T loadLibrary(Class<T> interfaceClass) {
return loadLibrary(null, interfaceClass);
}

Expand All @@ -465,7 +465,7 @@ public static <T extends Library> T loadLibrary(Class<T> interfaceClass) {
* dependent libraries are missing.
* @see #loadLibrary(String, Class, Map)
*/
public static <T extends Library> T loadLibrary(Class<T> interfaceClass, Map options) {
public static <T> T loadLibrary(Class<T> interfaceClass, Map options) {
return loadLibrary(null, interfaceClass, options);
}

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

Expand All @@ -503,7 +503,12 @@ public static <T extends Library> T loadLibrary(String name, Class<T> interfaceC
* @throws UnsatisfiedLinkError if the library cannot be found or
* dependent libraries are missing.
*/
public static <T extends Library> T loadLibrary(String name, Class<T> interfaceClass, Map options) {
public static <T> T loadLibrary(String name, Class<T> interfaceClass, Map options) {
if (!Library.class.isAssignableFrom(interfaceClass)) {
throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")"
+ " of library=" + name + " does not extend " + Library.class.getSimpleName());
}

Library.Handler handler = new Library.Handler(name, interfaceClass, options);
ClassLoader loader = interfaceClass.getClassLoader();
Object proxy = Proxy.newProxyInstance(loader, new Class[] {interfaceClass}, handler);
Expand Down Expand Up @@ -1396,7 +1401,7 @@ public static boolean registered(Class<?> cls) {
/** Unregister the native methods for the given class. */
private static native void unregister(Class<?> cls, long[] handles);

private static String getSignature(Class<?> cls) {
static String getSignature(Class<?> cls) {
if (cls.isArray()) {
return "[" + getSignature(cls.getComponentType());
}
Expand Down
3 changes: 1 addition & 2 deletions test/com/sun/jna/AnnotatedLibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public Class nativeType() {
});

options.put(Library.OPTION_TYPE_MAPPER, mapper);
AnnotationTestLibrary lib = (AnnotationTestLibrary)
Native.loadLibrary("testlib", AnnotationTestLibrary.class, options);
AnnotationTestLibrary lib = Native.loadLibrary("testlib", AnnotationTestLibrary.class, options);
assertEquals("Failed to convert integer return to boolean TRUE", true,
lib.returnInt32Argument(true));
assertTrue("Failed to get annotation from ParameterContext", hasAnnotation[0]);
Expand Down
Loading

0 comments on commit 27e40c2

Please # to comment.