Skip to content

Commit

Permalink
Improved refactoring operations
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Dec 20, 2022
1 parent ad6ea10 commit 4b98c91
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 27 deletions.
18 changes: 9 additions & 9 deletions BehaviouralFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -2885,7 +2885,7 @@ public void checkParameterNames()
if (activity == null && post != null)
{ Vector puses = post.getUses(pname);
if (puses.size() == 0)
{ System.err.println("!! Bad smell (UVA): parameter " + pname + " is unused in operation " + getName() + " postcondition.");
{ System.err.println("!! Code smell (UVA): parameter " + pname + " is unused in operation " + getName() + " postcondition.");
UVA++;
unusedVars.add(pname);
}
Expand All @@ -2894,7 +2894,7 @@ public void checkParameterNames()
if (activity != null)
{ Vector actuses = activity.getUses(pname);
if (actuses.size() == 0)
{ System.err.println("!! Bad smell (UVA): parameter " + pname + " is unused in operation " + getName() + " activity.");
{ System.err.println("!! Code smell (UVA): parameter " + pname + " is unused in operation " + getName() + " activity.");
UVA++;
unusedVars.add(pname);
}
Expand Down Expand Up @@ -3561,7 +3561,7 @@ else if (useCase != null)

out.println("*** Number of parameters of operation " + nme + " = " + pars);
if (pars > 10)
{ System.err.println("!!! Bad smell (EPL): too many parameters (" + pars + ") for " + nme);
{ System.err.println("!!! Code smell (EPL): too many parameters (" + pars + ") for " + nme);
System.err.println(">>> Recommend refactoring by introducing value object for parameters or splitting operation into parts");
}

Expand All @@ -3583,7 +3583,7 @@ else if (useCase != null)
int acomp = activity.syntacticComplexity();
out.println("*** Activity syntactic complexity = " + acomp);
if (acomp > 100)
{ System.err.println("!!! Bad smell (EOS): too high activity complexity (" + acomp + ") for " + nme);
{ System.err.println("!!! Code smell (EOS): too high activity complexity (" + acomp + ") for " + nme);
System.err.println(">>> Recommend refactoring by functional decomposition");
}
else if (acomp > 50)
Expand All @@ -3594,12 +3594,12 @@ else if (acomp > 50)
out.println("*** Total complexity of operation " + nme + " = " + complexity);
out.println();
if (cyc > 10)
{ System.err.println("!!! Bad smell (CC): high cyclomatic complexity (" + cyc + ") for " + nme);
{ System.err.println("!!! Code smell (CC): high cyclomatic complexity (" + cyc + ") for " + nme);
System.err.println(">>> Recommend refactoring by functional decomposition");
}

if (complexity > 100)
{ System.err.println("!!! Bad smell (EHS): too high complexity (" + complexity + ") for " + nme);
{ System.err.println("!!! Code smell (EHS): too high complexity (" + complexity + ") for " + nme);
System.err.println(">>> Recommend refactoring by functional decomposition");
}
else if (complexity > 50)
Expand Down Expand Up @@ -3638,7 +3638,7 @@ else if (useCase != null)

System.out.println("*** Number of parameters of operation " + nme + " = " + pars);
if (pars > 10)
{ System.err.println("!!! Bad smell (EPL): too many parameters (" + pars + ") for " + nme); }
{ System.err.println("!!! Code smell (EPL): too many parameters (" + pars + ") for " + nme); }

int complexity = 0;
int cyc = 0;
Expand All @@ -3661,9 +3661,9 @@ else if (useCase != null)
System.out.println("*** Total complexity of operation " + nme + " = " + complexity);
System.out.println();
if (cyc > 10)
{ System.err.println("!!! Bad smell (CC): high cyclomatic complexity (" + cyc + ") for " + nme); }
{ System.err.println("!!! Code smell (CC): high cyclomatic complexity (" + cyc + ") for " + nme); }
if (complexity > 100)
{ System.err.println("!!! Bad smell (EHS): too high complexity (" + complexity + ") for " + nme); }
{ System.err.println("!!! Code smell (EHS): too high complexity (" + complexity + ") for " + nme); }

return cyc;
}
Expand Down
39 changes: 31 additions & 8 deletions BinaryExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -2944,6 +2944,12 @@ else if (op != null)
clones.put(val,used);
left.findClones(clones,rule,op);
right.findClones(clones,rule,op);

if ("->iterate".equals(operator) && accumulator != null)
{ Expression expr = accumulator.getInitialExpression();
if (expr != null)
{ expr.findClones(clones,rule,op); }
}
}

public void findClones(java.util.Map clones,
Expand All @@ -2968,11 +2974,24 @@ else if (op != null)
cloneDefs.put(val, this);
left.findClones(clones,cloneDefs,rule,op);
right.findClones(clones,cloneDefs,rule,op);

if ("->iterate".equals(operator) && accumulator != null)
{ Expression expr = accumulator.getInitialExpression();
if (expr != null)
{ expr.findClones(clones,cloneDefs,rule,op); }
}

}

public void findMagicNumbers(java.util.Map mgns, String rule, String op)
{ left.findMagicNumbers(mgns,rule,op);
right.findMagicNumbers(mgns,rule,op);

if ("->iterate".equals(operator) && accumulator != null)
{ Expression expr = accumulator.getInitialExpression();
if (expr != null)
{ expr.findMagicNumbers(mgns,rule,op); }
}
}


Expand Down Expand Up @@ -7504,7 +7523,7 @@ public String exists1QueryForm(java.util.Map env, boolean local)
existsleft = left;
// localentity = left.getEntity(); // or entity of the elementType
if (left.elementType == null)
{ System.err.println("Warning: no element type for: " + left);
{ System.err.println("!! Warning: no element type for: " + left);
// JOptionPane.showMessageDialog(null, "no element type for " + left + " in " + this,
// "Design error", JOptionPane.ERROR_MESSAGE);
}
Expand All @@ -7518,7 +7537,7 @@ else if (operator.equals("#1"))
existsvar = beleft.left + "";
// localentity = beleft.right.getEntity(); // or entity of the elementType
if (beleft.right == null || beleft.right.elementType == null)
{ System.err.println("Warning: no element type of: " + beleft);
{ System.err.println("!! Warning: no element type of: " + beleft);
// JOptionPane.showMessageDialog(null, "no element type for " + beleft + " in " + this,
// "Design error", JOptionPane.ERROR_MESSAGE);
}
Expand Down Expand Up @@ -7601,7 +7620,7 @@ public String exists1QueryFormJava6(java.util.Map env, boolean local)
existsleft = left;
// localentity = left.getEntity(); // or entity of the elementType
if (left.elementType == null)
{ System.err.println("DESIGN ERROR: no element type for: " + left);
{ System.err.println("!! DESIGN ERROR: no element type for: " + left);
JOptionPane.showMessageDialog(null, "no element type for " + left + " in " + this,
"Design error", JOptionPane.ERROR_MESSAGE);
}
Expand All @@ -7615,7 +7634,7 @@ else if (operator.equals("#1"))
existsvar = beleft.left + "";
// localentity = beleft.right.getEntity(); // or entity of the elementType
if (beleft.right == null || beleft.right.elementType == null)
{ System.out.println("DESIGN ERROR: no element type of: " + beleft);
{ System.out.println("!! DESIGN ERROR: no element type of: " + beleft);
JOptionPane.showMessageDialog(null, "no element type for " + beleft + " in " + this,
"Design error", JOptionPane.ERROR_MESSAGE);
}
Expand Down Expand Up @@ -7699,7 +7718,7 @@ public String quantifierQueryFormJava7(java.util.Map env, boolean local)
existsleft = left;
// localentity = left.getEntity(); // or entity of the elementType
if (left.elementType == null)
{ System.err.println("DESIGN ERROR: no element type for: " + left);
{ System.err.println("!! DESIGN ERROR: no element type for: " + left);
JOptionPane.showMessageDialog(null, "no element type for " + left + " in " + this,
"Design error", JOptionPane.ERROR_MESSAGE);
}
Expand All @@ -7714,7 +7733,7 @@ else if (operator.equals("#1") || operator.equals("#LC") ||
existsvar = beleft.left + "";
// localentity = beleft.right.getEntity(); // or entity of the elementType
if (beleft.right == null || beleft.right.elementType == null)
{ System.out.println("DESIGN ERROR: no element type of: " + beleft);
{ System.out.println("!! DESIGN ERROR: no element type of: " + beleft);
JOptionPane.showMessageDialog(null, "no element type for " + beleft + " in " + this,
"Design error", JOptionPane.ERROR_MESSAGE);
}
Expand Down Expand Up @@ -16060,7 +16079,8 @@ public Scope resultScope()
else
{ return res2; }
}
if (operator.equals("=>") || operator.equals("#") || operator.equals("#1") ||
if (operator.equals("=>") ||
operator.equals("#") || operator.equals("#1") ||
operator.equals("#LC") || operator.equals("!"))
{ return right.resultScope(); } // this ?
return null;
Expand Down Expand Up @@ -16144,6 +16164,9 @@ public Expression substituteEq(final String oldVar,

// System.out.println("SUBSTITUTING " + oldVar + " BY " + newVal + " IN " + this);

if (oldVar.equals(this + ""))
{ return newVal; }

Expression newLeft = null;
if (left != null)
{ newLeft = left.substituteEq(oldVar,newVal); }
Expand Down Expand Up @@ -16327,7 +16350,7 @@ public Vector splitAnd(Vector sms)
else if (operator.equals("="))
{ return expandEqSucc(sms); }
else
{ System.out.println("Expression in succedent without = or &!");
{ System.out.println("!! Expression in succedent without = or &");
return res;
}
}
Expand Down
5 changes: 4 additions & 1 deletion ConditionalExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,10 @@ public boolean hasVariable(final String s)

public Expression substituteEq(final String oldVar,
final Expression newVal)
{ Expression newLeft = null;
{ if (oldVar.equals(this + ""))
{ return newVal; }

Expression newLeft = null;
if (ifExp != null)
{ newLeft = ifExp.substituteEq(oldVar,newVal); }
Expression newRight = null;
Expand Down
102 changes: 101 additions & 1 deletion Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4644,7 +4644,107 @@ public void extractOperations()
Vector copies = (Vector) clones.get(clne);
if (copies != null && copies.size() > 1)
{ Object obj = cdefs.get(clne);
System.out.println(">>> Extracting operation for clone: " + clne + " " + obj);
System.out.println(">>> Extracting operation for clone: " + clne + " " + obj);

if (obj instanceof Expression)
{ Expression expr = (Expression) obj;
Type etype = expr.getType();
Type elemtype = expr.getElementType();
System.out.println(">> Type: " + etype + " " + elemtype);
Vector vars = expr.getVariableUses();
Vector pars = new Vector();
Vector pnames = new Vector();
for (int j = 0; j < vars.size(); j++)
{ Expression ve = (Expression) vars.get(j);
if (pnames.contains(ve + "")) { }
else
{ Attribute par =
new Attribute(ve + "", ve.getType(), ModelElement.INTERNAL);
par.setElementType(ve.getElementType());
pars.add(par);
pnames.add(ve + "");
}
}

System.out.println(">> Variables: " + vars);
Vector attrs = expr.allAttributesUsedIn();
System.out.println(">> Attributes: " + attrs);

// new operation with type etype, postcondition
// result = expr

BasicExpression res =
BasicExpression.newVariableBasicExpression(
"result", etype);
res.setElementType(elemtype);

BinaryExpression eqn =
new BinaryExpression("=", res, expr);

String fname =
Identifier.nextIdentifier("factored_op");

BehaviouralFeature bf =
new BehaviouralFeature(fname,pars,true,etype);
bf.setPostcondition(eqn);

BasicExpression selfvar =
BasicExpression.newVariableBasicExpression(
"self", new Type(this));

BasicExpression bfcall =
BasicExpression.newCallBasicExpression(fname,
selfvar,pars);
for (int j = 0; j < ops; j++)
{ BehaviouralFeature op =
(BehaviouralFeature) operations.get(j);

op.substituteEq(clne, bfcall);
}

bf.setOwner(this);
this.addOperation(bf);
}
else if (obj instanceof Statement)
{ Statement stat = (Statement) obj;

Vector rets = Statement.getReturnValues(stat);
System.out.println(">> Return values: " + rets);
Vector jumps = Statement.getBreaksContinues(stat);
System.out.println(">> Jump statements: " + jumps);
Vector vars = stat.getVariableUses();
System.out.println(">> Variables: " + vars);

if (rets.size() == 0 && jumps.size() == 0 &&
vars.size() == 0)
{
String fname =
Identifier.nextIdentifier("factored_op");
BehaviouralFeature bf =
new BehaviouralFeature(
fname,new Vector(),false,null);
bf.setPostcondition(new BasicExpression(true));
bf.setActivity(stat);

BasicExpression selfvar =
BasicExpression.newVariableBasicExpression(
"self", new Type(this));

BasicExpression bfcall =
BasicExpression.newCallBasicExpression(fname,
selfvar);

for (int j = 0; j < ops; j++)
{ BehaviouralFeature op =
(BehaviouralFeature) operations.get(j);

op.substituteEq(clne, bfcall);
}

bf.setOwner(this);
this.addOperation(bf);
}
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion SetExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,10 @@ public Expression substitute(Expression old,

public Expression substituteEq(String old,
Expression n)
{ Vector elems = new Vector();
{ if (old.equals(this + ""))
{ return n; }

Vector elems = new Vector();
for (int i = 0; i < elements.size(); i++)
{ Expression e = (Expression) elements.get(i);
Expression be = e.substituteEq(old,n);
Expand Down
Loading

0 comments on commit 4b98c91

Please sign in to comment.