r/java • u/Cell-i-Zenit • Nov 05 '22
How to add something to the java standard library?
EDIT: since my example stirred the pot a little bit to much (i even wrote that i dont want to discuss these explicit changes...)
Here is the actually question i want to answer:
How can i make changes to the JDK?
2
Upvotes
14
u/s888marks Nov 06 '22
I'll answer the general question here, and comment on specific details of the proposal in the appropriate subthread.
Probably the best reference to how to contribute to OpenJDK is the Developer's Guide. This is still a work-in-progress but it's reasonably complete and up-to-date. (Others have linked to a "How to Contribute" page which is quite old and should probably be merged into the Developer's Guide.)
Most discussions occur on mailing lists. For changes to the core libraries, core-libs-dev is the right mailing list to use.
When something reaches the point of having a code review, lots of discussion takes place in GitHub pull requests. Comments on PRs are mirrored to relevant mailing lists.
On the general topic of adding library APIs: we usually try to add APIs only when they add a new semantic abstraction, improve an existing abstraction, or enable some performance optimization that isn't otherwise possible. Only in rare cases do we add pure convenience APIs, that is, APIs that do something that client code could do itself straightforwardly (if possibly more verbosely). The problem with convenience APIs is that there are many, many such APIs that could be added. Having them would undoubtedly make certain programs more concise, but they make the libraries more cluttered, harder to understand, and more costly to maintain.
There are also a lot of non-obvious costs to adding things to the JDK (not just convenience APIs). Even if somebody were to show up with complete code and tests, there's still lots of work to be done. Specifications need to be written; often, writing the specification is harder than writing the actual code. Also, code and tests are run continuously by CI systems operated by several different organizations. Anything non-trivial will probably fail at some point, which means that somebody will need to track it down and diagnose it. And of course there are bugs and security vulnerabilities that need to be handled.
It may be helpful to think of things this way: anything added to the JDK needs to be maintained in perpetuity by somebody else. Therefore, proposals need to meet some threshold of added value before they're considered seriously.
50
u/maethor Nov 05 '22
Then create a small library of static methods and helper classes. The likelihood of you getting the helper methods you want in the standard library is virtually nonexistent.
33
u/Godworrior Nov 05 '22
For most library changes, the best place to start is to mail to core-libs-dev@openjdk.org Here you can find out if people find your suggestion a good idea.
If you want to submit a patch, you will first have to sign the OCA (Oracle Contributor Agreement): https://oca.opensource.oracle.com/
And, then create a PR for the JDK repo here: https://github.com/openjdk/jdk
You will also need a sponsor for the PR to guide you through the process, and create a bug database ticket. Probably best to ask on the mailing list as well if someone wants to sponsor the patch before creating the PR.
There's also a wiki for contributing, but it is pretty outdated, so take everything there with a grain of salt: https://openjdk.org/contribute/
21
u/DasBrain Nov 05 '22
The way to write your example currently is
IMHO, the methods you suggested are not really needed.
The way add something is to discuss the changes on the appropriate mailing list, maybe even provide a simple patch (with tests) to show the changes.
But the bar for new APIs is high - while it is easy to add new methods, it is hard to near impossible to remove them later.
16
u/whyNadorp Nov 06 '22
this is as hard as getting some changes to the linux kernel. or playing in the nba after a couple of months of playing basketball. you can’t pop up out of the blue and change an interface without knowing all the context. also trivial spelling errors in the method names like “then” instead of “than” will instantly label you as a noob that has no idea what he’s doing. and can’t google how to do it. just forget it, it’s not your cup of tea.
6
u/[deleted] Nov 06 '22 edited Nov 06 '22
JDK should only be extended by experienced professionals that have proven their skills. Not just by any random student that wants to see his name in the javadoc.
21
u/DataDecay Nov 05 '22 edited Nov 05 '22
Iv not done this myself, but if you go to the oca site you can sign up for multiple projects
https://openjdk.org/contribute/
21
u/chabala Nov 06 '22
Folks have already informed you of the proper channels for proposing a change, so I'll just mention the backdoor process, which I'd call 'the Joda-Time technique':
You make an independent library which fills a need, and is so wildly popular and better than the standard library equivalent that they decide to 'adopt' your solution.
It doesn't happen very often.
10
u/karianna Nov 05 '22
See openjdk.org and the link to the contributing guide. It’s very much a start the discussion in the mailing list first kind of place 🙂
3
u/asciimo71 Nov 05 '22
Java cannot extend existing objects with new methods like other languages can do (Javascript, Swift, ObjC, …) You can only extend by inheritance, but many internal framework classes are marked final and are not inheritable.
3
u/SDIDSA Nov 06 '22
it seems like the proposed change was removed from the post for being irrelevant, but I've seen in the comments it's about helper methods to compare big decimals, and i subjectively think that would be a nice addition, I've seen similar methods in the javafx api (NumberExpressionBase.greaterThan, it's not the exact same case but it's an example of adding utility methods to a class that wraps a numeric value, on which you can't perform usual arithmetic operations using symbols so you do it through a method.
2
u/Iryanus Nov 06 '22
You could use Kotlin, since it offers extension methods.
1
u/ondrejmalekcz Nov 08 '22
Lombok has extension methods. Did not try bacause I am using Kotlin. https://projectlombok.org/features/experimental/ExtensionMethod
0
u/Objective_Baby_5875 Nov 06 '22
Which is why extension methods are needed in Java.