反射类查看类下面的所有方法及变量

最近项目要用到android的一个类Environment,在模拟器上有很多方法都没有。无法实现我的功能,我就把这个类反射了,看了一下它的结构
TextView tx = (TextView) findViewById(R.id.dd);
tx.setTextSize(24);
Environment e=new Environment();
StringBuffer sb=new StringBuffer();
Class T;
T = e.getClass();
Object value=null;
Method[] methods=T.getMethods();
Field[] fields=T.getDeclaredFields();
sb.append("方法名:\n");
for(int i=0;i<methods.length;i++){
Object obj;
try {
obj = T.newInstance();
value = methods[i].invoke(obj);
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}//通过class类反射一个对象实体!
catch (IllegalArgumentException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
} catch (InvocationTargetException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
  
sb.append(methods[i].getName()+"|"+value+"\n");
}
sb.append("字段名及类型:\n");
for(int i=0;i<fields.length;i++){
sb.append(fields[i].getName()+" | "+fields[i].getType()+"\n");
}

tx.setText(sb.toString());
}

你可能感兴趣的:(android)