java反射


一:得到属性

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 
@Inherited
public @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.youkuaiyun.com/aojiancc2/article/details/50857792




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值