Skip to content

Commit 5ef1178

Browse files
authored
GH #798: At execution Espressif-IDE getting 'Cannot invoke "org.eclipse.debug.core.ILaunchConfiguration.getType()" because "configuration" is null' (#804)
* fix npe related with active config * added test coverage
1 parent 838f4f8 commit 5ef1178

File tree

5 files changed

+162
-11
lines changed

5 files changed

+162
-11
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*******************************************************************************
2+
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
3+
* Use is subject to license terms.
4+
*******************************************************************************/
5+
package com.espressif.idf.core.build;
6+
7+
import java.util.Optional;
8+
import java.util.stream.Stream;
9+
10+
import org.eclipse.core.runtime.CoreException;
11+
import org.eclipse.core.runtime.jobs.Job;
12+
import org.eclipse.debug.core.ILaunchConfiguration;
13+
import org.eclipse.launchbar.core.ILaunchBarManager;
14+
15+
import com.espressif.idf.core.IDFCorePlugin;
16+
import com.espressif.idf.core.logging.Logger;
17+
18+
/**
19+
* This provider allows you getting the active launch configuration from the ILaunchBarManager, even if it is not yet
20+
* initialized. In this case, we look for the initialization Job and join it.
21+
*
22+
* @author Denys Almazov <denys.almazov@espressif.com>
23+
*
24+
*/
25+
public class ActiveLaunchConfigurationProvider
26+
{
27+
private ILaunchBarManager launchBarManager;
28+
29+
public ActiveLaunchConfigurationProvider(ILaunchBarManager launchBarManager)
30+
{
31+
this.launchBarManager = launchBarManager;
32+
}
33+
34+
public ActiveLaunchConfigurationProvider()
35+
{
36+
this(IDFCorePlugin.getService(ILaunchBarManager.class));
37+
}
38+
39+
public ILaunchConfiguration getActiveLaunchConfiguration() throws CoreException
40+
{
41+
ILaunchConfiguration configuration = launchBarManager.getActiveLaunchConfiguration();
42+
43+
if (configuration == null)
44+
{
45+
Job[] jobs = Job.getJobManager().find(null);
46+
@SuppressWarnings("restriction")
47+
Optional<Job> launchBarInitJob = Stream.of(jobs)
48+
.filter(job -> job.getName()
49+
.equals(org.eclipse.launchbar.core.internal.Messages.LaunchBarManager_0))
50+
.findAny();
51+
launchBarInitJob.ifPresent(job -> {
52+
try
53+
{
54+
job.join();
55+
}
56+
catch (InterruptedException e)
57+
{
58+
Logger.log(e);
59+
Thread.currentThread().interrupt();
60+
}
61+
});
62+
configuration = launchBarManager.getActiveLaunchConfiguration();
63+
}
64+
return configuration;
65+
}
66+
}

