Skip to content

Instantly share code, notes, and snippets.

View forax's full-sized avatar

Rémi Forax forax

View GitHub Profile
import java.util.Objects;
public interface FunctionalDeconstructionDemo {
// Example 1: Optional
public final class Optional<T> {
private T value;
private Optional(T value) {
this.value = value;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.invoke.MutableCallSite;
@forax
forax / RecordBuilder.java
Created June 8, 2023 21:37
A universal record builder with a TypeSafe API
import java.io.Serializable;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.RecordComponent;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.Map;
@forax
forax / Amount.java
Created August 20, 2020 21:21
Henry refacting: sealed interface + records
/*
* Copyright 2019-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@forax
forax / Marshaller.java
Created August 16, 2020 12:28
Serialize/deserialize classes using records as carrier objets
package fr.umlv.marshaller;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.invoke.MethodHandle;
@forax
forax / Main.java
Last active November 22, 2019 21:20
import static java.lang.invoke.MethodHandles.lookup;
import java.lang.reflect.InvocationTargetException;
import fr.umlv.nreflect.ReflectMirror;
public class Main {
private void foo(String s) {
System.out.println("hello " + s);
}
@forax
forax / InliningCacheInvoker.java
Created February 20, 2018 13:21
an invoker that cache up to 'depth' method handles allowing to inline call to them
import static java.lang.invoke.MethodHandles.exactInvoker;
import static java.lang.invoke.MethodHandles.foldArguments;
import static java.lang.invoke.MethodHandles.lookup;
import static java.lang.invoke.MethodType.methodType;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.invoke.MutableCallSite;
@forax
forax / Main.java
Created February 18, 2018 21:32
How to create a constant for the VM (JIT) that can change
package fr.umlv.inside.almostconst;
import java.util.function.Supplier;
public class Main {
private static final MostlyConstant<String> FOO = new MostlyConstant<>("HELL");
private static final Supplier<String> FOO_GETTER = FOO.getter();
private static int test(int max, int change) {
int sum = 0;
@forax
forax / InliningCacheInvokerMain.java
Created February 2, 2018 17:54
Transform a non constant method handle to a constant one by introducing an inlining cache in the middle
import static java.lang.invoke.MethodHandles.constant;
import static java.lang.invoke.MethodHandles.dropArguments;
import static java.lang.invoke.MethodHandles.guardWithTest;
import static java.lang.invoke.MethodHandles.lookup;
import static java.lang.invoke.MethodType.methodType;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MutableCallSite;
@forax
forax / ValueBasedClassFinder.java
Created January 24, 2018 18:30
Find all possible candidates to be a value based class in the JDK
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;