

We can also get more details about the method itself, in the same way, that we can for classes. This is known as being a Callable Reference: val str = "Hello" Once we have obtained a method reference, we can call it as if was really the method in question. However, in Kotlin this method reference can also be used to get reflection information about the target.

This looks exactly the same as in Java 8 to obtain a method reference, and we can use it in exactly the same way. In exactly the same way as with classes, we can obtain a reference to a Method or Property using the :: operator.
#Java reflection instantiate class by name code
As before, this works equally well on code written in standard Java as it does on code written in Kotlin. This includes class properties – defined with val or var, standard class methods, and top-level functions. In a very similar way, we can get access to all of the methods, properties, extensions, and other members of the class: val bigDecimalClass = BigDecimal::class These are all Method references as discussed in the next section. We can create new instances of a class from a Class Reference as well, in much the same way as in Java: val listClass = ArrayList::classĪlternatively, we can access the constructors and use an explicit one if we need to. Println(TestObject::class.objectInstance) Println(TestWithCompanion::panionObjectInstance) Kotlin adds to this the ability to obtain the Companion Object for an arbitrary class, and the Object instance for an Object class: println(TestWithCompanion::panionObject)

In Java, we can already move from a Class to its superclass, interfaces and the outer class it’s enclosed in – if appropriate. We also have ways to move around the class hierarchy. Some of these are standard Java concepts, and others are Kotlin specific concepts.įor example, we can easily find out if a Class is Abstract or Final, but we can also find out if the Class is a Data Class or a Companion Class: val stringClass = String::classĪssertEquals("kotlin.String", stringClass.qualifiedName) Once we have obtained a KClass object, there are some simple things that it can tell us about the class in question. If (f.getType().equals(Target.Val kotlinClass: KClass = someClass.kotlin (get_is method is public in Target class) for(int i = 0 i classType = Class.forName(f.getType().getName())

Please look at working code below for both options cast and reflection. In addition if you'd like to use reflection you have to declare get_is() method as public in your Target class You have to use this: Class classType = Class.forName(f.getType().getName()) Your problem is in here `Class classType = Class.forName(f.getName()) ' Of course, for that you have to know your class ahead before compile. You also can cast that newInstance to the desired class and then call method directly. You have to have instantiate your class and then you will be able to call needed method through reflection. As long as your get_is() is non-static you cannot call it from class even through reflection.You have to use reflection to call a method. object (you get it by calling Class.forName) does not have a method get_is().here I try to call method of the selected class The console return Unhandled exception type ClassNotFoundException import įield f = this.getClass().getDeclaredFields() įor(int i = 0 i classType = Class.forName(f.getName()) I make this code but it's a fail, when I pass the name by method forName() I need to do that because all my methods is used everywhere in my code and I need to find information from those in a single place. My first state it's find a good class, I manage that but after I find no possibility to instantiate from the class name. I try to instantiate class from a class name but I fail, my code is in Processing / Java Library.
