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());
}
}