Java to Swift Conversion
Overview
Converts Java 1.8 classes to Swift 5.0+ struct objects conforming to Codable. Output as markdown code blocks only — never modify project files.
Core Constraint
CRITICAL: Output Swift struct definitions as markdown code blocks only. Do NOT modify, edit, or alter any project files.
Type Mapping
| Java Type | Swift Type |
|---|---|
int / Integer |
Int |
long / Long |
Int64 |
float / Float |
Float |
double / Double |
Double |
boolean / Boolean |
Bool |
String |
String |
BigDecimal / decimal |
Double (default) or String (with @BigDecimalToString) |
Date |
Date |
Timestamp |
TimeInterval |
List<T> |
[T] |
Map<K,V> |
[K: V] |
Nullability Rules
| Java Annotation | Swift Type |
|---|---|
@NonNull / @NotNull |
Non-optional (String) |
No annotation / @Nullable |
Optional (String?) |
Default: All properties are optional unless explicitly marked non-null.
Naming Conventions
Struct names: PascalCase (OrderDetails, UserProfile, PaymentRequest)
Property names: ⚠️ KEEP IDENTICAL TO JAVA/SERVER-SIDE — preserves Codable serialization.
// ✅ Correct
struct OrderRequest: Codable {
let orderId: String // matches Java field name
let total_amount: String? // preserves snake_case
}
Swift's Codable maps property names directly to JSON keys — changing names breaks serialization.
Inheritance Handling
Swift structs don't support inheritance. Always flatten the hierarchy:
- Trace full inheritance chain — read all parent class files
- If any parent is
BaseReq→ EXCLUDE allBaseReqproperties (handled by network layer) - Combine all remaining properties from parent + child into one struct
- Document property source with inline comments
struct OrderRequest: Codable {
// From ApiRequest
let userId: String
// From OrderRequest
let orderId: Int64
// Note: BaseReq properties (token, timestamp) excluded — handled by network layer
}
Documentation
Migrate Java comments to Swift using /// triple-slash format:
- Preserve original language (Chinese stays Chinese, English stays English)
- Place comments immediately above each property
- For properties without Java comments, add brief descriptive comment
Structure Requirements
All converted structs must:
- Be defined as
struct(notclass) - Conform to
Codableprotocol - Use
letfor all properties (immutable) - Flatten all inherited properties into a single struct
Step-by-Step Workflow
- Analyze Java class — properties, annotations, types, inheritance
- If inheritance detected → request/read parent class files
- Handle
BaseReqexclusion if applicable - Create Swift struct with
Codableconformance - Apply type mapping (including
@BigDecimalToString) - Apply nullability (
@NonNull→ non-optional, else optional) - Use
letfor all properties - Keep property names identical to Java/server-side
- Migrate comments with
///preserving original language
Validation Checklist
-
@NonNull/@NotNullproperties are non-optional - All other properties are optional (
?) - Struct conforms to
Codable - All properties use
let - Inheritance detected and parent classes analyzed
- All inherited properties flattened into single struct
-
BaseReqproperties excluded if applicable - Property names identical to server-side model
- Output is markdown code block only (no file modifications)