Skip to content

Use ClassId name in prettifiedName if canonical name is null #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -642,14 +642,21 @@ open class ClassId(
open val simpleName: String get() = jClass.simpleName

/**
* For regular classes this is just a simple name
* For anonymous classes this includes the containing class and numeric indices of the anonymous class
* For regular classes this is just a simple name.
* For anonymous classes this includes the containing class and numeric indices of the anonymous class.
*
* Note: according to [java.lang.Class.getCanonicalName] documentation, local and anonymous classes
* do not have canonical names, as well as arrays whose elements don't have canonical classes themselves.
* In these cases prettified names are constructed using [ClassId.name] instead of [ClassId.canonicalName].
*/
val prettifiedName: String
get() = canonicalName
.substringAfterLast(".")
.replace(Regex("[^a-zA-Z0-9]"), "")
.let { if (this.isArray) it + "Array" else it }
get() {
val className = jClass.canonicalName ?: name // Explicit jClass reference to get null instead of exception
return className
.substringAfterLast(".")
.replace(Regex("[^a-zA-Z0-9]"), "")
.let { if (this.isArray) it + "Array" else it }
}

open val packageName: String get() = jClass.`package`?.name ?: "" // empty package for primitives

Expand Down