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

@@ -22,8 +22,6 @@ import java.util.jar.JarEntry;
import java.util.jar.Manifest;
import org.springframework.boot.loader.archive.Archive;
import org.springframework.boot.loader.archive.Archive.Entry;
import org.springframework.boot.loader.archive.Archive.EntryFilter;
/**
* Base class for executable archive {@link Launcher}s.
@@ -69,14 +67,7 @@ public abstract class ExecutableArchiveLauncher extends Launcher {
@Override
protected List<Archive> getClassPathArchives() throws Exception {
List<Archive> archives = new ArrayList<>(
this.archive.getNestedArchives(new EntryFilter() {
@Override
public boolean matches(Entry entry) {
return isNestedArchive(entry);
}
}));
this.archive.getNestedArchives(this::isNestedArchive));
postProcessClassPathArchives(archives);
return archives;
}

View File

@@ -128,32 +128,28 @@ public class LaunchedURLClassLoader extends URLClassLoader {
private void definePackage(final String className, final String packageName) {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws ClassNotFoundException {
String packageEntryName = packageName.replace('.', '/') + "/";
String classEntryName = className.replace('.', '/') + ".class";
for (URL url : getURLs()) {
try {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
JarFile jarFile = ((JarURLConnection) connection)
.getJarFile();
if (jarFile.getEntry(classEntryName) != null
&& jarFile.getEntry(packageEntryName) != null
&& jarFile.getManifest() != null) {
definePackage(packageName, jarFile.getManifest(),
url);
return null;
}
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
String packageEntryName = packageName.replace('.', '/') + "/";
String classEntryName = className.replace('.', '/') + ".class";
for (URL url : getURLs()) {
try {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
JarFile jarFile = ((JarURLConnection) connection)
.getJarFile();
if (jarFile.getEntry(classEntryName) != null
&& jarFile.getEntry(packageEntryName) != null
&& jarFile.getManifest() != null) {
definePackage(packageName, jarFile.getManifest(), url);
return null;
}
}
catch (IOException ex) {
// Ignore
}
}
return null;
catch (IOException ex) {
// Ignore
}
}
return null;
}, AccessController.getContext());
}
catch (java.security.PrivilegedActionException ex) {

View File

@@ -537,16 +537,11 @@ public class PropertiesLauncher extends Launcher {
// directories, meaning we are running from an executable JAR. We add nested
// entries from there with low priority (i.e. at end).
try {
lib.addAll(this.parent.getNestedArchives(new EntryFilter() {
@Override
public boolean matches(Entry entry) {
if (entry.isDirectory()) {
return entry.getName().equals(JarLauncher.BOOT_INF_CLASSES);
}
return entry.getName().startsWith(JarLauncher.BOOT_INF_LIB);
lib.addAll(this.parent.getNestedArchives((entry) -> {
if (entry.isDirectory()) {
return entry.getName().equals(JarLauncher.BOOT_INF_CLASSES);
}
return entry.getName().startsWith(JarLauncher.BOOT_INF_LIB);
}));
}
catch (IOException ex) {

View File

@@ -258,16 +258,11 @@ public class JarFile extends java.util.jar.JarFile {
private JarFile createJarFileFromDirectoryEntry(JarEntry entry) throws IOException {
final AsciiBytes sourceName = new AsciiBytes(entry.getName());
JarEntryFilter filter = new JarEntryFilter() {
@Override
public AsciiBytes apply(AsciiBytes name) {
if (name.startsWith(sourceName) && !name.equals(sourceName)) {
return name.substring(sourceName.length());
}
return null;
JarEntryFilter filter = (name) -> {
if (name.startsWith(sourceName) && !name.equals(sourceName)) {
return name.substring(sourceName.length());
}
return null;
};
return new JarFile(this.rootFile,
this.pathFromRoot + "!/"

View File

@@ -26,7 +26,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@@ -38,8 +37,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.loader.data.RandomAccessData.ResourceAccess;
import org.springframework.boot.loader.data.RandomAccessDataFile.FilePool;
@@ -290,17 +287,12 @@ public class RandomAccessDataFileTests {
ExecutorService executorService = Executors.newFixedThreadPool(20);
List<Future<Boolean>> results = new ArrayList<>();
for (int i = 0; i < 100; i++) {
results.add(executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
InputStream subsectionInputStream = RandomAccessDataFileTests.this.file
.getSubsection(0, 256)
.getInputStream(ResourceAccess.PER_READ);
byte[] b = new byte[256];
subsectionInputStream.read(b);
return Arrays.equals(b, BYTES);
}
results.add(executorService.submit(() -> {
InputStream subsectionInputStream = RandomAccessDataFileTests.this.file
.getSubsection(0, 256).getInputStream(ResourceAccess.PER_READ);
byte[] b = new byte[256];
subsectionInputStream.read(b);
return Arrays.equals(b, BYTES);
}));
}
for (Future<Boolean> future : results) {
@@ -327,21 +319,15 @@ public class RandomAccessDataFileTests {
"filePool");
FilePool spiedPool = spy(filePool);
ReflectionTestUtils.setField(this.file, "filePool", spiedPool);
willAnswer(new Answer<RandomAccessFile>() {
@Override
public RandomAccessFile answer(InvocationOnMock invocation) throws Throwable {
RandomAccessFile originalFile = (RandomAccessFile) invocation
.callRealMethod();
if (Mockito.mockingDetails(originalFile).isSpy()) {
return originalFile;
}
RandomAccessFile spiedFile = spy(originalFile);
willThrow(new IOException("Seek failed")).given(spiedFile)
.seek(anyLong());
return spiedFile;
willAnswer((invocation) -> {
RandomAccessFile originalFile = (RandomAccessFile) invocation
.callRealMethod();
if (Mockito.mockingDetails(originalFile).isSpy()) {
return originalFile;
}
RandomAccessFile spiedFile = spy(originalFile);
willThrow(new IOException("Seek failed")).given(spiedFile).seek(anyLong());
return spiedFile;
}).given(spiedPool).acquire();
for (int i = 0; i < 5; i++) {