最近看了java反射,自己练习的代码整理了一下,方便以后查阅
public class mytest {
public mytest(){}
public mytest(int k,String s,boolean b,double d){
a=k;
c=d;
this.s=b;
System.out.println("k="+k+",s="+s+",b="+b+",d="+d);
}
private static final String str="'";
public int a;
private double c;
private boolean s;
private List<String> list;
private Map<String,String> map;
public static void main(String[] args) {
test10();
}
/**
* 获取类的方法
* */
public static void test1(){
try{
Class c=Class.forName("java.util.Stack");
Method m[]=c.getDeclaredMethods();//方法
for(int i=0;i<m.length;i++){
System.out.println(m[i].toString());
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 判断类型
* */
public static void test2(){
try{
Class cls=Class.forName("com.hch.mytest");
boolean b1=cls.isInstance(new Integer(2));
System.out.println(b1);
boolean b2=cls.isInstance(new com.hch.mytest());
System.out.println(b2);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取方法名,返回类型,参数等
* */
public static void test3(){
try{
Class cls=Class.forName("com.hch.reflection.mytest");
Method methlist[]=cls.getDeclaredMethods();
for(int i=0;i<methlist.length;i++){
Method m=methlist[i];
System.out.println("name="+m.getName());
System.out.println("decl class="+m.getDeclaringClass());
Class pvec[]=m.getParameterTypes();
for(int j=0;j<pvec.length;j++){
System.out.println("param #"+j+":"+pvec[j]);
}
Class evec[]=m.getExceptionTypes();
for(int j=0;j<evec.length;j++){
System.out.println("exc #"+j+":"+evec[j]);
}
System.out.println("return type="+m.getReturnType());
System.out.println("----");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取构造方法,参数
* */
public static void test4(){
try{
Class cls=Class.forName("com.hch.reflection.mytest");
Constructor ctorlist[]=cls.getDeclaredConstructors();
for(int i=0;i<ctorlist.length;i++){
Constructor ct=ctorlist[i];
System.out.println("name:"+ct.getName());
System.out.println("decl class:"+ct.getDeclaringClass());
Class pvec[]=ct.getParameterTypes();
for(int j=0;j<pvec.length;j++){
System.out.println("param#"+j+":"+pvec[j]);
}
Class evec[]=ct.getExceptionTypes();
for(int j=0;j<evec.length;j++){
System.out.println("exc #"+j+":"+evec[j]);
}
System.out.println("---------------");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取类字段
* */
public static void test5(){
try{
Class cls=Class.forName("com.hch.reflection.mytest");
Field fieldlist[]=cls.getDeclaredFields();
for(int i=0;i<fieldlist.length;i++){
Field fld=fieldlist[i];
System.out.println("name="+fld.getName());
System.out.println("decl class="+fld.getDeclaringClass());
System.out.println("type="+fld.getType());
int mod=fld.getModifiers();
System.out.println("modifiers:"+Modifier.toString(mod));
System.out.println("-----------------");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 执行一个方法
* */
public static void test6(){
try{
Class cls=Class.forName("com.hch.reflection.mytest");
Class partypes[]=new Class[2];
partypes[0]=Integer.TYPE;
partypes[1]=Integer.TYPE;
Method meth=cls.getMethod("add", partypes);
com.hch.reflection.mytest m=new com.hch.reflection.mytest();
Object arglist[]=new Object[2];
arglist[0]=new Integer(37);
arglist[1]=new Integer(42);
Object retobj=meth.invoke(m, arglist);
Integer retval=(Integer)retobj;
System.out.println(retval.intValue());
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 创建新的对象
* */
public static void test7(){
try{
Class cls=Class.forName("com.hch.reflection.mytest");
Class partypes[]=new Class[4];
partypes[0]=Integer.TYPE;
partypes[1]=String.class;
partypes[2]=Boolean.TYPE;
partypes[3]=Double.TYPE;
Constructor ct=cls.getConstructor(partypes);
Object arglist[]=new Object[4];
arglist[0]=12;
arglist[1]="oooo";
arglist[2]=true;
arglist[3]=12.3f;
com.hch.reflection.mytest retobj=(mytest)ct.newInstance(arglist);
System.out.println("obj:"+retobj.a+",c:"+retobj.c);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 改变字段
* */
public static void test8(){
try{
Class cls=Class.forName("com.hch.reflection.mytest");
Field fld=cls.getField("a");
mytest fld2=new com.hch.reflection.mytest();
System.out.println("a="+fld2.a);
fld.setInt(fld2, 20);
System.out.println("a="+fld2.a);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 使用数组
* */
public static void test9(){
try{
Class cls=Class.forName("java.lang.String");
Object arr=Array.newInstance(cls, 10);
Array.set(arr,4,"mytest");
Array.set(arr, 5, "This is a test");
for(int i=0;i<10;i++){
System.out.println("i="+i+",value="+Array.get(arr, i));
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 3维数组
* */
public static void test10(){
try{
int dims[]=new int[]{5,10,15};
Object arr=Array.newInstance(Integer.TYPE, dims);
Object arrobj=Array.get(arr, 3);
Class cls=arrobj.getClass().getComponentType();
System.out.println(cls);
arrobj=Array.get(arrobj, 5);
Array.setInt(arrobj, 10, 37);
int arrcast[][][]=(int[][][])arr;
System.out.println(arrcast[3][5][10]);
}catch(Exception e){
e.printStackTrace();
}
}
public int add(int a,int b){
System.out.println("print:"+(a+b));
return a+b;
}
private int fl(Object p,int x)throws NullPointerException{
if(p==null){
throw new NullPointerException();
}
return x;
}
}