-
Notifications
You must be signed in to change notification settings - Fork 16
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
Showing
3 changed files
with
83 additions
and
13 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
54 changes: 54 additions & 0 deletions
54
core/src/main/scala-3/org/wartremover/contrib/warts/OldTime.scala
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,54 @@ | ||
package org.wartremover.contrib.warts | ||
|
||
import org.wartremover.WartTraverser | ||
import org.wartremover.WartUniverse | ||
import scala.collection.mutable | ||
|
||
object OldTime extends WartTraverser { | ||
private[this] val oldJavaTime: Set[String] = Set( | ||
"java.util.Date", | ||
"java.util.Calendar", | ||
"java.util.GregorianCalendar", | ||
"java.util.TimeZone", | ||
"java.text.DateFormat", | ||
"java.text.SimpleDateFormat" | ||
) | ||
|
||
private[this] final case class LineInFile(content: Option[String], startLine: Int) | ||
|
||
private[this] def oldJavaMessage = "The old Java time API is disabled. Use Java 8 java.time._ API instead." | ||
private[this] def jodaMessage = "JodaTime is disabled. Use Java 8 java.time._ API instead." | ||
|
||
def apply(u: WartUniverse): u.Traverser = { | ||
val linesWithError = mutable.Set.empty[LineInFile] | ||
new u.Traverser(this) { | ||
import q.reflect.* | ||
|
||
def addError(pos: Position, message: String): Unit = { | ||
error(pos, message) | ||
linesWithError.add(LineInFile(pos.sourceFile.content, pos.startLine)) | ||
} | ||
|
||
def errorAlreadyExists(pos: Position): Boolean = { | ||
linesWithError.contains(LineInFile(pos.sourceFile.content, pos.startLine)) | ||
} | ||
|
||
override def traverseTree(tree: Tree)(owner: Symbol): Unit = { | ||
tree match { | ||
case _ if hasWartAnnotation(tree) => | ||
case _ if errorAlreadyExists(tree.pos) => | ||
case t: TypeTree if t.symbol.fullName.startsWith("org.joda.time") => | ||
addError(t.pos, jodaMessage) | ||
case t: TypeTree if oldJavaTime(t.symbol.fullName) => | ||
addError(t.pos, oldJavaMessage) | ||
case t: New if t.tpt.symbol.fullName.startsWith("org.joda.time") => | ||
addError(t.pos, jodaMessage) | ||
case t: New if oldJavaTime(t.tpt.symbol.fullName) => | ||
addError(t.pos, oldJavaMessage) | ||
case _ => | ||
super.traverseTree(tree)(owner) | ||
} | ||
} | ||
} | ||
} | ||
} |
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