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