You can use a MutableCallSite with MethodHandles.constant if you really
need to to create "mostly final" variables. In addition null checks are
heavily optimised by the JVM to use segmentation faults and so can be
slightly faster than boolean checks sometimes. As well, JVM inlining handle
this quite naturally.
Altogether the full pattern is something like the following but with
exceptions and I typed this off the top of my head so I probably got the
ByteBuddy details incorrect:
public abstract class Trap {
public static Trap newInstance(CallSite cs) {
var clazz = return new ByteBuddy()
.subclass(Trap.class, Modifier.PUBLIC | Modifier.FINAL)
.defineMethod("getValue", Object.class, Modifier.PUBLIC | Modifier.
FINAL)
// strings can be constant pooled easily
.intercept(InvokeDynamic::/* some other stuff I don't remember
right now*/)
.defineField("methodHandle", CallSite.class, Modifier.PUBLIC |
Modifer.FINAL)
.make()
.load()
.getLoaded();
clazz.getField("methodHandle").set(null, cs);
return clazz.newInstance();
}
public abstract Object getValue();
}
public static final MutableCallSite CS = new MutableCallSite(MethodHandles.
constant(Object.class, ""));
public static final Trap PEOPLE_ARE_DOING_TRICKY_REFLECTION_STUFF_DEOPTIMIZE
= Trap.newInstance(CS);
public void mymethod() {
fastpath: {
if (PEOPLE_ARE_DOING_TRICKY_REFLECTION_STUFF_DEOPTIMIZE.getValue() ==
null) {
break fastpath;
}
// rest of code
return;
}
handleSlowpath();
}
But this is probably overkill.
--
You received this message because you are subscribed to the Google Groups
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.