Skip to content

loadLibrary method signature should check that the class extends Library at compile-time, not runtime #822

@hakanai

Description

@hakanai

loadLibrary as of v4.4.0 currently looks like this:

{code}
public static T loadLibrary(String name, Class interfaceClass, Map<String, ?> options) {
if (!Library.class.isAssignableFrom(interfaceClass)) {
throw new IllegalArgumentException("Interface (" + interfaceClass.getSimpleName() + ")"
+ " of library=" + name + " does not extend " + Library.class.getSimpleName());
}
{code}

This allows code to compile but then throws an error at runtime, which is trappy. We hit this when we had a library defined as:

{code}
public interface Kernel32Custom extends WinNT
{code}

WinNT did extend Library in v4.0.0 but does not in v4.4.0.

Instead, the signature should just take <T extends Library>.

{code}
public static T loadLibrary(String name, Class interfaceClass, Map<String, ?> options) {
{code}

Then this change would have shown up as a compile-time error, making it possible to catch the issue much sooner.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions