-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22b4fa8
commit 9d8d64e
Showing
15 changed files
with
238 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...n/groovy/io/gitlab/arturbosch/smartsmells/smells/complexcondition/ComplexCondition.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.gitlab.arturbosch.smartsmells.smells.complexcondition | ||
|
||
import com.github.javaparser.ast.expr.Expression | ||
import com.github.javaparser.ast.stmt.Statement | ||
import groovy.transform.Immutable | ||
import groovy.transform.ToString | ||
import io.gitlab.arturbosch.jpal.ast.source.SourcePath | ||
import io.gitlab.arturbosch.jpal.ast.source.SourceRange | ||
import io.gitlab.arturbosch.smartsmells.smells.DetectionResult | ||
import io.gitlab.arturbosch.smartsmells.smells.ElementTarget | ||
import io.gitlab.arturbosch.smartsmells.smells.LocalSpecific | ||
|
||
/** | ||
* @author Artur Bosch | ||
*/ | ||
@Immutable | ||
@ToString(includePackage = false) | ||
class ComplexCondition implements DetectionResult, LocalSpecific { | ||
|
||
String inScope //ClassSignature#MethodSignature | ||
String signature | ||
|
||
@Delegate | ||
SourcePath sourcePath | ||
@Delegate | ||
SourceRange sourceRange | ||
|
||
ElementTarget elementTarget = ElementTarget.LOCAL | ||
|
||
@Override | ||
String name() { | ||
return name | ||
} | ||
|
||
@Override | ||
String signature() { | ||
return signature | ||
} | ||
|
||
@Override | ||
LocalSpecific copy(Statement statement) { | ||
return null | ||
} | ||
|
||
@Override | ||
LocalSpecific copy(Expression expression) { | ||
return null | ||
} | ||
|
||
@Override | ||
String asCompactString() { | ||
return null | ||
} | ||
|
||
@Override | ||
String asComparableString() { | ||
return null | ||
} | ||
|
||
@Override | ||
ElementTarget elementTarget() { | ||
return elementTarget | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../io/gitlab/arturbosch/smartsmells/smells/complexcondition/ComplexConditionDetector.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.gitlab.arturbosch.smartsmells.smells.complexcondition | ||
|
||
import groovy.transform.CompileStatic | ||
import io.gitlab.arturbosch.smartsmells.common.Detector | ||
import io.gitlab.arturbosch.smartsmells.common.Visitor | ||
import io.gitlab.arturbosch.smartsmells.config.Defaults | ||
import io.gitlab.arturbosch.smartsmells.config.Smell | ||
|
||
/** | ||
* @author Artur Bosch | ||
*/ | ||
@CompileStatic | ||
class ComplexConditionDetector extends Detector<ComplexCondition> { | ||
|
||
private int threshold | ||
|
||
ComplexConditionDetector(int threshold = Defaults.COMPLEX_CONDITION) { | ||
this.threshold = threshold | ||
} | ||
|
||
@Override | ||
protected Visitor getVisitor() { | ||
return new ComplexConditionVisitor(threshold) | ||
} | ||
|
||
@Override | ||
Smell getType() { | ||
return Smell.COMPLEX_CONDITION | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...y/io/gitlab/arturbosch/smartsmells/smells/complexcondition/ComplexConditionVisitor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.gitlab.arturbosch.smartsmells.smells.complexcondition | ||
|
||
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration | ||
import com.github.javaparser.ast.body.EnumDeclaration | ||
import com.github.javaparser.ast.expr.Expression | ||
import com.github.javaparser.ast.stmt.DoStmt | ||
import com.github.javaparser.ast.stmt.IfStmt | ||
import com.github.javaparser.ast.stmt.WhileStmt | ||
import groovy.transform.CompileStatic | ||
import io.gitlab.arturbosch.jpal.ast.ClassHelper | ||
import io.gitlab.arturbosch.jpal.ast.EnumHelper | ||
import io.gitlab.arturbosch.jpal.ast.NodeHelper | ||
import io.gitlab.arturbosch.jpal.ast.source.SourcePath | ||
import io.gitlab.arturbosch.jpal.ast.source.SourceRange | ||
import io.gitlab.arturbosch.jpal.internal.Printer | ||
import io.gitlab.arturbosch.jpal.resolution.Resolver | ||
import io.gitlab.arturbosch.smartsmells.common.Visitor | ||
import io.gitlab.arturbosch.smartsmells.smells.ElementTarget | ||
import io.gitlab.arturbosch.smartsmells.smells.statechecking.StateCheckingVisitor | ||
import io.gitlab.arturbosch.smartsmells.util.Strings | ||
|
||
/** | ||
* @author Artur Bosch | ||
*/ | ||
@CompileStatic | ||
class ComplexConditionVisitor extends Visitor<ComplexCondition> { | ||
|
||
private String currentClassName = "" | ||
private int threshold | ||
|
||
ComplexConditionVisitor(int threshold) { | ||
this.threshold = threshold | ||
} | ||
|
||
@Override | ||
void visit(ClassOrInterfaceDeclaration n, Resolver arg) { | ||
currentClassName = ClassHelper.createFullSignature(n) | ||
super.visit(n, arg) | ||
} | ||
|
||
@Override | ||
void visit(EnumDeclaration n, Resolver arg) { | ||
currentClassName = EnumHelper.createFullSignature(n) | ||
super.visit(n, arg) | ||
} | ||
|
||
@Override | ||
void visit(IfStmt n, Resolver arg) { | ||
checkCondition(n.condition) | ||
super.visit(n, arg) | ||
} | ||
|
||
private void checkCondition(Expression expression) { | ||
String condition = expression.toString(Printer.NO_COMMENTS) | ||
int cases = Strings.amountOf(condition, "&&") + Strings.amountOf(condition, "||") + 1 | ||
if (cases > threshold) { | ||
def methodName = NodeHelper.findDeclaringMethod(expression) | ||
.map { it.declarationAsString } | ||
.orElse(StateCheckingVisitor.UNKNOWN_METHOD) | ||
def scope = currentClassName + "#" + methodName | ||
smells.add(new ComplexCondition(scope, condition, SourcePath.of(info), SourceRange.fromNode(expression), ElementTarget.LOCAL)) | ||
} | ||
} | ||
|
||
@Override | ||
void visit(DoStmt n, Resolver arg) { | ||
checkCondition(n.condition) | ||
super.visit(n, arg) | ||
} | ||
|
||
@Override | ||
void visit(WhileStmt n, Resolver arg) { | ||
checkCondition(n.condition) | ||
super.visit(n, arg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/groovy/io/gitlab/arturbosch/smartsmells/java/ComplexConditionDummy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.gitlab.arturbosch.smartsmells.java; | ||
|
||
/** | ||
* @author Artur Bosch | ||
*/ | ||
@SuppressWarnings("ALL") | ||
public class ComplexConditionDummy { | ||
|
||
public void method() { | ||
if (5 > 4 && 4 < 6 || (3 < 5 || 2 < 5)) { | ||
System.out.println(); | ||
} | ||
while (5 > 4 && 4 < 6 || (3 < 5 || 2 < 5)) { | ||
break; | ||
} | ||
do { | ||
System.out.println(); | ||
} while (5 > 4 && 4 < 6 || (3 < 5 || 2 < 5)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...gitlab/arturbosch/smartsmells/smells/complexcondition/ComplexConditionDetectorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.gitlab.arturbosch.smartsmells.smells.complexcondition | ||
|
||
import io.gitlab.arturbosch.smartsmells.common.Test | ||
import spock.lang.Specification | ||
|
||
/** | ||
* @author Artur Bosch | ||
*/ | ||
class ComplexConditionDetectorTest extends Specification { | ||
|
||
def "find one complex condition"() { | ||
expect: | ||
smells.size() == 3 | ||
smells[0].signature == "5 > 4 && 4 < 6 || (3 < 5 || 2 < 5)" | ||
|
||
where: | ||
smells = new ComplexConditionDetector().run(Test.COMPLEX_CONDITION_DUMMY_PATH) | ||
} | ||
|
||
} |