-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaMethodSorter.ts
More file actions
319 lines (268 loc) · 10.2 KB
/
Copy pathjavaMethodSorter.ts
File metadata and controls
319 lines (268 loc) · 10.2 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
import { JavaMethod, SortingOptions } from './types';
import { JavaParser } from './javaParser';
/**
* Java Method Sorter - sorts methods in Java classes to increase code readability
*
* Based on the eclipse-method-sorter plugin approach, this sorter provides techniques
* to sort methods following clean code principles.
*/
export class JavaMethodSorter {
private options: SortingOptions;
constructor(options: SortingOptions) {
this.options = options;
}
/**
* Sort methods in the given Java source code
*/
sort(source: string): string {
const parser = new JavaParser(source);
const javaClass = parser.parse();
if (!javaClass || javaClass.methods.length === 0) {
return source;
}
const sortedMethods = this.sortMethods(javaClass.methods);
return this.reconstructSource(javaClass.preMethodsContent, sortedMethods, javaClass.postMethodsContent);
}
/**
* Shuffle methods randomly in the given Java source code
*/
shuffleRandomly(source: string): string {
const parser = new JavaParser(source);
const javaClass = parser.parse();
if (!javaClass || javaClass.methods.length === 0) {
return source;
}
const shuffledMethods = this.shuffleArray([...javaClass.methods]);
return this.reconstructSource(javaClass.preMethodsContent, shuffledMethods, javaClass.postMethodsContent);
}
/**
* Sort methods according to configured options
*/
private sortMethods(methods: JavaMethod[]): JavaMethod[] {
let sorted = [...methods];
// Build call graph for invocation-based sorting
const callGraph = this.buildCallGraph(sorted);
// Apply sorting based on options
sorted.sort((a, b) => this.compareMethodsFull(a, b, callGraph));
// Apply clustering if enabled
if (this.options.clusterOverloadedMethods) {
sorted = this.clusterOverloadedMethods(sorted);
}
if (this.options.clusterGetterSetter) {
sorted = this.clusterGetterSetterMethods(sorted);
}
return sorted;
}
/**
* Full comparison of two methods using all configured criteria
*/
private compareMethodsFull(a: JavaMethod, b: JavaMethod, callGraph: Map<string, Set<string>>): number {
// 1. Constructors first (if enabled)
if (this.options.separateConstructors) {
if (a.isConstructor && !b.isConstructor) {return -1;}
if (!a.isConstructor && b.isConstructor) {return 1;}
}
// 2. Static methods (static initializers and static methods together)
if (a.isStatic && !b.isStatic) {return -1;}
if (!a.isStatic && b.isStatic) {return 1;}
// 3. Access level ordering (if enabled)
if (this.options.separateByAccessLevel) {
const accessDiff = a.accessLevel - b.accessLevel;
if (accessDiff !== 0) {return accessDiff;}
}
// 4. Invocation ordering
if (this.options.respectBeforeAfterRelation) {
const invocationOrder = this.compareByInvocation(a, b, callGraph);
if (invocationOrder !== 0) {return invocationOrder;}
}
// 5. Lexical ordering (if enabled)
if (this.options.applyLexicalOrdering) {
const lexicalDiff = a.name.localeCompare(b.name);
if (lexicalDiff !== 0) {return lexicalDiff;}
}
// 6. Fall back to original position
return a.originalPosition - b.originalPosition;
}
/**
* Compare methods based on invocation relationship
* Methods that call other methods should come before the methods they call
*/
private compareByInvocation(a: JavaMethod, b: JavaMethod, callGraph: Map<string, Set<string>>): number {
const aCalls = callGraph.get(a.name);
const bCalls = callGraph.get(b.name);
// If a calls b, a should come first
if (aCalls?.has(b.name)) {
return -1;
}
// If b calls a, b should come first
if (bCalls?.has(a.name)) {
return 1;
}
// Check transitive calls (depth-first approach)
if (this.options.sortingStrategy === 'depth-first') {
if (this.transitivelyCallsMethod(a.name, b.name, callGraph, new Set())) {
return -1;
}
if (this.transitivelyCallsMethod(b.name, a.name, callGraph, new Set())) {
return 1;
}
}
return 0;
}
/**
* Check if method A transitively calls method B
*/
private transitivelyCallsMethod(
methodA: string,
methodB: string,
callGraph: Map<string, Set<string>>,
visited: Set<string>
): boolean {
if (visited.has(methodA)) {
return false;
}
visited.add(methodA);
const calls = callGraph.get(methodA);
if (!calls) {
return false;
}
if (calls.has(methodB)) {
return true;
}
for (const calledMethod of calls) {
if (this.transitivelyCallsMethod(calledMethod, methodB, callGraph, visited)) {
return true;
}
}
return false;
}
/**
* Build a call graph from the methods
*/
private buildCallGraph(methods: JavaMethod[]): Map<string, Set<string>> {
const callGraph = new Map<string, Set<string>>();
const methodNames = new Set(methods.map(m => m.name));
for (const method of methods) {
const calls = new Set<string>();
for (const called of method.calledMethods) {
// Only include calls to methods in this class
if (methodNames.has(called) && called !== method.name) {
calls.add(called);
}
}
callGraph.set(method.name, calls);
}
return callGraph;
}
/**
* Cluster overloaded methods together
*/
private clusterOverloadedMethods(methods: JavaMethod[]): JavaMethod[] {
const clusters = new Map<string, JavaMethod[]>();
const orderedNames: string[] = [];
for (const method of methods) {
const baseName = method.name;
if (!clusters.has(baseName)) {
clusters.set(baseName, []);
orderedNames.push(baseName);
}
clusters.get(baseName)!.push(method);
}
const result: JavaMethod[] = [];
for (const name of orderedNames) {
result.push(...clusters.get(name)!);
}
return result;
}
/**
* Cluster getter and setter methods together
*/
private clusterGetterSetterMethods(methods: JavaMethod[]): JavaMethod[] {
const result: JavaMethod[] = [];
const processed = new Set<number>();
for (let i = 0; i < methods.length; i++) {
if (processed.has(i)) {continue;}
const method = methods[i];
result.push(method);
processed.add(i);
// If it's a getter, look for corresponding setter
if (method.isGetter) {
const fieldName = this.extractFieldNameFromGetter(method.name);
const setterName = 'set' + fieldName;
for (let j = i + 1; j < methods.length; j++) {
if (!processed.has(j) && methods[j].name === setterName) {
result.push(methods[j]);
processed.add(j);
break;
}
}
}
// If it's a setter, look for corresponding getter
if (method.isSetter && !method.isGetter) {
const fieldName = this.extractFieldNameFromSetter(method.name);
const getterName = 'get' + fieldName;
const boolGetterName = 'is' + fieldName;
for (let j = i + 1; j < methods.length; j++) {
if (!processed.has(j) &&
(methods[j].name === getterName || methods[j].name === boolGetterName)) {
result.push(methods[j]);
processed.add(j);
break;
}
}
}
}
return result;
}
/**
* Extract field name from getter method name
*/
private extractFieldNameFromGetter(methodName: string): string {
if (methodName.startsWith('get')) {
return methodName.substring(3);
}
if (methodName.startsWith('is')) {
return methodName.substring(2);
}
return methodName;
}
/**
* Extract field name from setter method name
*/
private extractFieldNameFromSetter(methodName: string): string {
if (methodName.startsWith('set')) {
return methodName.substring(3);
}
return methodName;
}
/**
* Reconstruct the source from sorted methods
*/
private reconstructSource(preContent: string, methods: JavaMethod[], postContent: string): string {
const methodTexts = methods.map(m => {
// Normalize fullText: remove leading newlines but preserve indentation
const normalizedFullText = m.fullText.replace(/^\n+/, '');
// Preserve leading content with indentation, only trim leading blank lines
// This keeps the comment/annotation indentation intact
const leading = m.leadingContent.replace(/^\n+/, '');
if (leading.trim()) {
return leading + normalizedFullText;
}
return normalizedFullText;
});
// Normalize preContent: remove trailing whitespace and excess newlines
// Add consistent single blank line before first method
const normalizedPreContent = preContent.replace(/\s+$/, '') + '\n\n';
return normalizedPreContent + methodTexts.join('\n\n') + postContent;
}
/**
* Fisher-Yates shuffle algorithm
*/
private shuffleArray<T>(array: T[]): T[] {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
}