Encountered when converting files into java file konlit pit, record it.
Java code:
Method forName = Class.class.getDeclaredMethod("forName", String.class);
Method getDeclaredMethod = Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);
Kotlin code:
Of course, with Android studio conversion more convenient, of course, did not turn quality assurance, basically you need to manually re-edit:
Android Studio -> Code -> Convert For Kotlin to the Java File File ->
false results after the conversion are as follows:
val forName = Class<*>::class.java.getDeclaredMethod("forName", String::class.java)
val getDeclaredMethod = Class<*>::class.java.getDeclaredMethod("getDeclaredMethod", String::class.java, Array<Class<*>>::class.java )
Here 'String' refers to the 'kotlin.String', but then we need to use reflection is certainly 'java.lang.String', so this is a pit.
Here the "Class < > :: class.java" will direct error: "only classes are allowed on the left hand side of a class literal"
It should be changed to "Class :: class.java", Ye Hao solve this pit.
The key is "the Array <Class < >> :: class.java" pit somewhat complex, where the 'Array' is 'kotlin.Array', Java in "Class []. Class" is a Java array type, corresponding to the kotlin is "arrayOf <Class <* >> ( ) :: class.java".
Finally, the correct result modified as follows:
val forName = Class::class.java.getDeclaredMethod("forName", java.lang.String::class.java)
val getDeclaredMethod = Class::class.java.getDeclaredMethod("getDeclaredMethod", java.lang.String::class.java, arrayOf<Class<*>>()::class.java)
Reproduced in: https: //www.jianshu.com/p/12eaf9ef69a2