From 7034b474eb95d8b378307fa98158f411f81e1894 Mon Sep 17 00:00:00 2001 From: Sebastian Thomschke Date: Mon, 22 Jul 2024 10:33:33 +0200 Subject: [PATCH] refact: enable NonNullByDefault for lsp4e.refactoring package --- .../lsp4e/refactoring/CreateFileChange.java | 16 +++++---- .../lsp4e/refactoring/DeleteExternalFile.java | 8 ++--- .../lsp4e/refactoring/LSPTextChange.java | 34 +++++++++++-------- .../lsp4e/refactoring/package-info.java | 6 ++++ 4 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java index baa12d253..9d43cfe87 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/CreateFileChange.java @@ -37,6 +37,7 @@ import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.lsp4e.LSPEclipseUtils; import org.eclipse.lsp4e.LanguageServerPlugin; import org.eclipse.lsp4e.ui.Messages; @@ -51,15 +52,15 @@ public class CreateFileChange extends ResourceChange { private final URI uri; private final String fSource; - private String fEncoding; + private @Nullable String fEncoding; private boolean fExplicitEncoding; private final long fStampToRestore; - public CreateFileChange(URI uri, String source, String encoding) { + public CreateFileChange(URI uri, String source, @Nullable String encoding) { this(uri, source, encoding, IResource.NULL_STAMP); } - public CreateFileChange(URI uri, String source, String encoding, long stampToRestore) { + public CreateFileChange(URI uri, String source, @Nullable String encoding, long stampToRestore) { Assert.isNotNull(uri, "uri"); //$NON-NLS-1$ Assert.isNotNull(source, "source"); //$NON-NLS-1$ this.uri = uri; @@ -81,7 +82,7 @@ public String getName() { } @Override - protected IFile getModifiedResource() { + protected @Nullable IFile getModifiedResource() { return LSPEclipseUtils.getFileHandle(this.uri); } @@ -98,10 +99,10 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { } @Override - public Change perform(IProgressMonitor pm) throws CoreException { + public @Nullable Change perform(IProgressMonitor pm) throws CoreException { pm.beginTask(NLS.bind(Messages.edit_CreateFile, uri), 3); - initializeEncoding(); + final var fEncoding = initializeEncoding(); try (InputStream is= new ByteArrayInputStream(fSource.getBytes(fEncoding))) { @@ -158,7 +159,7 @@ public Change perform(IProgressMonitor pm) throws CoreException { return null; } - private void initializeEncoding() { + private String initializeEncoding() { if (fEncoding == null) { fExplicitEncoding= false; IFile ifile = getModifiedResource(); @@ -188,5 +189,6 @@ private void initializeEncoding() { } } Assert.isNotNull(fEncoding); + return fEncoding; } } diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java index f8938b36a..a37d38c9d 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/DeleteExternalFile.java @@ -19,16 +19,16 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.lsp4e.LanguageServerPlugin; import org.eclipse.ltk.core.refactoring.Change; import org.eclipse.ltk.core.refactoring.RefactoringStatus; public class DeleteExternalFile extends Change { - private final @NonNull File file; + private final File file; - public DeleteExternalFile(@NonNull File file) { + public DeleteExternalFile(File file) { this.file = file; } @@ -48,7 +48,7 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { } @Override - public Change perform(IProgressMonitor pm) throws CoreException { + public @Nullable Change perform(IProgressMonitor pm) throws CoreException { try { Files.delete(this.file.toPath()); } catch (IOException e) { diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java index 5f1deeb6e..6e334494e 100644 --- a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/LSPTextChange.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.eclipse.lsp4e.refactoring; +import static org.eclipse.lsp4e.internal.NullSafetyHelper.*; + import java.net.URI; import org.eclipse.core.filebuffers.FileBuffers; @@ -26,7 +28,7 @@ import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.lsp4e.LSPEclipseUtils; @@ -47,22 +49,22 @@ @SuppressWarnings("restriction") public class LSPTextChange extends TextChange { - private final @NonNull URI fileUri; + private final URI fileUri; - private Either file; + private Either file = lateNonNull(); private int fAcquireCount; - private ITextFileBuffer fBuffer; - private @NonNull String newText; - private Range range; + private @Nullable ITextFileBuffer fBuffer; + private String newText; + private @Nullable Range range; - public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull TextEdit textEdit) { + public LSPTextChange(String name, URI fileUri, TextEdit textEdit) { super(name); this.fileUri = fileUri; this.newText = textEdit.getNewText(); this.range = textEdit.getRange(); } - public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull String newText) { + public LSPTextChange(String name, URI fileUri, String newText) { super(name); this.fileUri = fileUri; this.newText = newText; @@ -73,7 +75,7 @@ public LSPTextChange(@NonNull String name, @NonNull URI fileUri, @NonNull String protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException { fAcquireCount++; if (fAcquireCount > 1) { - return fBuffer.getDocument(); + return castNonNull(this.fBuffer).getDocument(); } IFile iFile = LSPEclipseUtils.getFileHandle(this.fileUri); @@ -106,9 +108,10 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException { // because that's used by the preview logic to compute the changed document. We do it here rather than in the constructor // since we need the document to translate line offsets into character offset. Strictly this would not work then // if the platform called getEdit() prior to this method being traversed, but it seems to be OK in practice. - final IDocument document = fBuffer.getDocument(); + final IDocument document = castNonNull(this.fBuffer).getDocument(); int offset = 0; int length = document.getLength(); + final var range = this.range; if (range != null && getEdit() == null) { try { offset = LSPEclipseUtils.toOffset(range.getStart(), document); @@ -124,7 +127,7 @@ protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException { @Override protected void commit(IDocument document, IProgressMonitor pm) throws CoreException { - this.fBuffer.commit(pm, true); + castNonNull(this.fBuffer).commit(pm, true); } @Override @@ -132,7 +135,7 @@ protected void releaseDocument(IDocument document, IProgressMonitor pm) throws C Assert.isTrue(fAcquireCount > 0); if (fAcquireCount == 1) { ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); - this.fBuffer.commit(pm, true); + castNonNull(this.fBuffer).commit(pm, true); if (this.file.isLeft()) { manager.disconnect(this.file.getLeft().getFullPath(), LocationKind.IFILE, pm); } else { @@ -143,7 +146,7 @@ protected void releaseDocument(IDocument document, IProgressMonitor pm) throws C } @Override - protected Change createUndoChange(UndoEdit edit) { + protected @Nullable Change createUndoChange(UndoEdit edit) { throw new UnsupportedOperationException("Should not be called!"); //$NON-NLS-1$ } @@ -158,7 +161,7 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { } @Override - public Object getModifiedElement() { + public @Nullable Object getModifiedElement() { IFile file = LSPEclipseUtils.getFileHandle(this.fileUri); if (file != null) { return file; @@ -170,7 +173,7 @@ public Object getModifiedElement() { } @Override - public Change perform(IProgressMonitor pm) throws CoreException { + public @Nullable Change perform(IProgressMonitor pm) throws CoreException { pm.beginTask("", 3); //$NON-NLS-1$ IDocument document = null; @@ -179,6 +182,7 @@ public Change perform(IProgressMonitor pm) throws CoreException { int offset = 0; int length = document.getLength(); + final var range = this.range; if (range != null) { offset = LSPEclipseUtils.toOffset(range.getStart(), document); length = LSPEclipseUtils.toOffset(range.getEnd(), document) - offset; diff --git a/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java new file mode 100644 index 000000000..54bab061f --- /dev/null +++ b/org.eclipse.lsp4e/src/org/eclipse/lsp4e/refactoring/package-info.java @@ -0,0 +1,6 @@ +@NonNullByDefault({ ARRAY_CONTENTS, PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT }) +package org.eclipse.lsp4e.refactoring; + +import static org.eclipse.jdt.annotation.DefaultLocation.*; + +import org.eclipse.jdt.annotation.NonNullByDefault;