How to Compile a Class at Runtime with Java 8 and 9

In some cases, it's really useful to be able to compile a class at runtime using the java.compiler module. You can e.g. load a Java source file from the database, compile it on the fly, and execute its code as if it were part of your application. In the upcoming jOOR 0.9.8, this will be … Continue reading How to Compile a Class at Runtime with Java 8 and 9

Correct Reflective Access to Interface Default Methods in Java 8, 9, 10

When performing reflective access to default methods in Java, Google seems to fail us. The solutions presented on Stack Overflow, for instance, seem to work only in a certain set of cases, and not on all Java versions. This article will illustrate different approaches to calling interface default methods through reflection, as may be required … Continue reading Correct Reflective Access to Interface Default Methods in Java 8, 9, 10

Add Some Entropy to Your JVM

Being able to generate true random numbers depends on the entropy in your system. Some claim, that this can be guaranteed by fair dice roll. Others think that replacing the OpenJDK's java.math.Random.nextInt() method with this body will help: public int nextInt() { return 14; } Source: http://www.redcode.nl/blog/2013/10/openjdk-and-xkcd-random-number/. But that's absurd. We all know that the best … Continue reading Add Some Entropy to Your JVM

A dirt-ugly hack to modify private final fields in Java

We all use reflection from time to time. We may even tamper with visibility through Java's Field.setAccessible() and similar methods. But this post here takes things to the extreme and shows how to modify private (static) final fields in Java. Think twice, when choosing this tool ;-) http://zarnekow.blogspot.ch/2013/01/java-hacks-changing-final-fields.html

A neater way to use reflection in Java

Reflection in Java really feels awkward. The java.lang.reflect API is very powerful and complete, and in that sense also very verbose. Unlike in most scripting languages, there is no convenient way to access methods and fields dynamically using reflection. By convenient, I mean things like this // PHP $method = 'my_method'; $field = 'my_field'; // … Continue reading A neater way to use reflection in Java