[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 5a8f420807
commit 3dddb550b6
28 changed files with 225 additions and 16 deletions

39
java-native/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>java-native</artifactId>
<name>java-native</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<jna.version>5.6.0</jna.version>
</properties>
<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>${jna.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- we don't want to reuse JVMs here ! -->
<reuseForks>false</reuseForks>
</configuration>
</plugin>
</plugins>
</build>
</project>

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,18 @@
package com.baeldung.jna;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import com.sun.jna.Native;
import com.sun.jna.Platform;
class CMathUnitTest {
@Test
void whenCallNative_thenSuccess() {
CMath lib = Native.load(Platform.isWindows() ? "msvcrt" : "c", CMath.class);
double result = lib.cosh(0);
assertEquals(1.0,result);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.jna;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.jupiter.api.Test;
import com.baeldung.jna.NativeFS.Stat;
import com.sun.jna.LastErrorException;
import com.sun.jna.Platform;
public class NativeFSUnitTest {
@Test
public void whenCallNative_thenSuccess() throws IOException {
NativeFS lib = NativeFS.INSTANCE;
File f = Files.createTempFile("junit", ".bin").toFile();
f.deleteOnExit();
Stat stat = new Stat();
try {
if (Platform.isWindows()) {
int rc = lib.stat(f.getAbsolutePath(), stat);
assertEquals(0, rc);
assertEquals(0,stat.st_size.longValue());
}
}
catch(LastErrorException error) {
fail("stat failed: error code=" + error.getErrorCode());
}
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.jna;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Test;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
class StdCUnitTest {
@BeforeClass
public static void setupProtectedMode() {
Native.setProtected(true);
}
@Test
public void whenMalloc_thenSuccess() {
StdC lib = StdC.INSTANCE;
Pointer p = lib.malloc(1024);
p.setMemory(0l, 1024l, (byte) 0);
lib.free(p);
}
@Test
public void whenAccessViolation_thenShouldThrowError() {
// Running this test on Linux requires additional setup using libjsig.so
// Details here: http://java-native-access.github.io/jna/5.6.0/javadoc/overview-summary.html#crash-protection
// IMPORTANT NOTICE: Code for illustration purposes only. DON'T DO THIS IN YOUR OWN CODE
if ( Platform.isWindows()) {
Error e = null;
Pointer p = new Pointer(0l);
try {
p.setMemory(0, 100*1024, (byte) 0);
}
catch(Error err) {
e = err;
}
assertNotNull(e, "Should throw Error");
}
}
}

Binary file not shown.

Binary file not shown.

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>jni</artifactId>
<name>jni</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -465,7 +465,7 @@
<module>jjwt</module>
<module>jmeter</module>
<module>jmh</module>
<module>jni</module>
<module>java-native</module>
<module>jooby</module>
<module>jsf</module>
<module>json</module>