Java代码审计实战:敏感信息泄露场景剖析与系统化防范指南
2026/6/22 11:50:31
提取方法:
内联方法:
提取变量:
if(user.age > 18 && user.age < 60)重构为const isWorkingAge = user.age > 18 && user.age < 60; if(isWorkingAge)搬移方法:
提取类:
引入参数对象:
提炼模块:
解耦依赖:
DDD重构:
IntelliJ IDEA:
VS Code:
| 情况 | 策略 | |---------------------|-------------| | 性能关键+可读性差 | 优化优先 | | 非关键路径+结构混乱 | 重构优先 |原始代码:
void processOrder(Order order) { // 验证逻辑(30行) // 计算逻辑(40行) // 持久化逻辑(25行) // 通知逻辑(20行) }重构后:
void processOrder(Order order) { validateOrder(order); calculateAmount(order); saveOrder(order); sendNotifications(order); }问题:复杂的折扣计算逻辑解决方案:
interface DiscountStrategy { BigDecimal apply(BigDecimal amount); } class RegularDiscount implements DiscountStrategy {...} class VIPDiscount implements DiscountStrategy {...}过程式代码:
struct Point { int x; int y; }; double distance(Point a, Point b) {...}面向对象重构:
class Point { private: int x, y; public: double distanceTo(const Point& other) {...} };