美文网首页Android
Java反射常用API

Java反射常用API

作者: gybguohao | 来源:发表于2018-09-02 11:52 被阅读52次

    getMethods()

    getMethods()获取本类以及父类中所有public修饰符修饰的方法,包括本类和父类实现的接口以及抽象方法。
    代码示例如下:

    ReflectTest.java

    public class ReflectTest extends ParentTest implements InterfaceA, InterfaceB {
    
        private void privateMethod() {
        }
    
        protected void protectedMethod() {
        }
    
        void defaultMethod() {
        }
    
        public void publicMethod() {
        }
        
        public void invokeMethod(String message) {
            Log.e("gybguohao", "接受到的信息:" + message);
        }
        
        @Override
        public void interfaceAMethod() {
        }
    
        @Override
        public void interfaceBMethod() {
        }
    
        @Override
        public void parentAbstractMethod() {
        }
    
    }
    

    ParentTest.java

    public abstract class ParentTest implements InterfaceC {
    
        private void privateParentMethod() {
        }
    
        protected void protectedParentMethod() {
        }
    
        void defaultParentMethod() {
        }
    
        public void publicParentMethod() {
        }
    
        @Override
        public void interfaceCMethod() {
        }
    
        public abstract void parentAbstractMethod();
        
    }
    

    InterfaceA.java

    public interface InterfaceA {
    
        void interfaceAMethod();
    
    }
    

    InterfaceB.java

    public interface InterfaceB {
    
        void interfaceBMethod();
    
    }
    

    InterfaceC.java

    public interface InterfaceC {
    
        void interfaceCMethod();
    
    }
    

    MainActivity.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ReflectTest reflectTest = new ReflectTest();
            Method[] methods;
    
            methods = reflectTest.getClass().getMethods();
            for (Method method : methods) {
                Log.e("gybguohao", "ReflectTest类中public修饰的方法有:" + method);
            }
    
        }
    
    }
    

    打印结果如下:

    ReflectTest类中public修饰的方法有:public boolean java.lang.Object.equals(java.lang.Object)
    ReflectTest类中public修饰的方法有:public final java.lang.Class java.lang.Object.getClass()
    ReflectTest类中public修饰的方法有:public int java.lang.Object.hashCode()
     
    //  1  
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ReflectTest.interfaceAMethod()
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ReflectTest.interfaceBMethod()
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ParentTest.interfaceCMethod()
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ReflectTest.invokeMethod(java.lang.String)   
    
    ReflectTest类中public修饰的方法有:public final native void java.lang.Object.notify()
    ReflectTest类中public修饰的方法有:public final native void java.lang.Object.notifyAll()
    
    //  2    
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ReflectTest.parentAbstractMethod()
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ReflectTest.publicMethod()
    ReflectTest类中public修饰的方法有:public void com.gyb.reflect.ParentTest.publicParentMethod()
        
    ReflectTest类中public修饰的方法有:public java.lang.String java.lang.Object.toString()
    ReflectTest类中public修饰的方法有:public final native void java.lang.Object.wait() throws java.lang.InterruptedException
    ReflectTest类中public修饰的方法有:public final void java.lang.Object.wait(long) throws java.lang.InterruptedException
    ReflectTest类中public修饰的方法有:public final native void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    

    因为所有类都是object的子类,所有log里面包含了很多object类的公共方法。但通过打印结果可以看出所有方法都是public修饰。其中标注1和2的六个方法,正是ReflectTest类以及父类ParentTest所有的public方法(包括实现的接口和抽象方法)。

    getDeclaredMethods()

    getDeclaredMethods()返回本类中的所有private、protected、默认的、public修饰符修饰的方法,包括实现的接口和抽象方法。
    还是沿用上面的几个类,代码示例如下:
    MainActivity.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ReflectTest reflectTest = new ReflectTest();
            Method[] methods;
    
    //        methods = reflectTest.getClass().getMethods();
    //        for (Method method : methods) {
    //            Log.e("gybguohao", "ReflectTest类中public修饰的方法有:" + method);
    //        }
            
            methods = reflectTest.getClass().getDeclaredMethods();
            for (Method method : methods) {
                Log.e("gybguohao", "ReflectTest类中的方法有:" + method);
            }
    
        }
    
    }
    

    打印结果如下:

    ReflectTest类中的方法有:private void com.gyb.reflect.ReflectTest.privateMethod()
    ReflectTest类中的方法有:void com.gyb.reflect.ReflectTest.defaultMethod()
    ReflectTest类中的方法有:public void com.gyb.reflect.ReflectTest.interfaceAMethod()
    ReflectTest类中的方法有:public void com.gyb.reflect.ReflectTest.interfaceBMethod()
    ReflectTest类中的方法有:public void com.gyb.reflect.ReflectTest.parentAbstractMethod()
    ReflectTest类中的方法有:protected void com.gyb.reflect.ReflectTest.protectedMethod()
    ReflectTest类中的方法有:public void com.gyb.reflect.ReflectTest.publicMethod()
    ReflectTest类中的方法有:public void com.gyb.reflect.ReflectTest.invokeMethod(java.lang.String)
    

    getModifiers()

    getModifiers()返回一个int类型的数值,该数值表示类、变量、方法被哪个修饰符所修饰。
    修饰符对应的数值如下所示:

    修饰符 数值
    默认 0
    public 1
    private 2
    protected 4
    static 8
    final 16
    synchronized 32
    volatile 64
    transient 128
    native 256
    interface 512
    abstract 1024

    代码示例如下:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ReflectTest reflectTest = new ReflectTest();
            Method[] methods;
    
    //        methods = reflectTest.getClass().getMethods();
    //        for (Method method : methods) {
    //            Log.e("gybguohao", "ReflectTest类中public修饰的方法有:" + method);
    //        }
            
            methods = reflectTest.getClass().getDeclaredMethods();
            for (Method method : methods) {
    //            Log.e("gybguohao", "ReflectTest类中的方法有:" + method);
                Log.e("gybguohao", "ReflectTest类中方法的修饰符对应的数值:" + method + " " + method.getModifiers());
            }
    
        }
    
    }
    

    打印结果如下:

    ReflectTest类中方法的修饰符对应的数值:private void com.gyb.reflect.ReflectTest.privateMethod() 2
    ReflectTest类中方法的修饰符对应的数值:void com.gyb.reflect.ReflectTest.defaultMethod() 0
    ReflectTest类中方法的修饰符对应的数值:public void com.gyb.reflect.ReflectTest.interfaceAMethod() 1
    ReflectTest类中方法的修饰符对应的数值:public void com.gyb.reflect.ReflectTest.interfaceBMethod() 1
    ReflectTest类中方法的修饰符对应的数值:public void com.gyb.reflect.ReflectTest.parentAbstractMethod() 1
    ReflectTest类中方法的修饰符对应的数值:protected void com.gyb.reflect.ReflectTest.protectedMethod() 4
    ReflectTest类中方法的修饰符对应的数值:public void com.gyb.reflect.ReflectTest.publicMethod() 1
    ReflectTest类中方法的修饰符对应的数值:public void com.gyb.reflect.ReflectTest.invokeMethod(java.lang.String) 1
    

    在这里只验证了private、protected、默认的、public四个修饰符,其他的请自行验证。

    getParameterTypes()

    getParameterTypes()返回方法中所有的参数类型。例如:

    //  获取method方法中所含参数的参数类型数组。
    Class<?>[] parameterTypes = method.getParameterTypes();
    for (Class paramType : parameterTypes) {
    
        //  获取method方法中每个参数的参数类型
        Log.e("gybguohao", "param name = " + paramType.getName());
        
    }
    
    //  获取method方法中参数个数
    int paramCount = parameterTypes.length;
    

    getInterfaces()

    getInterfaces()返回某个类直接实现的接口。例如:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ReflectTest reflectTest = new ReflectTest();
            Class<?>[] interfaceType = reflectTest.getClass().getInterfaces();
            for (Class<?> type : interfaceType) {
                Log.e("gybguohao", "ReflectTest类实现的接口:" + type);
            }
    
        }
    
    }
    

    最后的打印结果是:

    type = interface com.gyb.reflect.InterfaceA
    type = interface com.gyb.reflect.InterfaceB
    

    getSuperClass()

    getSuperClass()返回某个类的直接父类,如果该类没有直接父类,那么返回null。

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ReflectTest reflectTest = new ReflectTest();
            Log.e("gybguohao", "" + reflectTest.getClass().getSuperclass());
    
        }
    
    }
    

    最后的打印结果是:

    class com.gyb.reflect.ParentTest
    

    invoke()

    invoke()通过反射执行一个不能直接被调用的方法,反射的方法必须是public修饰符修饰的。

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ReflectTest reflectTest = new ReflectTest();
            Class<?> clazz = reflectTest.getClass();
            try {
                //  第一个参数是方法名,第二个参数类型数组
                Method method = clazz.getMethod("invokeMethod", String.class);
                //  第一个参数是执行这个方法的对象,如果这个方法是static修饰的,可直接使用null,
                //  第二个是参数数组
                method.invoke(clazz.newInstance(),"通过反射传入的信息");
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    

    结果打印如下:

    接受到的信息:通过反射传入的信息
    

    相关文章

      网友评论

        本文标题:Java反射常用API

        本文链接:https://www.haomeiwen.com/subject/ekfmwftx.html