Use lambdas when possible

Replace anonymous inner classes with lambda declarations (when possible
using method references).

See gh-9781
This commit is contained in:
Emanuel Campolo
2017-07-24 23:11:47 -07:00
committed by Phillip Webb
parent d16af43664
commit 2626a3a795
150 changed files with 983 additions and 2596 deletions

View File

@@ -110,12 +110,7 @@ public class JarWriter implements LoaderClassesWriter, AutoCloseable {
*/
public void writeManifest(final Manifest manifest) throws IOException {
JarArchiveEntry entry = new JarArchiveEntry("META-INF/MANIFEST.MF");
writeEntry(entry, new EntryWriter() {
@Override
public void write(OutputStream outputStream) throws IOException {
manifest.write(outputStream);
}
});
writeEntry(entry, manifest::write);
}
/**

View File

@@ -29,10 +29,7 @@ public interface Libraries {
/**
* Represents no libraries.
*/
Libraries NONE = new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
}
Libraries NONE = (callback) -> {
};
/**

View File

@@ -61,19 +61,17 @@ public abstract class MainClassFinder {
private static final String MAIN_METHOD_NAME = "main";
private static final FileFilter CLASS_FILE_FILTER = new FileFilter() {
@Override
public boolean accept(File file) {
return (file.isFile() && file.getName().endsWith(DOT_CLASS));
}
};
private static final FileFilter CLASS_FILE_FILTER = MainClassFinder::isClassFile;
private static final FileFilter PACKAGE_FOLDER_FILTER = new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory() && !file.getName().startsWith(".");
}
};
private static final FileFilter PACKAGE_FOLDER_FILTER = MainClassFinder::isPackageFolder;
private static boolean isClassFile(File file) {
return file.isFile() && file.getName().endsWith(DOT_CLASS);
}
private static boolean isPackageFolder(File file) {
return file.isDirectory() && !file.getName().startsWith(".");
}
/**
* Find the main class from a given folder.
@@ -82,12 +80,7 @@ public abstract class MainClassFinder {
* @throws IOException if the folder cannot be read
*/
public static String findMainClass(File rootFolder) throws IOException {
return doWithMainClasses(rootFolder, new MainClassCallback<String>() {
@Override
public String doWith(MainClass mainClass) {
return mainClass.getName();
}
});
return doWithMainClasses(rootFolder, MainClass::getName);
}
/**
@@ -163,12 +156,7 @@ public abstract class MainClassFinder {
}
private static void pushAllSorted(Deque<File> stack, File[] files) {
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(files, Comparator.comparing(File::getName));
for (File file : files) {
stack.push(file);
}
@@ -183,13 +171,7 @@ public abstract class MainClassFinder {
*/
public static String findMainClass(JarFile jarFile, String classesLocation)
throws IOException {
return doWithMainClasses(jarFile, classesLocation,
new MainClassCallback<String>() {
@Override
public String doWith(MainClass mainClass) {
return mainClass.getName();
}
});
return doWithMainClasses(jarFile, classesLocation, MainClass::getName);
}
/**

View File

@@ -230,21 +230,16 @@ public class Repackager {
try (JarWriter writer = new JarWriter(destination, launchScript)) {
final List<Library> unpackLibraries = new ArrayList<>();
final List<Library> standardLibraries = new ArrayList<>();
libraries.doWithLibraries(new LibraryCallback() {
@Override
public void library(Library library) throws IOException {
File file = library.getFile();
if (isZip(file)) {
if (library.isUnpackRequired()) {
unpackLibraries.add(library);
}
else {
standardLibraries.add(library);
}
libraries.doWithLibraries((library) -> {
File file = library.getFile();
if (isZip(file)) {
if (library.isUnpackRequired()) {
unpackLibraries.add(library);
}
else {
standardLibraries.add(library);
}
}
});
repackage(sourceJar, writer, unpackLibraries, standardLibraries);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -87,12 +87,7 @@ public class RunProcess {
if (!inheritedIO) {
redirectOutput(process);
}
SignalUtils.attachSignalHandler(new Runnable() {
@Override
public void run() {
handleSigInt();
}
});
SignalUtils.attachSignalHandler(this::handleSigInt);
if (waitForProcess) {
try {
return process.waitFor();
@@ -154,25 +149,20 @@ public class RunProcess {
private void redirectOutput(Process process) {
final BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
new Thread() {
@Override
public void run() {
try {
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
System.out.flush();
}
reader.close();
}
catch (Exception ex) {
// Ignore
new Thread(() -> {
try {
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
System.out.flush();
}
reader.close();
}
}.start();
catch (Exception ex) {
// Ignore
}
}).start();
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
package org.springframework.boot.loader.tools;
import sun.misc.Signal;
import sun.misc.SignalHandler;
/**
* Utilities for working with signal handling.
@@ -39,12 +38,7 @@ public final class SignalUtils {
* @param runnable the runnable to call on SIGINT.
*/
public static void attachSignalHandler(final Runnable runnable) {
Signal.handle(SIG_INT, new SignalHandler() {
@Override
public void handle(Signal signal) {
runnable.run();
}
});
Signal.handle(SIG_INT, (signal) -> runnable.run());
}
}

