summaryrefslogtreecommitdiff
path: root/core/src/main/scala/chisel3/util
diff options
context:
space:
mode:
authorAditya Naik2024-08-02 15:20:25 -0700
committerAditya Naik2024-08-02 15:20:25 -0700
commit8947a5900e390c19524ab3013b33f0a8ec07ea43 (patch)
tree69426553c4a4155b30a3077c5c6b6e72d784e4e5 /core/src/main/scala/chisel3/util
parentb058bdcf8c142641aacd92f9938fb350a90e0762 (diff)
Update Module.scala
Diffstat (limited to 'core/src/main/scala/chisel3/util')
-rw-r--r--core/src/main/scala/chisel3/util/Naming.scala23
1 files changed, 23 insertions, 0 deletions
diff --git a/core/src/main/scala/chisel3/util/Naming.scala b/core/src/main/scala/chisel3/util/Naming.scala
new file mode 100644
index 00000000..a78744b2
--- /dev/null
+++ b/core/src/main/scala/chisel3/util/Naming.scala
@@ -0,0 +1,23 @@
+package chisel3.util
+
+/* Generates a safe 'simple class name' from the given class, avoiding `Malformed class name` exceptions from `getClass.getSimpleName`
+ * when Java 8 is used.
+ */
+object simpleClassName {
+
+ def apply[T](clazz: Class[T]): String = {
+ /* The default class name is derived from the Java reflection derived class name. */
+ val baseName = clazz.getName
+
+ /* A sequence of string filters applied to the name */
+ val filters: Seq[String => String] =
+ Seq(((a: String) => raw"\$$+anon".r.replaceAllIn(a, "_Anon")) // Merge the "$$anon" name with previous name
+ )
+
+ filters
+ .foldLeft(baseName) { case (str, filter) => filter(str) } // 1. Apply filters to baseName
+ .split("\\.|\\$") // 2. Split string at '.' or '$'
+ .filterNot(_.forall(_.isDigit)) // 3. Drop purely numeric names
+ .last // 4. Use the last name
+ }
+}