Skip to content

Commit

Permalink
Eliminate duplication and feature envy
Browse files Browse the repository at this point in the history
  • Loading branch information
tsantalis committed Jul 4, 2024
1 parent d179eaa commit aa368b3
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 61 deletions.
19 changes: 1 addition & 18 deletions src/main/java/org/codetracker/BlockTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,24 +339,7 @@ public History.HistoryInfo<Block> blame() throws Exception {
if (startBlock == null) {
throw new CodeElementNotFoundException(filePath, changeHistory.getBlockType().getName(), changeHistory.getBlockStartLineNumber());
}
if (startBlock.getComposite() instanceof TryStatementObject) {
TryStatementObject tryStatement = (TryStatementObject)startBlock.getComposite();
if (tryStatement.getCatchClauses().size() > 0) {
CompositeStatementObject catchClause = tryStatement.getCatchClauses().get(0);
if (catchClause.getLocationInfo().getStartLine() == blameLineNumber || catchClause.getLocationInfo().getStartLine() == blameLineNumber + 1) {
startBlock.setClosingCurlyBracket(true);
}
}
else if (tryStatement.getFinallyClause() != null) {
CompositeStatementObject finnalyClause = tryStatement.getFinallyClause();
if (finnalyClause.getLocationInfo().getStartLine() == blameLineNumber || finnalyClause.getLocationInfo().getStartLine() == blameLineNumber + 1) {
startBlock.setClosingCurlyBracket(true);
}
}
}
if (startBlock.getLocation().getEndLine() == blameLineNumber && startBlock.getComposite() instanceof CompositeStatementObject) {
startBlock.setClosingCurlyBracket(true);
}
startBlock.checkClosingBracket(blameLineNumber);
changeHistory.get().addNode(startBlock);

ArrayDeque<Block> blocks = new ArrayDeque<>();
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/codetracker/ClassTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,7 @@ public HistoryInfo<Class> blame() throws Exception {
if (start == null) {
return null;
}
if (start.getLocation().getEndLine() == classDeclarationLineNumber) {
start.setClosingCurlyBracket(true);
}
start.checkClosingBracket(classDeclarationLineNumber);
start.setStart(true);
classChangeHistory.addNode(start);

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/codetracker/MethodTrackerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ public History.HistoryInfo<Method> blame() throws Exception {
if (start == null) {
throw new CodeElementNotFoundException(filePath, changeHistory.getMethodName(), changeHistory.getMethodDeclarationLineNumber());
}
if (start.getLocation().getEndLine() == this.changeHistory.getMethodDeclarationLineNumber()) {
start.setClosingCurlyBracket(true);
}
start.checkClosingBracket(changeHistory.getMethodDeclarationLineNumber());
changeHistory.get().addNode(start);

ArrayDeque<Method> methods = new ArrayDeque<>();
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/codetracker/element/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ public static Block of(AbstractStatement statement, Method method) {
return of((CompositeStatementObject) statement, method);
}

public void checkClosingBracket(int lineNumber) {
if (getComposite() instanceof TryStatementObject) {
TryStatementObject tryStatement = (TryStatementObject)getComposite();
if (tryStatement.getCatchClauses().size() > 0) {
CompositeStatementObject catchClause = tryStatement.getCatchClauses().get(0);
if (catchClause.getLocationInfo().getStartLine() == lineNumber || catchClause.getLocationInfo().getStartLine() == lineNumber + 1) {
setClosingCurlyBracket(true);
}
}
else if (tryStatement.getFinallyClause() != null) {
CompositeStatementObject finnalyClause = tryStatement.getFinallyClause();
if (finnalyClause.getLocationInfo().getStartLine() == lineNumber || finnalyClause.getLocationInfo().getStartLine() == lineNumber + 1) {
setClosingCurlyBracket(true);
}
}
}
if (getLocation().getEndLine() == lineNumber && getComposite() instanceof CompositeStatementObject) {
setClosingCurlyBracket(true);
}
}

@Override
public LocationInfo getLocation() {
return composite.getLocationInfo();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/codetracker/element/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public UMLAbstractClass getUmlClass() {
return umlClass;
}

public void checkClosingBracket(int lineNumber) {
if (getLocation().getEndLine() == lineNumber) {
setClosingCurlyBracket(true);
}
}

@Override
public LocationInfo getLocation() {
return umlClass.getLocationInfo();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/codetracker/element/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ public static String getDocumentsSha512(VariableDeclarationContainer info) {
return Util.getSHA512(info.getComments().stream().map(UMLComment::getText).collect(Collectors.joining(";")));
}

public void checkClosingBracket(int lineNumber) {
if (getLocation().getEndLine() == lineNumber) {
setClosingCurlyBracket(true);
}
}

@Override
public LocationInfo getLocation() {
return umlOperation.getLocationInfo();
Expand Down
41 changes: 4 additions & 37 deletions src/main/java/org/codetracker/util/AbstractCodeElementLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ protected CodeElement locateWithoutName(Version version, UMLModel umlModel) thro
else {
block = method.findBlockWithoutName(this::endLineBlockPredicate);
if (block != null) {
checkClosingBracket(block);
block.checkClosingBracket(lineNumber);
return block;
}
}
Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName);
if (attribute != null) {
return attribute;
}
checkClosingBracket(method);
method.checkClosingBracket(lineNumber);
return method;
}
Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName);
Expand All @@ -224,50 +224,17 @@ protected CodeElement locateWithoutName(Version version, UMLModel umlModel) thro
else {
block = attribute.findBlockWithoutName(this::endLineBlockPredicate);
if (block != null) {
checkClosingBracket(block);
block.checkClosingBracket(lineNumber);
return block;
}
}
return attribute;
}
Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithoutName);
if (clazz != null) {
checkClosingBracket(clazz);
clazz.checkClosingBracket(lineNumber);
return clazz;
}
throw new CodeElementNotFoundException(filePath, name, lineNumber);
}

private void checkClosingBracket(Class clazz) {
if (clazz.getLocation().getEndLine() == lineNumber) {
clazz.setClosingCurlyBracket(true);
}
}

private void checkClosingBracket(Method method) {
if (method.getLocation().getEndLine() == lineNumber) {
method.setClosingCurlyBracket(true);
}
}

private void checkClosingBracket(Block block) {
if (block.getComposite() instanceof TryStatementObject) {
TryStatementObject tryStatement = (TryStatementObject)block.getComposite();
if (tryStatement.getCatchClauses().size() > 0) {
CompositeStatementObject catchClause = tryStatement.getCatchClauses().get(0);
if (catchClause.getLocationInfo().getStartLine() == lineNumber || catchClause.getLocationInfo().getStartLine() == lineNumber + 1) {
block.setClosingCurlyBracket(true);
}
}
else if (tryStatement.getFinallyClause() != null) {
CompositeStatementObject finnalyClause = tryStatement.getFinallyClause();
if (finnalyClause.getLocationInfo().getStartLine() == lineNumber || finnalyClause.getLocationInfo().getStartLine() == lineNumber + 1) {
block.setClosingCurlyBracket(true);
}
}
}
if (block.getLocation().getEndLine() == lineNumber && block.getComposite() instanceof CompositeStatementObject) {
block.setClosingCurlyBracket(true);
}
}
}

0 comments on commit aa368b3

Please sign in to comment.