summaryrefslogtreecommitdiff
path: root/core/src/main/scala/chisel3/util
diff options
context:
space:
mode:
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
+ }
+}