r/java • • Oct 05 '23

How does the lombok magic work underneath?

https://www.unlogged.io/post/how-does-the-lombok-magic-work-underneath
133 Upvotes

143 comments sorted by

View all comments

Show parent comment

1

u/wildjokers Oct 06 '23

We don't invent new language constructs that java does not have.

@SneakyThrows?

11

u/rzwitserloot Oct 06 '23

It sounds like we invented something there, doesn't it?

We didn't, though. You can sneakythrows with plain ole java in a few ways.

A key thing to remember is that the concept of a checked exception is entirely a figment of javac. The reason that this:

public void foo() /* no throws clause */ {
  throw new IOException();
}

doesn't compile is simply because javac refuses to. If it did compile it, the class verifier wouldn't mind. the runtime wouldn't mind. This is why the same code as above written in kotlin can be compiled to a class file just fine.

Thus, all we have to do to call it 'java' is to figure out a way to make the combination of 'plain jane javac straight from the OpenJDK' and 'plain jane java.* core library' somehow do it. Once we get that far, the runtime and class verifier doesn't care.

And turns out, that's not actually difficult. There are 2 main ways:

Use java.* API that breaks the safety

It's deprecated now, but java.lang.Class has a newInstance() method that throws directly. It doesn't wrap stuff in InvocationTargetException like java.lang.reflect.Constructor's newInstance() method. As long as the checked exception you want to throw isn't specifically InstantiationException or IllegalAccessException, we can use this. here it is in action:

> cat Example.java

import java.io.IOException;

class Example {
  private static class Sneaky {
      Sneaky() throws IOException { throw new IOException(); }
    }

  static void sneakyThrowIOExInPlainJava() {
    try {
      Sneaky.class.newInstance();
    } catch (IllegalAccessException | InstantiationException e) {
      // won't happen
      throw new RuntimeException(e);
    }
  }

  public static void main(String[] args) {
    sneakyThrowIOExInPlainJava();
  }
}

> javac Example.java
> java Example
Exception in thread "main" java.io.IOException
	at Example$Sneaky.<init>(Example.java:5)

That's with a plain jane JDK-21 fresh off openjdk.net with no lombok or any other third party dependency or modification of anything whatsoever.

Thus proving that the act of throwing checked exceptions without declaring them is, itself, something plain jane java can do. How can it possibly be 'non java' then?

Generics

With some generics trickery we can fake out the compiler itself and make it compile a sneaky throw:

> cat Thrower.java

import java.io.IOException;
class Thrower {

  public static <E extends Throwable> void sneak(Throwable ex) throws E {
    throw (E) ex;
  }

  public static void main(String[] args) {
    sneak(new IOException());
  }
}

> javac Thrower.java
> java Thrower
Exception in thread "main" java.io.IOException
	at Thrower.main(Thrower.java:10)

This is so 'clean' a way to throw sneakily in pure java I'd call it downright elegant.

Clearly, @SneakyThrows is not inventing new constructs java does not have. It just makes it less boilerplatey to do this stuff. It's a 'generate me some java code' toolkit, not 'this is an entirely new language'.

5

u/ImpossibleTrade1385 Oct 06 '23

that's not a new language construct either, all that does is wrap all of the code in a try catch block that rethrows the exception in an unchecked manner