Skip to content

Commit 6955e0e

Browse files
committed
Enhancement.
1 parent e5d5415 commit 6955e0e

File tree

1 file changed

+61
-2
lines changed
  • edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis

1 file changed

+61
-2
lines changed

edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Stream.java

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static edu.cuny.hunter.streamrefactoring.core.analysis.Util.getLineNumberFromAST;
66
import static edu.cuny.hunter.streamrefactoring.core.analysis.Util.getLineNumberFromIR;
77
import static edu.cuny.hunter.streamrefactoring.core.analysis.Util.getPossibleTypesInterprocedurally;
8+
import static edu.cuny.hunter.streamrefactoring.core.analysis.Util.implementsBaseStream;
89
import static edu.cuny.hunter.streamrefactoring.core.analysis.Util.matches;
910
import static edu.cuny.hunter.streamrefactoring.core.safe.Util.instanceKeyCorrespondsWithInstantiationInstruction;
1011

@@ -27,8 +28,10 @@
2728
import org.eclipse.jdt.core.IMethod;
2829
import org.eclipse.jdt.core.IType;
2930
import org.eclipse.jdt.core.JavaModelException;
31+
import org.eclipse.jdt.core.dom.AST;
3032
import org.eclipse.jdt.core.dom.ASTNode;
3133
import org.eclipse.jdt.core.dom.CompilationUnit;
34+
import org.eclipse.jdt.core.dom.Expression;
3235
import org.eclipse.jdt.core.dom.IMethodBinding;
3336
import org.eclipse.jdt.core.dom.ITypeBinding;
3437
import org.eclipse.jdt.core.dom.MethodDeclaration;
@@ -315,8 +318,64 @@ protected void convertToParallel(CompilationUnitRewrite rewrite) {
315318
LOGGER.info("Converting to parallel.");
316319
MethodInvocation creation = this.getCreation();
317320
ASTRewrite astRewrite = rewrite.getASTRewrite();
318-
SimpleName newMethodName = creation.getAST().newSimpleName("parallelStream");
319-
astRewrite.replace(creation.getName(), newMethodName, null);
321+
322+
MethodInvocation termOp = findTerminalOperation(creation);
323+
324+
// get the terminal operation's expression.
325+
Expression expression = termOp.getExpression();
326+
327+
boolean done = false;
328+
329+
while (expression != null && !done) {
330+
if (expression.getNodeType() == ASTNode.METHOD_INVOCATION) {
331+
MethodInvocation inv = (MethodInvocation) expression;
332+
AST ast = creation.getAST();
333+
334+
switch (inv.getName().getIdentifier()) {
335+
case "sequential":
336+
// remove it.
337+
astRewrite.replace(inv, inv.getExpression(), null);
338+
break;
339+
case "parallel":
340+
done = true;
341+
break;
342+
case "stream": {
343+
// Replace with parallelStream().
344+
SimpleName newMethodName = ast.newSimpleName("parallelStream");
345+
astRewrite.replace(creation.getName(), newMethodName, null);
346+
break;
347+
}
348+
case "parallelStream":
349+
done = true;
350+
break;
351+
default: {
352+
// if we're at the end.
353+
if (inv.getExpression().getNodeType() != ASTNode.METHOD_INVOCATION
354+
|| inv.getExpression().getNodeType() == ASTNode.METHOD_INVOCATION
355+
&& !implementsBaseStream(inv.getExpression().resolveTypeBinding())) {
356+
MethodInvocation newMethodInvocation = ast.newMethodInvocation();
357+
newMethodInvocation.setName(ast.newSimpleName("parallel"));
358+
MethodInvocation invCopy = (MethodInvocation) ASTNode.copySubtree(ast, inv);
359+
newMethodInvocation.setExpression(invCopy);
360+
astRewrite.replace(inv, newMethodInvocation, null);
361+
}
362+
}
363+
}
364+
expression = inv.getExpression();
365+
} else
366+
done = true;
367+
}
368+
}
369+
370+
private static MethodInvocation findTerminalOperation(ASTNode astNode) {
371+
if (astNode == null)
372+
return null;
373+
else if (astNode.getNodeType() != ASTNode.METHOD_INVOCATION)
374+
throw new IllegalArgumentException(astNode + " must be a method invocation.");
375+
if (astNode.getParent().getNodeType() != ASTNode.METHOD_INVOCATION)
376+
return (MethodInvocation) astNode;
377+
else
378+
return findTerminalOperation(astNode.getParent());
320379
}
321380

322381
protected void convertToSequential(CompilationUnitRewrite rewrite) {

0 commit comments

Comments
 (0)