You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
248 lines
5.0 KiB
Markdown
248 lines
5.0 KiB
Markdown
---
|
|
name: understand-code
|
|
description: Help understand decompiled Java code, analyze business logic, and document code functionality. Use when user needs to comprehend code purpose, flow, or behavior.
|
|
version: 1.0.0
|
|
---
|
|
|
|
# Code Understanding Guide
|
|
|
|
This skill helps understand decompiled or existing Java code.
|
|
|
|
## When to Use
|
|
|
|
- User has decompiled code and needs to understand it
|
|
- User wants to document business logic
|
|
- User needs to trace code flow
|
|
- User wants to identify code patterns
|
|
|
|
## Analysis Approach
|
|
|
|
### 1. Start with Entry Points
|
|
|
|
Identify how the application is accessed:
|
|
|
|
```bash
|
|
# Find Servlet/Action classes
|
|
find <dir> -name "*Servlet.java" -o -name "*Action.java" | head -20
|
|
|
|
# Find Controller classes
|
|
find <dir> -name "*Controller.java" | head -20
|
|
|
|
# Find Service endpoints
|
|
find <dir> -name "*Service.java" -o -name "*ServiceImpl.java" | head -20
|
|
```
|
|
|
|
### 2. Trace Request Flow
|
|
|
|
For Struts applications:
|
|
|
|
```
|
|
HTTP Request
|
|
↓
|
|
Struts Filter (web.xml)
|
|
↓
|
|
Action Mapping (struts.xml)
|
|
↓
|
|
Action Class (execute method)
|
|
↓
|
|
Service Layer
|
|
↓
|
|
DAO Layer
|
|
↓
|
|
Database
|
|
```
|
|
|
|
For Spring applications:
|
|
|
|
```
|
|
HTTP Request
|
|
↓
|
|
DispatcherServlet
|
|
↓
|
|
Controller (@RequestMapping)
|
|
↓
|
|
Service Layer (@Service)
|
|
↓
|
|
Repository (@Repository)
|
|
↓
|
|
Database
|
|
```
|
|
|
|
### 3. Understand Domain Model
|
|
|
|
Analyze domain classes:
|
|
|
|
```bash
|
|
# Find domain/entity classes
|
|
find <dir> -name "*Domain.java" -o -name "*Entity.java" -o -name "*Bean.java"
|
|
|
|
# Check for common patterns
|
|
grep -l "get\|set" <dir>/*Domain.java | head -10
|
|
```
|
|
|
|
Domain class patterns:
|
|
- Fields with getters/setters
|
|
- Serializable implementation
|
|
- JPA/Hibernate annotations
|
|
- Relationships (OneToMany, ManyToOne)
|
|
|
|
### 4. Map Business Logic
|
|
|
|
Identify business operations:
|
|
|
|
```bash
|
|
# Find service methods
|
|
grep -r "public.*Service" <dir> | head -20
|
|
|
|
# Find transaction boundaries
|
|
grep -r "@Transactional\|beginTransaction" <dir>
|
|
|
|
# Find business calculations
|
|
grep -r "calculate\|compute\|process" <dir>
|
|
```
|
|
|
|
### 5. Configuration Analysis
|
|
|
|
Read configuration files:
|
|
|
|
```bash
|
|
# Spring context
|
|
cat <dir>/spring-application-context.xml
|
|
|
|
# Struts config
|
|
cat <dir>/struts.xml
|
|
|
|
# Database config
|
|
cat <dir>/database.properties
|
|
|
|
# Web config
|
|
cat <dir>/web.xml
|
|
```
|
|
|
|
## Code Patterns
|
|
|
|
### DAO Pattern
|
|
```java
|
|
public interface UserDao {
|
|
User findById(Long id);
|
|
List<User> findAll();
|
|
void save(User user);
|
|
void delete(User user);
|
|
}
|
|
|
|
public class UserDaoImpl implements UserDao {
|
|
// Implementation using iBatis/Hibernate
|
|
}
|
|
```
|
|
|
|
### Service Pattern
|
|
```java
|
|
public interface UserService {
|
|
User getUser(Long id);
|
|
void createUser(User user);
|
|
}
|
|
|
|
public class UserServiceImpl implements UserService {
|
|
private UserDao userDao;
|
|
|
|
@Transactional
|
|
public void createUser(User user) {
|
|
// Business logic
|
|
userDao.save(user);
|
|
}
|
|
}
|
|
```
|
|
|
|
### Action Pattern (Struts)
|
|
```java
|
|
public class UserAction extends ActionSupport {
|
|
private UserService userService;
|
|
private User user;
|
|
|
|
public String execute() {
|
|
// Handle request
|
|
user = userService.getUser(id);
|
|
return SUCCESS;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Documentation Generation
|
|
|
|
Generate code documentation:
|
|
|
|
### 1. Class Summary
|
|
|
|
For each class, document:
|
|
- Purpose/responsibility
|
|
- Key methods
|
|
- Dependencies
|
|
- Usage examples
|
|
|
|
### 2. Flow Diagrams
|
|
|
|
Create text-based flow diagrams:
|
|
|
|
```
|
|
User Login Flow:
|
|
┌─────────┐ ┌─────────┐ ┌─────────┐
|
|
│ Login │───→│ Action │───→│ Service │
|
|
│ Page │ │ │ │ │
|
|
└─────────┘ └─────────┘ └─────────┘
|
|
│ │
|
|
↓ ↓
|
|
┌─────────┐ ┌─────────┐
|
|
│ Validate │ │ DAO │
|
|
│ Input │ │ │
|
|
└─────────┘ └─────────┘
|
|
```
|
|
|
|
### 3. Package Documentation
|
|
|
|
Document each package:
|
|
|
|
```
|
|
com.hst.taxService.action
|
|
- Purpose: Struts actions for tax service web interface
|
|
- Key classes: TaxAction, ReportAction
|
|
- Dependencies: taxService.service, common.util
|
|
```
|
|
|
|
## Analysis Tools
|
|
|
|
### Quick Analysis
|
|
|
|
```bash
|
|
# Count methods per class
|
|
for f in $(find <dir> -name "*.java"); do
|
|
echo "$(grep -c "public\|private\|protected" "$f") $f"
|
|
done | sort -rn | head -20
|
|
|
|
# Find complex methods (many lines)
|
|
for f in $(find <dir> -name "*.java"); do
|
|
echo "$(wc -l < "$f") $f"
|
|
done | sort -rn | head -20
|
|
```
|
|
|
|
### Pattern Detection
|
|
|
|
```bash
|
|
# Find singletons
|
|
grep -r "getInstance\|private static" <dir>
|
|
|
|
# Find factories
|
|
grep -r "Factory\|createInstance" <dir>
|
|
|
|
# Find observers
|
|
grep -r "Listener\|Observer\|addEventListener" <dir>
|
|
```
|
|
|
|
## Integration
|
|
|
|
After understanding code:
|
|
|
|
1. Create README documentation
|
|
2. Generate API documentation
|
|
3. Identify refactoring opportunities
|
|
4. Plan modernization if needed
|