java反射

来源:互联网 发布:linux 局域网聊天 编辑:程序博客网 时间:2024/06/02 02:16


一:得到属性

Class<T> t;这里的t类似net的type可以通过对象得到Class<T> t = obj.Class


 所有属性,与访问修饰符无关

public <T> void Relef(Class<T> t) throws NoSuchFieldException, SecurityException {    Field[] fids = t.getDeclaredFields(); }

  所有公共属性

public <T> void Relef(Class<T> t) throws NoSuchFieldException, SecurityException {    Field[] fids = t.getFields(); }

更具属性名得到属性

public <T> void Relef(Class<T> t) throws NoSuchFieldException, SecurityException {    Field fids = t.getField("filename"); }


二:为属性赋值

public <T> void Relef(Class<T> t) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {    Field fids = t.getField("filename");    fids.setAccessible(true)//忽略访问符private也可以访问    fids.set(t,""); }

    得到静态属性

     

public Object getStaticProperty(String className, String fieldName)        throws Exception {    Class ownerClass = Class.forName(className);     Field field = ownerClass.getField(fieldName);     Object property = field.get(ownerClass);     return property;}


三 :更具类型创建对象

   
public <T> T get(Class<T> c) throws InstantiationException, IllegalAccessException{           T t = c.newInstance();          return t;       }   

四:调用方法

 public <T> void RelInvoke(Class<T> t) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { t.getDeclaredMethod("youwayname",int.class).invoke(t.newInstance(),1);//参数为int t.getDeclaredMethod("youwayname",int.class,String.class).invoke(t.newInstance(),new Object[]{1,"aa"});//参数为int,string }

Discovery the  execute performance  for  direct operate filed  is superior to method invoker


   上边反射调用的是用t.newInstance()新实例化对象的,也可以传一个已经有了的对象

    

public <T> void RelInvoke(Class<T> t,Object _ma) throws Exception {       t.getDeclaredMethod("switchContent",Fragment.class).invoke(_ma,new ShipScheFragment());//参数为Fragment    } 


五:得到注解

   得到属性注解

public void getAnnotation() throws NoSuchFieldException, SecurityException      {          Accounts_top_up tm = new Accounts_top_up();             ColumnAttribute an = tm.getClass().getDeclaredField("ID").getAnnotation(ColumnAttribute.class);         if(an.isAutoAdd()==true)        {        //执行相关操作        }    } 


属性注解定义

/*属性注解*/@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME) @Documented @Inheritedpublic @interface ColumnAttribute {boolean isprimary() default false;boolean isAutoAdd() default false;//是否在更新时自改变boolean isGUID() default false;//使用使用guid}

      属性注解在model中的使用

public class Accounts_top_up {@ColumnAttribute(isprimary = true)private Integer ID;@ColumnAttribute(isAutoAdd = true)private Integer USER_ID;public Integer getID() {return ID;}public void setID(Integer iD) {ID = iD;}public Integer getUSER_ID() {return USER_ID;}public void setUSER_ID(Integer uSER_ID) {USER_ID = uSER_ID;}}



六:得到类名

短类名

public static <T> String GetTableName(T t)    {        return   t.getClass().getSimpleName();    }

全类名

public static <T> String GetTableName(T t)    {        return   t.getClass().getName();    }



java反射在android上的运用,自动得到控件值

http://blog.csdn.net/aojiancc2/article/details/50857792




0 0
原创粉丝点击