vscode-java-dependency icon indicating copy to clipboard operation
vscode-java-dependency copied to clipboard

Display library settings for a dependency

Open danieltuzes opened this issue 4 years ago • 6 comments

VS Code says "By default, a referenced {binary}.jar will try to search {binary}-sources.jar under the same directory, and attach it as source if one match is found." I added the commons-math3-3.6.1.jar from Apache, and the source is right next to it with the proper suffix, however, the Project Manager for Java didn't add it to the workspace settings. Does the aforementioned feature exist? Do I do something incorrectly?

danieltuzes avatar Oct 25 '21 10:10 danieltuzes

Yes it will.

You can try put the commons-math3-3.6.1.jar into the referenced library and then trigger a Go to Definition to a class defined in commons-math3-3.6.1.jar.

The first time you will see a warning saying that source cannot be found, but in that background there is a task being triggered to download the source jar. After a couple of seconds you can trigger Go to Definition again, then you will see the source content.

Dose it answer your question?

jdneo avatar Nov 02 '21 01:11 jdneo

Thanks @jdneo for the hint. This technique is not mentioned on the VS Code page. I have the following code:

import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.MersenneTwister;

class QuickStart {
    public static void main(String[] args) {
        RandomGenerator prng = new MersenneTwister(0);
        for (int i = 0; i < 3; ++i) {
            long num = prng.nextLong();
            System.out.println(Long.toString(num) + "\t" + Long.toBinaryString(num));
        }
    }
}

When I press F12 on MersenneTwister(0), I can read the code of MersenneTwister.class. However, on the java project tab, I cannot see if the docs were included:

Screenshot 2021-11-03 194236

danieltuzes avatar Nov 03 '21 19:11 danieltuzes

Because technically, the source jar is patched to the build path. But I agree that making it visible in Projects explorer would be helpful to users.

jdneo avatar Nov 04 '21 00:11 jdneo

Thanks for the fast reply. Can you explain it with more words, please? I am new to Java. What does patch mean here?

Does it mean that there is no need for the jars I have, apart from commons-math3-3.6.1.jar? The extra jars:

  • commons-math3-3.6.1-javadoc.jar
  • commons-math3-3.6.1-sources.jar
  • commons-math3-3.6.1-test-sources.jar
  • commons-math3-3.6.1-tests.jar
  • commons-math3-3.6.1-tools.jar

Or do you say that these are used by VS Code, but are not shown in the java projects?

danieltuzes avatar Nov 04 '21 11:11 danieltuzes

patch does not have special meaning, it's just the extension's implementation detail.

When you have a build tool(i.e. Maven/Gradle) for your project, those 'extra jars' will be automatically downloaded by the build tool.

But when it comes to unmanaged folder (without build tool), the extra jars won't be downloaded. The reason why you can still see the source is that it's a feature we implemented in the extension.

If a jar in the referenced library has Maven related metadata, we can infer it's source jar and download for users.

jdneo avatar Nov 05 '21 01:11 jdneo

Instead directly display the source jars of a dependency in the explorer, other IDEs usually use another dedicated view to display those metadata. For example, IDEA:

image

jdneo avatar Apr 12 '23 06:04 jdneo