diff --git a/src/main/java/analyzer/exercises/leap/DoNotUseJavaTime.java b/src/main/java/analyzer/exercises/leap/AvoidBuiltInMethods.java
similarity index 63%
rename from src/main/java/analyzer/exercises/leap/DoNotUseJavaTime.java
rename to src/main/java/analyzer/exercises/leap/AvoidBuiltInMethods.java
index 376deaf9..e74faf8f 100644
--- a/src/main/java/analyzer/exercises/leap/DoNotUseJavaTime.java
+++ b/src/main/java/analyzer/exercises/leap/AvoidBuiltInMethods.java
@@ -4,12 +4,12 @@
import analyzer.CommentType;
/**
- * @see Markdown Template
+ * @see Markdown Template
*/
-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
diff --git a/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java b/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
index e3ebf684..b06b28ec 100644
--- a/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
+++ b/src/main/java/analyzer/exercises/leap/LeapAnalyzer.java
@@ -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;
@@ -14,6 +14,10 @@
public class LeapAnalyzer extends VoidVisitorAdapter implements Analyzer {
private static final Set TEST_CASES = Set.of(1960, 1996, 2000, 2400);
+ private static final Set DISALLOWED_IMPORTS = Set.of(
+ "java.time",
+ "java.util.GregorianCalendar"
+ );
private final Set intLiterals = new HashSet<>();
private Analysis analysis;
@@ -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);
diff --git a/src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java b/src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java
index fb2bb0c6..512cbc3c 100644
--- a/src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java
+++ b/src/test/java/analyzer/exercises/leap/LeapAnalyzerTest.java
@@ -21,7 +21,8 @@ protected Analyzer getAnalyzer() {
private static Stream 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()})
);
}
diff --git a/src/test/resources/analyzer/exercises/leap/UsesGregorianCalendar.java b/src/test/resources/analyzer/exercises/leap/UsesGregorianCalendar.java
new file mode 100644
index 00000000..8f5c80b1
--- /dev/null
+++ b/src/test/resources/analyzer/exercises/leap/UsesGregorianCalendar.java
@@ -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);
+ }
+}
\ No newline at end of file