Skip to content

Instantly share code, notes, and snippets.

Avatar

Rafael Winterhalter raphw

View GitHub Profile
@raphw
raphw / run.sh
Last active May 13, 2022
Runs Mockito on Graal VM
View run.sh
#!/bin/bash
set -x
set -e
if [[ -z $1 ]]; then
echo "Specify location of Graal VM as first argument"
exit 1
fi
$1/bin/gu install native-image
rm -rf META-INF sample sample.build_artifacts.txt Sample.* *.jar
echo "public class Sample {
@raphw
raphw / XsdSample.java
Last active Aug 14, 2021
Sample for use of schemata without location specification.
View XsdSample.java
import org.w3c.dom.ls.LSInput;
import javax.xml.XMLConstants;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
@raphw
raphw / Main.java
Created Jul 31, 2020
Example of generic delegation
View Main.java
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 Jun 10, 2020
Demo of type annotation errors
View SampleRecord.java
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
Bean registration example
View MyAutoConfiguration.java
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();
View Executable_Constructor.patch
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 Jul 22, 2019
Fix for JDK-8202469 and JDK-8202473
View TypeAnnotation.java
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 Jan 11, 2020
Example of a shadingproof JNI binding.
View NativeBinding.java
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
Class remapper ASM/Byte Buddy example
View Bar.java
package x;
public class Bar extends Foo {
@Override
public void m() {
System.out.println("bar");
}
}
@raphw
raphw / bridge_generic.sh
Last active Jan 22, 2019
Generation of a visibility bridge method.
View bridge_generic.sh
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