-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass_hierarchy_tracking_demo.rs
More file actions
482 lines (411 loc) · 16.1 KB
/
class_hierarchy_tracking_demo.rs
File metadata and controls
482 lines (411 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//! Comprehensive demonstration of Class Hierarchy Tracking System
//!
//! This example showcases the advanced class hierarchy tracking system that detects:
//! - Class moves between files with inheritance preservation
//! - Method migrations between classes (pull up, push down, extract)
//! - Hierarchy changes (parent changes, flattening, extraction)
//! - Interface/trait/protocol implementation changes
//!
//! This is particularly useful for class-based languages like Java, C++, PHP, Swift, and Ruby.
use smart_diff_engine::{
ClassHierarchyAnalysisResult, ClassHierarchyTracker,
ClassHierarchyTrackerConfig, ClassNode, MethodInfo, MethodMigrationType, Visibility,
};
use std::collections::HashMap;
fn main() {
println!("=== Class Hierarchy Tracking Demo ===\n");
// Create tracker with default configuration
let tracker = ClassHierarchyTracker::new(ClassHierarchyTrackerConfig::default());
// Scenario 1: Class Move with Inheritance Preservation
println!("📦 Scenario 1: Class Move with Inheritance Preservation");
println!("--------------------------------------------------------");
demo_class_move(&tracker);
println!();
// Scenario 2: Method Pull Up Refactoring
println!("⬆️ Scenario 2: Method Pull Up Refactoring");
println!("--------------------------------------------------------");
demo_method_pull_up(&tracker);
println!();
// Scenario 3: Method Push Down Refactoring
println!("⬇️ Scenario 3: Method Push Down Refactoring");
println!("--------------------------------------------------------");
demo_method_push_down(&tracker);
println!();
// Scenario 4: Class Flattening (Remove Inheritance, Inline Methods)
println!("🔨 Scenario 4: Class Flattening");
println!("--------------------------------------------------------");
demo_class_flattening(&tracker);
println!();
// Scenario 5: Interface/Trait Changes
println!("🔌 Scenario 5: Interface/Trait Changes");
println!("--------------------------------------------------------");
demo_interface_changes(&tracker);
println!();
// Scenario 6: Complex Hierarchy Refactoring
println!("🌳 Scenario 6: Complex Hierarchy Refactoring");
println!("--------------------------------------------------------");
demo_complex_hierarchy(&tracker);
println!();
println!("=== Demo Complete ===");
}
/// Demo: Class moved from one file to another, preserving inheritance
fn demo_class_move(tracker: &ClassHierarchyTracker) {
// Source: DataProcessor in old/processors/DataProcessor.java
let source_classes = create_simple_hierarchy(
"DataProcessor",
Some("BaseProcessor".to_string()),
"old/processors/DataProcessor.java",
);
// Target: DataProcessor moved to new/core/DataProcessor.java
let target_classes = create_simple_hierarchy(
"DataProcessor",
Some("BaseProcessor".to_string()),
"new/core/DataProcessor.java",
);
let source_hierarchy = tracker.build_hierarchy(&source_classes).unwrap();
let target_hierarchy = tracker.build_hierarchy(&target_classes).unwrap();
let result = tracker
.analyze_hierarchy_changes(&source_hierarchy, &target_hierarchy)
.unwrap();
print_class_moves(&result);
}
/// Demo: Method pulled up from child to parent class
fn demo_method_pull_up(tracker: &ClassHierarchyTracker) {
// Source: validate() method in Child class
let mut source_classes = HashMap::new();
let parent_methods = vec![create_method("process", "void process()")];
let child_methods = vec![
create_method("process", "void process()"),
create_method("validate", "boolean validate()"), // Will be pulled up
];
source_classes.insert(
"Parent".to_string(),
create_class("Parent", None, parent_methods, "Parent.java"),
);
source_classes.insert(
"Child".to_string(),
create_class(
"Child",
Some("Parent".to_string()),
child_methods,
"Child.java",
),
);
// Target: validate() method moved to Parent class
let mut target_classes = HashMap::new();
let parent_methods = vec![
create_method("process", "void process()"),
create_method("validate", "boolean validate()"), // Pulled up
];
let child_methods = vec![create_method("process", "void process()")];
target_classes.insert(
"Parent".to_string(),
create_class("Parent", None, parent_methods, "Parent.java"),
);
target_classes.insert(
"Child".to_string(),
create_class(
"Child",
Some("Parent".to_string()),
child_methods,
"Child.java",
),
);
let source_hierarchy = tracker.build_hierarchy(&source_classes).unwrap();
let target_hierarchy = tracker.build_hierarchy(&target_classes).unwrap();
let result = tracker
.analyze_hierarchy_changes(&source_hierarchy, &target_hierarchy)
.unwrap();
print_method_migrations(&result);
}
/// Demo: Method pushed down from parent to child class
fn demo_method_push_down(tracker: &ClassHierarchyTracker) {
// Source: specialized() method in Parent class
let mut source_classes = HashMap::new();
let parent_methods = vec![
create_method("process", "void process()"),
create_method("specialized", "void specialized()"), // Will be pushed down
];
let child_methods = vec![create_method("process", "void process()")];
source_classes.insert(
"Parent".to_string(),
create_class("Parent", None, parent_methods, "Parent.java"),
);
source_classes.insert(
"Child".to_string(),
create_class(
"Child",
Some("Parent".to_string()),
child_methods,
"Child.java",
),
);
// Target: specialized() method moved to Child class
let mut target_classes = HashMap::new();
let parent_methods = vec![create_method("process", "void process()")];
let child_methods = vec![
create_method("process", "void process()"),
create_method("specialized", "void specialized()"), // Pushed down
];
target_classes.insert(
"Parent".to_string(),
create_class("Parent", None, parent_methods, "Parent.java"),
);
target_classes.insert(
"Child".to_string(),
create_class(
"Child",
Some("Parent".to_string()),
child_methods,
"Child.java",
),
);
let source_hierarchy = tracker.build_hierarchy(&source_classes).unwrap();
let target_hierarchy = tracker.build_hierarchy(&target_classes).unwrap();
let result = tracker
.analyze_hierarchy_changes(&source_hierarchy, &target_hierarchy)
.unwrap();
print_method_migrations(&result);
}
/// Demo: Class flattening - inheritance removed, parent methods inlined
fn demo_class_flattening(tracker: &ClassHierarchyTracker) {
// Source: Child extends Parent
let mut source_classes = HashMap::new();
let parent_methods = vec![create_method("parentMethod", "void parentMethod()")];
let child_methods = vec![create_method("childMethod", "void childMethod()")];
source_classes.insert(
"Parent".to_string(),
create_class("Parent", None, parent_methods, "Parent.java"),
);
source_classes.insert(
"Child".to_string(),
create_class(
"Child",
Some("Parent".to_string()),
child_methods,
"Child.java",
),
);
// Target: Child no longer extends Parent, parent methods inlined
let mut target_classes = HashMap::new();
let parent_methods = vec![create_method("parentMethod", "void parentMethod()")];
let child_methods = vec![
create_method("childMethod", "void childMethod()"),
create_method("parentMethod", "void parentMethod()"), // Inlined
];
target_classes.insert(
"Parent".to_string(),
create_class("Parent", None, parent_methods, "Parent.java"),
);
target_classes.insert(
"Child".to_string(),
create_class("Child", None, child_methods, "Child.java"), // No parent
);
let source_hierarchy = tracker.build_hierarchy(&source_classes).unwrap();
let target_hierarchy = tracker.build_hierarchy(&target_classes).unwrap();
let result = tracker
.analyze_hierarchy_changes(&source_hierarchy, &target_hierarchy)
.unwrap();
print_hierarchy_changes(&result);
}
/// Demo: Interface and trait implementation changes
fn demo_interface_changes(tracker: &ClassHierarchyTracker) {
// Source: Class implements Serializable
let mut source_classes = HashMap::new();
let mut source_class = create_class(
"DataModel",
None,
vec![create_method("getData", "Object getData()")],
"DataModel.java",
);
source_class.interfaces = vec!["Serializable".to_string()];
source_classes.insert("DataModel".to_string(), source_class);
// Target: Class implements Serializable, Comparable, and uses Loggable trait
let mut target_classes = HashMap::new();
let mut target_class = create_class(
"DataModel",
None,
vec![create_method("getData", "Object getData()")],
"DataModel.java",
);
target_class.interfaces = vec!["Serializable".to_string(), "Comparable".to_string()];
target_class.traits = vec!["Loggable".to_string()];
target_classes.insert("DataModel".to_string(), target_class);
let source_hierarchy = tracker.build_hierarchy(&source_classes).unwrap();
let target_hierarchy = tracker.build_hierarchy(&target_classes).unwrap();
let result = tracker
.analyze_hierarchy_changes(&source_hierarchy, &target_hierarchy)
.unwrap();
print_interface_changes(&result);
}
/// Demo: Complex hierarchy with multiple refactorings
fn demo_complex_hierarchy(tracker: &ClassHierarchyTracker) {
println!("Creating a complex 3-level hierarchy with multiple changes...");
println!("Source: GrandParent -> Parent -> Child");
println!("Target: GrandParent -> Parent -> Child (with method migrations and moves)");
println!();
// This would be a more complex scenario - simplified for demo
let source_classes = create_simple_hierarchy("Child", Some("Parent".to_string()), "old/Child.java");
let target_classes = create_simple_hierarchy("Child", Some("Parent".to_string()), "new/Child.java");
let source_hierarchy = tracker.build_hierarchy(&source_classes).unwrap();
let target_hierarchy = tracker.build_hierarchy(&target_classes).unwrap();
let result = tracker
.analyze_hierarchy_changes(&source_hierarchy, &target_hierarchy)
.unwrap();
print_statistics(&result);
}
// Helper functions
fn create_method(name: &str, signature: &str) -> MethodInfo {
MethodInfo {
name: name.to_string(),
signature: signature.to_string(),
visibility: Visibility::Public,
is_static: false,
is_abstract: false,
is_override: false,
line: 1,
}
}
fn create_class(
name: &str,
parent: Option<String>,
methods: Vec<MethodInfo>,
file_path: &str,
) -> ClassNode {
ClassNode {
qualified_name: name.to_string(),
name: name.to_string(),
parent,
interfaces: Vec::new(),
traits: Vec::new(),
methods,
fields: Vec::new(),
file_path: file_path.to_string(),
line: 1,
is_abstract: false,
is_interface: false,
}
}
// Print functions
fn print_class_moves(result: &ClassHierarchyAnalysisResult) {
if result.class_moves.is_empty() {
println!(" No class moves detected");
return;
}
for class_move in &result.class_moves {
println!(" ✓ Class '{}' moved:", class_move.class_name);
println!(" From: {}", class_move.source_file);
println!(" To: {}", class_move.target_file);
println!(
" Inheritance preserved: {}",
if class_move.inheritance_preserved {
"✓"
} else {
"✗"
}
);
println!(
" Interfaces preserved: {}",
if class_move.interfaces_preserved {
"✓"
} else {
"✗"
}
);
println!(" Methods moved: {}", class_move.moved_methods.len());
println!(" Confidence: {:.1}%", class_move.confidence * 100.0);
}
}
fn print_method_migrations(result: &ClassHierarchyAnalysisResult) {
if result.method_migrations.is_empty() {
println!(" No method migrations detected");
return;
}
for migration in &result.method_migrations {
let migration_type_str = match migration.migration_type {
MethodMigrationType::PullUp => "⬆️ Pull Up",
MethodMigrationType::PushDown => "⬇️ Push Down",
MethodMigrationType::MovedToSibling => "↔️ Move to Sibling",
MethodMigrationType::ExtractedToNewClass => "📦 Extract to New Class",
MethodMigrationType::MovedToUnrelated => "🔀 Move to Unrelated",
};
println!(" {} Method '{}':", migration_type_str, migration.method_name);
println!(" From: {} ({})", migration.source_class, migration.source_file);
println!(" To: {} ({})", migration.target_class, migration.target_file);
println!(" Signature: {}", migration.signature);
println!(" Confidence: {:.1}%", migration.confidence * 100.0);
}
}
fn print_hierarchy_changes(result: &ClassHierarchyAnalysisResult) {
if result.hierarchy_changes.is_empty() {
println!(" No hierarchy changes detected");
return;
}
for change in &result.hierarchy_changes {
let change_type_str = format!("{:?}", change.change_type);
println!(" {} Class '{}':", change_type_str, change.class_name);
if let Some(old_parent) = &change.old_parent {
println!(" Old parent: {}", old_parent);
}
if let Some(new_parent) = &change.new_parent {
println!(" New parent: {}", new_parent);
}
println!(" File: {}", change.file_path);
println!(" Confidence: {:.1}%", change.confidence * 100.0);
}
}
fn print_interface_changes(result: &ClassHierarchyAnalysisResult) {
if result.interface_changes.is_empty() {
println!(" No interface/trait changes detected");
return;
}
for change in &result.interface_changes {
let change_type_str = format!("{:?}", change.change_type);
println!(
" {} '{}' on class '{}':",
change_type_str, change.interface_name, change.class_name
);
println!(" File: {}", change.file_path);
println!(" Confidence: {:.1}%", change.confidence * 100.0);
}
}
fn print_statistics(result: &ClassHierarchyAnalysisResult) {
let stats = &result.statistics;
println!(" 📊 Hierarchy Statistics:");
println!(" Total classes: {}", stats.total_classes);
println!(
" Classes with inheritance: {}",
stats.classes_with_inheritance
);
println!(
" Classes with interfaces: {}",
stats.classes_with_interfaces
);
println!(" Classes with traits: {}", stats.classes_with_traits);
println!(" Max hierarchy depth: {}", stats.max_hierarchy_depth);
println!(
" Avg methods per class: {:.1}",
stats.avg_methods_per_class
);
println!();
println!(" 🔍 Detected Changes:");
println!(" Class moves: {}", stats.total_class_moves);
println!(" Method migrations: {}", stats.total_method_migrations);
println!(" Hierarchy changes: {}", stats.total_hierarchy_changes);
}
fn create_simple_hierarchy(
class_name: &str,
parent: Option<String>,
file_path: &str,
) -> HashMap<String, ClassNode> {
let mut classes = HashMap::new();
let methods = vec![
create_method("process", "void process()"),
create_method("validate", "boolean validate()"),
];
classes.insert(
class_name.to_string(),
create_class(class_name, parent, methods, file_path),
);
classes
}