0

I'm trying to debug into the JDK source files. To be more specific, I'm interested in studying the internal implementation of the Collection classes.

So I have created a Simple Java Project, added some sample code like this one:

import java.util.HashMap;
import java.util.Map;

public class HashMapWorking {
    public static void main(String[] args) {
        Map<String, String> demoMap = new HashMap<>();

        demoMap.put("one", "first");
        demoMap.put("two", "second");
        demoMap.put("three", "third");
    }
}

I need to know, for example, what happens when I call the demoMap.put() method.

I have added the source archive using the Edit Source Lookup Path dialog:

enter image description here

Now my problem is that when I debug into the put() method in the java.util.HashMap class, the Variables tab doesn't list out its member variables. Instead I just get to see the arguments being passed to the respective methods:

enter image description here

The local variables withing methods aren't captured either

What can I do to be able to debug through the java source the way I could debug into any other source classes?

I tried referencing the src.zip as well as the extracted version of src.zip in the Edit Source Lookup Path dialog, neither of them worked.

Kindly advise.

6
  • You have to step into to see those variables. In Window > Preferences: Java > Installed JRE you have probably a JRE instead of a JDK causing the troubles with the missing sources. Commented Nov 3, 2019 at 18:59
  • @howlger: a JRE doesn't contain src.zip, and the Oracle JREs at least don't use the directory name format shown. Commented Nov 3, 2019 at 20:27
  • @dave_thompson_085 Sure, because the JRE instead of a JDK in Window > Preferences: Java > Installed JREs the sources have been missed. You run the application on a JRE with attached sources of a JDK, not on a JDK as you can see from the parameter name arg0 instead of key. Independent of that, you have to Step Into to see what you want. Commented Nov 3, 2019 at 20:41
  • @howlger: I did try switching the Installed JRE value among jdk and jre. In either case I didn't get it through. Commented Nov 4, 2019 at 7:16
  • @VikramSherigar With a JDK instead of a JRE you see the sources without the need to attach it and in the Variables view key instead of arg0 will be shown. To see the local variables within the method" you have to Step Into as it has been stopped before the line in which the variable was first used. The member variables (fields) of the class are not shown for static methods because static stuff has no reference to an instance. Commented Nov 4, 2019 at 7:40

1 Answer 1

1

Class fields. In the display you posted, you are not actually stopped in the HashMap.put(K,V) method but in the HashMap.hash(Object) method (which put calls). hash(Object) is a static method -- it executes without any 'instance' context (specifically, this).

If you go down the stack to the put(K,V) method (or any other instance method), you will see it has a HashMap object as this. If not already expanded, click the plus box to see all fields in the class.

Although static fields (only) are accessible to a static method (and depending on visibility, to methods in other classes that don't have or use an instance), the 'Variables' window/view doesn't have a way to find them. You can access invididual static fields in the 'Expressions' window/view -- although in standard (JDK) classes I have looked at static fields are mostly constants whose value is given in the source code, thus looking at them with the debugger isn't very useful.

Local variables. The older Oracle builds apparently are compiled with LineNumber (and SourceFile) debug information but without LocalVariable debug information. These are separate sub-options; see the help message or manual for javac. Without this information in the classfile, the debugger does not display local variables, or names for parameters (it does display values for parameters, as arg0 arg1 etc, because type information is in the basic classfile data).

I don't know why they did this; perhaps it was a leftover from the pre-opened-source days. The Oracle builds for newer versions (j9 and up) and some of the OpenJDK builds I could easily check do have LocalVariables. You could try one of those (using a newer version may depend to some extent on your Eclipse version).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.