-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global analyzer to catch common mistakes
- Loading branch information
1 parent
c7c64e1
commit 42f0f67
Showing
10 changed files
with
201 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/avoid_print_statements.md">Markdown Template</a> | ||
*/ | ||
public class AvoidPrintStatements extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.general.avoid_print_statements"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.INFORMATIVE; | ||
} | ||
} |
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,19 @@ | ||
package analyzer.comments; | ||
|
||
import analyzer.Comment; | ||
import analyzer.CommentType; | ||
|
||
/** | ||
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/general/do_not_use_main_method.md">Markdown Template</a> | ||
*/ | ||
public class DoNotUseMainMethod extends Comment { | ||
@Override | ||
public String getKey() { | ||
return "java.general.do_not_use_main_method"; | ||
} | ||
|
||
@Override | ||
public CommentType getType() { | ||
return CommentType.ESSENTIAL; | ||
} | ||
} |
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,52 @@ | ||
package analyzer.exercises; | ||
|
||
import analyzer.Analysis; | ||
import analyzer.Analyzer; | ||
import analyzer.comments.AvoidPrintStatements; | ||
import analyzer.comments.DoNotUseMainMethod; | ||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; | ||
|
||
import java.util.List; | ||
|
||
public class GlobalAnalyzer extends VoidVisitorAdapter<Void> implements Analyzer { | ||
private Analysis analysis; | ||
|
||
@Override | ||
public void analyze(List<CompilationUnit> compilationUnits, Analysis analysis) { | ||
this.analysis = analysis; | ||
for (CompilationUnit compilationUnit : compilationUnits) { | ||
compilationUnit.accept(this, null); | ||
} | ||
} | ||
|
||
@Override | ||
public void visit(MethodDeclaration n, Void arg) { | ||
if (isMainMethod(n)) { | ||
analysis.addComment(new DoNotUseMainMethod()); | ||
} | ||
|
||
super.visit(n, arg); | ||
} | ||
|
||
@Override | ||
public void visit(MethodCallExpr n, Void arg) { | ||
if (isPrintStatement(n)) { | ||
analysis.addComment(new AvoidPrintStatements()); | ||
} | ||
|
||
super.visit(n, arg); | ||
} | ||
|
||
private static boolean isMainMethod(MethodDeclaration node) { | ||
return node.getNameAsString().equals("main") && node.isStatic() && node.getType().isVoidType(); | ||
} | ||
|
||
private static boolean isPrintStatement(MethodCallExpr node) { | ||
return node.getScope() | ||
.map(scope -> scope.toString().matches("System\\.(?:out|err)")) | ||
.orElse(false); | ||
} | ||
} |
12 changes: 10 additions & 2 deletions
12
...t/java/analyzer/ExerciseAnalyzerTest.java → src/test/java/analyzer/AnalyzerTest.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
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,79 @@ | ||
package analyzer.exercises; | ||
|
||
import analyzer.Analyzer; | ||
import analyzer.AnalyzerTest; | ||
import analyzer.comments.AvoidPrintStatements; | ||
import analyzer.comments.DoNotUseMainMethod; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class GlobalAnalyzerTest extends AnalyzerTest { | ||
@Override | ||
protected Analyzer getAnalyzer() { | ||
return new GlobalAnalyzer(); | ||
} | ||
|
||
@MethodSource | ||
@ParameterizedTest | ||
public void solutionsWithMainMethod(String solution) { | ||
var actual = analyzeString(solution); | ||
assertThat(actual.getComments()).contains(new DoNotUseMainMethod()); | ||
} | ||
|
||
private static Stream<String> solutionsWithMainMethod() { | ||
return Stream.of( | ||
""" | ||
class Solution { | ||
public static void main(String... args) {} | ||
}""", | ||
""" | ||
class Solution { | ||
public static void main(String ...args) {} | ||
}""", | ||
""" | ||
class Solution { | ||
public static void main(String[] args) {} | ||
}""" | ||
); | ||
} | ||
|
||
@MethodSource | ||
@ParameterizedTest | ||
public void solutionsWithPrintStatements(String solution) { | ||
var actual = analyzeString(solution); | ||
assertThat(actual.getComments()).contains(new AvoidPrintStatements()); | ||
} | ||
|
||
private static Stream<String> solutionsWithPrintStatements() { | ||
return Stream.of( | ||
""" | ||
class Solution { | ||
void method() { | ||
System.out.println("Printing line to stdout"); | ||
} | ||
}""", | ||
""" | ||
class Solution { | ||
void method() { | ||
System.err.println("Printing line to stderr"); | ||
} | ||
}""", | ||
""" | ||
class Solution { | ||
void method() { | ||
System.out.print("Printing to stdout"); | ||
} | ||
}""", | ||
""" | ||
class Solution { | ||
void method() { | ||
System.err.print("Printing to stderr"); | ||
} | ||
}""" | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
{} | ||
{"comments": [ | ||
{ | ||
"comment": "java.general.do_not_use_main_method", | ||
"type": "essential" | ||
}, | ||
{ | ||
"comment": "java.general.avoid_print_statements", | ||
"type": "informative" | ||
} | ||
]} |
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