使用Reflection来释放一个class所申请的资源

public void disposeFields()
    {
        
super.dispose();
        Class cls 
= this.getClass();
        java.lang.reflect.Field[] fields 
= cls.getDeclaredFields();
        
for (int i=0; i<fields.length;i++)
        {
            
try
            {
                
int m = fields.getModifiers();
                
if ( ! java.lang.reflect.Modifier.isFinal(m) )
                {
                    fields.set(
thisnull);
                }
            }
            
catch (IllegalAccessException e)
            {
            }
            
catch (Exception e)
            {
            }
        }
    }

 

posted @ 2011-01-03 12:49  super119  阅读(173)  评论(0编辑  收藏  举报