2007-08-15

Java SE 6 instrumentation 强大的功能

来源: Impress Watch中文站 作者:胡 睿 吕 晶 评论 0 条
 
 public static byte[] getBytesFromFile(String fileName) {
  try {
   // precondition
   File file = new File(fileName);
   InputStream is = new FileInputStream(file);
   long length = file.length();
   byte[] bytes = new byte[(int) length];

   // Read in the bytes
   int offset = 0;
   int numRead = 0;
   while (offset <bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
    offset = numRead;
   }

   if (offset < bytes.length) {
    throw new IOException("Could not completely read file " file.getName());
   }
   is.close();
   return bytes;
  } catch (Exception e) {
   System.out.println("error occurs in _ClassTransformer!" e.getClass().getName());
   return null;
  }
 }

 public byte[] transform(ClassLoader l, String className, Class<?> c,
ProtectionDomain pd, byte[] b) throws IllegalClassFormatException {
  if (!className.equals("TransClass")) {
   return null;
  }
  return getBytesFromFile(classNumberReturns2);
 }
}


  这个类实现了 ClassFileTransformer 接口。其中,getBytesFromFile 方法根据文件名读入二进制字符流,而 ClassFileTransformer 当中规定的 transform 方法则完成了类定义的替换转换。


共3页: 上一页 [1] [2] 3 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:Ruby on Rails解析  
下一篇:Java技术在多数据库系统中的应用研究
    评论加载中…
 热门排行
 推荐文章
     

 相关文章
网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号