Java反射机制的使用

AmigoEditText reflectEditTextFromAmigoSearchView(){
    	AmigoEditText amigoEditText = null;
        if(mAmigoSearchView != null){
        	
            Class<AmigoSearchView> amigoSearchViewClass = (Class<AmigoSearchView> )mAmigoSearchView.getClass();
//            Class superClass = amigoSearchViewClass.getSuperclass();
            
             if(amigoSearchViewClass != null){
            	 try {
//                Field field = superClass.getDeclaredField("mSearchSrcTextView");
            		 Field field = amigoSearchViewClass.getDeclaredField("mQueryTextView");
                field.setAccessible(true);
                Object obj = field.get(mAmigoSearchView);
                if(obj instanceof AmigoEditText){
                    amigoEditText = (AmigoEditText)obj;
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                Log.e("AgendaActivity", "no such filed name mSearchSrcTextView in AmigoSearchView");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Log.e("AgendaActivity", "can not access the field of mAmigoSearchView");
            }
        }
             }
            
        return amigoEditText;
    }

注意事项:

1.getField(String)和getDeclaredField的区别

getField(String)只是能获取你有权限获取的;而getDeclaredField可以获取任何权限的Field。

2.getDeclaredField也不能获取父类的成员。需要通过clazz.getSuperClass()去获取其父类,这个返回的父类类对象是代表他所有父类,通过这个Class对象,可以获取

  clazz的所有直接父类的成员,但是不能获取父类的父类的成员,需要进一步下去才可以。

3.getDeclaredFields()可以获取对应Class的所有成员

4.当没有该成员会抛出异常,或者父类为借口时,会返回null。

5.使用field时,如果该field为private,则需要需要field.setAccessible(true);


类的动态加载:

try {
        Class<?> clazz = Class.forName("com.mediatek.dcfdecoder.DcfDecoder");
        Object dcfDecoder = clazz.newInstance();
        
		Method method = clazz.getDeclaredMethod("forceDecryptFile", String.class,int.class);
		return (byte[])(method.invoke(dcfDecoder, filePath, consume));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

try {
          Class<?> clazz = Class.forName("com.mediatek.common.MPlugin");
  		Method method = clazz.getDeclaredMethod("createInstance", String.class,Context.class);
  		sImageOptions  = (IImageOptionsExt)(method.invoke(null, IImageOptionsExt.class.getName(), context.getApplicationContext()));
  		} catch (Exception e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}

这样做的用处是,编译环境没有这个类,而运行环境中有这个类。这样编译就不会检查。

如果调用的是静态方法,invoke()方法的第一个参数为null即可。

实例化使用newinstance(),会调用该类的无参构造函数,且该函数必须是可访问的。

如果需要调用有参的构造函数,可以使用反射调用Constructor,这样还可以调用私有构造方法

在获取Method时,若方法参数是基本类型(如:int,boolean),就传入int.class或者Integer.TYPE

如下代码展示了如何使用反射构造方法去实例化一个

Class cls=Class.forName("test.reflect.A");  //import class
Constructor cst=cls.getConstructor(new Class[]{String.class}); //get the constructor
Object obj=cst.newInstance(new Object[]{new String("invoke Construct method!")}); //get a new instance 





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值