@@ -38,6 +38,10 @@ public static class AnalysisResult {
3838 public String testClassName = "" ;
3939 public String testCaseName = "" ;
4040 public String projectName = "" ;
41+ public List <String > beforeMethods = new ArrayList <>();
42+ public List <String > beforeAllMethods = new ArrayList <>();
43+ public List <String > afterMethods = new ArrayList <>();
44+ public List <String > afterAllMethods = new ArrayList <>();
4145
4246 // Helper to create the JSON filename
4347 public String getJsonFileName () {
@@ -89,6 +93,9 @@ public AnalysisResult analyzeTestCase(CompilationUnit testCu, MethodDeclaration
8993 }
9094 }
9195
96+ // Extract lifecycle methods (@Before, @BeforeAll, @After, @AfterAll) from the test class
97+ extractLifecycleMethods (testCu , result , originalFileSource );
98+
9299 // Perform DFS-like analysis for parsed_statements_sequence and production_function_implementations
93100 dfsAnalyze (testCu , testMethodDeclaration , 0 , result , result .testClassName + "." + result .testCaseName + getParameters (testMethodDeclaration .resolveBinding ()), originalFileSource );
94101 return result ;
@@ -432,4 +439,74 @@ public String getSourceFilePathForClass(String className, String sourceRootPath)
432439 System .err .println ("Warning: Source file not found for class " + className + " under roots: " + potentialSourceRoots );
433440 return null ;
434441 }
442+
443+ private void extractLifecycleMethods (CompilationUnit testCu , AnalysisResult result , String originalFileSource ) {
444+ // Get all type declarations in the compilation unit
445+ for (Object type : testCu .types ()) {
446+ if (type instanceof TypeDeclaration ) {
447+ TypeDeclaration typeDecl = (TypeDeclaration ) type ;
448+
449+ // Get all methods in the type
450+ for (MethodDeclaration method : typeDecl .getMethods ()) {
451+ // Check for lifecycle annotations
452+ for (Object modifier : method .modifiers ()) {
453+ if (modifier instanceof Annotation ) {
454+ Annotation annotation = (Annotation ) modifier ;
455+ String annotationName = annotation .getTypeName ().getFullyQualifiedName ();
456+
457+ String methodSourceCode = extractMethodSourceCode (method , originalFileSource );
458+
459+ // Check for @Before annotations (JUnit 4 and 5)
460+ if ("Before" .equals (annotationName ) ||
461+ "org.junit.Before" .equals (annotationName ) ||
462+ "org.junit.jupiter.api.BeforeEach" .equals (annotationName ) ||
463+ "BeforeEach" .equals (annotationName )) {
464+ result .beforeMethods .add (methodSourceCode );
465+ System .out .println (" Found @Before method: " + method .getName ().getIdentifier ());
466+ }
467+
468+ // Check for @BeforeAll annotations (JUnit 5)
469+ else if ("BeforeAll" .equals (annotationName ) ||
470+ "org.junit.jupiter.api.BeforeAll" .equals (annotationName ) ||
471+ "BeforeClass" .equals (annotationName ) ||
472+ "org.junit.BeforeClass" .equals (annotationName )) {
473+ result .beforeAllMethods .add (methodSourceCode );
474+ System .out .println (" Found @BeforeAll/@BeforeClass method: " + method .getName ().getIdentifier ());
475+ }
476+
477+ // Check for @After annotations (JUnit 4 and 5)
478+ else if ("After" .equals (annotationName ) ||
479+ "org.junit.After" .equals (annotationName ) ||
480+ "org.junit.jupiter.api.AfterEach" .equals (annotationName ) ||
481+ "AfterEach" .equals (annotationName )) {
482+ result .afterMethods .add (methodSourceCode );
483+ System .out .println (" Found @After method: " + method .getName ().getIdentifier ());
484+ }
485+
486+ // Check for @AfterAll annotations (JUnit 5)
487+ else if ("AfterAll" .equals (annotationName ) ||
488+ "org.junit.jupiter.api.AfterAll" .equals (annotationName ) ||
489+ "AfterClass" .equals (annotationName ) ||
490+ "org.junit.AfterClass" .equals (annotationName )) {
491+ result .afterAllMethods .add (methodSourceCode );
492+ System .out .println (" Found @AfterAll/@AfterClass method: " + method .getName ().getIdentifier ());
493+ }
494+ }
495+ }
496+ }
497+ }
498+ }
499+ }
500+
501+ private String extractMethodSourceCode (MethodDeclaration method , String originalFileSource ) {
502+ int startPos = method .getStartPosition ();
503+ int length = method .getLength ();
504+
505+ if (startPos >= 0 && length > 0 && (startPos + length ) <= originalFileSource .length ()) {
506+ return originalFileSource .substring (startPos , startPos + length );
507+ } else {
508+ System .err .println ("Warning: Invalid start/length for lifecycle method source code extraction. Method: " + method .getName ().getIdentifier ());
509+ return "// Error extracting source code for " + method .getName ().getIdentifier ();
510+ }
511+ }
435512}
0 commit comments