Skip to content

Commit 070278c

Browse files
vaadin-botArtur-
andauthored
fix: Do not throw exception if no IDE is found (#16058) (#16068)
Fixes #16043 Co-authored-by: Artur <artur@vaadin.com>
1 parent 9d28e90 commit 070278c

2 files changed

Lines changed: 77 additions & 21 deletions

File tree

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/OpenInCurrentIde.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static Optional<Info> findIdeCommand(List<Info> processes) {
164164
}
165165

166166
private static String getCommandAndArguments(Info info) {
167-
return info.commandLine().get();
167+
return info.commandLine().orElse(null);
168168
}
169169

170170
private static List<ProcessHandle> getParentProcesses() {
@@ -178,13 +178,16 @@ private static List<ProcessHandle> getParentProcesses() {
178178
}
179179

180180
static boolean isEclipse(Info info) {
181-
String lowerCase = info.command().get().toLowerCase(Locale.ENGLISH);
182-
// Eclipse has a lot of other products like Temurin and Adoptium so we
183-
// cannot
184-
// check with "contains"
185-
return lowerCase.endsWith("eclipse")
186-
|| lowerCase.endsWith("eclipse.exe");
181+
Optional<String> cmd = info.command();
182+
if (cmd.isPresent()) {
183+
String lowerCmd = cmd.get().toLowerCase(Locale.ENGLISH);
184+
// Eclipse has a lot of other products like Temurin and Adoptium so
185+
// we cannot check with "contains"
186+
return lowerCmd.endsWith("eclipse")
187+
|| lowerCmd.endsWith("eclipse.exe");
188+
}
187189

190+
return false;
188191
}
189192

190193
static boolean isIdea(Info info) {
@@ -193,7 +196,8 @@ static boolean isIdea(Info info) {
193196

194197
private static String getIdeaBinary(Info info) {
195198
String commandAndArguments = getCommandAndArguments(info);
196-
if (commandAndArguments.contains("idea_rt.jar")) {
199+
if (commandAndArguments != null
200+
&& commandAndArguments.contains("idea_rt.jar")) {
197201
String replaced = commandAndArguments
198202
.replaceFirst(".*[:;]([^:;]*)(idea_rt.jar).*", "$1$2");
199203
if (!replaced.equals(commandAndArguments)) {
@@ -208,10 +212,7 @@ private static String getIdeaBinary(Info info) {
208212
}
209213
}
210214
}
211-
if (info.command().get().contains("idea")) {
212-
return info.command().get();
213-
}
214-
return null;
215+
return info.command().filter(cmd -> cmd.contains("idea")).orElse(null);
215216
}
216217

217218
static boolean isVSCode(Info info) {
@@ -220,11 +221,14 @@ static boolean isVSCode(Info info) {
220221
return true;
221222
}
222223

223-
String cmd = getCommandAndArguments(info).toLowerCase(Locale.ENGLISH);
224-
if (cmd.contains("vscode") || cmd.contains("vs code")
225-
|| cmd.contains("code helper")
226-
|| cmd.contains("visual studio code")) {
227-
return true;
224+
String cmd = getCommandAndArguments(info);
225+
if (cmd != null) {
226+
String cmdLower = cmd.toLowerCase(Locale.ENGLISH);
227+
if (cmdLower.contains("vscode") || cmdLower.contains("vs code")
228+
|| cmdLower.contains("code helper")
229+
|| cmdLower.contains("visual studio code")) {
230+
return true;
231+
}
228232
}
229233

230234
return false;

vaadin-dev-server/src/test/java/com/vaadin/base/devserver/OpenInCurrentIdeTest.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,68 @@ public void eclipseOnLinuxDetected() {
528528

529529
}
530530

531+
@Test
532+
public void runFromCommandLineWorks() {
533+
String cmd1 = "/opt/homebrew/Cellar/openjdk/19.0.2/libexec/openjdk.jdk/Contents/Home/bin/java";
534+
String[] args1 = new String[] { "-XX:TieredStopAtLevel=1", "-Xdebug",
535+
"-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5731",
536+
"-cp", "lotsofjars.jar",
537+
"com.example.application.Application", };
538+
539+
String cmd2 = "/opt/homebrew/opt/openjdk/libexec/openjdk.jdk/Contents/Home/bin/java";
540+
String[] args2 = new String[] { "-classpath",
541+
"/opt/homebrew/Cellar/maven/3.9.0/libexec/boot/plexus-classworlds-2.6.0.jar",
542+
"-Dclassworlds.conf=/opt/homebrew/Cellar/maven/3.9.0/libexec/bin/m2.conf",
543+
"-Dmaven.home=/opt/homebrew/Cellar/maven/3.9.0/libexec",
544+
"-Dlibrary.jansi.path=/opt/homebrew/Cellar/maven/3.9.0/libexec/lib/jansi-native",
545+
"-Dmaven.multiModuleProjectDirectory=/home/foo/test/openide",
546+
"org.codehaus.plexus.classworlds.launcher.Launcher",
547+
"spring-boot:run", };
548+
549+
String cmd3 = "/bin/zsh";
550+
String[] args3 = new String[] {};
551+
552+
String cmd4 = null;
553+
String[] args4 = null;
554+
555+
String cmd5 = "/System/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal";
556+
String[] args5 = new String[] {};
557+
558+
String cmd6 = null;
559+
String[] args6 = null;
560+
561+
Info info1 = mock(cmd1, args1);
562+
Info info2 = mock(cmd2, args2);
563+
Info info3 = mock(cmd3, args3);
564+
Info info4 = mock(cmd4, args4);
565+
Info info5 = mock(cmd5, args5);
566+
Info info6 = mock(cmd6, args6);
567+
568+
List<Info> processes = new ArrayList<>();
569+
processes.add(info1);
570+
processes.add(info2);
571+
processes.add(info3);
572+
processes.add(info4);
573+
processes.add(info5);
574+
processes.add(info6);
575+
576+
Optional<Info> ideCommand = OpenInCurrentIde.findIdeCommand(processes);
577+
Assert.assertTrue(ideCommand.isEmpty());
578+
}
579+
531580
private Info mock(String cmd) {
532581
return mock(cmd, cmd);
533582
}
534583

535584
private Info mock(String cmd, String[] arguments) {
536585
Info info = Mockito.mock(Info.class);
537-
Mockito.when(info.command()).thenReturn(Optional.of(cmd));
538-
Mockito.when(info.arguments()).thenReturn(Optional.of(arguments));
539-
Mockito.when(info.commandLine()).thenReturn(
540-
Optional.of(cmd + " " + String.join(" ", arguments)));
586+
Mockito.when(info.command()).thenReturn(Optional.ofNullable(cmd));
587+
Mockito.when(info.arguments())
588+
.thenReturn(Optional.ofNullable(arguments));
589+
if (cmd != null && arguments != null) {
590+
Mockito.when(info.commandLine()).thenReturn(
591+
Optional.of(cmd + " " + String.join(" ", arguments)));
592+
}
541593

542594
return info;
543595
}

0 commit comments

Comments
 (0)