1414import java .io .IOException ;
1515import java .io .StringReader ;
1616import java .util .ArrayList ;
17+ import java .util .Arrays ;
18+ import java .util .Collections ;
1719import java .util .Date ;
1820import java .util .LinkedHashSet ;
1921import java .util .List ;
2830
2931import org .apache .commons .lang3 .StringUtils ;
3032import org .eclipse .core .resources .IProject ;
33+ import org .eclipse .core .resources .IResource ;
3134import org .eclipse .core .resources .ResourcesPlugin ;
3235import org .eclipse .core .runtime .CoreException ;
3336import org .eclipse .core .runtime .IStatus ;
@@ -176,26 +179,29 @@ private static String[][] computeClassPath(String mainClass, String projectName)
176179 project = projects .get (0 );
177180 }
178181
179- return computeClassPath (project , !isMainClassInTestFolder (project , mainClass ));
182+ IJavaElement testElement = findMainClassInTestFolders (project , mainClass );
183+ List <IResource > mappedResources = (testElement != null && testElement .getResource () != null )
184+ ? Arrays .asList (testElement .getResource ()) : Collections .EMPTY_LIST ;
185+ return computeClassPath (project , testElement == null , mappedResources );
180186 }
181187
182188 /**
183189 * Compute runtime classpath of a java project.
184190 *
185- * @param javaProject
186- * java project
187- * @param excludeTestCode whether to exclude the test code and test dependencies.
191+ * @param javaProject java project
192+ * @param excludeTestCode whether to exclude the test code and test dependencies
193+ * @param mappedResources the associated resources with the application
188194 * @return class path
189195 * @throws CoreException
190196 * CoreException
191197 */
192- private static String [][] computeClassPath (IJavaProject javaProject , boolean excludeTestCode )
198+ private static String [][] computeClassPath (IJavaProject javaProject , boolean excludeTestCode , List < IResource > mappedResources )
193199 throws CoreException {
194200 if (javaProject == null ) {
195201 throw new IllegalArgumentException ("javaProject is null" );
196202 }
197203
198- ILaunchConfiguration launchConfig = new JavaApplicationLaunchConfiguration (javaProject .getProject (), excludeTestCode );
204+ ILaunchConfiguration launchConfig = new JavaApplicationLaunchConfiguration (javaProject .getProject (), excludeTestCode , mappedResources );
199205 IRuntimeClasspathEntry [] unresolved = JavaRuntime .computeUnresolvedRuntimeClasspath (launchConfig );
200206 IRuntimeClasspathEntry [] resolved = JavaRuntime .resolveRuntimeClasspath (unresolved , launchConfig );
201207 Set <String > classpaths = new LinkedHashSet <>();
@@ -219,19 +225,24 @@ private static String[][] computeClassPath(IJavaProject javaProject, boolean exc
219225 }
220226
221227 /**
222- * Test whether the main class is located in test folders.
228+ * Try to find the associated java element with the main class from the test folders.
229+ *
223230 * @param project the java project containing the main class
224231 * @param mainClass the main class name
225- * @return whether the main class is located in test folders
232+ * @return the associated java element
226233 */
227- private static boolean isMainClassInTestFolder (IJavaProject project , String mainClass ) {
234+ private static IJavaElement findMainClassInTestFolders (IJavaProject project , String mainClass ) {
235+ if (project == null || StringUtils .isBlank (mainClass )) {
236+ return null ;
237+ }
238+
228239 // get a list of test folders and check whether main class is here
229240 int constraints = IJavaSearchScope .SOURCES ;
230241 IJavaElement [] testFolders = JdtUtils .getTestPackageFragmentRoots (project );
231242 if (testFolders .length > 0 ) {
232243 try {
233244
234- List <Object > mainClassesInTestFolder = new ArrayList <>();
245+ List <IJavaElement > mainClassesInTestFolder = new ArrayList <>();
235246 SearchPattern pattern = SearchPattern .createPattern (mainClass , IJavaSearchConstants .CLASS ,
236247 IJavaSearchConstants .DECLARATIONS ,
237248 SearchPattern .R_CASE_SENSITIVE | SearchPattern .R_EXACT_MATCH );
@@ -242,7 +253,7 @@ private static boolean isMainClassInTestFolder(IJavaProject project, String main
242253 public void acceptSearchMatch (SearchMatch match ) {
243254 Object element = match .getElement ();
244255 if (element instanceof IJavaElement ) {
245- mainClassesInTestFolder .add (element );
256+ mainClassesInTestFolder .add (( IJavaElement ) element );
246257 }
247258 }
248259 };
@@ -251,12 +262,15 @@ public void acceptSearchMatch(SearchMatch match) {
251262 SearchEngine .getDefaultSearchParticipant ()
252263 }, scope , requestor , null /* progress monitor */ );
253264
254- return !mainClassesInTestFolder .isEmpty ();
265+ if (!mainClassesInTestFolder .isEmpty ()) {
266+ return mainClassesInTestFolder .get (0 );
267+ }
255268 } catch (Exception e ) {
256269 logger .log (Level .SEVERE , String .format ("Searching the main class failure: %s" , e .toString ()), e );
257270 }
258271 }
259- return false ;
272+
273+ return null ;
260274 }
261275
262276 private static class JavaApplicationLaunchConfiguration extends LaunchConfiguration {
@@ -265,19 +279,20 @@ private static class JavaApplicationLaunchConfiguration extends LaunchConfigurat
265279 + "<listAttribute key=\" org.eclipse.debug.core.MAPPED_RESOURCE_PATHS\" >\n "
266280 + "</listAttribute>\n "
267281 + "<listAttribute key=\" org.eclipse.debug.core.MAPPED_RESOURCE_TYPES\" >\n "
268- + "<listEntry value=\" 1\" />\n "
269282 + "</listAttribute>\n "
270283 + "</launchConfiguration>" ;
271284 private IProject project ;
272285 private boolean excludeTestCode ;
286+ private List <IResource > mappedResources ;
273287 private String classpathProvider ;
274288 private String sourcepathProvider ;
275289 private LaunchConfigurationInfo launchInfo ;
276290
277- protected JavaApplicationLaunchConfiguration (IProject project , boolean excludeTestCode ) throws CoreException {
291+ protected JavaApplicationLaunchConfiguration (IProject project , boolean excludeTestCode , List < IResource > mappedResources ) throws CoreException {
278292 super (String .valueOf (new Date ().getTime ()), null , false );
279293 this .project = project ;
280294 this .excludeTestCode = excludeTestCode ;
295+ this .mappedResources = mappedResources ;
281296 if (ProjectUtils .isMavenProject (project )) {
282297 classpathProvider = "org.eclipse.m2e.launchconfig.classpathProvider" ;
283298 sourcepathProvider = "org.eclipse.m2e.launchconfig.sourcepathProvider" ;
@@ -309,6 +324,11 @@ public String getAttribute(String attributeName, String defaultValue) throws Cor
309324 return super .getAttribute (attributeName , defaultValue );
310325 }
311326
327+ @ Override
328+ public IResource [] getMappedResources () throws CoreException {
329+ return mappedResources .toArray (new IResource [0 ]);
330+ }
331+
312332 @ Override
313333 protected LaunchConfigurationInfo getInfo () throws CoreException {
314334 return this .launchInfo ;
0 commit comments