@@ -33,10 +33,11 @@ class ParsedOptimization:
3333 target_method_source : str
3434 new_fields : list [str ] # Source text of new fields to add
3535 new_helper_methods : list [str ] # Source text of new helper methods to add
36+ new_imports : list [str ] # Import statements to add (e.g., "import java.nio.file.Files;")
3637
3738
3839def _parse_optimization_source (new_source : str , target_method_name : str , analyzer : JavaAnalyzer ) -> ParsedOptimization :
39- """Parse optimization source to extract method and additional class members.
40+ """Parse optimization source to extract method, imports, and additional class members.
4041
4142 The new_source may contain:
4243 - Just a method definition
@@ -48,13 +49,20 @@ def _parse_optimization_source(new_source: str, target_method_name: str, analyze
4849 analyzer: JavaAnalyzer instance.
4950
5051 Returns:
51- ParsedOptimization with the method and any additional members.
52+ ParsedOptimization with the method, imports, and any additional members.
5253
5354 """
5455 new_fields : list [str ] = []
5556 new_helper_methods : list [str ] = []
5657 target_method_source = new_source # Default to the whole source
5758
59+ # Extract import statements from the candidate code
60+ new_imports : list [str ] = []
61+ for imp in analyzer .find_imports (new_source ):
62+ prefix = "import static " if imp .is_static else "import "
63+ suffix = ".*" if imp .is_wildcard else ""
64+ new_imports .append (f"{ prefix } { imp .import_path } { suffix } ;" )
65+
5866 # Check if this is a full class or just a method
5967 classes = analyzer .find_classes (new_source )
6068
@@ -92,10 +100,57 @@ def _parse_optimization_source(new_source: str, target_method_name: str, analyze
92100 new_fields .append (field .source_text )
93101
94102 return ParsedOptimization (
95- target_method_source = target_method_source , new_fields = new_fields , new_helper_methods = new_helper_methods
103+ target_method_source = target_method_source ,
104+ new_fields = new_fields ,
105+ new_helper_methods = new_helper_methods ,
106+ new_imports = new_imports ,
96107 )
97108
98109
110+ def _add_missing_imports (source : str , candidate_imports : list [str ], analyzer : JavaAnalyzer ) -> str :
111+ """Add import statements from the optimization candidate that are missing in the original source.
112+
113+ Args:
114+ source: The original source code.
115+ candidate_imports: Import statements from the candidate (e.g., ["import java.nio.file.Files;"]).
116+ analyzer: JavaAnalyzer instance.
117+
118+ Returns:
119+ Source code with missing imports added.
120+
121+ """
122+ existing_imports = analyzer .find_imports (source )
123+ existing_import_strs = set ()
124+ for imp in existing_imports :
125+ prefix = "import static " if imp .is_static else "import "
126+ suffix = ".*" if imp .is_wildcard else ""
127+ existing_import_strs .add (f"{ prefix } { imp .import_path } { suffix } ;" )
128+
129+ missing_imports = [imp for imp in candidate_imports if imp not in existing_import_strs ]
130+ if not missing_imports :
131+ return source
132+
133+ logger .debug ("Adding %d missing imports: %s" , len (missing_imports ), missing_imports )
134+
135+ # Insert after the last existing import, or after the package declaration
136+ lines = source .splitlines (keepends = True )
137+ insert_line = 0
138+
139+ if existing_imports :
140+ insert_line = max (imp .end_line for imp in existing_imports )
141+ else :
142+ # No existing imports — insert after package declaration
143+ for i , line in enumerate (lines ):
144+ if line .strip ().startswith ("package " ):
145+ insert_line = i + 1
146+ break
147+
148+ import_block = "" .join (imp + "\n " for imp in missing_imports )
149+ before = lines [:insert_line ]
150+ after = lines [insert_line :]
151+ return "" .join (before ) + import_block + "" .join (after )
152+
153+
99154def _insert_class_members (
100155 source : str , class_name : str , fields : list [str ], methods : list [str ], analyzer : JavaAnalyzer
101156) -> str :
@@ -237,6 +292,10 @@ def replace_function(
237292 # Parse the optimization to extract components
238293 parsed = _parse_optimization_source (new_source , func_name , analyzer )
239294
295+ # Add any new imports from the optimization candidate
296+ if parsed .new_imports :
297+ source = _add_missing_imports (source , parsed .new_imports , analyzer )
298+
240299 # Find the method in the original source
241300 methods = analyzer .find_methods (source )
242301 target_method = None
0 commit comments