Home > Software design >  How to know what a Java artifact is exactly used for?
How to know what a Java artifact is exactly used for?

Time:10-20

This may sound as a weird question but here it goes. I'm learning java coming from node, and although I'm getting fairly comfortable with the syntax/language/ecosystem, I still find it very difficult to know what a given package is exactly used for, in terms of code. For example

Node.js

"lodash: 4.0.0" -> import _ from lodash -> _.shuffle()

Easy to see what lodash is used for and besides, googling lodash directs you straight to the documentation website.

Java

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring5</artifactId>
  <version>3.0.12.RELEASE</version>
</dependency>

Looking at the code gives you stuff like :

import org.thymeleaf.spring5.SpringTemplateEngine;
import org.springframework.context.MessageSource;

Meaning you have to understand Spring design to understand what this package will be used for. How about a google search ? It's even worse! Only a list of all the package versions. What and how it is exactly used for, on the other hand, is not listed at all !

It makes mastering what package is needed for somewhat painful for me.

Any solution to this?

CodePudding user response:

Yeah, this is a kind of weird question indeed. All-in all I think its a matter of time that takes to migrate from one ecosystem to another. Also I believe, probably you've picked a bad example.

How about something more simple (and yes, lodash conceptually is very simple): What about, say, logging:

so you import a logging library (say you're using slf4j with loback/log4j2 whatever -- pretty common these days) then in the import you do:

import org.slf4j.Logger;

You see the package slf4j - and have a documentation in google just like with lodash Then you use the logger and that's easy.

Now with spring its way more complicated because there is nothing I'm aware so "powerful" and "complex" at the same time the java script parlance. So it provides plethora of APIs and classes, it takes quite a while to grasp its concepts. But in general when you see import org.springframework something - you can google for springframework and you'll get to the documentation. The documentation is huge though, but again, its another story.

CodePudding user response:

It's not a complete answer, but this might help:

It's a best practice for libraries to use a unique prefix for class packages, e.g. 'org.thymeleaf' for thymeleaf.jar, 'org.thymeleaf.spring5' for thymeleaf-spring5.jar.

You can lookup which class belongs to which jar (if they are in the central maven repository) via https://search.maven.org/, e.g. using the full class name fc:org.thymeleaf.spring5.SpringTemplateEngine or the simple class name c:SpringTemplateEngine.

Unfortunately search.maven.org does not show the list of all classes or packages in a jar, you actually have to either download them or find a javadoc page on the library's own website.

IIRC, bintray was better at this, but unfortunately, JFrog stopped that free service.

  • Related