Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor to solve smells #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Designite/SourceModel/Parsable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Designite.SourceModel;

public interface Parsable {
void parse();
}
8 changes: 4 additions & 4 deletions src/Designite/SourceModel/SM_EntitiesWithType.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public String getTypeOverallToString() {
// return "generic";
}

@Override
public void parse() {
}
// @Override
// public void parse() {
//
// }
}
89 changes: 53 additions & 36 deletions src/Designite/SourceModel/SM_Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import Designite.visitors.InstanceOfVisitor;
import Designite.visitors.ThrowVisitor;

public class SM_Method extends SM_SourceItem implements Vertex {
public class SM_Method extends SM_SourceItem implements Vertex, Parsable {

private boolean abstractMethod;
private boolean finalMethod;
Expand Down Expand Up @@ -103,17 +103,56 @@ public MethodDeclaration getMethodDeclaration() {
return methodDeclaration;
}

private void parseParameters() {
for (SM_Parameter param : parameterList) {
param.parse();
private void prepareCalledMethodsList() {
MethodInvVisitor invVisitor = new MethodInvVisitor(methodDeclaration);
methodDeclaration.accept(invVisitor);
List<MethodInvocation> invList = invVisitor.getCalledMethods();
if (invList.size() > 0) {
calledMethods.addAll(invList);
}
}

private void parseLocalVar() {
for (SM_LocalVar var : localVarList) {
var.parse();
private void prepareInstanceOfVisitorList() {
InstanceOfVisitor instanceOfVisitor = new InstanceOfVisitor();
methodDeclaration.accept(instanceOfVisitor);
List<Type> instanceOfTypes = instanceOfVisitor.getTypesInInstanceOf();
if (instanceOfTypes.size() > 0) {
typesInInstanceOf.addAll(instanceOfTypes);
}
}

private void prepareParametersList(SingleVariableDeclaration var) {
VariableVisitor parameterVisitor = new VariableVisitor(this);
// methodDeclaration.accept(parameterVisitor);
var.accept(parameterVisitor);
List<SM_Parameter> pList = parameterVisitor.getParameterList();
if (pList.size() > 0) {
parameterList.addAll(pList);
}
}

//SM_Parameter uses an empty parse method. So commenting this.
// private void parseParameters() {
// for (SM_Parameter param : parameterList) {
// param.parse();
// }
// }

private void prepareLocalVarList() {
LocalVarVisitor localVarVisitor = new LocalVarVisitor(this);
methodDeclaration.accept(localVarVisitor);
List<SM_LocalVar> lList = localVarVisitor.getLocalVarList();
if (lList.size() > 0) {
localVarList.addAll(lList);
}
}

//SM_LocalVar inherits SM_EntitiesWithType which inter uses an empty parse method. So, commenting this.
// private void parseLocalVar() {
// for (SM_LocalVar var : localVarList) {
// var.parse();
// }
// }

public String getMethodBody() {
if (this.hasBody())
Expand Down Expand Up @@ -145,32 +184,16 @@ public void printDebugLog(PrintWriter writer) {
//TODO: Modularize parser with private functions
@Override
public void parse() {
MethodInvVisitor invVisitor = new MethodInvVisitor(methodDeclaration);
methodDeclaration.accept(invVisitor);
List<MethodInvocation> invList = invVisitor.getCalledMethods();
if (invList.size() > 0) {
calledMethods.addAll(invList);
}
prepareCalledMethodsList();

List<SingleVariableDeclaration> variableList = methodDeclaration.parameters();
for (SingleVariableDeclaration var : variableList) {
VariableVisitor parameterVisitor = new VariableVisitor(this);
// methodDeclaration.accept(parameterVisitor);
var.accept(parameterVisitor);
List<SM_Parameter> pList = parameterVisitor.getParameterList();
if (pList.size() > 0) {
parameterList.addAll(pList);
}
parseParameters();
prepareParametersList(var);
// parseParameters();
}

LocalVarVisitor localVarVisitor = new LocalVarVisitor(this);
methodDeclaration.accept(localVarVisitor);
List<SM_LocalVar> lList = localVarVisitor.getLocalVarList();
if (lList.size() > 0) {
localVarList.addAll(lList);
}
parseLocalVar();
prepareLocalVarList();
// parseLocalVar();

DirectAceessFieldVisitor directAceessFieldVisitor = new DirectAceessFieldVisitor();
methodDeclaration.accept(directAceessFieldVisitor);
Expand All @@ -182,14 +205,8 @@ public void parse() {
if (thisAccesses.size() > 0) {
thisAccessesInMethod.addAll(thisAccesses);
}

InstanceOfVisitor instanceOfVisitor = new InstanceOfVisitor();
methodDeclaration.accept(instanceOfVisitor);
List<Type> instanceOfTypes = instanceOfVisitor.getTypesInInstanceOf();
if (instanceOfTypes.size() > 0) {
typesInInstanceOf.addAll(instanceOfTypes);
}

prepareInstanceOfVisitorList();

ThrowVisitor throwVisithor = new ThrowVisitor();
methodDeclaration.accept(throwVisithor);
throwsException = throwVisithor.throwsException();
Expand Down
6 changes: 3 additions & 3 deletions src/Designite/SourceModel/SM_Package.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import Designite.metrics.TypeMetricsExtractor;
import org.eclipse.jdt.core.dom.CompilationUnit;

import Designite.InputArgs;
Expand All @@ -17,7 +18,7 @@
import Designite.utils.Constants;
import Designite.utils.models.Edge;

public class SM_Package extends SM_SourceItem {
public class SM_Package extends SM_SourceItem implements Parsable{
private List<CompilationUnit> compilationUnitList;
private List<SM_Type> typeList = new ArrayList<>();
private SM_Project parentProject;
Expand Down Expand Up @@ -117,8 +118,7 @@ public void resolve() {
public void extractTypeMetrics() {
for (SM_Type type : typeList) {
type.extractMethodMetrics();
TypeMetrics metrics = new TypeMetrics(type);
metrics.extractMetrics();
TypeMetrics metrics = new TypeMetricsExtractor(type).extractMetrics();
metricsMapping.put(type, metrics);
exportMetricsToCSV(metrics, type.getName());
updateDependencyGraph(type);
Expand Down
8 changes: 4 additions & 4 deletions src/Designite/SourceModel/SM_Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public String toString() {
+ ", is=" + getTypeBinding().getNodeType();
}

@Override
public void parse() {
}
// @Override
// public void parse() {
//
// }

}
2 changes: 1 addition & 1 deletion src/Designite/SourceModel/SM_Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import Designite.utils.Logger;
import Designite.utils.models.Graph;

public class SM_Project extends SM_SourceItem {
public class SM_Project extends SM_SourceItem implements Parsable {

private InputArgs inputArgs;
private List<String> sourceFileList;
Expand Down
2 changes: 1 addition & 1 deletion src/Designite/SourceModel/SM_SourceItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class SM_SourceItem {
/**
* This is the first pass of parsing a source code entity.
*/
public abstract void parse();
// public abstract void parse();

/**
* This method establishes relationships among source-code entities. Such
Expand Down
19 changes: 10 additions & 9 deletions src/Designite/SourceModel/SM_Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.Map;

import Designite.metrics.MethodMetricsExtractor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
Expand All @@ -25,7 +26,7 @@
import Designite.visitors.StaticFieldAccessVisitor;

//TODO check EnumDeclaration, AnnotationTypeDeclaration and nested classes
public class SM_Type extends SM_SourceItem implements Vertex {
public class SM_Type extends SM_SourceItem implements Vertex, Parsable {


private boolean isAbstract = false;
Expand Down Expand Up @@ -221,11 +222,12 @@ private void parseMethods() {
}
}

private void parseFields() {
for (SM_Field field : fieldList) {
field.parse();
}
}
//SM_Field inherits SM_EntitiesWithType which inter uses an empty parse method. So, commenting this.
// private void parseFields() {
// for (SM_Field field : fieldList) {
// field.parse();
// }
// }

@Override
public void printDebugLog(PrintWriter writer) {
Expand Down Expand Up @@ -263,7 +265,7 @@ public void parse() {
List<SM_Field> fList = fieldVisitor.getFields();
if (fList.size() > 0)
fieldList.addAll(fList);
parseFields();
// parseFields();

StaticFieldAccessVisitor fieldAccessVisitor = new StaticFieldAccessVisitor();
typeDeclaration.accept(fieldAccessVisitor);
Expand Down Expand Up @@ -349,8 +351,7 @@ private void addUniqueReference(SM_Type type, SM_Type typeToAdd, boolean invardR

public void extractMethodMetrics() {
for (SM_Method method : methodList) {
MethodMetrics metrics = new MethodMetrics(method);
metrics.extractMetrics();
MethodMetrics metrics = new MethodMetricsExtractor(method).extractMetrics();
metricsMapping.put(method, metrics);
exportMethodMetricsToCSV(metrics, method.getName());
}
Expand Down
4 changes: 3 additions & 1 deletion src/Designite/SourceModel/TypeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class TypeInfo {
private boolean parametrizedType;
private List<SM_Type> nonPrimitiveTypeParameters = new ArrayList<>();

private static final int COMMA_LENGTH = 2;

public SM_Type getTypeObj() {
return typeObj;
}
Expand Down Expand Up @@ -56,7 +58,7 @@ public String getStringOfNonPrimitiveParameters() {
}

private String removeLastComma(String str) {
return (str.length() > 2) ? str.substring(0, str.length() - 2) : str;
return (str.length() > COMMA_LENGTH) ? str.substring(0, str.length() - COMMA_LENGTH) : str;
}

public int getNumOfNonPrimitiveParameters() {
Expand Down
76 changes: 23 additions & 53 deletions src/Designite/metrics/MethodMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,14 @@
import Designite.SourceModel.SM_Field;
import Designite.SourceModel.SM_Method;
import Designite.SourceModel.SM_Type;
import Designite.visitors.MethodControlFlowVisitor;

public class MethodMetrics implements MetricExtractor {
public class MethodMetrics extends Metrics {

private int numOfParameters;
private int cyclomaticComplexity;
private int numOfLines;

private SM_Method method;

public MethodMetrics(SM_Method method) {
this.method = method;
}

@Override
public void extractMetrics() {
extractNumOfParametersMetrics();
extractCyclomaticComplexity();
extractNumberOfLines();
}

private void extractNumOfParametersMetrics() {
numOfParameters = method.getParameterList().size();
}

private void extractCyclomaticComplexity() {
cyclomaticComplexity = calculateCyclomaticComplexity();
}

private int calculateCyclomaticComplexity() {
MethodControlFlowVisitor visitor = new MethodControlFlowVisitor();
method.getMethodDeclaration().accept(visitor);
return visitor.getNumOfIfStatements()
+ visitor.getNumOfSwitchCaseStatementsWitoutDefault()
+ visitor.getNumOfForStatements()
+ visitor.getNumOfWhileStatements()
+ visitor.getNumOfDoStatements()
+ visitor.getNumOfForeachStatements()
+ 1;
}

private void extractNumberOfLines() {
if (methodHasBody()) {
String body = method.getMethodDeclaration().getBody().toString();
int length = body.length();
// long newlines = body.lines().count();
numOfLines = length - body.replace("\n", "").length();
}
}

private boolean methodHasBody() {
return method.getMethodDeclaration().getBody() != null;
}


public int getNumOfParameters() {
return numOfParameters;
}
Expand All @@ -71,16 +25,32 @@ public int getNumOfLines() {
return numOfLines;
}

public void setNumOfParameters(int numOfParameters) {
this.numOfParameters = numOfParameters;
}

public void setCyclomaticComplexity(int cyclomaticComplexity) {
this.cyclomaticComplexity = cyclomaticComplexity;
}

public void setNumOfLines(int numOfLines) {
this.numOfLines = numOfLines;
}

public void setMethod(SM_Method method){
this.method = method;
}

public SM_Method getMethod() {
return method;
}

public List<SM_Field> getDirectFieldAccesses() {
return method.getDirectFieldAccesses();
}

public List<SM_Type> getSMTypesInInstanceOf() {
return method.getSMTypesInInstanceOf();
}

public SM_Method getMethod() {
return method;
}

}
Loading
Loading