Skip to content

Commit

Permalink
Merge pull request #9 from mariazevedo88/fix/WMCandNOCCalculation
Browse files Browse the repository at this point in the history
Fixing WMC, CC, and NOC calculation
  • Loading branch information
Mariana Azevedo authored Jul 7, 2019
2 parents 45516a5 + 5abb6d6 commit 8754b52
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 33 deletions.
67 changes: 51 additions & 16 deletions src/com/o3smeasures/astvisitors/CyclomaticComplexityVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.ParenthesizedExpression;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SwitchCase;
Expand Down Expand Up @@ -127,6 +128,10 @@ public boolean visit(TryStatement node) {
if(isSameMethod(node)) {
cyclomaticComplexityIndex++;
sumCyclomaticComplexity++;
if(node.getFinally() != null) {
cyclomaticComplexityIndex++;
sumCyclomaticComplexity++;
}
isFirstVisitingPerMethod = false;
return true;
}
Expand Down Expand Up @@ -252,13 +257,30 @@ public boolean visit(LambdaExpression node) {
*/
@Override
public boolean visit(ReturnStatement node) {
if(isSameMethod(node) && node.getExpression() instanceof MethodInvocation) {
if(isSameMethod(node)){
inspectMethodInvocations(node.getExpression());
if(node.getExpression() instanceof ParenthesizedExpression) {
ParenthesizedExpression exp = (ParenthesizedExpression) node.getExpression();
if(exp.getExpression() instanceof ConditionalExpression) {
ConditionalExpression condExp = (ConditionalExpression) exp.getExpression();
inspectMethodInvocations(condExp.getThenExpression());
inspectMethodInvocations(condExp.getElseExpression());
cyclomaticComplexityIndex++;
sumCyclomaticComplexity++;
isFirstVisitingPerMethod = false;
}
}
return true;
}
return false;
}

private void inspectMethodInvocations(Expression node) {
if(node instanceof MethodInvocation) {
cyclomaticComplexityIndex++;
sumCyclomaticComplexity++;
isFirstVisitingPerMethod = false;
return true;
}
return false;
}

/**
Expand Down Expand Up @@ -289,9 +311,9 @@ private void inspectExpression(Expression exprs) {
* @author Mariana Azevedo
* @since 13/07/2014
*
* @return Double
* @return double
*/
public Double getCyclomaticComplexityIndex(){
public double getCyclomaticComplexityIndex(){
if(cyclomaticComplexityIndex == 0d) cyclomaticComplexityIndex++;
return new BigDecimal(cyclomaticComplexityIndex, new MathContext(2, RoundingMode.UP)).doubleValue();
}
Expand All @@ -313,9 +335,9 @@ public void cleanVariables(){
* @author Mariana Azevedo
* @since 13/07/2014
*
* @return Double
* @return double
*/
public Double getAllCyclomaticComplexity() {
public double getAllCyclomaticComplexity() {
if(sumCyclomaticComplexity == 0d) sumCyclomaticComplexity++;
return sumCyclomaticComplexity;
}
Expand All @@ -342,8 +364,8 @@ public void setMethodName(String methodName) {
*/
private boolean isSameMethod(Statement node) {
if(node.getParent().getParent() instanceof MethodDeclaration) {
return ((MethodDeclaration) node.getParent().getParent())
.getName().toString().equals(methodName);
MethodDeclaration declaration = (MethodDeclaration) node.getParent().getParent();
return declaration.getName().toString().equals(methodName);
}

return false;
Expand All @@ -359,11 +381,11 @@ private boolean isSameMethod(Statement node) {
* @return boolean
*/
private boolean isSameMethod(CatchClause node) {
if(node.getParent().getParent() instanceof MethodDeclaration) {
return ((MethodDeclaration) node.getParent().getParent())
.getName().toString().equals(methodName);
if(node.getParent().getParent().getParent() instanceof MethodDeclaration) {
MethodDeclaration declaration = (MethodDeclaration) node.getParent()
.getParent().getParent();
return declaration.getName().toString().equals(methodName);
}

return false;
}

Expand All @@ -378,11 +400,24 @@ private boolean isSameMethod(CatchClause node) {
*/
private boolean isSameMethod(Expression node) {
if(node.getParent().getParent() instanceof MethodDeclaration) {
return ((MethodDeclaration) node.getParent().getParent())
.getName().toString().equals(methodName);
MethodDeclaration declaration = (MethodDeclaration) node.getParent().getParent();
return declaration.getName().toString().equals(methodName);
}

return false;
}

/**
* Method that checks whether a TryStatement has a catch clause.
* @author Mariana Azevedo
* @since 06/07/2019
*
* @param node - TryStatement
*/
public void checkCatchFinallyClausesInWeightMethodsClass(TryStatement node) {
if(!node.catchClauses().isEmpty()) {
cyclomaticComplexityIndex++;
sumCyclomaticComplexity++;
}
}

}
48 changes: 43 additions & 5 deletions src/com/o3smeasures/astvisitors/WeightMethodsPerClassVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ConditionalExpression;
import org.eclipse.jdt.core.dom.DoStatement;
import org.eclipse.jdt.core.dom.ExpressionMethodReference;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.TryStatement;
Expand All @@ -38,6 +41,8 @@ public class WeightMethodsPerClassVisitor extends ASTVisitor{
private CyclomaticComplexityVisitor visitor;
private static WeightMethodsPerClassVisitor instance;

private String methodName;

public WeightMethodsPerClassVisitor(){
super();
this.wmcIndex = 0d;
Expand Down Expand Up @@ -71,10 +76,9 @@ private void calculateWeightMethods(CompilationUnit node){

for (Object type : node.types()){

if ((type instanceof TypeDeclaration) && !((TypeDeclaration) type).isInterface()){
if ((type instanceof TypeDeclaration)){

List<TypeDeclaration> bodyDeclarationsList = ((TypeDeclaration) type).
bodyDeclarations();
List<TypeDeclaration> bodyDeclarationsList = ((TypeDeclaration) type).bodyDeclarations();
Iterator<TypeDeclaration> itBodyDeclaration = bodyDeclarationsList.iterator();

while (itBodyDeclaration.hasNext()){
Expand Down Expand Up @@ -125,6 +129,9 @@ private void coveringStatements(Iterator<Statement> itStatements) {
* @param itStatement
*/
private void getStatementType(Object itStatement) {

this.visitor.setMethodName(getMethodName());

if (itStatement instanceof CatchClause){
this.visitor.visit((CatchClause)itStatement);
}else if (itStatement instanceof ForStatement){
Expand All @@ -135,6 +142,8 @@ private void getStatementType(Object itStatement) {
this.visitor.visit((WhileStatement)itStatement);
}else if (itStatement instanceof TryStatement){
this.visitor.visit((TryStatement)itStatement);
this.visitor.checkCatchFinallyClausesInWeightMethodsClass
((TryStatement)itStatement);
}else if (itStatement instanceof ConditionalExpression){
this.visitor.visit((ConditionalExpression)itStatement);
}else if (itStatement instanceof SwitchCase){
Expand All @@ -143,16 +152,23 @@ private void getStatementType(Object itStatement) {
this.visitor.visit((DoStatement)itStatement);
}else if (itStatement instanceof ExpressionStatement){
this.visitor.visit((ExpressionStatement)itStatement);
}else if(itStatement instanceof ExpressionMethodReference) {
this.visitor.visit((ExpressionMethodReference)itStatement);
}else if(itStatement instanceof LambdaExpression) {
this.visitor.visit((LambdaExpression)itStatement);
}else if(itStatement instanceof ReturnStatement) {
this.visitor.visit((ReturnStatement)itStatement);
}
}

/**
* Method to get the WMC value.
* @author Mariana Azevedo
* @since 13/07/2014
* @return Double
* @return double
*/
public Double getWeightMethodsPerClassIndex(){
public double getWeightMethodsPerClassIndex(){
if(wmcIndex == 0d) wmcIndex++;
return new BigDecimal(this.wmcIndex, new MathContext(2, RoundingMode.UP)).doubleValue();
}

Expand All @@ -166,4 +182,26 @@ public void cleanArraysAndVariable(){
this.visitor = CyclomaticComplexityVisitor.getInstance();
this.visitor.cleanVariables();
}

/**
* Method to set the name of the method evaluated.
* @author Mariana Azevedo
* @since 06/07/2019
*
* @param methodName
*/
public void setMethodName(String methodName) {
this.methodName = methodName;
}

/**
* Method to get the name of the method evaluated.
* @author Mariana Azevedo
* @since 07/07/2019
*
* @return String
*/
public String getMethodName() {
return methodName;
}
}
10 changes: 4 additions & 6 deletions src/com/o3smeasures/javamodel/NumberOfChildrenJavaModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,16 @@ public void calculateValue(ICompilationUnit unit) {
for (IType type : types) {
ITypeHierarchy th= type.newTypeHierarchy((IJavaProject) type.getAncestor(IJavaElement.JAVA_PROJECT), null);

if (th != null) subtypesList = th.getAllSubtypes(type);
if (th != null) subtypesList = th.getSubtypes(type);

if (subtypesList != null) length = subtypesList.length;

Double value = new BigDecimal(length, new MathContext(2, RoundingMode.UP)).doubleValue();
double value = new BigDecimal(length, new MathContext(2, RoundingMode.UP)).doubleValue();
setNocValue(value);
}

}catch (JavaModelException exception1) {
logger.error(exception1);
}catch (NullPointerException exception2){
logger.error(exception2);
}catch (JavaModelException | NullPointerException ex) {
logger.error(ex);
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/com/o3smeasures/measures/WeightMethodsPerClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.internal.core.SourceMethod;

import com.o3smeasures.astvisitors.ClassVisitor;
import com.o3smeasures.astvisitors.WeightMethodsPerClassVisitor;
Expand All @@ -17,6 +18,7 @@
* @since 13/07/2014
*
*/
@SuppressWarnings("restriction")
public class WeightMethodsPerClass extends Measure{

private double value;
Expand All @@ -28,10 +30,10 @@ public class WeightMethodsPerClass extends Measure{

public WeightMethodsPerClass(){
super();
this.value = 0d;
this.mean = 0d;
this.max = 0d;
this.min = 0d;
this.value = 1d;
this.mean = 1d;
this.max = 1d;
this.min = 1d;
this.classWithMaxValue = "";
this.isEnable = true;
addApplicableGranularity(Granularity.METHOD);
Expand Down Expand Up @@ -90,7 +92,7 @@ public double getMeanValue() {
*/
@Override
public double getRefValue() {
return 0d;
return 1d;
}

/**
Expand Down Expand Up @@ -141,6 +143,7 @@ public <T> void measure(T unit) {

CompilationUnit parse = parse(unit);
WeightMethodsPerClassVisitor visitor = WeightMethodsPerClassVisitor.getInstance();
visitor.setMethodName(((SourceMethod)unit).getElementName());
visitor.cleanArraysAndVariable();
parse.accept(visitor);

Expand Down Expand Up @@ -211,7 +214,7 @@ public void setClassWithMaxValue(String value) {

@Override
public void setMinValue(double value) {
if (min > value || min == 0d){
if (min > value || min == 1d){
this.min = value;
}
}
Expand Down

0 comments on commit 8754b52

Please sign in to comment.