Skip to content

Commit

Permalink
Add java.util.Calendar to DenyListedApiDetector (#351)
Browse files Browse the repository at this point in the history
* Improve code coverage of DenyListedApiDetector

* Add deny list entries for java.util.Calendar

* Improve deny list error messages for java.util.Calendar
  • Loading branch information
jbduncan authored Feb 17, 2025
1 parent 7cb36a6 commit a29179f
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ internal class DenyListedApiDetector : Detector(), SourceCodeScanner, XmlScanner
errorMessage =
"Use java.time.Instant or java.time.ZonedDateTime instead. There is no reason to use java.util.Date in Java 8+.",
),
DenyListedEntry(
className = "java.util.Calendar",
fieldName = MatchAll,
errorMessage =
"Use java.time.Instant or java.time.ZonedDateTime instead. There is no reason to use java.util.Calendar in Java 8+.",
),
DenyListedEntry(
className = "java.util.Calendar",
functionName = MatchAll,
errorMessage =
"Use java.time.Instant or java.time.ZonedDateTime instead. There is no reason to use java.util.Calendar in Java 8+.",
),
DenyListedEntry(
className = "java.text.DateFormat",
fieldName = MatchAll,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,209 @@ class DenyListedApiDetectorTest : BaseSlackLintTest() {
)
}

@Test
fun javaUtilDate() {
lint()
.files(
kotlin(
"""
package foo
import java.util.Date
class SomeClass {
val now = Date()
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.Instant or java.time.ZonedDateTime instead. There is no reason to use java.util.Date in Java 8+. [DenyListedApi]
val now = Date()
~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun javaTextDateFormatField() {
lint()
.files(
kotlin(
"""
package foo
import java.text.DateFormat
class SomeClass {
val yearField = DateFormat.YEAR_FIELD
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.DateTimeFormatter instead. There is no reason to use java.text.DateFormat in Java 8+. [DenyListedApi]
val yearField = DateFormat.YEAR_FIELD
~~~~~~~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun javaTextDateFormatFunction() {
lint()
.files(
kotlin(
"""
package foo
import java.text.DateFormat
class SomeClass {
val dateFormat = DateFormat.getDateInstance()
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.DateTimeFormatter instead. There is no reason to use java.text.DateFormat in Java 8+. [DenyListedApi]
val dateFormat = DateFormat.getDateInstance()
~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun javaTextSimpleDateFormatField() {
lint()
.files(
kotlin(
"""
package foo
import java.text.SimpleDateFormat
class SomeClass {
val yearField = SimpleDateFormat.YEAR_FIELD
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.DateTimeFormatter instead. There is no reason to use java.text.DateFormat in Java 8+. [DenyListedApi]
val yearField = SimpleDateFormat.YEAR_FIELD
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun javaTextSimpleDateFormatFunction() {
lint()
.files(
kotlin(
"""
package foo
import java.text.SimpleDateFormat
class SomeClass {
val dateFormat = SimpleDateFormat.getDateInstance()
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.DateTimeFormatter instead. There is no reason to use java.text.DateFormat in Java 8+. [DenyListedApi]
val dateFormat = SimpleDateFormat.getDateInstance()
~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun javaUtilCalendarField() {
lint()
.files(
kotlin(
"""
package foo
import java.util.Calendar
class SomeClass {
val hourOfDay = Calendar.HOUR_OF_DAY
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.Instant or java.time.ZonedDateTime instead. There is no reason to use java.util.Calendar in Java 8+. [DenyListedApi]
val hourOfDay = Calendar.HOUR_OF_DAY
~~~~~~~~~~~~~~~~~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun javaUtilCalendarFunction() {
lint()
.files(
kotlin(
"""
package foo
import java.util.Calendar
class SomeClass {
val calendar = Calendar.getInstance()
}
"""
)
.indented()
)
.run()
.expect(
"""
src/foo/SomeClass.kt:6: Error: Use java.time.Instant or java.time.ZonedDateTime instead. There is no reason to use java.util.Calendar in Java 8+. [DenyListedApi]
val calendar = Calendar.getInstance()
~~~~~~~~~~~
1 errors, 0 warnings
"""
.trimIndent()
)
}

@Test
fun rxCompletableParameterless() {
lint()
Expand Down

0 comments on commit a29179f

Please sign in to comment.