Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public abstract class AbstractDuplicateCheck extends AbstractCheck {

protected int minComplexity = 50;

protected DetailAST inViolationAST = null;


/**
* Sets the minimum complexity of the check
Expand All @@ -44,6 +46,10 @@ public void visitToken(DetailAST ast) {
return;
}

if(inViolationAST != null) {
return;
}

int complexity = TreeUtils.astComplexity(ast);
if (complexity < minComplexity) {
return;
Expand All @@ -52,13 +58,21 @@ public void visitToken(DetailAST ast) {
for (DetailAST original : checkedAst.keySet()) {
if (astEquals(original, ast)) {
logViolation(ast, original);
inViolationAST = ast;
return;
}
}

checkedAst.put(ast, getFilePath());
}

@Override
public void leaveToken(DetailAST ast) {
if(inViolationAST == ast) {
inViolationAST = null;
}
super.leaveToken(ast);
}

/**
* Logs violations of duplicates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ public void should_FindErrors_when_DuplicateBlocks() throws CheckstyleException
testFiles(3, fileName);
}

@Test
@DisplayName("Should find the correct number of errors in code that has nested duplicate blocks")
public void should_FindErrors_when_NestedDuplicateBlocks() throws CheckstyleException {
String fileName = "testInputs/duplicateBlock/should_FindErrors_when_NestedDuplicateBlocks.java";
testFiles(2, fileName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class should_FindErrors_when_DuplicateBlocks {
public void hi() {
int i = 7;
int j = 4;
if(i > j) {
if (i <= j) {
System.out.println(i + j);
System.out.println(i - j);
System.out.println(j - i);
}
}
i = 2;
j = 0;
if(i > j) {
if (i <= j) {
System.out.println(i + j);
System.out.println(i - j);
System.out.println(j - i);
}
}
i = 17;
j = 17;
if(i > j) {
if (i <= j) {
System.out.println(i + j);
System.out.println(i - j);
System.out.println(j - i);
}
}
}
}