JDriven Blog

Detect & delete unreferenced code with ArchUnit

Posted on by  
Tim te Beek

When you maintain a large Java project for a longer period, the moments where you’re finally able to remove unused code can be very satisfying. No more upkeep, library version migrations or dark corners to maintain, for code that’s no longer being used. But finding out which parts of the code base can be removed can be a challenge, and tooling in this space seems not to have kept pace with recent development practices in Java. In this post we’ll outline an approach to find unreferenced code with ArchUnit, which allows you to iteratively detect & delete unused code from your Java projects.

Continue reading →

Java Joy: Optional orElse orElseGet That Is The Question

Posted on by  
Hubert Klein Ikkink

The Optional class has the orElse and orElseGet methods to return a value when the Optional object is empty. This is useful to return a default value for example. But there is a small difference between the two methods. The orElseGet method needs a Supplier argument that returns a value of the type of the Optional value. The Supplier is only invoked when the Optional value is empty. The statement passed as argument to the orElse method is always executed, even when the Optional value is not empty. Preferrably we should use orElseGet as it will only invoke statements if needed.

In the following example code we see when our method getDefaultGreeting is invoked by using orElse and orElseGet with an empty and non-empty Optional object:

Continue reading →

iTerm2

Posted on by  
Justus Brugman

Since I’ve been working on a Mac, I replaced the default terminal with iTerm2. It provides some nice features like searching, autocomplete, or allowing to see images in the terminal. But this one is my favorite one, the undo close tab / session.

Don’t you just hate it when you have multiple terminal tabs open, and accidentally close one? Just that one where you had an important process running, or tailing an error log? I do! :) This is just a short blogpost how iTerm2 helps me having more fun using my terminal. Actually it’s not much of a blogpost, this provides some screenshots. Sometimes an image says more than words.

Continue reading →

Formatting in pre-commit hook

Posted on by  
Christophe Hesters

Many projects force their code to be formatted. We use spotless for this purpose. It can check for propper formatting, and also format the code for you. Then build pipeline checks if the code is properly formatted. Failing pipelines due to formatting errors are annoying and cost a lot of time and money. This blog proposes a solution.

Git has some hooks that you can install. Hooks are scripts that are run at specific events such as pre-commit, post-commit, pre-push, prepare-commit-msg, pre-rebase, etc. I created a script that formats the code in the pre-commit hook. It does:

  1. This scripts tracks any of the java or kotlin files in the staging area.

  2. Formats the code using spotless if there are any java/kotlin files.

  3. It adds the files from step 1 again to the staging area, to pick up the re-formatted changes.

The commit now contains formatted code. This saves a lot of time, instead of finding out that the build failed 5 minutes later.

The pre-commit hook looks like:

If you use another formatter, you can easily update the script.

You can install it by running the following command in your repository root:

Continue reading →

How to hack a box - Enumeration

Posted on by  
Niels van Nieuwenburg

Welcome back to the blog series about how to hack a box! In the past few blogs we’ve gone through a few steps which gives you an idea of how you can hack a box. We went from the Introduction, to Exploration, to Gaining Access. In this blog, we’ll cover the basics of Enumeration.

DISCLAIMER: Never attempt to execute one of these steps on a machine where you don’t have explicit permission for from the owner. This is illegal and will get you in trouble.

Continue reading →

Oh, those bits and bytes

Posted on by  
Jacob van Lingen

The time is right. Your work is done. The last letter of your Java code has been written. You let the IDE compile the code and a new running version of your app is ready to be released. You’ve done this a thousand times, there’s nothing new on the horizon. The question of what lies beneath, what happens under the hood, has never occurred to you. Until now!

Continue reading →

Deprecation with Replace hits

Posted on by  
Christophe Hesters

When code evolves we usually deprecate old code. Sometimes we come across deprecations without any hints with what to replace it with. Kotlin has a solution for this by allowing you to specify a replace instruction.

For example, we created have an old REST client.

Continue reading →

Async Retrofit with coroutines and Spring Boot

Posted on by  
Christophe Hesters

Spring boot supports a non-blocking programming model with the spring-webflux module. Webflux supports a Reactive API using the Reactor library Flux and Mono API types. This model forces you to write your code in a different style than most people are used to. It generally is much harder to follow and debug.

This post shows you a short example of using Kotlin’s Coroutines instead of Reactor to write your code in a more imperative and easier to follow style. It includes examples of using Retrofit as HTTP client to access external API’s concurrently in a non-blocking way.

Continue reading →

Custom SSLContext with Apaches fluent HttpClient5

Posted on by  
Sjoerd During

Apaches fluent httpclient API is a facade API to simplify the httpclients usage for standard use cases. It’s also better readable and results in cleaner code. In this post we’ll see how to use a custom SSLContext with the fluent API. We’ll use the new 5.0 version because it contains some changes compared to 4.x.

Continue reading →

shadow-left