[BAEL-4204] JNA (#10113)

* [BAEL-4203] JNA

* [BAEL-4203] JNA
This commit is contained in:
psevestre
2020-10-01 17:13:15 -03:00
committed by GitHub
parent fa3946cae7
commit 9466ddeb74
29 changed files with 225 additions and 16 deletions

View File

@@ -0,0 +1,10 @@
package com.baeldung.jna;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public interface CMath extends Library {
CMath INSTANCE = Native.load(Platform.isWindows() ? "msvcrt" : "c", CMath.class);
double cosh(double value);
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.jna;
public class Main {
public static void main(String[] args) {
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.jna;
import java.util.Collections;
import java.util.Map;
import com.sun.jna.FunctionMapper;
import com.sun.jna.LastErrorException;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.Structure;
import com.sun.jna.Structure.FieldOrder;
public interface NativeFS extends Library {
FunctionMapper mapper = (library,method) -> {
if (Platform.isWindows()) {
return "_" + method.getName();
}
else {
return "__x" + method.getName(); // On Linux, stat is actually _xstat
}
};
public NativeFS INSTANCE = Native.load(Platform.isWindows() ? "msvcrt" : "c",
NativeFS.class,
Collections.singletonMap(Library.OPTION_FUNCTION_MAPPER, mapper));
int stat(String path, Stat stat) throws LastErrorException;
@FieldOrder({"st_dev","st_ino","st_mode","st_nlink","st_uid","st_gid","st_rdev","st_size","st_atime","st_mtime","st_ctime"})
public class Stat extends Structure {
public int st_dev;
public int st_ino;
public short st_mode;
public short st_nlink;
public short st_uid;
public short st_gid;
public int st_rdev;
public NativeLong st_size;
public NativeLong st_atime;
public NativeLong st_mtime;
public NativeLong st_ctime;
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.jna;
import com.sun.jna.LastErrorException;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
public interface StdC extends Library {
StdC INSTANCE = Native.load(Platform.isWindows() ? "msvcrt" : "c", StdC.class );
Pointer malloc(long n);
void free(Pointer p);
Pointer memset(Pointer p, int c, long n);
int open(String path, int flags) throws LastErrorException;
int close(int fd) throws LastErrorException;
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.jni;
public class ExampleObjectsJNI {
static {
System.loadLibrary("native");
}
public static void main(String[] args) {
ExampleObjectsJNI instance = new ExampleObjectsJNI();
UserData newUser = instance.createUser("John Doe", 450.67);
instance.printUserData(newUser);
}
public native UserData createUser(String name, double balance);
public native String printUserData(UserData user);
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.jni;
public class ExampleParametersJNI {
static {
System.loadLibrary("native");
}
public static void main(String[] args) {
System.out.println("Java: My full name: " + new ExampleParametersJNI().sayHelloToMe("Martin", false));
long sumFromNative = new ExampleParametersJNI().sumIntegers(456, 44);
System.out.println("Java: The sum coming from native code is: " + sumFromNative);
}
// Declare another method sumIntegers that receives two integers and return a long with the sum
public native long sumIntegers(int first, int second);
// Declare another method sayHelloToMe that receives the name and gender and returns the proper salutation
public native String sayHelloToMe(String name, boolean isFemale);
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.jni;
public class HelloWorldJNI {
static {
System.loadLibrary("native");
}
public static void main(String[] args) {
new HelloWorldJNI().sayHello();
}
// Declare a native method sayHello() that receives no arguments and returns void
public native String sayHello();
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.jni;
public class UserData {
public String name;
public double balance;
public String getUserInfo() {
return "[name]=" + name + ", [balance]=" + balance;
}
}