调试JDK源码

来源:互联网 发布:淘宝密码忘了怎么办 编辑:程序博客网 时间:2024/06/05 02:25

  一.解压src.zip

  解压src.zip到E:\workspace\下,

  src.zip在安装的C:\Program Files\Java\jdk1.8.0_25下

  二.javac -g重编译

  重新编译src\java\util下的HashMap.java

  Windows下进入DOS环境,输入

  E:\workspace\src\java\util

  然后再输入E:就直接到了E:\workspace\src\java\util

  默认如果不带-g编译是没有调试信息是不够的。

  # javac -g HashMap.java

  

  三.替换rt.jar

  将编译好的所有的HashMap.class都放入C:\Program Files\Java\jdk1.8.0_25\jre\lib的rt.jar

  

  

  说明:需要做好备份以防搞错。

  参考:eclipse如何debug调试jdk源码

  初调HashMap,如何修改JDK的源码进行调试

  编译JDK源代码,开启Debug信息

  四.调试HashMap

  先看看HashMap的理论吧

  

  [java] view plain copy

  

在CODE上查看代码片

  

派生到我的代码片

  import java.util.HashMap;

  import java.util.Map;

  import org.junit.Test;

  public class TestHash {

  @Test

  public void testHashMap() throws Exception {

  System.out.println("==========================");

  Map m = new HashMap();

  for (int i = 0; i < 18; i++) {

  m.put((char) (i + 65) + (char) (i + 66) + (char) (i + 67) + "", i + ">>>http://blog.csdn.net/unix21/");

  }

  System.out.println("==========================");

  }

  }

  下面是源码

  [java] view plain copy

  

在CODE上查看代码片

  

派生到我的代码片

  /**

  * Associates the specified value with the specified key in this map.

  * If the map previously contained a mapping for the key, the old

  * value is replaced.

  *

  * @param key key with which the specified value is to be associated

  * @param value value to be associated with the specified key

  * @return the previous value associated with key, or

  * null if there was no mapping for key.

  * (A null return can also indicate that the map

  * previously associated null with key.)

  */

  public V put(K key, V value) {

  return putVal(hash(key), key, value, false, true);

  }

  /**

  * Implements Map.put and related methods

  *

  * @param hash hash for key

  * @param key the key

  * @param value the value to put

  * @param onlyIfAbsent if true, don't change existing value

  * @param evict if false, the table is in creation mode.

  * @return previous value, or null if none

  */

  final V putVal(int hash, K key, V value, boolean onlyIfAbsent,

  boolean evict) {

  Node[] tab; Node p; int n, i;

  if ((tab = table) == null || (n = tab.length) == 0)

  n = (tab = resize()).length;

  if ((p = tab[i = (n - 1) & hash]) == null)

  tab[i] = newNode(hash, key, value, null);

  else {

  Node e; K k;

  if (p.hash == hash &&

  ((k = p.key) == key || (key != null && key.equals(k))))

  e = p;

  else if (p instanceof TreeNode)

  e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);

  else {

  for (int binCount = 0; ; ++binCount) {

  if ((e = p.next) == null) {

  p.next = newNode(hash, key, value, null);

  if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st

  treeifyBin(tab, hash);

  break;

  }

  if (e.hash == hash &&

  ((k = e.key) == key || (key != null && key.equals(k))))

  break;

  p = e;

  }

  }

  if (e != null) { // existing mapping for key

  V oldValue = e.value;

  if (!onlyIfAbsent || oldValue == null)

  e.value = value;

  afterNodeAccess(e);

  return oldValue;

  }

  }

  ++modCount;

  if (++size > threshold)

  resize();

  afterNodeInsertion(evict);

  return null;

  }

  1.第一次进入源码

  先初始化增长因子

  

  

  一开始声明一个

  [java] view plain copy

  

在CODE上查看代码片

  

派生到我的代码片

  transient Node[] table;

  java 的transient关键字为我们提供了便利,你只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。

  Java transient关键字使用小记

0 0
原创粉丝点击