Skip to content

Instantly share code, notes, and snippets.

View raphw's full-sized avatar

Rafael Winterhalter raphw

View GitHub Profile
@raphw
raphw / event.txt
Last active February 8, 2017 19:20
XML Stream sample
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
try (InputStream in = new FileInputStream("/home/rafael/foo.xml")) {
XMLEventReader xer = xif.createXMLEventReader(in);
XMLEventWriter xew = xof.createXMLEventWriter(System.out);
JAXBContext jc = JAXBContext.newInstance(Bar.class);
@raphw
raphw / Dispatcher.java
Created January 2, 2017 00:11
Call protected method without Unsafe on Java 9.
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.pool.TypePool;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@raphw
raphw / Foo.java
Created October 11, 2016 15:10
Dead code without return sample.
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import static net.bytebuddy.matcher.ElementMatchers.named;
public class Foo {
@raphw
raphw / WeakeningAgent.java
Last active January 23, 2021 11:40
A Java agent for fixing exports for an app that is not yet Java 9 aware.
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.*;
public class WeakeningAgent {
public static void premain(String argument, Instrumentation instrumentation) {
boolean full = argument != null && argument.equals("full");
Set<Module> importing = new HashSet<>(), exporting = new HashSet<>();
@raphw
raphw / Foo.java
Last active September 23, 2016 09:30
Java agent problem on J9.
public class Foo {
public static void main(String[] args) throws UnmodifiableClassException, InterruptedException {
// jar: https://bintray.com/raphw/maven/download_file?file_path=net%2Fbytebuddy%2Fbyte-buddy-agent%2F1.4.26%2Fbyte-buddy-agent-1.4.26.jar
Instrumentation instrumentation = ByteBuddyAgent.install();
System.out.println(instrumentation.isRedefineClassesSupported());
System.out.println(instrumentation.isRetransformClassesSupported());
@raphw
raphw / keybase.md
Last active February 16, 2017 22:13
keybase.md

Keybase proof

I hereby claim:

  • I am raphw on github.
  • I am raphw (https://keybase.io/raphw) on keybase.
  • I have a public key ASCRLoOS-VdPyzfuhZ_xgec5Zht5jaGp2ScPVs_qJ-kXEgo

To claim this, I am signing this object:

@raphw
raphw / Bar.java
Last active March 2, 2016 21:04
Weak parent class loader test
public class Bar {
public static Object baz() {
return new Object();
}
}
@raphw
raphw / BootstrapAgent.java
Last active March 6, 2025 18:06
An example agent that intercepts a method of the bootstrap class loader.
package net.bytebuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
@raphw
raphw / Foo.java
Last active January 21, 2016 08:47
Incorrect resolution of type annotations on nested parameterized type.
public class Foo {
public static void main(String[] args) throws Exception {
System.out.println(Arrays.asList(Foo.class.getDeclaredField("bar").getAnnotatedType().getAnnotations()));
System.out.println(Arrays.asList(Foo.class.getDeclaredField("qux").getAnnotatedType().getAnnotations()));
}
GenericNested<Void>.@TypeAnnotation Inner bar;
Nested.@TypeAnnotation Inner qux;
@raphw
raphw / Foo.java
Last active January 20, 2016 11:08
Annotations for owner types are invers to the type navigation order.
class Foo<T> {
abstract class Bar {
// type path is not null but .
abstract void qux(@TypeAnnotation(1) Bar arg);
abstract void baz(Foo<T>.@TypeAnnotation(7) Bar arg);
}
}
@Target(Element.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)