Skip to content

Commit

Permalink
Catch use of GregorianCalendar.isLeapYear
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma committed Jan 20, 2024
1 parent 1cd6abe commit 9178e66
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import analyzer.CommentType;

/**
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/do_not_use_java_time.md">Markdown Template</a>
* @see <a href="https://github.com/exercism/website-copy/blob/main/analyzer-comments/java/leap/avoid_built_in_methods.md">Markdown Template</a>
*/
class DoNotUseJavaTime extends Comment {
class AvoidBuiltInMethods extends Comment {
@Override
public String getKey() {
return "java.leap.do_not_use_java_time";
return "java.leap.avoid_built_in_methods";
}

@Override
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import analyzer.Analyzer;
import analyzer.comments.AvoidHardCodedTestCases;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;

import java.util.HashSet;
Expand All @@ -14,6 +14,10 @@

public class LeapAnalyzer extends VoidVisitorAdapter<Void> implements Analyzer {
private static final Set<Integer> TEST_CASES = Set.of(1960, 1996, 2000, 2400);
private static final Set<String> DISALLOWED_IMPORTS = Set.of(
"java.time",
"java.util.GregorianCalendar"
);

private final Set<Integer> intLiterals = new HashSet<>();
private Analysis analysis;
Expand All @@ -36,9 +40,10 @@ public void visit(CompilationUnit n, Void arg) {
}

@Override
public void visit(MethodCallExpr n, Void arg) {
if (n.toString().contains("Year.isLeap")) {
this.analysis.addComment(new DoNotUseJavaTime());
public void visit(ImportDeclaration n, Void arg) {
var name = n.getNameAsString();
if (DISALLOWED_IMPORTS.stream().anyMatch(name::contains)) {
this.analysis.addComment(new AvoidBuiltInMethods());
}

super.visit(n, arg);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ protected Analyzer getAnalyzer() {
private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("Optimal.java", new Comment[0]),
Arguments.of("UsesJavaTime.java", new Comment[]{new DoNotUseJavaTime()}),
Arguments.of("UsesJavaTime.java", new Comment[]{new AvoidBuiltInMethods()}),
Arguments.of("UsesGregorianCalendar.java", new Comment[]{new AvoidBuiltInMethods()}),
Arguments.of("HardCodedTestCases.java", new Comment[]{new AvoidHardCodedTestCases()})
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.util.Calendar;
import java.util.GregorianCalendar;

class Leap {
boolean isLeapYear(int year) {
var calendar = new GregorianCalendar(Calendar.ALL_STYLES);
return calendar.isLeapYear(year);
}
}

0 comments on commit 9178e66

Please sign in to comment.