Skip to content

Commit

Permalink
instrumentation of chained method calls (builder pattern)
Browse files Browse the repository at this point in the history
issue #58 : #58
  • Loading branch information
miho committed Aug 4, 2015
1 parent 475fbfc commit c6432d8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
public interface Invocation extends CodeEntity {

public String getVariableName();
public void setVariableName(String variableName);

public String getMethodName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import eu.mihosoft.vrl.workflow.VNode;
import eu.mihosoft.vrl.workflow.VisualizationRequest;
import eu.mihosoft.vrl.workflow.WorkflowUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import javafx.collections.FXCollections;
Expand All @@ -66,7 +68,7 @@
class InvocationImpl implements Invocation {

private String id;
private final String varName;
private String varName;
private final String methodName;
private final ObservableList<IArgument> arguments = FXCollections.observableArrayList();
private final boolean constructor;
Expand Down Expand Up @@ -103,6 +105,12 @@ public InvocationImpl(
// } else {
// returnValue = parent.createVariable(this);
// }
init(varName);

}

private void init(String varName) {

if (varName != null && !varName.isEmpty()) {
Variable var = null;
try {
Expand All @@ -111,7 +119,7 @@ public InvocationImpl(
// will be checked later (see if below)
}

if (!isStatic && !isScope() && var == null) {
if (!isStatic() && !isScope() && var == null) {

throw new IllegalArgumentException(
"Variable '"
Expand All @@ -126,7 +134,16 @@ public InvocationImpl(
if (isScope()) {
// nothing (see ScopeInvocationImpl)
} else {
node = parent.getFlow().newNode();
if (node == null) {
node = parent.getFlow().newNode();
} else {
List<Connector> delList = new ArrayList<>();
delList.addAll(node.getConnectors());
for (Connector c : delList) {
node.removeConnector(c);
}
}

node.getValueObject().setValue(this);

Connector controlflowInput = node.setMainInput(
Expand All @@ -147,7 +164,7 @@ public InvocationImpl(
controlflowOutput.setMaxNumberOfConnections(1);

int argIndex = 0;
for (IArgument arg : args) {
for (IArgument arg : arguments) {
node.addInput(WorkflowUtil.DATA_FLOW).getValueObject().
setValue(new ArgumentValue(argIndex, arg));
argIndex++;
Expand All @@ -162,7 +179,6 @@ public InvocationImpl(
node.setTitle(varName + "." + methodName + "()");

}

}

@Override
Expand Down Expand Up @@ -388,6 +404,7 @@ public void fireEvent(CodeEvent evt) {
/**
* @return the textRenderingEnabled
*/
@Override
public boolean isTextRenderingEnabled() {
return textRenderingEnabled;
}
Expand All @@ -399,4 +416,10 @@ public void setTextRenderingEnabled(boolean textRenderingEnabled) {
this.textRenderingEnabled = textRenderingEnabled;
}

@Override
public void setVariableName(String variableName) {
this.varName = variableName;
init(varName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,15 @@ public ControlFlowScope transform(ControlFlowScope ce) {
Invocation inv = invocations.get(i);

System.out.println("i : " + inv);

String varName = newVarName();

// cf.declareVariable("", Type.STRING, varName+"_pre_arg");
// Variable rightArgVariable = ce.getVariable(varName+"_pre_arg");
// Invocation preArgInv = cf.invokeOperator("",
// Argument.constArg(Type.STRING, "pre-m-call: " + inv.getMethodName() + ", args: "),
// Argument.varArg(result.getVariable(varName)), Operator.PLUS);
// cf.assignVariable("", rightArgVariable.getName(), Argument.invArg(preArgInv));


Invocation preEventInv
= cf.callMethod("", "this", "println", Type.VOID, Argument.constArg(
Type.STRING, "pre-m-call: " + inv.getMethodName()));
Expand All @@ -100,29 +98,39 @@ public ControlFlowScope transform(ControlFlowScope ce) {
filter(a -> Objects.equals(a.getInvocation().orElse(null), inv)).
findAny().isPresent();

if (iIsArgOfNextI) {

// TODO 04.08.2015 chained method check buggy, needs to be modeled too
boolean retValIsObjectOfNextI
= nextI.getVariableName() != null
&& nextI.getVariableName().isEmpty();

if (iIsArgOfNextI || retValIsObjectOfNextI) {

cf.declareVariable("", inv.getReturnType(), varName);
cf.assignVariable("", varName, Argument.invArg(inv));

int[] argumentsToReplace = nextI.getArguments().stream().
filter(a -> Objects.equals(a.getInvocation().orElse(null), inv)).
mapToInt(a -> nextI.getArguments().indexOf(a)).toArray();

for (int aIndex : argumentsToReplace) {
nextI.getArguments().set(aIndex,
Argument.varArg(result.getVariable(varName)));
if (iIsArgOfNextI) {
int[] argumentsToReplace = nextI.getArguments().stream().
filter(a -> Objects.equals(a.getInvocation().orElse(null), inv)).
mapToInt(a -> nextI.getArguments().indexOf(a)).toArray();

for (int aIndex : argumentsToReplace) {
nextI.getArguments().set(aIndex,
Argument.varArg(result.getVariable(varName)));
}
} else {
nextI.setVariableName(varName);
}
cf.declareVariable("", Type.STRING, varName+"_post_arg");
Variable rightArgVariable = ce.getVariable(varName+"_post_arg");

cf.declareVariable("", Type.STRING, varName + "_post_arg");
Variable rightArgVariable = ce.getVariable(varName + "_post_arg");
Invocation retValPostArgInv = cf.invokeOperator("",
Argument.constArg(Type.STRING, "post-m-call: " + inv.getMethodName() + ", retVal: "),
Argument.varArg(result.getVariable(varName)), Operator.PLUS);
cf.assignVariable("", rightArgVariable.getName(), Argument.invArg(retValPostArgInv));

retValArg = Argument.varArg(rightArgVariable);
}

}

cf.getInvocations().add(inv);
Expand Down

0 comments on commit c6432d8

Please sign in to comment.