Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

<groupId>com.github.drrb.rustjava</groupId>
<artifactId>java-rust-example</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1-SNAPSHOT</version>

<name>Java Rust Example</name>
<description>Example of calling a Rust library from Java</description>
Expand All @@ -45,7 +45,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.0.0</version>
<version>4.5.2</version>
</dependency>

<!-- Test Dependencies -->
Expand All @@ -61,6 +61,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<version>1.3</version>
<scope>test</scope>
</dependency>

<!-- Direct dependencies on modules that used to be part of the Java SE, for Java 9+ support -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -70,17 +77,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<version>2.22.2</version>
<configuration>
<!-- Don't try to load JNA from the host system, use our dependency instead (needed for Appveyor, which has an old JNA on it) -->
<argLine>-Djna.nosys=true</argLine>
Expand All @@ -93,7 +100,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<version>1.12</version>
<executions>
<execution>
<id>compile-build-addon</id>
Expand All @@ -120,7 +127,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.2.1</version>
<version>1.6.0</version>
<executions>
<execution>
<id>compile-rust-crates</id>
Expand All @@ -141,7 +148,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<version>2.6</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
Expand All @@ -162,20 +169,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.jasig.maven</groupId>
<artifactId>maven-notice-plugin</artifactId>
<version>1.0.4</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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.");
}
Expand Down Expand Up @@ -107,13 +108,9 @@ private static String osArchName() {
}

private static List<Path> crates() throws IOException {
List<Path> 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<Path> rustSources() throws IOException {
Expand Down Expand Up @@ -142,14 +139,13 @@ private static List<Path> findFiles(Path startPath, FileFinder finder) throws IO
}

private static boolean inNetbeans() {
for (Map.Entry<String, String> 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) {
Expand All @@ -174,14 +170,10 @@ private static boolean isDylib(Path path, BasicFileAttributes attributes) {
}

private static Date newestChange(List<Path> 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")
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -265,30 +243,30 @@ private static String currentOsString() {
private static abstract class FileFinder implements FileVisitor<Path> {
private final List<Path> found = new LinkedList<>();

public List<Path> getFound() {
List<Path> 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);
}
return FileVisitResult.CONTINUE;
}

@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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/drrb/javarust/Greetings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/&lt;platform-specific-name&gt;.
*/
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
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/com/github/drrb/javarust/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public class Main {

public static void main(String[] args) {
List<String> arguments = asList(args);
if (arguments.isEmpty()) {
name = "World";
} else {
name = arguments.get(0);
}
name = arguments.isEmpty() ? "World" : arguments.get(0);
Greetings.INSTANCE.printGreeting(name);
}

Expand Down
15 changes: 7 additions & 8 deletions src/test/java/com/github/drrb/javarust/GreetingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -96,21 +97,19 @@ public void apply(GreetingSet.ByReference greetingSet) {
}
});

List<String> greetingStrings = new LinkedList<>();
for (Greeting greeting : greetings) {
greetingStrings.add(greeting.getText());
}
List<String> greetingStrings = greetings.stream()
.map(Greeting::getText)
.collect(toList());

assertThat(greetingStrings, contains("Hello!", "Hello again!"));
}

@Test
public void shouldGetAStructFromRustContainingAnArrayOfStructs() {
try (GreetingSet result = library.renderGreetings()) {
List<String> greetings = new LinkedList<>();
for (Greeting greeting : result.getGreetings()) {
greetings.add(greeting.getText());
}
List<String> greetings = result.getGreetings().stream()
.map(Greeting::getText)
.collect(toList());

assertThat(greetings, contains("Hello!", "Hello again!"));
}
Expand Down