refactor: Standardize 32 non-controller DTOs with lombok annotations
This commit is contained in:
44
refactor_dtos.ps1
Normal file
44
refactor_dtos.ps1
Normal file
@@ -0,0 +1,44 @@
|
||||
$files = Get-ChildItem -Recurse -Filter *.java | Where-Object { $_.FullName -match "\\dto\\" -and $_.Name -notmatch "(Request|Response)\.java" -and $_.Name -ne "SampleGlowMessage.java" }
|
||||
|
||||
foreach ($file in $files) {
|
||||
$content = Get-Content $file.FullName -Raw
|
||||
|
||||
# 1. Remove @Data if it exists
|
||||
$content = $content -replace '(?m)^\s*@Data\s*\r?\n', ''
|
||||
|
||||
# 2. Check if it's an interface or enum, skip if so
|
||||
if ($content -match 'public interface ' -or $content -match 'public enum ') {
|
||||
continue
|
||||
}
|
||||
|
||||
# 3. Add imports if they don't exist
|
||||
$imports = @("lombok.Getter", "lombok.Setter", "lombok.Builder", "lombok.NoArgsConstructor", "lombok.AllArgsConstructor")
|
||||
$importBlock = ""
|
||||
foreach ($imp in $imports) {
|
||||
if (-not ($content -match "import $imp;")) {
|
||||
$importBlock += "import $imp;`r`n"
|
||||
}
|
||||
}
|
||||
|
||||
if ($importBlock -ne "") {
|
||||
# Insert imports after package declaration
|
||||
$content = $content -replace '(?m)^(package\s+[^;]+;)\r?\n', "`$1`r`n`r`n$importBlock"
|
||||
}
|
||||
|
||||
# 4. Add annotations if they don't exist
|
||||
$annotations = @()
|
||||
if (-not ($content -match '@Getter')) { $annotations += "@Getter" }
|
||||
if (-not ($content -match '@Setter')) { $annotations += "@Setter" }
|
||||
if (-not ($content -match '@Builder')) { $annotations += "@Builder" }
|
||||
if (-not ($content -match '@NoArgsConstructor')) { $annotations += "@NoArgsConstructor" }
|
||||
if (-not ($content -match '@AllArgsConstructor')) { $annotations += "@AllArgsConstructor" }
|
||||
|
||||
if ($annotations.Count -gt 0) {
|
||||
$annoStr = ($annotations -join "`r`n") + "`r`n"
|
||||
# Insert annotations before public class
|
||||
$content = $content -replace '(?m)^(public\s+class\s+)', "$annoStr`$1"
|
||||
}
|
||||
|
||||
Set-Content -Path $file.FullName -Value $content -Encoding UTF8
|
||||
Write-Host "Processed: $($file.Name)"
|
||||
}
|
||||
66
refactor_dtos.py
Normal file
66
refactor_dtos.py
Normal file
@@ -0,0 +1,66 @@
|
||||
import os
|
||||
import glob
|
||||
import re
|
||||
|
||||
def process_file(filepath):
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
except UnicodeDecodeError:
|
||||
with open(filepath, 'r', encoding='cp949') as f:
|
||||
content = f.read()
|
||||
|
||||
# Remove @Data
|
||||
content = re.sub(r'^\s*@Data\s*\n', '', content, flags=re.MULTILINE)
|
||||
|
||||
# Skip interfaces and enums
|
||||
if 'public interface ' in content or 'public enum ' in content:
|
||||
return
|
||||
|
||||
if 'public abstract class ' in content:
|
||||
return
|
||||
|
||||
# Add imports
|
||||
imports = ["lombok.Getter", "lombok.Setter", "lombok.Builder", "lombok.NoArgsConstructor", "lombok.AllArgsConstructor"]
|
||||
import_block = ""
|
||||
for imp in imports:
|
||||
if f"import {imp};" not in content:
|
||||
import_block += f"import {imp};\n"
|
||||
|
||||
if import_block:
|
||||
content = re.sub(r'^(package\s+[^;]+;)\s*\n', f"\\1\n\n{import_block}", content, count=1, flags=re.MULTILINE)
|
||||
|
||||
# Add annotations
|
||||
annotations = []
|
||||
if '@Getter' not in content: annotations.append('@Getter')
|
||||
if '@Setter' not in content: annotations.append('@Setter')
|
||||
if '@Builder' not in content: annotations.append('@Builder')
|
||||
if '@NoArgsConstructor' not in content: annotations.append('@NoArgsConstructor')
|
||||
if '@AllArgsConstructor' not in content: annotations.append('@AllArgsConstructor')
|
||||
|
||||
if annotations:
|
||||
anno_str = '\n'.join(annotations) + '\n'
|
||||
content = re.sub(r'^(public\s+class\s+)', f"{anno_str}\\1", content, count=1, flags=re.MULTILINE)
|
||||
|
||||
# Remove manual ToolMetadata() and ZtUsacInDto() constructors
|
||||
content = re.sub(r'^\s*public\s+ToolMetadata\(\)\s*\{\}\s*\n', '', content, flags=re.MULTILINE)
|
||||
content = re.sub(r'^\s*public\s+ZtUsacInDto\(\)\s*\{\s*setPuseYn\("Y"\);\s*\}\s*\n', '', content, flags=re.MULTILINE)
|
||||
|
||||
# Fix puseYn builder default
|
||||
if 'private String puseYn;' in content and 'ZtUsacInDto' in filepath:
|
||||
content = content.replace('private String puseYn;', '@Builder.Default\n private String puseYn = "Y";')
|
||||
|
||||
# Remove @Builder from AuditInfo (to prevent inheritance clash)
|
||||
if 'AuditInfo.java' in filepath:
|
||||
content = content.replace('@Builder\n', '')
|
||||
|
||||
with open(filepath, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
print(f"Processed: {filepath}")
|
||||
|
||||
# Find all DTOs
|
||||
for root, dirs, files in os.walk('.'):
|
||||
if '\\dto\\' in root or '/dto/' in root:
|
||||
for file in files:
|
||||
if file.endswith('.java') and not file.endswith('Request.java') and not file.endswith('Response.java') and file != 'SampleGlowMessage.java':
|
||||
process_file(os.path.join(root, file))
|
||||
Reference in New Issue
Block a user