Skip to content

Instantly share code, notes, and snippets.

Avatar

Rémi Forax forax

View GitHub Profile
@forax
forax / Amount.java
Created Aug 20, 2020
Henry refacting: sealed interface + records
View Amount.java
/*
* 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 Aug 16, 2020
Serialize/deserialize classes using records as carrier objets
View Marshaller.java
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;
View Main.java
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 Feb 20, 2018
an invoker that cache up to 'depth' method handles allowing to inline call to them
View InliningCacheInvoker.java
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 Feb 18, 2018
How to create a constant for the VM (JIT) that can change
View Main.java
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 Feb 2, 2018
Transform a non constant method handle to a constant one by introducing an inlining cache in the middle
View InliningCacheInvokerMain.java
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 Jan 24, 2018
Find all possible candidates to be a value based class in the JDK
View ValueBasedClassFinder.java
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;
@forax
forax / Constantifier.java
Created Jan 12, 2018
How to simulate @stable semantics with method handles ?
View Constantifier.java
import static java.lang.invoke.MethodHandles.constant;
import static java.lang.invoke.MethodHandles.dropArguments;
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;
@forax
forax / ASTToMHCompiler.java
Created Jan 3, 2018
A compiler that takes a toy AST and generate the corresponding method handle tree
View ASTToMHCompiler.java
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;
@forax
forax / DefaultFileSystem.java
Created Apr 22, 2017
A dummy FileSystemlProvider
View DefaultFileSystem.java
package filesystem_provider;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.WatchService;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;