bundles/com.espressif.idf.core/src/com/espressif/idf/core/build/IDFBuildConfiguration.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
9393
import org.eclipse.debug.core.DebugPlugin;
9494
import org.eclipse.debug.core.ILaunchConfiguration;
95-
import org.eclipse.debug.core.ILaunchConfigurationType;
9695
import org.eclipse.debug.core.ILaunchManager;
9796
import org.eclipse.debug.core.ILaunchMode;
9897
import org.eclipse.launchbar.core.ILaunchBarManager;
@@ -114,6 +113,7 @@
114113
public class IDFBuildConfiguration extends CBuildConfiguration
115114
{
116115

116+
private static final ActiveLaunchConfigurationProvider LAUNCH_CONFIG_PROVIDER = new ActiveLaunchConfigurationProvider();
117117
private static final String NINJA = "Ninja"; //$NON-NLS-1$
118118
protected static final String COMPILE_COMMANDS_JSON = "compile_commands.json"; //$NON-NLS-1$
119119
protected static final String COMPONENTS = "components"; //$NON-NLS-1$
@@ -248,15 +248,14 @@ public String getProperty(String name)
248248
{
249249
try
250250
{
251-
ILaunchConfiguration configuration = IDFCorePlugin.getService(ILaunchBarManager.class)
252-
.getActiveLaunchConfiguration();
253-
ILaunchConfigurationType debugConfigurationType = DebugPlugin.getDefault().getLaunchManager()
254-
.getLaunchConfigurationType(IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE);
255-
if (configuration.getType().equals(debugConfigurationType))
251+
ILaunchConfiguration configuration = LAUNCH_CONFIG_PROVIDER.getActiveLaunchConfiguration();
252+
if (configuration != null
253+
&& configuration.getType().getIdentifier().equals(IDFLaunchConstants.DEBUG_LAUNCH_CONFIG_TYPE))
256254
{
257255
configuration = getBoundConfiguration(configuration);
258256
}
259-
String property = configuration.getAttribute(name, StringUtil.EMPTY);
257+
String property = configuration == null ? StringUtil.EMPTY
258+
: configuration.getAttribute(name, StringUtil.EMPTY);
260259
property = property.isBlank() ? getSettings().get(name, StringUtil.EMPTY) : property;
261260
return property;
262261
}

bundles/com.espressif.idf.launch.serial.core/src/com/espressif/idf/launch/serial/internal/SerialFlashLaunchConfigProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class SerialFlashLaunchConfigProvider extends IDFCoreLaunchConfigProvider
3030
@Override
3131
public boolean supports(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException
3232
{
33-
return target.getTypeId().equals(IDFLaunchConstants.ESP_LAUNCH_TARGET_TYPE);
33+
return target != null && target.getTypeId().equals(IDFLaunchConstants.ESP_LAUNCH_TARGET_TYPE);
3434
}
3535

3636
@Override

tests/com.espressif.idf.core.test/META-INF/MANIFEST.MF

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Bundle-RequiredExecutionEnvironment: JavaSE-11
99
Require-Bundle: com.espressif.idf.core;bundle-version="1.0.1",
1010
junit-jupiter-api,
1111
org.junit,
12-
junit-jupiter-params
12+
junit-jupiter-params,
13+
org.eclipse.launchbar.core
1314
Bundle-ClassPath: .,
1415
lib/jmock-2.12.0.jar,
1516
lib/commons-collections4-4.4.jar,
@@ -72,6 +73,7 @@ Export-Package: com.espressif.idf.core.test,
7273
org.jmock.auto,
7374
org.jmock.lib
7475
Import-Package: org.eclipse.core.runtime,
76+
org.eclipse.core.runtime.jobs,
77+
org.eclipse.core.variables,
7578
org.junit.jupiter.params,
76-
org.junit.jupiter.params.provider,
77-
org.eclipse.core.variables
79+
org.junit.jupiter.params.provider
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*******************************************************************************
2+
* Copyright 2023 Espressif Systems (Shanghai) PTE LTD. All rights reserved.
3+
* Use is subject to license terms.
4+
*******************************************************************************/
5+
package com.espressif.idf.core.test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.mockito.Mockito.mock;
9+
import static org.mockito.Mockito.when;
10+
11+
import org.eclipse.core.runtime.CoreException;
12+
import org.eclipse.core.runtime.IProgressMonitor;
13+
import org.eclipse.core.runtime.IStatus;
14+
import org.eclipse.core.runtime.Status;
15+
import org.eclipse.core.runtime.jobs.Job;
16+
import org.eclipse.debug.core.ILaunchConfiguration;
17+
import org.eclipse.launchbar.core.ILaunchBarManager;
18+
import org.junit.jupiter.api.DisplayNameGeneration;
19+
import org.junit.jupiter.api.DisplayNameGenerator;
20+
import org.junit.jupiter.api.Test;
21+
22+
import com.espressif.idf.core.build.ActiveLaunchConfigurationProvider;
23+
import com.espressif.idf.core.logging.Logger;
24+
25+
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
26+
class ActiveLaunchConfigurationTest
27+
{
28+
29+
private static final String EXPECTED_LAUNCH_CONFIG_NAME = "expected_launch_config_name";
30+
31+
@Test
32+
void get_active_launch_configuration_returns_expected_config_when_init_launchbar_job_is_active()
33+
throws CoreException
34+
{
35+
ILaunchBarManager launchBarManager = mock(ILaunchBarManager.class);
36+
ILaunchConfiguration launchConfiguration = mock(ILaunchConfiguration.class);
37+
38+
when(launchConfiguration.getName()).thenReturn(EXPECTED_LAUNCH_CONFIG_NAME);
39+
when(launchBarManager.getActiveLaunchConfiguration()).thenReturn(null);
40+
ActiveLaunchConfigurationProvider provider = new ActiveLaunchConfigurationProvider(launchBarManager);
41+
runInitLaunchBarJob(launchBarManager, launchConfiguration);
42+
ILaunchConfiguration configuration = provider.getActiveLaunchConfiguration();
43+
44+
assertEquals(EXPECTED_LAUNCH_CONFIG_NAME, configuration.getName());
45+
}
46+
47+
@Test
48+
void get_active_launch_configuration_returns_expected_config_when_init_launchbar_job_is_not_active()
49+
throws CoreException
50+
{
51+
ILaunchBarManager launchBarManager = mock(ILaunchBarManager.class);
52+
ILaunchConfiguration launchConfiguration = mock(ILaunchConfiguration.class);
53+
54+
when(launchConfiguration.getName()).thenReturn(EXPECTED_LAUNCH_CONFIG_NAME);
55+
when(launchBarManager.getActiveLaunchConfiguration()).thenReturn(launchConfiguration);
56+
ActiveLaunchConfigurationProvider provider = new ActiveLaunchConfigurationProvider(launchBarManager);
57+
ILaunchConfiguration configuration = provider.getActiveLaunchConfiguration();
58+
59+
assertEquals(EXPECTED_LAUNCH_CONFIG_NAME, configuration.getName());
60+
}
61+
62+
private void runInitLaunchBarJob(ILaunchBarManager launchBarManager, ILaunchConfiguration launchConfiguration)
63+
{
64+
@SuppressWarnings("restriction")
65+
Job job = new Job(org.eclipse.launchbar.core.internal.Messages.LaunchBarManager_0)
66+
{
67+
68+
protected IStatus run(IProgressMonitor monitor)
69+
{
70+
try
71+
{
72+
when(launchBarManager.getActiveLaunchConfiguration()).thenReturn(launchConfiguration);
73+
}
74+
catch (CoreException e)
75+
{
76+
Logger.log(e);
77+
}
78+
return Status.OK_STATUS;
79+
}
80+
};
81+
job.schedule();
82+
}
83+
84+
}

0 commit comments

Comments
 (0)