View File

@@ -56,10 +56,7 @@ import static org.mockito.Mockito.mock;
*/
public class RepackagerTests {
private static final Libraries NO_LIBRARIES = new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
}
private static final Libraries NO_LIBRARIES = (callback) -> {
};
private static final long JAN_1_1980;
@@ -301,14 +298,10 @@ public class RepackagerTests {
File file = this.testJarFile.getFile();
libJarFile.setLastModified(JAN_1_1980);
Repackager repackager = new Repackager(file);
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
callback.library(new Library(libJarFile, LibraryScope.COMPILE));
callback.library(
new Library(libJarFileToUnpack, LibraryScope.COMPILE, true));
callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
}
repackager.repackage((callback) -> {
callback.library(new Library(libJarFile, LibraryScope.COMPILE));
callback.library(new Library(libJarFileToUnpack, LibraryScope.COMPILE, true));
callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
});
assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFile.getName())).isTrue();
assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName()))
@@ -331,12 +324,9 @@ public class RepackagerTests {
Repackager repackager = new Repackager(file);
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage("Duplicate library");
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
}
repackager.repackage((callback) -> {
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
});
}
@@ -355,14 +345,8 @@ public class RepackagerTests {
given(layout.getLibraryDestination(anyString(), eq(LibraryScope.COMPILE)))
.willReturn("test-lib/");
repackager.setLayout(layout);
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
callback.library(new Library(libJarFile, scope));
}
});
repackager.repackage(
(callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(hasEntry(file, "test/" + libJarFile.getName())).isTrue();
assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib"))
.isEqualTo("test-lib/");
@@ -382,14 +366,8 @@ public class RepackagerTests {
final LibraryScope scope = mock(LibraryScope.class);
given(layout.getLauncherClassName()).willReturn("testLauncher");
repackager.setLayout(layout);
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
callback.library(new Library(libJarFile, scope));
}
});
repackager.repackage(
(callback) -> callback.library(new Library(libJarFile, scope)));
assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib"))
.isNull();
assertThat(getManifest(file).getMainAttributes().getValue("Main-Class"))
@@ -447,17 +425,13 @@ public class RepackagerTests {
public void dontRecompressZips() throws Exception {
TestJarFile nested = new TestJarFile(this.temporaryFolder);
nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
final File nestedFile = nested.getFile();
File nestedFile = nested.getFile();
this.testJarFile.addFile("test/nested.jar", nestedFile);
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
callback.library(new Library(nestedFile, LibraryScope.COMPILE));
}
});
repackager.repackage((callback) -> callback
.library(new Library(nestedFile, LibraryScope.COMPILE)));
try (JarFile jarFile = new JarFile(file)) {
assertThat(
@@ -494,25 +468,16 @@ public class RepackagerTests {
throws Exception {
TestJarFile nested = new TestJarFile(this.temporaryFolder);
nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
final File nestedFile = nested.getFile();
this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(),
nested.getFile());
File nestedFile = nested.getFile();
String name = "BOOT-INF/lib/" + nestedFile.getName();
this.testJarFile.addFile(name, nested.getFile());
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
callback.library(new Library(nestedFile, LibraryScope.COMPILE, true));
}
});
repackager.repackage((callback) -> callback
.library(new Library(nestedFile, LibraryScope.COMPILE, true)));
try (JarFile jarFile = new JarFile(file)) {
assertThat(
jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getComment())
.startsWith("UNPACK:");
assertThat(jarFile.getEntry(name).getComment()).startsWith("UNPACK:");
}
}
@@ -521,23 +486,18 @@ public class RepackagerTests {
throws Exception {
TestJarFile nested = new TestJarFile(this.temporaryFolder);
nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
final File nestedFile = nested.getFile();
File nestedFile = nested.getFile();
this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(),
nested.getFile());
this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
File file = this.testJarFile.getFile();
Repackager repackager = new Repackager(file);
long sourceLength = nestedFile.length();
repackager.repackage(new Libraries() {
@Override
public void doWithLibraries(LibraryCallback callback) throws IOException {
nestedFile.delete();
File toZip = RepackagerTests.this.temporaryFolder.newFile();
ZipUtil.packEntry(toZip, nestedFile);
callback.library(new Library(nestedFile, LibraryScope.COMPILE));
}
repackager.repackage((callback) -> {
nestedFile.delete();
File toZip = RepackagerTests.this.temporaryFolder.newFile();
ZipUtil.packEntry(toZip, nestedFile);
callback.library(new Library(nestedFile, LibraryScope.COMPILE));
});
try (JarFile jarFile = new JarFile(file)) {
assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getSize())