Skip to content

Commit cdae3ee

Browse files
committed
Clean up EvaluateConvertToParallelStreamRefactoringHandler.java.
1 parent da5e54d commit cdae3ee

File tree

1 file changed

+74
-81
lines changed

1 file changed

+74
-81
lines changed

edu.cuny.hunter.streamrefactoring.eval/src/edu/cuny/hunter/streamrefactoring/eval/handlers/EvaluateConvertToParallelStreamRefactoringHandler.java

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,82 @@
6161

6262
/**
6363
* Our sample handler extends AbstractHandler, an IHandler base class.
64-
*
64+
*
6565
* @see org.eclipse.core.commands.IHandler
6666
* @see org.eclipse.core.commands.AbstractHandler
6767
*/
6868
public class EvaluateConvertToParallelStreamRefactoringHandler extends AbstractHandler {
6969

70-
private static final int LOGGING_LEVEL = IStatus.INFO;
7170
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;
7372
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+
}
74140

75141
/**
76142
* the command has been executed, so extract extract the needed information from
@@ -206,28 +272,26 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
206272
Set<Stream> optimizableStreams = processor.getOptimizableStreams();
207273
resultsPrinter.print(optimizableStreams.size()); // number.
208274

209-
for (Stream stream : optimizableStreams) {
275+
for (Stream stream : optimizableStreams)
210276
optimizedStreamPrinter.printRecord(javaProject.getElementName(), stream.getCreation(),
211277
stream.getCreation().getStartPosition(), stream.getCreation().getLength(),
212278
Util.getMethodIdentifier(stream.getEnclosingEclipseMethod()),
213279
stream.getEnclosingType().getFullyQualifiedName());
214-
}
215280

216281
// failed streams.
217-
for (Stream stream : processor.getUnoptimizableStreams()) {
282+
for (Stream stream : processor.getUnoptimizableStreams())
218283
nonOptimizedStreamPrinter.printRecord(javaProject.getElementName(), stream.getCreation(),
219284
stream.getCreation().getStartPosition(), stream.getCreation().getLength(),
220285
Util.getMethodIdentifier(stream.getEnclosingEclipseMethod()),
221286
stream.getEnclosingType() == null ? null
222287
: stream.getEnclosingType().getFullyQualifiedName());
223-
}
224288

225289
// failed preconditions.
226290
List<RefactoringStatusEntry> errorEntries = Arrays.stream(status.getEntries())
227291
.filter(RefactoringStatusEntry::isError).collect(Collectors.toList());
228292
resultsPrinter.print(errorEntries.size()); // number.
229293

230-
for (RefactoringStatusEntry entry : errorEntries) {
294+
for (RefactoringStatusEntry entry : errorEntries)
231295
if (!entry.isFatalError()) {
232296
Object correspondingElement = entry.getData();
233297

@@ -245,7 +309,6 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
245309
: failedStream.getEnclosingType().getFullyQualifiedName(),
246310
entry.getCode(), entry.getMessage());
247311
}
248-
}
249312

250313
// Refactoring type counts.
251314
for (Refactoring refactoring : Refactoring.values())
@@ -266,15 +329,14 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
266329

267330
// actually perform the refactoring if there are no fatal
268331
// errors.
269-
if (shouldPerformChange()) {
332+
if (this.shouldPerformChange())
270333
if (!status.hasFatalError()) {
271334
resultsTimeCollector.start();
272335
Change change = new ProcessorBasedRefactoring(processor)
273336
.createChange(new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
274337
change.perform(new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN));
275338
resultsTimeCollector.stop();
276339
}
277-
}
278340

279341
// ensure that we can build the project.
280342
if (!javaProject.isConsistent())
@@ -339,7 +401,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
339401

340402
private Set<SearchMatch> findReferences(Set<? extends IJavaElement> elements) throws CoreException {
341403
Set<SearchMatch> ret = new HashSet<>();
342-
for (IJavaElement elem : elements) {
404+
for (IJavaElement elem : elements)
343405
new SearchEngine().search(
344406
SearchPattern.createPattern(elem, IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH),
345407
new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() },
@@ -350,43 +412,9 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
350412
ret.add(match);
351413
}
352414
}, new NullProgressMonitor());
353-
}
354415
return ret;
355416
}
356417

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-
390418
private boolean shouldPerformChange() {
391419
String performChangePropertyValue = System.getenv(PERFORM_CHANGE_PROPERTY_KEY);
392420

@@ -395,39 +423,4 @@ private boolean shouldPerformChange() {
395423
else
396424
return Boolean.valueOf(performChangePropertyValue);
397425
}
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-
}
433426
}

0 commit comments

Comments
 (0)