sonar fixes #10

focus on rules:
- Collection.isEmpty() should be used to test for emptiness
- Lambdas should be replaced with method references
- Synchronized classes Vector, Hashtable, Stack and StringBuffer
  should not be used
- Collapsible "if" statements should be merged
- Modifiers should be declared in the correct order
- The diamond operator ("<>") should be used
- Array designators "[]" should be on the type, not the variable
- Null checks should not be used with "instanceof"

Change-Id: Ie8c1de43563c01f5b7462726be80a7bd18ae7b24
Signed-off-by: Markus Ofterdinger <markus.ofterdinger@sap.com>
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/console/DSPStreamsProxy.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/console/DSPStreamsProxy.java
index 17ca989..5ff6756 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/console/DSPStreamsProxy.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/console/DSPStreamsProxy.java
@@ -57,7 +57,7 @@
 					outputStream.append(result);
 				}
 			}).exceptionally((t) -> {
-				if (t.getCause() != null && t.getCause() instanceof ResponseErrorException) {
+				if (t.getCause() instanceof ResponseErrorException) {
 					ResponseErrorException exception = (ResponseErrorException) t.getCause();
 					ResponseError error = exception.getResponseError();
 					errorStream.append(error.getMessage() + System.lineSeparator());
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java
index 573d80b..5a8e91b 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPBreakpointManager.java
@@ -64,7 +64,7 @@
 	/**
 	 * Initialize the manager and send all platform breakpoints to the debug
 	 * adapter.
-	 * 
+	 *
 	 * @return the completeable future to signify when the breakpoints are all sent.
 	 */
 	public CompletableFuture<Void> initialize() {
@@ -219,7 +219,7 @@
 
 			Source source = entry.getKey();
 			List<SourceBreakpoint> bps = entry.getValue();
-			Long[] lines = bps.stream().map(sb -> sb.getLine()).toArray(Long[]::new);
+			Long[] lines = bps.stream().map(SourceBreakpoint::getLine).toArray(Long[]::new);
 			SourceBreakpoint[] sourceBps = bps.toArray(new SourceBreakpoint[bps.size()]);
 
 			SetBreakpointsArguments arguments = new SetBreakpointsArguments();
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugElement.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugElement.java
index 7211a93..08de8bf 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugElement.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugElement.java
@@ -24,7 +24,7 @@
 import org.eclipse.lsp4e.debug.DSPPlugin;
 import org.eclipse.lsp4j.debug.services.IDebugProtocolServer;
 
-abstract public class DSPDebugElement extends DebugElement {
+public abstract class DSPDebugElement extends DebugElement {
 
 	private String errorMessage;
 
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java
index 139f5e7..1934c24 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/DSPDebugTarget.java
@@ -94,7 +94,7 @@
 	/**
 	 * Once we have received initialized event, this member will be "done" as a flag
 	 */
-	private CompletableFuture<Void> initialized = new CompletableFuture<Void>();
+	private CompletableFuture<Void> initialized = new CompletableFuture<>();
 
 	/**
 	 * The cached set of current threads. This should generally not be directly
@@ -123,7 +123,7 @@
 	private Map<String, Object> dspParameters;
 
 	public DSPDebugTarget(ILaunch launch, Runnable processCleanup, InputStream in, OutputStream out,
-			Map<String, Object> dspParameters) throws CoreException {
+			Map<String, Object> dspParameters) {
 		super(null);
 		this.in = in;
 		this.out = out;
@@ -315,7 +315,7 @@
 				source = getThread(body.getThreadId());
 			}
 			if (source == null || body.getAllThreadsContinued() == null || body.getAllThreadsContinued()) {
-				Arrays.asList(getThreads()).forEach(t -> t.continued());
+				Arrays.asList(getThreads()).forEach(DSPThread::continued);
 			}
 			if (source != null) {
 				source.fireResumeEvent(DebugEvent.CLIENT_REQUEST);
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceInputStream.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceInputStream.java
index 20572b1..c547737 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceInputStream.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceInputStream.java
@@ -30,7 +30,7 @@
 	}
 
 	@Override
-	public int read(byte b[], int off, int len) throws IOException {
+	public int read(byte[] b, int off, int len) throws IOException {
 		int n = in.read(b, off, len);
 		trace.write(b, off, n);
 		trace.flush();
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceOutputStream.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceOutputStream.java
index f67b685..aae53fd 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceOutputStream.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/debugmodel/TraceOutputStream.java
@@ -29,7 +29,7 @@
 	}
 
 	@Override
-	public void write(byte b[], int off, int len) throws IOException {
+	public void write(byte[] b, int off, int len) throws IOException {
 		trace.write(b, off, len);
 		trace.flush();
 		out.write(b, off, len);
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java
index cea206f..8b1b175 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPLaunchDelegate.java
@@ -77,7 +77,7 @@
 			this.server = other.server;
 			this.port = other.port;
 
-			this.dspParameters = new HashMap<String, Object>(other.dspParameters);
+			this.dspParameters = new HashMap<>(other.dspParameters);
 		}
 
 		/**
@@ -209,7 +209,7 @@
 		// Make a copy so we can modify locally as needed.
 		builder = new DSPLaunchDelegateLaunchBuilder(builder);
 		SubMonitor subMonitor = SubMonitor.convert(builder.monitor, 100);
-		builder.dspParameters = new HashMap<String, Object>(builder.dspParameters);
+		builder.dspParameters = new HashMap<>(builder.dspParameters);
 
 		boolean customDebugAdapter = builder.configuration.getAttribute(DSPPlugin.ATTR_CUSTOM_DEBUG_ADAPTER, false);
 		boolean customLaunchParameters = builder.configuration.getAttribute(DSPPlugin.ATTR_CUSTOM_LAUNCH_PARAMS, false);
@@ -318,7 +318,7 @@
 				} else {
 					inputStream = debugAdapterProcess.getInputStream();
 					outputStream = debugAdapterProcess.getOutputStream();
-					cleanup = () -> debugAdapterProcess.destroy();
+					cleanup = debugAdapterProcess::destroy;
 				}
 			} else {
 
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java
index f629e71..d90ec2a 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/launcher/DSPMainTab.java
@@ -229,7 +229,7 @@
 			connectDebugServer.setSelection(!launch);
 			debugCommandText.setText(configuration.getAttribute(DSPPlugin.ATTR_DSP_CMD, ""));
 			List<String> args = configuration.getAttribute(DSPPlugin.ATTR_DSP_ARGS, Collections.emptyList());
-			if (args.size() == 0) {
+			if (args.isEmpty()) {
 				debugArgsText.setText("");
 			} else if (args.size() == 1) {
 				debugArgsText.setText(args.get(0));
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java
index 48a1a63..fa8da2f 100644
--- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java
+++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java
@@ -77,9 +77,7 @@
 					is.close();
 				}
 			}
-		} catch (CoreException ex) {
-			// continue without content type
-		} catch (IOException ex) {
+		} catch (CoreException | IOException ex) {
 			// continue without content type
 		}
 
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java
index a22a29c..f76114d 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServersRegistry.java
@@ -72,7 +72,7 @@
 	private static final String ENABLED_WHEN_ATTRIBUTE = "enabledWhen"; //$NON-NLS-1$
 	private static final String ENABLED_WHEN_DESC = "description"; //$NON-NLS-1$
 
-	public static abstract class LanguageServerDefinition {
+	public abstract static class LanguageServerDefinition {
 		public final @NonNull String id;
 		public final @NonNull String label;
 		public final @NonNull Map<IContentType, String> langugeIdMappings;
@@ -351,10 +351,8 @@
 	 * @param file
 	 * @param serverDefinition
 	 * @return whether the given serverDefinition is suitable for the file
-	 * @throws CoreException
-	 * @throws IOException
 	 */
-	public boolean matches(@NonNull IFile file, @NonNull LanguageServerDefinition serverDefinition) throws IOException, CoreException {
+	public boolean matches(@NonNull IFile file, @NonNull LanguageServerDefinition serverDefinition) {
 		return getAvailableLSFor(LSPEclipseUtils.getFileContentTypes(file)).contains(serverDefinition);
 	}
 
@@ -362,11 +360,8 @@
 	 * @param document
 	 * @param serverDefinition
 	 * @return whether the given serverDefinition is suitable for the file
-	 * @throws CoreException
-	 * @throws IOException
 	 */
-	public boolean matches(@NonNull IDocument document, @NonNull LanguageServerDefinition serverDefinition)
-			throws IOException, CoreException {
+	public boolean matches(@NonNull IDocument document, @NonNull LanguageServerDefinition serverDefinition) {
 		return getAvailableLSFor(LSPEclipseUtils.getDocumentContentTypes(document)).contains(serverDefinition);
 	}
 
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java
index 0ed0654..a5df189 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServiceAccessor.java
@@ -72,7 +72,7 @@
 	 */
 	public static void clearStartedServers() {
 		synchronized (startedServers) {
-			startedServers.forEach(wrapper -> wrapper.stop());
+			startedServers.forEach(LanguageServerWrapper::stop);
 			startedServers.clear();
 		}
 	}
@@ -146,7 +146,7 @@
 			@Nullable Predicate<ServerCapabilities> request) throws IOException {
 		synchronized (startedServers) {
 			Collection<LanguageServerWrapper> wrappers = getLSWrappers(file, request);
-			return wrappers.stream().map(wrapper -> wrapper.getInitializedServer().thenApplyAsync((server) -> {
+			return wrappers.stream().map(wrapper -> wrapper.getInitializedServer().thenApplyAsync(server -> {
 				try {
 					wrapper.connect(file, null);
 				} catch (IOException e) {
@@ -397,22 +397,14 @@
 
 	private static Collection<LanguageServerWrapper> getMatchingStartedWrappers(@NonNull IFile file,
 			@Nullable Predicate<ServerCapabilities> request) {
-		synchronized(startedServers) {
-			return startedServers.stream()
-					.filter(wrapper -> {
-						try {
-							return wrapper.isConnectedTo(file.getLocation())
-									||
-									(LanguageServersRegistry.getInstance().matches(file, wrapper.serverDefinition)
-											&& wrapper.canOperate(file.getProject()));
-						} catch (IOException | CoreException e) {
-							LanguageServerPlugin.logError(e);
-							return false;
-						}
-					})
-					.filter(wrapper -> request == null || (wrapper.getServerCapabilities() == null
-							|| request.test(wrapper.getServerCapabilities())))
-				.collect(Collectors.toList());
+		synchronized (startedServers) {
+			return startedServers.stream().filter(wrapper -> {
+				return wrapper.isConnectedTo(file.getLocation())
+						|| (LanguageServersRegistry.getInstance().matches(file, wrapper.serverDefinition)
+								&& wrapper.canOperate(file.getProject()));
+			}).filter(wrapper -> request == null
+					|| (wrapper.getServerCapabilities() == null || request.test(wrapper.getServerCapabilities())))
+					.collect(Collectors.toList());
 		}
 	}
 
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ServerMessageHandler.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ServerMessageHandler.java
index 42b53f9..d90a33e 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ServerMessageHandler.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ServerMessageHandler.java
@@ -45,8 +45,8 @@
 
 	private static class LSPNotification extends AbstractUiNotification {
 
-		final private String label;
-		final private MessageParams messageParams;
+		private final String label;
+		private final MessageParams messageParams;
 
 		public LSPNotification(String label, MessageParams messageParams) {
 			super("lsp.notification"); //$NON-NLS-1$
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSCompletionProposalComparator.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSCompletionProposalComparator.java
index 15a55ed..d8036ad 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSCompletionProposalComparator.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSCompletionProposalComparator.java
@@ -37,13 +37,12 @@
 		} else if (o1.getRankCategory() > o2.getRankCategory()) {
 			return +1;
 		}
-		if (o1.getRankCategory() < 5 && o2.getRankCategory() < 5) {
-			if (!(o1.getRankScore() == -1 && o2.getRankScore() == -1)) {
-				if (o2.getRankScore() == -1 || o1.getRankScore() < o2.getRankScore()) {
-					return -1;
-				} else if (o1.getRankScore() == -1 || o1.getRankScore() > o2.getRankScore()) {
-					return +1;
-				}
+		if ((o1.getRankCategory() < 5 && o2.getRankCategory() < 5)
+				&& (!(o1.getRankScore() == -1 && o2.getRankScore() == -1))) {
+			if (o2.getRankScore() == -1 || o1.getRankScore() < o2.getRankScore()) {
+				return -1;
+			} else if (o1.getRankScore() == -1 || o1.getRankScore() > o2.getRankScore()) {
+				return +1;
 			}
 		}
 		String c1 = o1.getSortText();
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSIncompleteCompletionProposal.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSIncompleteCompletionProposal.java
index f905ab6..f2ff9f8 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSIncompleteCompletionProposal.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSIncompleteCompletionProposal.java
@@ -107,12 +107,12 @@
 		};
 	};
 
-	final protected CompletionItem item;
+	protected final CompletionItem item;
 	private int initialOffset = -1;
 	protected int bestOffset = -1;
 	protected int currentOffset = -1;
 	protected ITextViewer viewer;
-	final private IDocument document;
+	private final IDocument document;
 	private IRegion selection;
 	private LinkedPosition firstPosition;
 	// private LSPDocumentInfo info;
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java
index 03033fe..3d1875c 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatHandler.java
@@ -57,7 +57,7 @@
 		if (part instanceof ITextEditor) {
 			Collection<LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor(
 					LSPEclipseUtils.getDocument((ITextEditor) part),
-					(capabilities) -> LSPFormatter.supportFormatting(capabilities));
+					LSPFormatter::supportFormatting);
 			ISelection selection = ((ITextEditor) part).getSelectionProvider().getSelection();
 			return !infos.isEmpty() && !selection.isEmpty() && selection instanceof ITextSelection;
 		}
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java
index 97b8462..fd06609 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java
@@ -46,7 +46,8 @@
 
 	public CompletableFuture<List<? extends TextEdit>> requestFormatting(@NonNull IDocument document,
 			@NonNull ITextSelection textSelection) {
-		Collection<@NonNull LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor(document, capabilities -> supportFormatting(capabilities));
+		Collection<@NonNull LSPDocumentInfo> infos = LanguageServiceAccessor.getLSPDocumentInfosFor(document,
+				LSPFormatter::supportFormatting);
 		if (infos.isEmpty()) {
 			return CompletableFuture.completedFuture(Collections.emptyList());
 		}
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java
index f45834d..863c1a2 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/outline/SymbolsModel.java
@@ -11,15 +11,16 @@
  *******************************************************************************/
 package org.eclipse.lsp4e.outline;
 
+import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import java.util.Stack;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.lsp4j.DocumentSymbol;
@@ -61,7 +62,7 @@
 					Comparator.comparingInt(pos -> ((Position) pos).getLine())
 							.thenComparingInt(pos -> ((Position) pos).getCharacter())));
 
-			Stack<SymbolInformation> parentStack = new Stack<>();
+			Deque<SymbolInformation> parentStack = new ArrayDeque<>();
 			parentStack.push(ROOT_SYMBOL_INFORMATION);
 			SymbolInformation previousSymbol = null;
 			for (Either<SymbolInformation, DocumentSymbol> either : response) {
diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java
index 3a5b8ab..609d7b2 100644
--- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java
+++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/ui/LSPImages.java
@@ -101,7 +101,7 @@
 		declareRegistryImage(IMG_REFERENCE, OBJECT + "reference.png"); //$NON-NLS-1$
 	}
 
-	private final static void declareRegistryImage(String key, String path) {
+	private static final void declareRegistryImage(String key, String path) {
 		ImageDescriptor desc = ImageDescriptor.getMissingImageDescriptor();
 		Bundle bundle = Platform.getBundle(LanguageServerPlugin.PLUGIN_ID);
 		URL url = null;