Java反射调用kotlin中的类,Object类,Companion对象

Java反射调用kotlin中的类,Object类,Companion对象

1. Java反射调用kotlin中的普通类

kotlin普通类:

package com.common;
class TestNormal {
    fun get():String{
        return "Nolmal abc"
    }
    fun showNum(v:Int){
        println("Nolmal showNum:" + v)
    }
}

通过Java反射调用:

public void testNormal() {
        try {
            Class c = Class.forName("com.common.TestNormal");
            Object ins = c.newInstance();
            System.out.println("object:" + ins);

            Method meth1 = c.getDeclaredMethod("get");
            Object r1 = meth1.invoke(ins);
            System.out.println("method1: " + meth1);
            System.out.println("r1:"+r1+", type: " + r1.getClass());

            Method meth2 = c.getDeclaredMethod("showNum", int.class);
            Object r2 = meth2.invoke(ins, 10086);
            System.out.println("meth2: " + meth2);
            System.out.println("r2: " + r2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2. Java反射调用kotlin中的Object类

kotlin普通类:

package com.common;
object TestRef {
    fun get():String{
        return "TestRef abc"
    }
    fun showNum(v:Int){
        println("TestRef showNum:" + v)
    }
}

通过Java反射调用:

public void testObject() {
        try {
            Class c = Class.forName("com.common.TestRef");

            Field insField = c.getDeclaredField("INSTANCE");
            Object ins = insField.get(null);
            System.out.println("object:" + ins);

            Method meth1 = c.getDeclaredMethod("get");
            Object r1 = meth1.invoke(ins);
            System.out.println("method1: " + meth1);
            System.out.println("r1:"+r1+", type: " + r1.getClass());

            Method meth2 = c.getDeclaredMethod("showNum", int.class);
            Object r2 = meth2.invoke(ins, 10086);
            System.out.println("meth2: " + meth2);
            System.out.println("r2: " + r2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3. Java反射调用kotlin中的Companion对象

kotlin普通类:

package com.common;
class TestCom {
    companion object {
        fun get():String{
            return "TestCom abc"
        }
        fun showNum(v:Int){
            println("TestCom showNum:" + v)
        }
    }
}

通过Java反射调用:

public void testCompanion() {
        try {
            Class c = Class.forName("com.common.TestCom$Companion");

            Object ins = Class.forName("com.common.TestCom").getField("Companion").get(null);

            Method meth1 = c.getDeclaredMethod("get");
            Object r1 = meth1.invoke(ins);
            System.out.println("method1: " + meth1);
            System.out.println("r1:"+r1+", type: " + r1.getClass());

            Method meth2 = c.getDeclaredMethod("showNum", int.class);
            Object r2 = meth2.invoke(ins, 10086);
            System.out.println("meth2: " + meth2);
            System.out.println("r2: " + r2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Kotlin,Android,java,kotlin)