• Java反射机制DOME


    Java反射机制
    public class TestHibernate {
        @Test
        public void TestHb(){
            try {
                Class cs = Class.forName("testHibernate.Test1");    //classload 类 
                Object oj = cs.newInstance();                        //通过类NEW一个对象
                Method[] mt = cs.getMethods();                        //获取所有方法
                for (Method method : mt) {                            //轮询出所有函数方法
                    if(method.getName()=="m13"){                    //通过函数名判断函数
                        method.invoke(oj,1,2);
                        for(Class parameter:method.getParameterTypes()){    //轮询出函数方法的参数
                        System.out.println(parameter.getName());            //获取参数类型
                        }
                        System.out.println("返回值"+method.getReturnType());    //获取返回值类型
                    }else if(method.getName()=="m12"){
                        method.invoke(oj);                            //执行函数方法无参
                    }else if(method.getName()=="getmeth"){
                        System.out.println("123"+method.getReturnType());
                        Object object = method.invoke(oj);            //获取函数方法返回的类对象
                        Class ccc = object.getClass();                //通过类对象获得类
                        Field ff = ccc.getField("x2");             //获取变量对象
                         int x1 = (int)ff.get(object);                //获取属性值
                        //getDeclaredMethod可获取私有方法
                        Method met = ccc.getDeclaredMethod("xxx",new Class[]{int.class,int.class});
                        
                        met.setAccessible(true);//设置可成 可访问
                        Object i = met.invoke(object, x1,2);//调用带参函数方法
                        System.out.println(i+"xxxxxxxxxxxxxxxxxxxxxxxxxx");
                    }
                    
                }    
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | NoSuchMethodException | NoSuchFieldException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
    }
    class Test2{
        static{
            System.out.println("classLoad  test2");
        }
        public int x2 = 3;
        private int xxx(int x1,int x2){
            return x1*x2;
        }
    }
    class Test1{
        static{
            System.out.println("classLoad  test1");
        }
        {
            System.out.println("new objcet  tset1");
        }
        
        public Test2 getmeth(){
            return new Test2();
        }
        public void m12(){
            System.out.println("m12");
        }
        public String m13(int b,int r){
            System.out.println(b+r);
            return b+r+"";
        }
    }

    本dome为学习测试使用的

    Java反射机制

    public class TestHibernate {

    @Test

    public void TestHb(){

    try {

    Class cs = Class.forName("testHibernate.Test1");        //classload

    Object oj = cs.newInstance();                                                //通过类NEW一个对象

    Method[] mt = cs.getMethods();                                                //获取所有方法

    for (Method method : mt) {                                                        //轮询出所有函数方法

    if(method.getName()=="m13"){                                        //通过函数名判断函数

    method.invoke(oj,1,2);

    for(Class parameter:method.getParameterTypes()){        //轮询出函数方法的参数

    System.out.println(parameter.getName());                        //获取参数类型

    }

    System.out.println("返回值"+method.getReturnType());        //获取返回值类型

    }else if(method.getName()=="m12"){

    method.invoke(oj);                                                        //执行函数方法无参

    }else if(method.getName()=="getmeth"){

    System.out.println("123"+method.getReturnType());

    Object object = method.invoke(oj);                        //获取函数方法返回的类对象

    Class ccc = object.getClass();                                //通过类对象获得类

    Field ff = ccc.getField("x2");                         //获取变量对象

     int x1 = (int)ff.get(object);                                //获取属性值

    //getDeclaredMethod可获取私有方法

    Method met = ccc.getDeclaredMethod("xxx",new Class[]{int.class,int.class});

     

    met.setAccessible(true);//设置可成 可访问

    Object i = met.invoke(object, x1,2);//调用带参函数方法

    System.out.println(i+"xxxxxxxxxxxxxxxxxxxxxxxxxx");

    }

     

    }        

    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | NoSuchMethodException | NoSuchFieldException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }        

    }

    }

    class Test2{

    static{

    System.out.println("classLoad  test2");

    }

    public int x2 = 3;

    private int xxx(int x1,int x2){

    return x1*x2;

    }

    }

    class Test1{

    static{

    System.out.println("classLoad  test1");

    }

    {

    System.out.println("new objcet  tset1");

    }

     

    public Test2 getmeth(){

    return new Test2();

    }

    public void m12(){

    System.out.println("m12");

    }

    public String m13(int b,int r){

    System.out.println(b+r);

    return b+r+"";

    }

    }

  • 相关阅读:
    ToString格式化
    [转]vc中socket编程步骤
    [XAML学习资料] 验证用户提供的数据ValidationRule
    VS2008下安装boost
    [ASP.NET用户验证一]Forms验证
    VISTA IIS Worker Process 已停止工作 解决办法
    [MFC入门二]四种不同对象
    删除TFS项目
    [WPF学习资料] WPF简介
    IIS7下配置PHP5
  • 原文地址:https://www.cnblogs.com/b422/p/java_reflection.html
Copyright © 2020-2023  润新知