-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_signature_demo.rs
More file actions
437 lines (363 loc) · 14.7 KB
/
function_signature_demo.rs
File metadata and controls
437 lines (363 loc) · 14.7 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
//! Demonstration of comprehensive function signature extraction capabilities
use smart_diff_parser::{Language, TreeSitterParser};
use smart_diff_semantic::{
FunctionSignatureConfig, FunctionSignatureExtractor, FunctionType, GenericVariance, Visibility,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Smart Code Diff - Function Signature Extraction Demo");
println!("===================================================");
// Demo basic function signature extraction
demo_basic_signature_extraction()?;
// Demo advanced signature analysis
demo_advanced_signature_analysis()?;
// Demo function similarity comparison
demo_function_similarity()?;
// Demo overload detection
demo_overload_detection()?;
// Demo cross-language signature handling
demo_cross_language_signatures()?;
Ok(())
}
fn demo_basic_signature_extraction() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Basic Function Signature Extraction ---");
let parser = TreeSitterParser::new()?;
let mut extractor = FunctionSignatureExtractor::with_defaults(Language::Java);
let java_code = r#"
package com.example.service;
import java.util.List;
import java.util.Optional;
public class UserService {
private final UserRepository repository;
public UserService(UserRepository repository) {
this.repository = repository;
}
public User createUser(String name, String email) {
validateInput(name, email);
User user = new User(name, email);
return repository.save(user);
}
public Optional<User> findUserById(Long id) {
if (id == null || id <= 0) {
return Optional.empty();
}
return repository.findById(id);
}
public List<User> findUsersByEmail(String email) {
return repository.findByEmail(email);
}
private void validateInput(String name, String email) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Name cannot be empty");
}
if (email == null || !email.contains("@")) {
throw new IllegalArgumentException("Invalid email");
}
}
public static UserService getInstance() {
return new UserService(new DefaultUserRepository());
}
@Override
public String toString() {
return "UserService{repository=" + repository + "}";
}
}
"#;
let parse_result = parser.parse(java_code, Language::Java)?;
let extraction_result = extractor.extract_signatures("UserService.java", &parse_result)?;
println!(
"Extracted {} function signatures:",
extraction_result.signatures.len()
);
for signature in &extraction_result.signatures {
println!(
"\n Function: {} ({})",
signature.name,
format!("{:?}", signature.function_type)
);
println!(" Qualified name: {}", signature.qualified_name);
println!(" Visibility: {:?}", signature.visibility);
if !signature.parameters.is_empty() {
println!(" Parameters: {}", signature.parameters.len());
for (i, param) in signature.parameters.iter().enumerate() {
println!(
" {}. {}: {} {}",
i + 1,
param.name,
param.param_type.to_string(),
if param.is_optional { "(optional)" } else { "" }
);
}
}
println!(" Return type: {}", signature.return_type.to_string());
if !signature.modifiers.is_empty() {
println!(" Modifiers: {}", signature.modifiers.join(", "));
}
if !signature.annotations.is_empty() {
println!(" Annotations: {}", signature.annotations.join(", "));
}
if let Some(metrics) = &signature.complexity_metrics {
println!(" Complexity:");
println!(" Cyclomatic: {}", metrics.cyclomatic_complexity);
println!(" Cognitive: {}", metrics.cognitive_complexity);
println!(" Lines of code: {}", metrics.lines_of_code);
println!(" Nesting depth: {}", metrics.nesting_depth);
}
if !signature.dependencies.is_empty() {
println!(" Dependencies: {}", signature.dependencies.join(", "));
}
println!(
" Location: {}:{}-{}",
signature.file_path, signature.line, signature.end_line
);
}
// Show extraction statistics
let stats = &extraction_result.extraction_stats;
println!("\nExtraction Statistics:");
println!(" Total functions: {}", stats.total_functions);
println!(" Public functions: {}", stats.public_functions);
println!(" Private functions: {}", stats.private_functions);
println!(" Static functions: {}", stats.static_functions);
println!(" Constructors: {}", stats.constructors);
println!(" Overloaded functions: {}", stats.overloaded_functions);
println!(" Generic functions: {}", stats.generic_functions);
println!(" Complex functions: {}", stats.complex_functions);
Ok(())
}
fn demo_advanced_signature_analysis() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Advanced Function Signature Analysis ---");
let parser = TreeSitterParser::new()?;
let mut extractor = FunctionSignatureExtractor::with_defaults(Language::Java);
let complex_code = r#"
public abstract class DataProcessor<T extends Comparable<T>, R> {
@SafeVarargs
public static <E> List<E> createList(E... elements) {
List<E> list = new ArrayList<>();
for (E element : elements) {
if (element != null) {
list.add(element);
}
}
return list;
}
public abstract <U extends T> ProcessResult<R> process(
@NotNull U input,
@Nullable ProcessOptions options,
Consumer<String> progressCallback
) throws ProcessingException;
protected final synchronized R processWithRetry(
T input,
int maxRetries,
Duration timeout
) {
for (int attempt = 1; attempt <= maxRetries; attempt++) {
try {
ProcessResult<R> result = process(input, null, null);
if (result.isSuccess()) {
return result.getData();
}
if (attempt < maxRetries) {
Thread.sleep(timeout.toMillis() * attempt);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ProcessingException("Processing interrupted", e);
} catch (Exception e) {
if (attempt == maxRetries) {
throw new ProcessingException("Max retries exceeded", e);
}
}
}
throw new ProcessingException("Processing failed after " + maxRetries + " attempts");
}
public Optional<R> processOptional(T input) {
try {
R result = processWithRetry(input, 3, Duration.ofSeconds(1));
return Optional.ofNullable(result);
} catch (ProcessingException e) {
return Optional.empty();
}
}
// Overloaded methods
public void configure(String config) {
configure(config, true);
}
public void configure(String config, boolean validate) {
configure(config, validate, Duration.ofMinutes(5));
}
public void configure(String config, boolean validate, Duration timeout) {
// Implementation details...
}
}
"#;
let parse_result = parser.parse(complex_code, Language::Java)?;
let extraction_result = extractor.extract_signatures("DataProcessor.java", &parse_result)?;
println!("Advanced signature analysis results:");
for signature in &extraction_result.signatures {
println!(
"\n {} ({})",
signature.name,
format!("{:?}", signature.function_type)
);
// Show generic parameters
if !signature.generic_parameters.is_empty() {
println!(" Generic parameters:");
for generic in &signature.generic_parameters {
println!(
" {}: {:?} with {} bounds",
generic.name,
generic.variance,
generic.bounds.len()
);
for bound in &generic.bounds {
println!(" extends {}", bound.to_string());
}
}
}
// Show parameter details
if !signature.parameters.is_empty() {
println!(" Parameters:");
for param in &signature.parameters {
let flags = vec![
if param.is_optional { "optional" } else { "" },
if param.is_varargs { "varargs" } else { "" },
]
.into_iter()
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join(", ");
println!(
" {}: {} {}",
param.name,
param.param_type.to_string(),
if flags.is_empty() {
""
} else {
&format!("({})", flags)
}
);
if !param.annotations.is_empty() {
println!(" Annotations: {}", param.annotations.join(", "));
}
}
}
// Show complexity metrics
if let Some(metrics) = &signature.complexity_metrics {
println!(" Complexity Analysis:");
println!(
" Cyclomatic complexity: {}",
metrics.cyclomatic_complexity
);
println!(
" Cognitive complexity: {}",
metrics.cognitive_complexity
);
println!(" Lines of code: {}", metrics.lines_of_code);
println!(" Nesting depth: {}", metrics.nesting_depth);
println!(" Branch count: {}", metrics.branch_count);
println!(" Loop count: {}", metrics.loop_count);
println!(" Function calls: {}", metrics.call_count);
// Complexity assessment
let complexity_level = if metrics.cyclomatic_complexity <= 5 {
"Low"
} else if metrics.cyclomatic_complexity <= 10 {
"Medium"
} else {
"High"
};
println!(" Complexity level: {}", complexity_level);
}
// Show signature hashes
println!(" Signature hash: {}", &signature.signature_hash[..8]);
println!(" Normalized hash: {}", &signature.normalized_hash[..8]);
}
// Show overloaded functions
if !extraction_result.overloaded_functions.is_empty() {
println!("\nOverloaded Functions:");
for (name, overloads) in &extraction_result.overloaded_functions {
if overloads.len() > 1 {
println!(" {}: {} overloads", name, overloads.len());
for (i, overload) in overloads.iter().enumerate() {
println!(" {}. {} parameters", i + 1, overload.parameters.len());
}
}
}
}
Ok(())
}
fn demo_function_similarity() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Function Similarity Comparison ---");
// This would demonstrate comparing functions from different files
// For brevity, we'll show the concept
println!("Function similarity analysis compares:");
println!(" - Name similarity (40% weight)");
println!(" - Exact matches: 1.0");
println!(" - Normalized matches (case, underscores): 0.95");
println!(" - Edit distance based: 0.0-0.9");
println!(" - Parameter similarity (30% weight)");
println!(" - Parameter count matching");
println!(" - Parameter type equivalence");
println!(" - Optional/varargs flag matching");
println!(" - Return type similarity (20% weight)");
println!(" - Type equivalence checking");
println!(" - Generic parameter matching");
println!(" - Modifier similarity (10% weight)");
println!(" - Visibility matching");
println!(" - Static/abstract/final matching");
println!("\nSimilarity thresholds:");
println!(" - Exact match: 1.0");
println!(" - High similarity: 0.8-0.99");
println!(" - Potential match: 0.7-0.79");
println!(" - Low similarity: 0.3-0.69");
println!(" - No match: 0.0-0.29");
Ok(())
}
fn demo_overload_detection() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Function Overload Detection ---");
println!("Overload detection identifies:");
println!(" - Functions with the same name but different signatures");
println!(" - Parameter count variations");
println!(" - Parameter type variations");
println!(" - Generic parameter variations");
println!("\nOverload analysis benefits:");
println!(" - API evolution tracking");
println!(" - Refactoring impact assessment");
println!(" - Code complexity measurement");
println!(" - Documentation generation");
Ok(())
}
fn demo_cross_language_signatures() -> Result<(), Box<dyn std::error::Error>> {
println!("\n--- Cross-Language Function Signature Handling ---");
let languages_and_examples = vec![
(
Language::Java,
"public List<String> processData(String input, boolean validate)",
),
(
Language::Python,
"def process_data(input: str, validate: bool = True) -> List[str]",
),
(
Language::JavaScript,
"function processData(input, validate = true)",
),
(
Language::Cpp,
"std::vector<std::string> processData(const std::string& input, bool validate)",
),
(
Language::C,
"char** process_data(const char* input, int validate, int* result_count)",
),
];
println!("Equivalent function signatures across languages:");
for (language, signature) in languages_and_examples {
println!(" {:?}: {}", language, signature);
}
println!("\nCross-language normalization handles:");
println!(" - Naming conventions (camelCase vs snake_case)");
println!(" - Type system differences (List<String> vs list[str])");
println!(" - Parameter syntax variations");
println!(" - Default parameter handling");
println!(" - Generic/template parameter mapping");
Ok(())
}