3636import org .eclipse .debug .core .sourcelookup .AbstractSourceLookupDirector ;
3737import org .eclipse .debug .core .sourcelookup .containers .ProjectSourceContainer ;
3838import org .eclipse .jdt .core .IJavaProject ;
39- import org .eclipse .jdt .core .JavaModelException ;
4039import org .eclipse .jdt .debug .core .IJavaStackFrame ;
4140import org .eclipse .jdt .debug .eval .ICompiledExpression ;
4241import org .eclipse .jdt .internal .debug .core .model .JDIDebugTarget ;
@@ -139,7 +138,7 @@ public CompletableFuture<Value> evaluate(String expression, ThreadReference thre
139138 */
140139 private void initializeProjectCandidates (String mainclass ) {
141140 IWorkspaceRoot root = ResourcesPlugin .getWorkspace ().getRoot ();
142- List < IJavaProject > projects = Arrays .stream (root .getProjects ()).map (JdtUtils ::getJavaProject ).filter (p -> {
141+ projectCandidates = Arrays .stream (root .getProjects ()).map (JdtUtils ::getJavaProject ).filter (p -> {
143142 try {
144143 return p != null && p .hasBuildState ();
145144 } catch (Exception e ) {
@@ -149,71 +148,59 @@ private void initializeProjectCandidates(String mainclass) {
149148 }).collect (Collectors .toList ());
150149
151150
152- if (projects .size () > 1 && StringUtils .isNotBlank (mainclass )) {
153- projects = Arrays .stream (root .getProjects ()).map (JdtUtils ::getJavaProject ).filter (p -> {
154- try {
155- return p .findType (mainclass ) != null ;
156- } catch (JavaModelException e ) {
157- // ignore
158- }
159- return false ;
160- }).collect (Collectors .toList ());
161- visitedClassNames .add (mainclass );
162- }
163-
164- if (projects .size () == 1 ) {
165- project = projects .get (0 );
151+ if (StringUtils .isNotBlank (mainclass )) {
152+ filterProjectCandidatesByClass (mainclass );
166153 }
154+ }
167155
168- projectCandidates = projects ;
156+ private void filterProjectCandidatesByClass (String className ) {
157+ projectCandidates = visitedClassNames .contains (className ) ? projectCandidates
158+ : projectCandidates .stream ().filter (p -> {
159+ try {
160+ return p .findType (className ) != null ;
161+ } catch (Exception e ) {
162+ // ignore
163+ }
164+ return false ;
165+ }).collect (Collectors .toList ());
166+ visitedClassNames .add (className );
169167 }
170168
171- private void findJavaProjectByStackFrame (ThreadReference thread , int depth ) {
169+ private IJavaProject findJavaProjectByStackFrame (ThreadReference thread , int depth ) {
172170 if (projectCandidates == null ) {
173171 // initial candidate projects by main class (projects contains this main class)
174172 initializeProjectCandidates ((String ) options .get (Constants .MAIN_CLASS ));
175- if (project != null ) {
176- return ;
177- }
178173 }
179174
180175 if (projectCandidates .size () == 0 ) {
181176 logger .severe ("No project is available for evaluation." );
182- throw new IllegalStateException ("No project is available for evaluation ." );
177+ throw new IllegalStateException ("Cannot evaluate, please specify projectName in launch.json ." );
183178 }
184179
180+
185181 try {
186182 StackFrame sf = thread .frame (depth );
187183 String typeName = sf .location ().method ().declaringType ().name ();
188184 // narrow down candidate projects by current class
189- List <IJavaProject > validProjects = visitedClassNames .contains (typeName ) ? projectCandidates
190- : projectCandidates .stream ().filter (p -> {
191- try {
192- return !visitedClassNames .contains (typeName ) && p .findType (typeName ) != null ;
193- } catch (Exception e ) {
194- // ignore
195- }
196- return false ;
197- }).collect (Collectors .toList ());
198- visitedClassNames .add (typeName );
199- if (validProjects .size () == 1 ) {
200- project = validProjects .get (0 );
201- } else if (validProjects .size () == 0 ) {
202- logger .severe ("No project is available for evaluation." );
203- throw new IllegalStateException ("No project is available for evaluation, ." );
204- } else {
205- // narrow down projects
206- projectCandidates = validProjects ;
207- logger .severe ("Multiple projects are valid for evaluation." );
208- throw new IllegalStateException ("Multiple projects are found, please specify projectName in launch.json." );
209- }
210-
185+ filterProjectCandidatesByClass (typeName );
211186 } catch (Exception ex ) {
212- // ignore
187+ logger .severe ("Cannot evaluate when the project is not specified, due to exception: " + ex .getMessage ());
188+ throw new IllegalStateException ("Cannot evaluate, please specify projectName in launch.json." );
189+ }
190+
191+ if (projectCandidates .size () == 1 ) {
192+ return projectCandidates .get (0 );
193+ }
194+
195+ if (projectCandidates .size () == 0 ) {
196+ logger .severe ("No project is available for evaluation." );
197+ throw new IllegalStateException ("Cannot evaluate, please specify projectName in launch.json." );
198+ } else {
199+ // narrow down projects
200+ logger .severe ("Multiple projects are valid for evaluation." );
201+ throw new IllegalStateException ("Cannot evaluate, please specify projectName in launch.json." );
213202 }
214203
215- logger .severe ("Cannot evaluate when the project is not specified." );
216- throw new IllegalStateException ("Please specify projectName in launch.json." );
217204 }
218205
219206
@@ -291,7 +278,7 @@ private void ensureDebugTarget(VirtualMachine vm, ThreadReference thread, int de
291278 if (project == null ) {
292279 String projectName = (String ) options .get (Constants .PROJECT_NAME );
293280 if (StringUtils .isBlank (projectName )) {
294- findJavaProjectByStackFrame (thread , depth );
281+ project = findJavaProjectByStackFrame (thread , depth );
295282 } else {
296283 IJavaProject javaProject = JdtUtils .getJavaProject (projectName );
297284 if (javaProject == null ) {
0 commit comments