forEach(Main::myMethod)未执行,setForStatement调用无输出求助
setForStatement Not Executing (No "hello5" Output) Let's break down why your setForStatement method isn't running (and thus no "hello5" is printed) and walk through how to fix it:
Core Issue
The line cu.getNodesByType(ForStmt.class).forEach(Main::setForStatement) isn't finding any ForStmt nodes in your parsed compilation unit. That means the for-each loop never executes, so your method never gets called.
Likely Causes & Fixes
1. Your Target File Class.java Has No Traditional for Loops
JavaParser's ForStmt class maps specifically to C-style traditional for loops (e.g., for(int i=0; i<10; i++)). If your Class.java uses enhanced for loops (like for (String item : list)), you need to use ForeachStmt.class instead of ForStmt.class.
First, check what type of loops exist in your source file, then adjust your code:
// For traditional C-style for loops cu.getNodesByType(ForStmt.class).forEach(Main::setForStatement); // OR for enhanced for-each loops cu.getNodesByType(ForeachStmt.class).forEach(Main::setForStatement);
2. File Parsing Failed (Invalid Java or Wrong Path)
If Class.java isn't valid Java code, or your program can't locate the file, the parsed CompilationUnit (cu) will be incomplete or empty. Add these checks right after parsing to confirm:
try(FileInputStream in = new FileInputStream(fileName)){ cu = JavaParser.parse(in); } // Check for parsing errors if (!cu.getProblems().isEmpty()) { System.out.println("Parsing errors detected:"); cu.getProblems().forEach(problem -> System.out.println("- " + problem.getMessage())); return; // Exit early if parsing failed } // Verify the target class was found Optional<ClassOrInterfaceDeclaration> targetClass = cu.getClassByName("Class"); if (targetClass.isEmpty()) { System.out.println("Target class 'Class' not found in the parsed file!"); return; }
Also, double-check that Class.java is in your program's working directory. If not, use an absolute path (e.g., C:/your-project/Class.java) instead of a relative path.
3. Debug Node Count to Confirm Trigger
Before calling forEach, print how many ForStmt nodes were found. This will immediately tell you if the issue is missing nodes:
List<ForStmt> forLoops = cu.getNodesByType(ForStmt.class); System.out.println("Found " + forLoops.size() + " ForStmt nodes"); forLoops.forEach(Main::setForStatement);
If the count is 0, circle back to checking your source file and parsing step.
Bonus: Safeguard Against ClassCast Exceptions
Your setForStatement method has an unsafe cast: (MethodCallExpr) forStmt.getInitialization().get(0). If the for loop's initialization isn't a method call, this will crash your program. Add checks to make it robust:
private static void setForStatement(ForStmt forStmt) { System.out.println("hello5"); if (forStmt.getInitialization().isEmpty()) { System.out.println("For loop has no initialization statement"); return; } Node initNode = forStmt.getInitialization().get(0); if (!(initNode instanceof MethodCallExpr)) { System.out.println("Initialization is not a MethodCallExpr: " + initNode); return; } MethodCallExpr initializer = (MethodCallExpr) initNode; SimpleName name = initializer.getName(); // Rest of your logic... }
Final Checklist
- Confirm
Class.javacontains valid traditional for loops (or switch toForeachStmtif using enhanced loops) - Verify the file path is correct and the file parses without errors
- Debug the number of found
ForStmtnodes to confirm the loop is being triggered
内容的提问来源于stack exchange,提问作者Viola

