diff --git a/pom.xml b/pom.xml index 23db604..cce278b 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ along with this program. If not, see . com.github.drrb.rustjava java-rust-example - 1.0-SNAPSHOT + 1.1-SNAPSHOT Java Rust Example Example of calling a Rust library from Java @@ -45,7 +45,7 @@ along with this program. If not, see . net.java.dev.jna jna - 4.0.0 + 4.5.2 @@ -61,6 +61,13 @@ along with this program. If not, see . 1.3 test + + + + javax.xml.bind + jaxb-api + 2.3.0 + @@ -70,17 +77,17 @@ along with this program. If not, see . org.apache.maven.plugins maven-compiler-plugin - 3.1 + 3.8.1 - 1.7 - 1.7 + 1.8 + 1.8 org.apache.maven.plugins maven-surefire-plugin - 2.18.1 + 2.22.2 -Djna.nosys=true @@ -93,7 +100,7 @@ along with this program. If not, see . org.codehaus.mojo build-helper-maven-plugin - 1.9.1 + 1.12 compile-build-addon @@ -120,7 +127,7 @@ along with this program. If not, see . exec-maven-plugin org.codehaus.mojo - 1.2.1 + 1.6.0 compile-rust-crates @@ -141,7 +148,7 @@ along with this program. If not, see . org.apache.maven.plugins maven-assembly-plugin - 2.5.3 + 2.6 false @@ -162,20 +169,6 @@ along with this program. If not, see . - - - org.jasig.maven - maven-notice-plugin - 1.0.4 - - - - generate - - process-resources - - - diff --git a/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java b/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java index 0942401..1dde669 100644 --- a/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java +++ b/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java @@ -27,14 +27,17 @@ import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import static java.util.Arrays.asList; +import java.util.Arrays; +import static java.util.stream.Collectors.toList; +import java.util.Comparator; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Locale; -import java.util.Map; /** - * + * Provides the functionality to compile Rust crates + * as a maven action. */ public class CompileRustCrates { @@ -45,9 +48,7 @@ public static void main(String[] args) throws Exception { Paths.get("target", "rust-libs").toFile().mkdirs(); if (changesDetected()) { System.out.println("Changes detected. Compiling all Rust crates!"); - for (Path crate : crates()) { - compile(crate); - } + crates().forEach(CompileRustCrates::compile); } else { System.out.println("No changes detected. Not recompiling Rust crates."); } @@ -107,13 +108,9 @@ private static String osArchName() { } private static List crates() throws IOException { - List crates = new LinkedList<>(); - for (Path rustSource : rustSources()) { - if (isCrate(rustSource)) { - crates.add(rustSource); - } - } - return crates; + return rustSources().stream() + .filter(CompileRustCrates::isCrate) + .collect(toList()); } private static List rustSources() throws IOException { @@ -142,14 +139,13 @@ private static List findFiles(Path startPath, FileFinder finder) throws IO } private static boolean inNetbeans() { - for (Map.Entry envVars : System.getenv().entrySet()) { - String key = envVars.getKey(); - String value = envVars.getValue(); - if (key.matches("JAVA_MAIN_CLASS_\\d+") && value.equals("org.netbeans.Main")) { - return true; - } - } - return false; + return System.getenv().entrySet() + .stream() + .anyMatch(envVars -> { + String key = envVars.getKey(); + String value = envVars.getValue(); + return key.matches("JAVA_MAIN_CLASS_\\d+") && value.equals("org.netbeans.Main"); + }); } private static boolean isRustSource(Path path, BasicFileAttributes attributes) { @@ -174,14 +170,10 @@ private static boolean isDylib(Path path, BasicFileAttributes attributes) { } private static Date newestChange(List paths) { - Date lastChange = EPOCH; - for (Path path : paths) { - Date change = mtime(path); - if (change.getTime() > lastChange.getTime()) { - lastChange = change; - } - } - return lastChange; + return paths.stream() + .map(CompileRustCrates::mtime) + .max(Comparator.comparingLong(Date::getTime)) + .orElse(EPOCH); } @SuppressWarnings("CallToPrintStackTrace") @@ -204,21 +196,13 @@ public String jnaArchString() { WINDOWS("win") { @Override public String jnaArchString() { - if (currentIs64Bit()) { - return "win32-x86-64"; - } else { - return "win32-x86"; - } + return currentIs64Bit() ? "win32-x86-64" : "win32-x86"; } }, GNU_SLASH_LINUX("nux") { @Override public String jnaArchString() { - if (currentIs64Bit()) { - return "linux-x86-64"; - } else { - return "linux-x86"; - } + return currentIs64Bit() ? "linux-x86-64" : "linux-x86"; } }, UNKNOWN() { @@ -229,28 +213,22 @@ public String jnaArchString() { }; private final String[] substrings; - private Os(String... substrings) { + Os(String... substrings) { this.substrings = substrings; } public abstract String jnaArchString(); public static Os getCurrent() { - for (Os os : values()) { - if (os.isCurrent()) { - return os; - } - } - return UNKNOWN; + return Arrays.stream(values()) + .filter(Os::isCurrent) + .findFirst() + .orElse(UNKNOWN); } public boolean isCurrent() { - for (String substring : substrings) { - if (currentOsString().contains(substring)) { - return true; - } - } - return false; + return Arrays.stream(substrings) + .anyMatch(substring -> currentOsString().contains(substring)); } private static boolean currentIs64Bit() { @@ -265,17 +243,17 @@ private static String currentOsString() { private static abstract class FileFinder implements FileVisitor { private final List found = new LinkedList<>(); - public List getFound() { + List getFound() { return found; } @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { return FileVisitResult.CONTINUE; } @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { if (accept(file, attrs)) { found.add(file); } @@ -283,12 +261,12 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO } @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { + public FileVisitResult visitFileFailed(Path file, IOException exc) { return FileVisitResult.CONTINUE; } @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + public FileVisitResult postVisitDirectory(Path dir, IOException exc) { return FileVisitResult.CONTINUE; } diff --git a/src/main/java/com/github/drrb/javarust/Greetings.java b/src/main/java/com/github/drrb/javarust/Greetings.java index c395be5..cdcdd39 100644 --- a/src/main/java/com/github/drrb/javarust/Greetings.java +++ b/src/main/java/com/github/drrb/javarust/Greetings.java @@ -36,7 +36,7 @@ public interface Greetings extends Library { * Maven will run scripts/rust-compile.sh, which will compile the crate and * copy it into target/classes/<platform-specific-name>. */ - Greetings INSTANCE = (Greetings) Native.loadLibrary(JNA_LIBRARY_NAME, Greetings.class); + Greetings INSTANCE = Native.loadLibrary(JNA_LIBRARY_NAME, Greetings.class); /** * Passing a parameter to a Rust function diff --git a/src/main/java/com/github/drrb/javarust/Main.java b/src/main/java/com/github/drrb/javarust/Main.java index 1201620..f7b6d1f 100644 --- a/src/main/java/com/github/drrb/javarust/Main.java +++ b/src/main/java/com/github/drrb/javarust/Main.java @@ -28,11 +28,7 @@ public class Main { public static void main(String[] args) { List arguments = asList(args); - if (arguments.isEmpty()) { - name = "World"; - } else { - name = arguments.get(0); - } + name = arguments.isEmpty() ? "World" : arguments.get(0); Greetings.INSTANCE.printGreeting(name); } diff --git a/src/test/java/com/github/drrb/javarust/GreetingsTest.java b/src/test/java/com/github/drrb/javarust/GreetingsTest.java index 0cb09e8..87dcea0 100644 --- a/src/test/java/com/github/drrb/javarust/GreetingsTest.java +++ b/src/test/java/com/github/drrb/javarust/GreetingsTest.java @@ -27,6 +27,7 @@ import java.util.List; import static com.github.drrb.javarust.test.Matchers.is; +import static java.util.stream.Collectors.toList; import static org.hamcrest.Matchers.contains; import static org.junit.Assert.assertThat; @@ -96,10 +97,9 @@ public void apply(GreetingSet.ByReference greetingSet) { } }); - List greetingStrings = new LinkedList<>(); - for (Greeting greeting : greetings) { - greetingStrings.add(greeting.getText()); - } + List greetingStrings = greetings.stream() + .map(Greeting::getText) + .collect(toList()); assertThat(greetingStrings, contains("Hello!", "Hello again!")); } @@ -107,10 +107,9 @@ public void apply(GreetingSet.ByReference greetingSet) { @Test public void shouldGetAStructFromRustContainingAnArrayOfStructs() { try (GreetingSet result = library.renderGreetings()) { - List greetings = new LinkedList<>(); - for (Greeting greeting : result.getGreetings()) { - greetings.add(greeting.getText()); - } + List greetings = result.getGreetings().stream() + .map(Greeting::getText) + .collect(toList()); assertThat(greetings, contains("Hello!", "Hello again!")); }