Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: improve handling of arrays #1079

Merged
merged 7 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) thr
int lineNumber = textSelection.getStartLine();
IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager()
.getBreakpoints(DSPPlugin.ID_DSP_DEBUG_MODEL);
for (int i = 0; i < breakpoints.length; i++) {
IBreakpoint breakpoint = breakpoints[i];
for (final IBreakpoint breakpoint : breakpoints) {
if (breakpoint instanceof ILineBreakpoint lineBreakpoint
&& resource.equals(breakpoint.getMarker().getResource())
&& lineBreakpoint.getLineNumber() == (lineNumber + 1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.eclipse.lsp4e.debug.console.DSPProcess;
import org.eclipse.lsp4e.debug.console.DSPStreamsProxy;
import org.eclipse.lsp4e.debug.debugmodel.TransportStreams.DefaultTransportStreams;
import org.eclipse.lsp4e.internal.ArrayUtil;
import org.eclipse.lsp4j.debug.BreakpointEventArguments;
import org.eclipse.lsp4j.debug.Capabilities;
import org.eclipse.lsp4j.debug.ConfigurationDoneArguments;
Expand Down Expand Up @@ -313,13 +314,13 @@ private CompletableFuture<?> initialize(Map<String, Object> dspParameters, IProg

private void terminated() {
fTerminated = true;
Arrays.stream(getThreads()).forEach(t -> {
for (final DSPThread t : getThreads()) {
try {
t.terminate();
} catch (DebugException e) {
DSPPlugin.logError(e);
}
});
}
final var process = this.process;
if (process != null && process.canTerminate()) {
try {
Expand Down Expand Up @@ -452,7 +453,7 @@ public void continued(ContinuedEventArguments body) {
DSPDebugElement source = null;
source = getThread(body.getThreadId());
if (source == null || body.getAllThreadsContinued() == null || body.getAllThreadsContinued()) {
Arrays.asList(getThreads()).forEach(DSPThread::continued);
ArrayUtil.forEach(getThreads(), DSPThread::continued);
}
if (source != null) {
source.fireResumeEvent(DebugEvent.CLIENT_REQUEST);
Expand All @@ -468,10 +469,10 @@ public void stopped(StoppedEventArguments body) {
source = getThread(body.getThreadId());
}
if (source == null || body.getAllThreadsStopped() == null || body.getAllThreadsStopped()) {
Arrays.asList(getThreads()).forEach(t -> {
for (final DSPThread t : getThreads()) {
t.stopped();
t.fireChangeEvent(DebugEvent.CHANGE);
});
}
}

if (source != null) {
Expand Down Expand Up @@ -506,8 +507,7 @@ public boolean canSuspend() {
@Override
public boolean isSuspended() {
DSPThread[] dspThreads = getThreads();
boolean anyMatch = Arrays.asList(dspThreads).stream().anyMatch(DSPThread::isSuspended);
return anyMatch;
return ArrayUtil.anyMatch(dspThreads, DSPThread::isSuspended);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.eclipse.lsp4j.debug.StackFrame;

public class DSPStackFrame extends DSPDebugElement implements IStackFrame {

private static final IRegisterGroup[] NO_REGISTER_GROUPS = new IRegisterGroup[0];

private final DSPThread thread;
private StackFrame stackFrame;
private final int depth;
Expand Down Expand Up @@ -157,7 +160,7 @@ public DSPThread getThread() {

@Override
public IRegisterGroup[] getRegisterGroups() throws DebugException {
return new IRegisterGroup[0];
return NO_REGISTER_GROUPS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.debug.core.model.IThread;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.lsp4e.debug.DSPPlugin;
import org.eclipse.lsp4e.internal.ArrayUtil;
import org.eclipse.lsp4j.debug.ContinueArguments;
import org.eclipse.lsp4j.debug.NextArguments;
import org.eclipse.lsp4j.debug.PauseArguments;
Expand All @@ -35,6 +36,10 @@
import org.eclipse.lsp4j.debug.Thread;

public class DSPThread extends DSPDebugElement implements IThread {

private static final IStackFrame[] NO_STACK_FRAMES = new IStackFrame[0];
private static final IBreakpoint[] NO_BREAKPOINTS = new IBreakpoint[0];

private final Integer id;
/**
* The name may not be known, if it is requested we will ask for it from the
Expand Down Expand Up @@ -221,18 +226,13 @@ public boolean hasStackFrames() throws DebugException {

@Override
public @Nullable IStackFrame getTopStackFrame() throws DebugException {
IStackFrame[] stackFrames = getStackFrames();
if (stackFrames.length > 0) {
return stackFrames[0];
} else {
return null;
}
return ArrayUtil.findFirst(getStackFrames());
}

@Override
public IStackFrame[] getStackFrames() throws DebugException {
if (!isSuspended()) {
return new IStackFrame[0];
return NO_STACK_FRAMES;
}
if (!refreshFrames.getAndSet(false)) {
synchronized (frames) {
Expand Down Expand Up @@ -263,12 +263,12 @@ public IStackFrame[] getStackFrames() throws DebugException {
return future.get();
} catch (RuntimeException | ExecutionException e) {
if (isTerminated()) {
return new DSPStackFrame[0];
return NO_STACK_FRAMES;
}
throw newTargetRequestFailedException(e.getMessage(), e);
} catch (InterruptedException e) {
java.lang.Thread.currentThread().interrupt();
return new DSPStackFrame[0];
return NO_STACK_FRAMES;
}
}

Expand All @@ -291,7 +291,7 @@ public String getName() {
@Override
public IBreakpoint[] getBreakpoints() {
// TODO update breakpoints from stopped messages from server
return new IBreakpoint[0];
return NO_BREAKPOINTS;
}

public Integer getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public final class DSPValue extends DSPDebugElement implements IValue {

private static final IVariable[] NO_VARIABLES = new IVariable[0];

private final @Nullable DSPVariable modelVariable;
private final Integer variablesReference;
private final String value;
Expand All @@ -41,7 +43,7 @@ public DSPValue(DSPDebugTarget debugger, Integer variablesReference, String valu
@Override
public IVariable @Nullable [] getVariables() throws DebugException {
if (!hasVariables()) {
return new IVariable[0];
return NO_VARIABLES;
}
if (cachedVariables == null) {
final var arguments = new VariablesArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static org.eclipse.lsp4e.internal.NullSafetyHelper.lateNonNull;
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -270,7 +269,7 @@ public void performApply(ILaunchConfigurationWorkingCopy configuration) {
if (arg == null) {
configuration.setAttribute(DSPPlugin.ATTR_DSP_ARGS, (String) null);
} else {
configuration.setAttribute(DSPPlugin.ATTR_DSP_ARGS, Arrays.asList(arg.split("\\s+"))); //$NON-NLS-1$
configuration.setAttribute(DSPPlugin.ATTR_DSP_ARGS, List.of(arg.split("\\s+"))); //$NON-NLS-1$
}
configuration.setAttribute(DSPPlugin.ATTR_DSP_MONITOR_DEBUG_ADAPTER,
monitorAdapterLauncherProcessCheckbox.getSelection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*******************************************************************************/
package org.eclipse.lsp4e.debug.presentation;

import static org.eclipse.lsp4e.internal.ArrayUtil.NO_STRINGS;
import static org.eclipse.lsp4e.internal.NullSafetyHelper.castNonNull;

import org.eclipse.core.runtime.Adapters;
Expand All @@ -21,6 +22,7 @@
import org.eclipse.lsp4e.debug.debugmodel.DSPDebugTarget;
import org.eclipse.lsp4e.debug.debugmodel.DSPStackFrame;
import org.eclipse.lsp4e.debug.debugmodel.DSPValue;
import org.eclipse.lsp4e.internal.ArrayUtil;
import org.eclipse.lsp4j.debug.EvaluateArguments;
import org.eclipse.lsp4j.debug.EvaluateResponse;

Expand Down Expand Up @@ -63,7 +65,7 @@ public String getExpressionText() {

@Override
public String[] getErrorMessages() {
return new String[0];
return NO_STRINGS;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ private Font italic() {

Font dialogFont = JFaceResources.getDialogFont();
FontData[] fontData = dialogFont.getFontData();
for (int i = 0; i < fontData.length; i++) {
FontData data = fontData[i];
for (final FontData data : fontData) {
data.setStyle(SWT.ITALIC);
}
Display display = getDisplay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*******************************************************************************/
package org.eclipse.lsp4e.debug.sourcelookup;

import static org.eclipse.lsp4e.internal.ArrayUtil.NO_OBJECTS;

import java.nio.file.Paths;

import org.eclipse.core.runtime.CoreException;
Expand All @@ -23,7 +25,7 @@ public Object[] findSourceElements(String name) throws CoreException {
if (name != null && Paths.get(name).isAbsolute()) {
return new Object[] { name };
}
return new Object[0];
return NO_OBJECTS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*******************************************************************************/
package org.eclipse.lsp4e.jdt;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -52,7 +51,7 @@ public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocat
lsContentAssistProcessor.computeCompletionProposals(context.getViewer(), context.getInvocationOffset()));

try {
return Arrays.asList(asJavaProposals(future));
return List.of(asJavaProposals(future));
} catch (ExecutionException | TimeoutException e) {
LanguageServerPlugin.logError(e);
javaCompletionSpecificErrorMessage = createErrorMessage(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class LspJavaQuickAssistProcessor extends LSPCodeActionQuickAssistProcessor implements IQuickAssistProcessor {

private static final int RELEVANCE = 100;
private static final IJavaCompletionProposal[] NO_JAVA_COMPLETION_PROPOSALS = new IJavaCompletionProposal[0];

private IQuickAssistInvocationContext getContext(IInvocationContext context) {
return new IQuickAssistInvocationContext() {
Expand Down Expand Up @@ -63,7 +64,7 @@ public boolean hasAssists(@NonNullByDefault({}) IInvocationContext context) thro

ICompletionProposal[] proposals = computeQuickAssistProposals(getContext(context));
if (proposals == null)
return new IJavaCompletionProposal[0];
return NO_JAVA_COMPLETION_PROPOSALS;

final var javaProposals = new IJavaCompletionProposal[proposals.length];
for (int i = 0; i < proposals.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -272,7 +271,7 @@ public void testTextEditInsertSameOffset() throws Exception {
new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), " throws "),
new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "Exception") };
IDocument document = viewer.getDocument();
LSPEclipseUtils.applyEdits(document, Arrays.asList(edits));
LSPEclipseUtils.applyEdits(document, List.of(edits));
Assert.assertEquals(" throws Exception", document.get());
}

Expand All @@ -285,7 +284,7 @@ public void testTextEditSplittedLineEndings() throws Exception {
IDocument document = viewer.getDocument();
int linesBeforeApplyEdits = document.getNumberOfLines();
// WHEN the TextEdit gets applied to the document:
LSPEclipseUtils.applyEdits(document, Arrays.asList(edits));
LSPEclipseUtils.applyEdits(document, List.of(edits));
// THEN line1 has been swapped with line 3:
Assert.assertEquals("line3\r\nline2\r\nline1\r\n", document.get());
// AND the number of lines is still the same, because we have not appended a line:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -86,7 +85,7 @@ private static InlayHintLabelPart createInlayLabelPart(String text, String comma

private static InlayHint createMultiLabelInlayHint(InlayHintLabelPart... parts) {
final var inlay = new InlayHint();
inlay.setLabel(Arrays.asList(parts));
inlay.setLabel(List.of(parts));
inlay.setPosition(new Position(0, 0));
return inlay;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import java.io.IOException;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.externaltools.internal.IExternalToolConstants;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void earlyStartup() {
mockServerLauch = workingCopy.doSave();
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.lsp4e.test.content-type2"),
LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, mockServerLauch.getName()),
Collections.singleton(ILaunchManager.RUN_MODE));
Set.of(ILaunchManager.RUN_MODE));
}
} catch (CoreException e) {
LanguageServerPlugin.logError(e);
Expand All @@ -87,7 +88,7 @@ public void earlyStartup() {
private String getClassPath(Class<?> clazz) {
ClassLoader loader = clazz.getClassLoader();
if (loader instanceof URLClassLoader urlClassLoader) {
return Arrays.asList(urlClassLoader.getURLs()).stream().map(url -> url.getFile()).collect(Collectors.joining(System.getProperty("path.separator")));
return List.of(urlClassLoader.getURLs()).stream().map(url -> url.getFile()).collect(Collectors.joining(System.getProperty("path.separator")));
}
final var toProcess = new LinkedList<Bundle>();
final var processed = new HashSet<Bundle>();
Expand Down
Loading
Loading