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 / gist:e733e6af6114eff55149
Created June 1, 2014 16:13
Lispy interpreter written in Java
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
import static java.util.stream.Stream.concat;
import static java.util.stream.Stream.of;
import java.io.Console;
@forax
forax / LookupProxyTest.java
Created July 24, 2016 17:46
get the trusted lookup using Unsafe.defineAnonymousClass
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.util.Base64;
public class LookupProxyTest {
static final String PROXY_CLASS =
"yv66vgAAADUAGQoABAAQCQAKABEHABIHABMBAAY8aW5pdD4BAAMoKVYBAARD" +
@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 / ASTToMHCompiler.java
Created January 3, 2018 16:05
A compiler that takes a toy AST and generate the corresponding method handle tree
import static java.lang.invoke.MethodHandles.constant;
import static java.lang.invoke.MethodHandles.dropArguments;
import static java.lang.invoke.MethodHandles.empty;
import static java.lang.invoke.MethodHandles.identity;
import static java.lang.invoke.MethodHandles.loop;
import static java.lang.invoke.MethodHandles.publicLookup;
import static java.lang.invoke.MethodType.methodType;
import static java.util.stream.IntStream.range;
import java.lang.invoke.MethodHandle;