Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin kotlinClass.class.getName() cannot return package name but only simple class name

AClass.class.getName();

if AClass is a java class, this method will return package name and class name. but when i convert AClass java file to Kotlin file ,it will only return a class name. so system cannot find this class path

the code above

like image 982
叶钦富 Avatar asked Jun 14 '17 03:06

叶钦富


1 Answers

there are many ways to get the full qualified name of a java Class in kotlin:

get name via the property KClass.qualifiedName:

val name = AClass::class.qualifiedName; 

OR get name via the property Class.name:

val name = AClass::class.java.name; 

OR get name via the method Class#getName:

val name = AClass::class.java.getName(); 

the table of the qualified name of a class as below:

|-----------------------|-----------------------|-----------------------| |                       |          Class        |     Anonymous Class   | |-----------------------|-----------------------|-----------------------| | KClass.qualifiedName  |    foo.bar.AClass     |         null          | |-----------------------|-----------------------|-----------------------| | Class.name            |    foo.bar.AClass     |    foo.bar.AClass$1   | |-----------------------|-----------------------|-----------------------| | Class.getName()       |    foo.bar.AClass     |    foo.bar.AClass$1   | |-----------------------|-----------------------|-----------------------| 
like image 58
holi-java Avatar answered Sep 19 '22 08:09

holi-java