61
61
62
62
/**
63
63
* Our sample handler extends AbstractHandler, an IHandler base class.
64
- *
64
+ *
65
65
* @see org.eclipse.core.commands.IHandler
66
66
* @see org.eclipse.core.commands.AbstractHandler
67
67
*/
68
68
public class EvaluateConvertToParallelStreamRefactoringHandler extends AbstractHandler {
69
69
70
- private static final int LOGGING_LEVEL = IStatus .INFO ;
71
70
private static final boolean BUILD_WORKSPACE = false ;
72
- private static final String PERFORM_CHANGE_PROPERTY_KEY = "edu.cuny.hunter.streamrefactoring.eval.performChange" ;
71
+ private static final int LOGGING_LEVEL = IStatus . INFO ;
73
72
private static final boolean PERFORM_CHANGE_DEFAULT = false ;
73
+ private static final String PERFORM_CHANGE_PROPERTY_KEY = "edu.cuny.hunter.streamrefactoring.eval.performChange" ;
74
+
75
+ private static String [] buildAttributeColumns (String attribute ) {
76
+ return new String [] { "subject" , "stream" , "start pos" , "length" , "method" , "type FQN" , attribute };
77
+ }
78
+
79
+ private static CSVPrinter createCSVPrinter (String fileName , String [] header ) throws IOException {
80
+ return new CSVPrinter (new FileWriter (fileName , true ), CSVFormat .EXCEL .withHeader (header ));
81
+ }
82
+
83
+ private static IType [] getAllDeclaringTypeSubtypes (IMethod method ) throws JavaModelException {
84
+ IType declaringType = method .getDeclaringType ();
85
+ ITypeHierarchy typeHierarchy = declaringType .newTypeHierarchy (new NullProgressMonitor ());
86
+ IType [] allSubtypes = typeHierarchy .getAllSubtypes (declaringType );
87
+ return allSubtypes ;
88
+ }
89
+
90
+ private static Set <IMethod > getAllMethods (IJavaProject javaProject ) throws JavaModelException {
91
+ Set <IMethod > methods = new HashSet <>();
92
+
93
+ // collect all methods from this project.
94
+ IPackageFragment [] packageFragments = javaProject .getPackageFragments ();
95
+ for (IPackageFragment iPackageFragment : packageFragments ) {
96
+ ICompilationUnit [] compilationUnits = iPackageFragment .getCompilationUnits ();
97
+ for (ICompilationUnit iCompilationUnit : compilationUnits ) {
98
+ IType [] allTypes = iCompilationUnit .getAllTypes ();
99
+ for (IType type : allTypes )
100
+ Collections .addAll (methods , type .getMethods ());
101
+ }
102
+ }
103
+ return methods ;
104
+ }
105
+
106
+ private static int getMethodLinesOfCode (IMethod method ) {
107
+ AbstractMetricSource metricSource = Dispatcher .getAbstractMetricSource (method );
108
+
109
+ if (metricSource != null ) {
110
+ Metric value = metricSource .getValue ("MLOC" );
111
+ int mLOC = value .intValue ();
112
+ return mLOC ;
113
+ } else {
114
+ System .err .println ("WARNING: Could not retrieve metric source for method: " + method );
115
+ return 0 ;
116
+ }
117
+ }
118
+
119
+ private static int getProjectLinesOfCode (IJavaProject javaProject ) {
120
+ AbstractMetricSource metricSource = Dispatcher .getAbstractMetricSource (javaProject );
121
+
122
+ if (metricSource != null ) {
123
+ Metric value = metricSource .getValue ("TLOC" );
124
+ int tLOC = value .intValue ();
125
+ return tLOC ;
126
+ } else {
127
+ System .err .println ("WARNING: Could not retrieve metric source for project: " + javaProject );
128
+ return 0 ;
129
+ }
130
+ }
131
+
132
+ private static void printStreamAttributesWithMultipleValues (Set <?> set , CSVPrinter printer , Stream stream ,
133
+ String method , IJavaProject project ) throws IOException {
134
+ if (set != null )
135
+ for (Object object : set )
136
+ printer .printRecord (project .getElementName (), stream .getCreation (),
137
+ stream .getCreation ().getStartPosition (), stream .getCreation ().getLength (), method ,
138
+ stream .getEnclosingType ().getFullyQualifiedName (), object .toString ());
139
+ }
74
140
75
141
/**
76
142
* the command has been executed, so extract extract the needed information from
@@ -206,28 +272,26 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
206
272
Set <Stream > optimizableStreams = processor .getOptimizableStreams ();
207
273
resultsPrinter .print (optimizableStreams .size ()); // number.
208
274
209
- for (Stream stream : optimizableStreams ) {
275
+ for (Stream stream : optimizableStreams )
210
276
optimizedStreamPrinter .printRecord (javaProject .getElementName (), stream .getCreation (),
211
277
stream .getCreation ().getStartPosition (), stream .getCreation ().getLength (),
212
278
Util .getMethodIdentifier (stream .getEnclosingEclipseMethod ()),
213
279
stream .getEnclosingType ().getFullyQualifiedName ());
214
- }
215
280
216
281
// failed streams.
217
- for (Stream stream : processor .getUnoptimizableStreams ()) {
282
+ for (Stream stream : processor .getUnoptimizableStreams ())
218
283
nonOptimizedStreamPrinter .printRecord (javaProject .getElementName (), stream .getCreation (),
219
284
stream .getCreation ().getStartPosition (), stream .getCreation ().getLength (),
220
285
Util .getMethodIdentifier (stream .getEnclosingEclipseMethod ()),
221
286
stream .getEnclosingType () == null ? null
222
287
: stream .getEnclosingType ().getFullyQualifiedName ());
223
- }
224
288
225
289
// failed preconditions.
226
290
List <RefactoringStatusEntry > errorEntries = Arrays .stream (status .getEntries ())
227
291
.filter (RefactoringStatusEntry ::isError ).collect (Collectors .toList ());
228
292
resultsPrinter .print (errorEntries .size ()); // number.
229
293
230
- for (RefactoringStatusEntry entry : errorEntries ) {
294
+ for (RefactoringStatusEntry entry : errorEntries )
231
295
if (!entry .isFatalError ()) {
232
296
Object correspondingElement = entry .getData ();
233
297
@@ -245,7 +309,6 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
245
309
: failedStream .getEnclosingType ().getFullyQualifiedName (),
246
310
entry .getCode (), entry .getMessage ());
247
311
}
248
- }
249
312
250
313
// Refactoring type counts.
251
314
for (Refactoring refactoring : Refactoring .values ())
@@ -266,15 +329,14 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
266
329
267
330
// actually perform the refactoring if there are no fatal
268
331
// errors.
269
- if (shouldPerformChange ()) {
332
+ if (this . shouldPerformChange ())
270
333
if (!status .hasFatalError ()) {
271
334
resultsTimeCollector .start ();
272
335
Change change = new ProcessorBasedRefactoring (processor )
273
336
.createChange (new SubProgressMonitor (monitor , IProgressMonitor .UNKNOWN ));
274
337
change .perform (new SubProgressMonitor (monitor , IProgressMonitor .UNKNOWN ));
275
338
resultsTimeCollector .stop ();
276
339
}
277
- }
278
340
279
341
// ensure that we can build the project.
280
342
if (!javaProject .isConsistent ())
@@ -339,7 +401,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
339
401
340
402
private Set <SearchMatch > findReferences (Set <? extends IJavaElement > elements ) throws CoreException {
341
403
Set <SearchMatch > ret = new HashSet <>();
342
- for (IJavaElement elem : elements ) {
404
+ for (IJavaElement elem : elements )
343
405
new SearchEngine ().search (
344
406
SearchPattern .createPattern (elem , IJavaSearchConstants .REFERENCES , SearchPattern .R_EXACT_MATCH ),
345
407
new SearchParticipant [] { SearchEngine .getDefaultSearchParticipant () },
@@ -350,43 +412,9 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
350
412
ret .add (match );
351
413
}
352
414
}, new NullProgressMonitor ());
353
- }
354
415
return ret ;
355
416
}
356
417
357
- private static IType [] getAllDeclaringTypeSubtypes (IMethod method ) throws JavaModelException {
358
- IType declaringType = method .getDeclaringType ();
359
- ITypeHierarchy typeHierarchy = declaringType .newTypeHierarchy (new NullProgressMonitor ());
360
- IType [] allSubtypes = typeHierarchy .getAllSubtypes (declaringType );
361
- return allSubtypes ;
362
- }
363
-
364
- private static int getMethodLinesOfCode (IMethod method ) {
365
- AbstractMetricSource metricSource = Dispatcher .getAbstractMetricSource (method );
366
-
367
- if (metricSource != null ) {
368
- Metric value = metricSource .getValue ("MLOC" );
369
- int mLOC = value .intValue ();
370
- return mLOC ;
371
- } else {
372
- System .err .println ("WARNING: Could not retrieve metric source for method: " + method );
373
- return 0 ;
374
- }
375
- }
376
-
377
- private static int getProjectLinesOfCode (IJavaProject javaProject ) {
378
- AbstractMetricSource metricSource = Dispatcher .getAbstractMetricSource (javaProject );
379
-
380
- if (metricSource != null ) {
381
- Metric value = metricSource .getValue ("TLOC" );
382
- int tLOC = value .intValue ();
383
- return tLOC ;
384
- } else {
385
- System .err .println ("WARNING: Could not retrieve metric source for project: " + javaProject );
386
- return 0 ;
387
- }
388
- }
389
-
390
418
private boolean shouldPerformChange () {
391
419
String performChangePropertyValue = System .getenv (PERFORM_CHANGE_PROPERTY_KEY );
392
420
@@ -395,39 +423,4 @@ private boolean shouldPerformChange() {
395
423
else
396
424
return Boolean .valueOf (performChangePropertyValue );
397
425
}
398
-
399
- private static Set <IMethod > getAllMethods (IJavaProject javaProject ) throws JavaModelException {
400
- Set <IMethod > methods = new HashSet <>();
401
-
402
- // collect all methods from this project.
403
- IPackageFragment [] packageFragments = javaProject .getPackageFragments ();
404
- for (IPackageFragment iPackageFragment : packageFragments ) {
405
- ICompilationUnit [] compilationUnits = iPackageFragment .getCompilationUnits ();
406
- for (ICompilationUnit iCompilationUnit : compilationUnits ) {
407
- IType [] allTypes = iCompilationUnit .getAllTypes ();
408
- for (IType type : allTypes ) {
409
- Collections .addAll (methods , type .getMethods ());
410
- }
411
- }
412
- }
413
- return methods ;
414
- }
415
-
416
- private static CSVPrinter createCSVPrinter (String fileName , String [] header ) throws IOException {
417
- return new CSVPrinter (new FileWriter (fileName , true ), CSVFormat .EXCEL .withHeader (header ));
418
- }
419
-
420
- private static void printStreamAttributesWithMultipleValues (Set <?> set , CSVPrinter printer , Stream stream ,
421
- String method , IJavaProject project ) throws IOException {
422
- if (set != null )
423
- for (Object object : set ) {
424
- printer .printRecord (project .getElementName (), stream .getCreation (),
425
- stream .getCreation ().getStartPosition (), stream .getCreation ().getLength (), method ,
426
- stream .getEnclosingType ().getFullyQualifiedName (), object .toString ());
427
- }
428
- }
429
-
430
- private static String [] buildAttributeColumns (String attribute ) {
431
- return new String [] { "subject" , "stream" , "start pos" , "length" , "method" , "type FQN" , attribute };
432
- }
433
426
}
0 commit comments