Skip to content

Commit

Permalink
refact: improve null analysis
Browse files Browse the repository at this point in the history
This PR bumps no-npe to version 1.1.0 (which contains more refined
external null annotation) and applies all necessary code changes to
satisfy null analysis checks by the Eclipse compiler.
  • Loading branch information
sebthom committed Sep 4, 2024
1 parent e00186d commit 02be209
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public DSPBreakpointManager(IBreakpointManager platformBreakpointManager, IDebug
*
* @return the completeable future to signify when the breakpoints are all sent.
*/
public CompletableFuture<Void> initialize() {
public CompletableFuture<@Nullable Void> initialize() {
platformBreakpointManager.addBreakpointListener(this);
platformBreakpointManager.addBreakpointManagerListener(this);
return resendAllTargetBreakpoints(platformBreakpointManager.isEnabled());
Expand Down Expand Up @@ -101,7 +101,7 @@ public void breakpointManagerEnablementChanged(boolean enabled) {
resendAllTargetBreakpoints(enabled);
}

private CompletableFuture<Void> resendAllTargetBreakpoints(boolean enabled) {
private CompletableFuture<@Nullable Void> resendAllTargetBreakpoints(boolean enabled) {
IBreakpoint[] breakpoints = platformBreakpointManager.getBreakpoints();
for (IBreakpoint breakpoint : breakpoints) {
if (supportsBreakpoint(breakpoint)) {
Expand Down Expand Up @@ -211,7 +211,7 @@ private void deleteBreakpointFromMap(IBreakpoint breakpoint) {
}
}

private CompletableFuture<Void> sendBreakpoints() {
private CompletableFuture<@Nullable Void> sendBreakpoints() {
final var all = new ArrayList<CompletableFuture<Void>>();
for (Iterator<Entry<Source, List<SourceBreakpoint>>> iterator = targetBreakpoints.entrySet()
.iterator(); iterator.hasNext();) {
Expand All @@ -228,7 +228,7 @@ private CompletableFuture<Void> sendBreakpoints() {
arguments.setBreakpoints(sourceBps);
arguments.setSourceModified(false);
CompletableFuture<SetBreakpointsResponse> future = debugProtocolServer.setBreakpoints(arguments);
CompletableFuture<Void> future2 = future.thenAccept((SetBreakpointsResponse bpResponse) -> {
CompletableFuture<@Nullable Void> future2 = future.thenAccept((SetBreakpointsResponse bpResponse) -> {
// TODO update platform breakpoint with new info
});
all.add(future2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,8 @@ private CompletableFuture<?> initialize(Map<String, Object> dspParameters, IProg
}
return q;
});
CompletableFuture<Void> configurationDoneFuture = CompletableFuture.allOf(initialized, capabilitiesFuture)
.thenRun(() -> {
monitor.worked(10);
});
CompletableFuture<@Nullable Void> configurationDoneFuture = CompletableFuture
.allOf(initialized, capabilitiesFuture).thenRun(() -> monitor.worked(10));
if (ILaunchManager.DEBUG_MODE.equals(launch.getLaunchMode())) {
configurationDoneFuture = configurationDoneFuture.thenCompose(v -> {
monitor.worked(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public Map<String, Object> getDspParameters() {
public String toString() {
return "DSPLaunchDelegateLaunchBuilder [configuration=" + configuration + ", mode=" + mode + ", launch="
+ launch + ", monitor=" + monitor + ", launchNotConnect=" + launchNotConnect + ", debugCmd="
+ debugCmd + ", debugCmdArgs=" + debugCmdArgs + ", debugCmdEnvVars=" + List.of(debugCmdEnvVars)
+ debugCmd + ", debugCmdArgs=" + debugCmdArgs //
+ ", debugCmdEnvVars=" + (debugCmdEnvVars == null ? null : List.of(debugCmdEnvVars))
+ ", monitorDebugAdapter=" + monitorDebugAdapter + ", server=" + server + ", port=" + port
+ ", dspParameters=" + dspParameters + "]";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private ICompletionProposal[] asJavaProposals(CompletableFuture<ICompletionPropo
public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context,
IProgressMonitor monitor) {
IContextInformation[] contextInformation = lsContentAssistProcessor.computeContextInformation(context.getViewer(), context.getInvocationOffset());
return List.of(contextInformation);
return contextInformation == null ? List.of() : List.of(contextInformation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public void documentSaved(IFileBuffer buffer) {

}

public CompletableFuture<Void> documentClosed() {
public CompletableFuture<@Nullable Void> documentClosed() {
final var identifier = LSPEclipseUtils.toTextDocumentIdentifier(fileUri);
WILL_SAVE_WAIT_UNTIL_TIMEOUT_MAP.remove(identifier.getUri());
// When LS is shut down all documents are being disconnected. No need to send
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public IStatus run(IProgressMonitor monitor) {
}

@Override
public CompletableFuture<Void> registerCapability(RegistrationParams params) {
public CompletableFuture<@Nullable Void> registerCapability(RegistrationParams params) {
return CompletableFuture.runAsync(() -> wrapper.registerCapability(params));
}

@Override
public CompletableFuture<Void> unregisterCapability(UnregistrationParams params) {
public CompletableFuture<@Nullable Void> unregisterCapability(UnregistrationParams params) {
return CompletableFuture.runAsync(() -> wrapper.unregisterCapability(params));
}

Expand Down Expand Up @@ -182,12 +182,12 @@ private void updateCodeMinings() {
}

@Override
public CompletableFuture<Void> refreshCodeLenses() {
public CompletableFuture<@Nullable Void> refreshCodeLenses() {
return CompletableFuture.runAsync(() -> UI.getDisplay().syncExec(this::updateCodeMinings));
}

@Override
public CompletableFuture<Void> refreshInlayHints() {
public CompletableFuture<@Nullable Void> refreshInlayHints() {
return CompletableFuture.runAsync(() -> UI.getDisplay().syncExec(this::updateCodeMinings));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void dirtyStateChanged(IFileBuffer buffer, boolean isDirty) {

protected @Nullable StreamConnectionProvider lspStreamProvider;
private @Nullable Future<?> launcherFuture;
private @Nullable CompletableFuture<Void> initializeFuture;
private @Nullable CompletableFuture<@Nullable Void> initializeFuture;
private final AtomicReference<@Nullable IProgressMonitor> initializeFutureMonitorRef = new AtomicReference<>();
private final int initializeFutureNumberOfStages = 7;
private @Nullable LanguageServer languageServer;
Expand Down Expand Up @@ -389,7 +389,7 @@ private Job createInitializeLanguageServerJob() {
protected IStatus run(IProgressMonitor monitor) {
final var initializeFutureMonitor = SubMonitor.convert(monitor, initializeFutureNumberOfStages);
initializeFutureMonitorRef.set(initializeFutureMonitor);
CompletableFuture<Void> currentInitializeFuture = initializeFuture;
CompletableFuture<@Nullable Void> currentInitializeFuture = initializeFuture;
try {
if (currentInitializeFuture != null) {
currentInitializeFuture.join();
Expand Down Expand Up @@ -714,9 +714,9 @@ private boolean supportsWorkspaceFolderCapability() {
* @param uri
* @return null if not disconnection has happened, a future tracking the disconnection state otherwise
*/
public @Nullable CompletableFuture<Void> disconnect(URI uri) {
public @Nullable CompletableFuture<@Nullable Void> disconnect(URI uri) {
DocumentContentSynchronizer documentListener = this.connectedDocuments.remove(uri);
CompletableFuture<Void> documentClosedFuture = null;
CompletableFuture<@Nullable Void> documentClosedFuture = null;
if (documentListener != null) {
documentListener.getDocument().removePrenotifiedDocumentListener(documentListener);
documentClosedFuture = documentListener.documentClosed();
Expand Down Expand Up @@ -778,7 +778,7 @@ protected LanguageServer getServer() {
protected CompletableFuture<LanguageServer> getInitializedServer() {
start();

final CompletableFuture<Void> currentInitializeFuture = initializeFuture;
final CompletableFuture<@Nullable Void> currentInitializeFuture = initializeFuture;
if (currentInitializeFuture != null && !currentInitializeFuture.isDone()) {
return currentInitializeFuture.thenApply(r -> castNonNull(this.languageServer));
}
Expand Down
6 changes: 2 additions & 4 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServers.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4e.LanguageServersRegistry.LanguageServerDefinition;
Expand Down Expand Up @@ -267,10 +266,9 @@ public IDocument getDocument() {
CompletableFuture<@Nullable LanguageServerWrapper> connect(CompletableFuture<@Nullable LanguageServerWrapper> wrapperFuture) {
return wrapperFuture.thenCompose(wrapper -> {
if (wrapper != null) {
@NonNullByDefault({})
CompletableFuture<LanguageServerWrapper> serverFuture = wrapper.connectDocument(document);
if (serverFuture != null) {
return serverFuture;
return (CompletableFuture<@Nullable LanguageServerWrapper>) serverFuture;
}
}
return CompletableFuture.completedFuture(null);
Expand Down Expand Up @@ -422,7 +420,7 @@ public static <T> CompletableFuture<List<T>> addAll(CompletableFuture<List<T>> a
* (not chained with other futures) so cancelling the futures in
* this stream will send a cancellation event to the LSs.</p>
*/
private <T> Stream<CompletableFuture<T>> executeOnServers(
private <@Nullable T> Stream<CompletableFuture<T>> executeOnServers(
BiFunction<? super LanguageServerWrapper, LanguageServer, ? extends CompletableFuture<T>> fn) {
return getServers().stream().map(serverFuture -> {
// wrap in AtomicReference to allow dereferencing in downstream future
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public LSPCodeMining(CodeLens codeLens, IDocument document, LanguageServerWrappe
}

@Override
protected CompletableFuture<Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
protected CompletableFuture<@Nullable Void> doResolve(ITextViewer viewer, IProgressMonitor monitor) {
final ServerCapabilities serverCapabilities = languageServerWrapper.getServerCapabilities();
if (serverCapabilities == null) {
return CompletableFuture.completedFuture(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public class LSContentAssistProcessor implements IContentAssistProcessor {
private @Nullable IDocument currentDocument;
private @Nullable String errorMessage;
private final boolean errorAsCompletionItem;
private @Nullable CompletableFuture<List<Void>> completionLanguageServersFuture;
private @Nullable CompletableFuture<List<@Nullable Void>> completionLanguageServersFuture;
private volatile char[] completionTriggerChars = NO_CHARS;
private @Nullable CompletableFuture<List<Void>> contextInformationLanguageServersFuture;
private @Nullable CompletableFuture<List<@Nullable Void>> contextInformationLanguageServersFuture;
private volatile char[] contextTriggerChars = NO_CHARS;
private final boolean incompleteAsCompletionItem;

Expand Down Expand Up @@ -327,7 +327,7 @@ private static IContextInformation toContextInformation(SignatureInformation inf
return new ContextInformation(information.getLabel(), signature.toString());
}

private void getFuture(@Nullable CompletableFuture<List<Void>> future) {
private void getFuture(@Nullable CompletableFuture<?> future) {
if (future == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class LSPDocumentLinkPresentationReconcilingStrategy
/** The target viewer. */
private @Nullable ITextViewer viewer;

private @Nullable CompletableFuture<Void> request;
private @Nullable CompletableFuture<@Nullable Void> request;

private @Nullable IDocument document;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<!-- External Eclipse null Annotations, see https://github.com/vegardit/no-npe -->
<groupId>com.vegardit.no-npe</groupId>
<artifactId>no-npe-eea-all</artifactId>
<version>1.0.5</version>
<version>1.1.0</version>
<type>jar</type>
</dependency>
</dependencies>
Expand Down

0 comments on commit 02be209

Please sign in to comment.