Download app/modules/analyzer_java.py from rk147/secforge: direct link, hf CLI and curl.
- Browser
- Download file 3.98 kB
-
https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_java.py
- Command line
-
hf download hf://spaces/rk147/secforge/app/modules/analyzer_java.py
-
curl -L -o analyzer_java.py https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/analyzer_java.py
3.98 kB
| """Java code analyzer using Checkstyle.""" | |
| import subprocess | |
| import tempfile | |
| import os | |
| import xml.etree.ElementTree as ET | |
| from app.modules.analyzer_base import CodeAnalyzer, AnalysisResult | |
| class JavaAnalyzer(CodeAnalyzer): | |
| def __init__(self, language: str = "java"): | |
| super().__init__(language) | |
| self.checkstyle_available = self._check_checkstyle() | |
| def _check_checkstyle(self) -> bool: | |
| try: | |
| result = subprocess.run( | |
| ["checkstyle", "-version"], | |
| capture_output=True, timeout=5 | |
| ) | |
| return result.returncode == 0 | |
| except Exception: | |
| return False | |
| def analyze(self, code: str) -> AnalysisResult: | |
| if not self.checkstyle_available: | |
| return self._analyze_fallback(code) | |
| with tempfile.NamedTemporaryFile(mode="w", suffix=".java", delete=False, encoding="utf-8") as f: | |
| f.write(code) | |
| temp_path = f.name | |
| try: | |
| result = subprocess.run( | |
| ["checkstyle", "-f", "xml", temp_path], | |
| capture_output=True, text=True, timeout=15 | |
| ) | |
| issues = [] | |
| try: | |
| root = ET.fromstring(result.stdout) | |
| for file_elem in root.findall("file"): | |
| for error_elem in file_elem.findall("error"): | |
| line = error_elem.get("line", "0") | |
| symbol = error_elem.get("source", "").split(".")[-1] | |
| message = error_elem.get("message", "") | |
| severity = error_elem.get("severity", "warning").lower() | |
| severity_map = {"error": "high", "warning": "medium", "info": "low"} | |
| severity = severity_map.get(severity, "low") | |
| issues.append(self.format_issue( | |
| line=int(line) if line.isdigit() else 0, | |
| symbol=symbol, | |
| message=message, | |
| category="style", | |
| severity=severity, | |
| )) | |
| except ET.ParseError: | |
| pass | |
| return AnalysisResult(self.language, issues[:5]) | |
| except Exception as e: | |
| return AnalysisResult(self.language, error=str(e)) | |
| finally: | |
| if os.path.exists(temp_path): | |
| os.unlink(temp_path) | |
| def _analyze_fallback(self, code: str) -> AnalysisResult: | |
| issues = [] | |
| lines = code.split("\n") | |
| for i, line in enumerate(lines, 1): | |
| stripped = line.strip() | |
| if stripped.startswith("public ") and " = " in stripped: | |
| if not any(kw in stripped for kw in ["final", "static"]): | |
| issues.append(self.format_issue( | |
| line=i, | |
| symbol="mutable-field", | |
| message="Public fields should be final or use getters/setters", | |
| category="correctness", | |
| severity="medium", | |
| )) | |
| # FIX: check "String" in THIS line only, not the entire file | |
| if "==" in line and "String" in line: | |
| if ".equals(" not in line: | |
| issues.append(self.format_issue( | |
| line=i, | |
| symbol="string-comparison", | |
| message="Use .equals() to compare strings, not ==", | |
| category="correctness", | |
| severity="high", | |
| )) | |
| if line.rstrip() != line: | |
| issues.append(self.format_issue( | |
| line=i, | |
| symbol="trailing-spaces", | |
| message="Trailing whitespace", | |
| category="style", | |
| severity="low", | |
| )) | |
| return AnalysisResult(self.language, issues[:5]) |