Skip to content

Instantly share code, notes, and snippets.

View raphw's full-sized avatar

Rafael Winterhalter raphw

View GitHub Profile
@raphw
raphw / Main.java
Created July 31, 2020 10:12
Example of generic delegation
public class Main {
public static void main(String[] args) {
Structure<Alpha, Alpha1, Alpha2> first = new First(new Alpha());
Structure<Beta, Beta1, Beta2> second = new Second(new Beta());
Structure<Object, Object, Object> template = new Template();
first.getValue().setValue(first.getValue().getValue());
//first.getValue().setValue(second.getValue().getValue());
//first.getValue().setValue(template.getValue().getValue());
@raphw
raphw / SampleRecord.java
Created June 10, 2020 21:02
Demo of type annotation errors
package foo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.*;
import java.util.concurrent.Callable;
public record SampleRecord(@RegularAnnotation @TypeAnnotation Callable<@TypeAnnotation ?>foo) {
@raphw
raphw / MyAutoConfiguration.java
Created May 15, 2020 13:44
Bean registration example
class MyAutoConfiguration {
@Bean
public static BeanFactoryPostProcessor behandleserviceRegistrationBeanFactory() {
return factory -> {
if (!(factory instanceof BeanDefinitionRegistry)) {
throw new IllegalStateException("Unsupported bean factory: " + factory.getClass().getTypeName());
}
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
String someBean = Arrays.stream(factory.getBeanNamesForType(SomeBeanType.class)).findFirst().orElseThrow();
@raphw
raphw / Executable_Constructor.patch
Created July 22, 2019 20:43
Fix for JDK-8202471.
Index: src/java.base/share/classes/java/lang/reflect/Executable.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/java.base/share/classes/java/lang/reflect/Executable.java (revision 55752:8ae33203d600a7c9f9b2be9b31a0eb8197270ab1)
+++ src/java.base/share/classes/java/lang/reflect/Executable.java (revision 55752+:8ae33203d600+)
@@ -38,6 +38,7 @@
import sun.reflect.annotation.AnnotationSupport;
import sun.reflect.annotation.TypeAnnotationParser;
@raphw
raphw / TypeAnnotation.java
Last active July 22, 2019 20:44
Fix for JDK-8202469 and JDK-8202473
package sample;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface TypeAnnotation {
@raphw
raphw / NativeBinding.java
Last active January 11, 2020 21:52
Example of a shadingproof JNI binding.
package com.shadeproof;
import net.bytebuddy.ByteBuddy;
public abstract class NativeBinding {
public static final NativeBinding INSTANCE;
static {
System.load("mylibrary");
@raphw
raphw / Bar.java
Created May 9, 2019 19:29
Class remapper ASM/Byte Buddy example
package x;
public class Bar extends Foo {
@Override
public void m() {
System.out.println("bar");
}
}
@raphw
raphw / bridge_generic.sh
Last active January 22, 2019 19:34
Generation of a visibility bridge method.
mkdir pkg1
echo "package pkg1; class SuperClass<X> { public X m() { return null; } }" > pkg1/SuperClass.java
echo "package pkg1; public class SubClass extends SuperClass<Void> { }" > pkg1/SubClass.java
${JAVA_HOME}/bin/javac pkg1/*.java
mkdir pkg2
echo "package pkg2; class Main { public static void main(String[] args) throws Exception { pkg1.SubClass.class.getMethod(\"m\").invoke(new pkg1.SubClass()); } }" > pkg2/Main.java
${JAVA_HOME}/bin/javac pkg2/*.java
${JAVA_HOME}/bin/java -cp . pkg2.Main
@raphw
raphw / bridge.sh
Created January 22, 2019 19:09
Generation of a visibility bridge method.
mkdir pkg1
echo "package pkg1; class SuperClass<X> { public X m() { return null; } }" > pkg1/SuperClass.java
echo "package pkg1; public class SubClass extends SuperClass<Void> { }" > pkg1/SubClass.java
${JAVA_HOME}/bin/javac pkg1/*.java
mkdir pkg2
echo "package pkg2; class Main { public static void main(String[] args) throws Exception { pkg1.SubClass.class.getMethod(\"m\").invoke(new pkg1.SubClass()); } }" > pkg2/Main.java
${JAVA_HOME}/bin/javac pkg2/*.java
${JAVA_HOME}/bin/java -cp . pkg2.Main
@raphw
raphw / ReactiveMain.java
Created October 22, 2018 11:35
A reactive stream processor
import static org.asynchttpclient.Dsl.asyncHttpClient;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;