Skip to content

Commit 4b98c91

Browse files
authored
Improved refactoring operations
1 parent ad6ea10 commit 4b98c91

9 files changed

+278
-27
lines changed

BehaviouralFeature.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -2885,7 +2885,7 @@ public void checkParameterNames()
28852885
if (activity == null && post != null)
28862886
{ Vector puses = post.getUses(pname);
28872887
if (puses.size() == 0)
2888-
{ System.err.println("!! Bad smell (UVA): parameter " + pname + " is unused in operation " + getName() + " postcondition.");
2888+
{ System.err.println("!! Code smell (UVA): parameter " + pname + " is unused in operation " + getName() + " postcondition.");
28892889
UVA++;
28902890
unusedVars.add(pname);
28912891
}
@@ -2894,7 +2894,7 @@ public void checkParameterNames()
28942894
if (activity != null)
28952895
{ Vector actuses = activity.getUses(pname);
28962896
if (actuses.size() == 0)
2897-
{ System.err.println("!! Bad smell (UVA): parameter " + pname + " is unused in operation " + getName() + " activity.");
2897+
{ System.err.println("!! Code smell (UVA): parameter " + pname + " is unused in operation " + getName() + " activity.");
28982898
UVA++;
28992899
unusedVars.add(pname);
29002900
}
@@ -3561,7 +3561,7 @@ else if (useCase != null)
35613561

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

@@ -3583,7 +3583,7 @@ else if (useCase != null)
35833583
int acomp = activity.syntacticComplexity();
35843584
out.println("*** Activity syntactic complexity = " + acomp);
35853585
if (acomp > 100)
3586-
{ System.err.println("!!! Bad smell (EOS): too high activity complexity (" + acomp + ") for " + nme);
3586+
{ System.err.println("!!! Code smell (EOS): too high activity complexity (" + acomp + ") for " + nme);
35873587
System.err.println(">>> Recommend refactoring by functional decomposition");
35883588
}
35893589
else if (acomp > 50)
@@ -3594,12 +3594,12 @@ else if (acomp > 50)
35943594
out.println("*** Total complexity of operation " + nme + " = " + complexity);
35953595
out.println();
35963596
if (cyc > 10)
3597-
{ System.err.println("!!! Bad smell (CC): high cyclomatic complexity (" + cyc + ") for " + nme);
3597+
{ System.err.println("!!! Code smell (CC): high cyclomatic complexity (" + cyc + ") for " + nme);
35983598
System.err.println(">>> Recommend refactoring by functional decomposition");
35993599
}
36003600

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

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

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

36683668
return cyc;
36693669
}

BinaryExpression.java

+31-8
Original file line numberDiff line numberDiff line change
@@ -2944,6 +2944,12 @@ else if (op != null)
29442944
clones.put(val,used);
29452945
left.findClones(clones,rule,op);
29462946
right.findClones(clones,rule,op);
2947+
2948+
if ("->iterate".equals(operator) && accumulator != null)
2949+
{ Expression expr = accumulator.getInitialExpression();
2950+
if (expr != null)
2951+
{ expr.findClones(clones,rule,op); }
2952+
}
29472953
}
29482954

29492955
public void findClones(java.util.Map clones,
@@ -2968,11 +2974,24 @@ else if (op != null)
29682974
cloneDefs.put(val, this);
29692975
left.findClones(clones,cloneDefs,rule,op);
29702976
right.findClones(clones,cloneDefs,rule,op);
2977+
2978+
if ("->iterate".equals(operator) && accumulator != null)
2979+
{ Expression expr = accumulator.getInitialExpression();
2980+
if (expr != null)
2981+
{ expr.findClones(clones,cloneDefs,rule,op); }
2982+
}
2983+
29712984
}
29722985

29732986
public void findMagicNumbers(java.util.Map mgns, String rule, String op)
29742987
{ left.findMagicNumbers(mgns,rule,op);
29752988
right.findMagicNumbers(mgns,rule,op);
2989+
2990+
if ("->iterate".equals(operator) && accumulator != null)
2991+
{ Expression expr = accumulator.getInitialExpression();
2992+
if (expr != null)
2993+
{ expr.findMagicNumbers(mgns,rule,op); }
2994+
}
29762995
}
29772996

29782997

@@ -7504,7 +7523,7 @@ public String exists1QueryForm(java.util.Map env, boolean local)
75047523
existsleft = left;
75057524
// localentity = left.getEntity(); // or entity of the elementType
75067525
if (left.elementType == null)
7507-
{ System.err.println("Warning: no element type for: " + left);
7526+
{ System.err.println("!! Warning: no element type for: " + left);
75087527
// JOptionPane.showMessageDialog(null, "no element type for " + left + " in " + this,
75097528
// "Design error", JOptionPane.ERROR_MESSAGE);
75107529
}
@@ -7518,7 +7537,7 @@ else if (operator.equals("#1"))
75187537
existsvar = beleft.left + "";
75197538
// localentity = beleft.right.getEntity(); // or entity of the elementType
75207539
if (beleft.right == null || beleft.right.elementType == null)
7521-
{ System.err.println("Warning: no element type of: " + beleft);
7540+
{ System.err.println("!! Warning: no element type of: " + beleft);
75227541
// JOptionPane.showMessageDialog(null, "no element type for " + beleft + " in " + this,
75237542
// "Design error", JOptionPane.ERROR_MESSAGE);
75247543
}
@@ -7601,7 +7620,7 @@ public String exists1QueryFormJava6(java.util.Map env, boolean local)
76017620
existsleft = left;
76027621
// localentity = left.getEntity(); // or entity of the elementType
76037622
if (left.elementType == null)
7604-
{ System.err.println("DESIGN ERROR: no element type for: " + left);
7623+
{ System.err.println("!! DESIGN ERROR: no element type for: " + left);
76057624
JOptionPane.showMessageDialog(null, "no element type for " + left + " in " + this,
76067625
"Design error", JOptionPane.ERROR_MESSAGE);
76077626
}
@@ -7615,7 +7634,7 @@ else if (operator.equals("#1"))
76157634
existsvar = beleft.left + "";
76167635
// localentity = beleft.right.getEntity(); // or entity of the elementType
76177636
if (beleft.right == null || beleft.right.elementType == null)
7618-
{ System.out.println("DESIGN ERROR: no element type of: " + beleft);
7637+
{ System.out.println("!! DESIGN ERROR: no element type of: " + beleft);
76197638
JOptionPane.showMessageDialog(null, "no element type for " + beleft + " in " + this,
76207639
"Design error", JOptionPane.ERROR_MESSAGE);
76217640
}
@@ -7699,7 +7718,7 @@ public String quantifierQueryFormJava7(java.util.Map env, boolean local)
76997718
existsleft = left;
77007719
// localentity = left.getEntity(); // or entity of the elementType
77017720
if (left.elementType == null)
7702-
{ System.err.println("DESIGN ERROR: no element type for: " + left);
7721+
{ System.err.println("!! DESIGN ERROR: no element type for: " + left);
77037722
JOptionPane.showMessageDialog(null, "no element type for " + left + " in " + this,
77047723
"Design error", JOptionPane.ERROR_MESSAGE);
77057724
}
@@ -7714,7 +7733,7 @@ else if (operator.equals("#1") || operator.equals("#LC") ||
77147733
existsvar = beleft.left + "";
77157734
// localentity = beleft.right.getEntity(); // or entity of the elementType
77167735
if (beleft.right == null || beleft.right.elementType == null)
7717-
{ System.out.println("DESIGN ERROR: no element type of: " + beleft);
7736+
{ System.out.println("!! DESIGN ERROR: no element type of: " + beleft);
77187737
JOptionPane.showMessageDialog(null, "no element type for " + beleft + " in " + this,
77197738
"Design error", JOptionPane.ERROR_MESSAGE);
77207739
}
@@ -16060,7 +16079,8 @@ public Scope resultScope()
1606016079
else
1606116080
{ return res2; }
1606216081
}
16063-
if (operator.equals("=>") || operator.equals("#") || operator.equals("#1") ||
16082+
if (operator.equals("=>") ||
16083+
operator.equals("#") || operator.equals("#1") ||
1606416084
operator.equals("#LC") || operator.equals("!"))
1606516085
{ return right.resultScope(); } // this ?
1606616086
return null;
@@ -16144,6 +16164,9 @@ public Expression substituteEq(final String oldVar,
1614416164

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

16167+
if (oldVar.equals(this + ""))
16168+
{ return newVal; }
16169+
1614716170
Expression newLeft = null;
1614816171
if (left != null)
1614916172
{ newLeft = left.substituteEq(oldVar,newVal); }
@@ -16327,7 +16350,7 @@ public Vector splitAnd(Vector sms)
1632716350
else if (operator.equals("="))
1632816351
{ return expandEqSucc(sms); }
1632916352
else
16330-
{ System.out.println("Expression in succedent without = or &!");
16353+
{ System.out.println("!! Expression in succedent without = or &");
1633116354
return res;
1633216355
}
1633316356
}

ConditionalExpression.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,10 @@ public boolean hasVariable(final String s)
519519

520520
public Expression substituteEq(final String oldVar,
521521
final Expression newVal)
522-
{ Expression newLeft = null;
522+
{ if (oldVar.equals(this + ""))
523+
{ return newVal; }
524+
525+
Expression newLeft = null;
523526
if (ifExp != null)
524527
{ newLeft = ifExp.substituteEq(oldVar,newVal); }
525528
Expression newRight = null;

Entity.java

+101-1
Original file line numberDiff line numberDiff line change
@@ -4644,7 +4644,107 @@ public void extractOperations()
46444644
Vector copies = (Vector) clones.get(clne);
46454645
if (copies != null && copies.size() > 1)
46464646
{ Object obj = cdefs.get(clne);
4647-
System.out.println(">>> Extracting operation for clone: " + clne + " " + obj);
4647+
System.out.println(">>> Extracting operation for clone: " + clne + " " + obj);
4648+
4649+
if (obj instanceof Expression)
4650+
{ Expression expr = (Expression) obj;
4651+
Type etype = expr.getType();
4652+
Type elemtype = expr.getElementType();
4653+
System.out.println(">> Type: " + etype + " " + elemtype);
4654+
Vector vars = expr.getVariableUses();
4655+
Vector pars = new Vector();
4656+
Vector pnames = new Vector();
4657+
for (int j = 0; j < vars.size(); j++)
4658+
{ Expression ve = (Expression) vars.get(j);
4659+
if (pnames.contains(ve + "")) { }
4660+
else
4661+
{ Attribute par =
4662+
new Attribute(ve + "", ve.getType(), ModelElement.INTERNAL);
4663+
par.setElementType(ve.getElementType());
4664+
pars.add(par);
4665+
pnames.add(ve + "");
4666+
}
4667+
}
4668+
4669+
System.out.println(">> Variables: " + vars);
4670+
Vector attrs = expr.allAttributesUsedIn();
4671+
System.out.println(">> Attributes: " + attrs);
4672+
4673+
// new operation with type etype, postcondition
4674+
// result = expr
4675+
4676+
BasicExpression res =
4677+
BasicExpression.newVariableBasicExpression(
4678+
"result", etype);
4679+
res.setElementType(elemtype);
4680+
4681+
BinaryExpression eqn =
4682+
new BinaryExpression("=", res, expr);
4683+
4684+
String fname =
4685+
Identifier.nextIdentifier("factored_op");
4686+
4687+
BehaviouralFeature bf =
4688+
new BehaviouralFeature(fname,pars,true,etype);
4689+
bf.setPostcondition(eqn);
4690+
4691+
BasicExpression selfvar =
4692+
BasicExpression.newVariableBasicExpression(
4693+
"self", new Type(this));
4694+
4695+
BasicExpression bfcall =
4696+
BasicExpression.newCallBasicExpression(fname,
4697+
selfvar,pars);
4698+
for (int j = 0; j < ops; j++)
4699+
{ BehaviouralFeature op =
4700+
(BehaviouralFeature) operations.get(j);
4701+
4702+
op.substituteEq(clne, bfcall);
4703+
}
4704+
4705+
bf.setOwner(this);
4706+
this.addOperation(bf);
4707+
}
4708+
else if (obj instanceof Statement)
4709+
{ Statement stat = (Statement) obj;
4710+
4711+
Vector rets = Statement.getReturnValues(stat);
4712+
System.out.println(">> Return values: " + rets);
4713+
Vector jumps = Statement.getBreaksContinues(stat);
4714+
System.out.println(">> Jump statements: " + jumps);
4715+
Vector vars = stat.getVariableUses();
4716+
System.out.println(">> Variables: " + vars);
4717+
4718+
if (rets.size() == 0 && jumps.size() == 0 &&
4719+
vars.size() == 0)
4720+
{
4721+
String fname =
4722+
Identifier.nextIdentifier("factored_op");
4723+
BehaviouralFeature bf =
4724+
new BehaviouralFeature(
4725+
fname,new Vector(),false,null);
4726+
bf.setPostcondition(new BasicExpression(true));
4727+
bf.setActivity(stat);
4728+
4729+
BasicExpression selfvar =
4730+
BasicExpression.newVariableBasicExpression(
4731+
"self", new Type(this));
4732+
4733+
BasicExpression bfcall =
4734+
BasicExpression.newCallBasicExpression(fname,
4735+
selfvar);
4736+
4737+
for (int j = 0; j < ops; j++)
4738+
{ BehaviouralFeature op =
4739+
(BehaviouralFeature) operations.get(j);
4740+
4741+
op.substituteEq(clne, bfcall);
4742+
}
4743+
4744+
bf.setOwner(this);
4745+
this.addOperation(bf);
4746+
}
4747+
}
46484748
}
46494749
}
46504750
}

SetExpression.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,10 @@ public Expression substitute(Expression old,
964964

965965
public Expression substituteEq(String old,
966966
Expression n)
967-
{ Vector elems = new Vector();
967+
{ if (old.equals(this + ""))
968+
{ return n; }
969+
970+
Vector elems = new Vector();
968971
for (int i = 0; i < elements.size(); i++)
969972
{ Expression e = (Expression) elements.get(i);
970973
Expression be = e.substituteEq(old,n);

0 commit comments

Comments
 (0)