码迷,mamicode.com
首页 > 编程语言 > 详细

Java反射机制DOME

时间:2016-04-17 17:30:45      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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+"";

}

}

Java反射机制DOME

标签:

原文地址:http://www.cnblogs.com/b422/p/java_reflection.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!