-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava_method_profiling.py
More file actions
59 lines (53 loc) · 1.9 KB
/
Copy pathjava_method_profiling.py
File metadata and controls
59 lines (53 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import subprocess
import os
from dotenv import load_dotenv
from utils import Logger
import sys
load_dotenv()
USER_PREFIX = os.getenv('USER_PREFIX')
logger = Logger("logs", sys.argv[2]).logger
PROJECT_DIR = f"{USER_PREFIX}/src/java_code_extraction"
JAVA_CLASS_NAME = "JavaCodeExtraction"
TARGET_CLASSES_DIR = os.path.join(PROJECT_DIR, "target", "classes")
EXTRA_JARS = ""
def compile_java_project():
"""
Runs 'mvn clean compile' inside PROJECT_DIR to compile the Java code.
"""
logger.info("Compiling Java project...")
cmd = ["mvn", "clean", "compile"]
try:
result = subprocess.run(cmd, cwd=PROJECT_DIR, capture_output=True, text=True, check=True)
logger.info("Java project compiled successfully.\n")
return True
except subprocess.CalledProcessError as e:
logger.error(f"Error compiling Java project:\n{e.stderr}")
return False
def get_method_source_code(file_path: str, method_name: str) -> str:
cmd = [
"mvn",
"-q",
"exec:java",
f"-Dexec.args=print {file_path} {method_name}",
]
try:
result = subprocess.run(cmd, cwd=PROJECT_DIR, capture_output=True, text=True, check=True)
output = result.stdout.strip()
logger.info(f"method source code: {output}")
return output
except subprocess.CalledProcessError as e:
logger.error(f"Error in get_method_source_code:\n{e.stderr}")
def replace_method_body(file_path: str, method_name: str) -> str:
cmd = [
"mvn",
"-q",
"exec:java",
f"-Dexec.args=replace {file_path} {method_name}",
]
try:
result = subprocess.run(cmd, cwd=PROJECT_DIR, capture_output=True, text=True, check=True)
logger.info(f"Method body replaced successfully")
return True
except subprocess.CalledProcessError as e:
logger.error(f"Error in replace_method_body:\n{e.stderr}")
return False