Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example
- import android.util.Log
- class ClassA {
- var iWantToUseThisFromTheInnerClass = "someValue"
- fun runThisToStart() {
- val classB = ClassB(InnerClassA::class.java)
- classB.doSomething()
- }
- inner class InnerClassA(text: String) {
- init {
- Log.d("ClassA", "Constructor invoked " + text)
- }
- }
- }
- package com.example
- import java.lang.reflect.InvocationTargetException
- class ClassB<T>(private var mModelClass: Class<T>) {
- val someText = "whatever"
- fun doSomething():T {
- try {
- val constructor = mModelClass.getConstructor(String::class.java)
- return constructor.newInstance(someText)
- } catch (e: NoSuchMethodException) { // Throws this exception
- throw RuntimeException(e)
- } catch (e: InvocationTargetException) {
- throw RuntimeException(e)
- } catch (e: InstantiationException) {
- throw RuntimeException(e)
- } catch (e: IllegalAccessException) {
- throw RuntimeException(e)
- }
- }
- }
Add Comment
Please, Sign In to add comment