use of org.objectweb.asm.commons.Method in project deltaspike by apache.
the class AsmDeltaSpikeProxyClassGenerator method defineDeltaSpikeProxyMethods.
private static void defineDeltaSpikeProxyMethods(ClassWriter cw, Type proxyType) {
try {
// implement #setInvocationHandler
Method asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setInvocationHandler", DeltaSpikeProxyInvocationHandler.class));
GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
mg.visitCode();
mg.loadThis();
mg.loadArg(0);
mg.checkCast(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
mg.putField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
mg.returnValue();
mg.visitMaxs(2, 1);
mg.visitEnd();
// implement #getInvocationHandler
asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("getInvocationHandler"));
mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
mg.visitCode();
mg.loadThis();
mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
mg.returnValue();
mg.visitMaxs(2, 1);
mg.visitEnd();
// implement #setDelegateInvocationHandler
asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setDelegateInvocationHandler", InvocationHandler.class));
mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
mg.visitCode();
mg.loadThis();
mg.loadArg(0);
mg.checkCast(TYPE_INVOCATION_HANDLER);
mg.putField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, TYPE_INVOCATION_HANDLER);
mg.returnValue();
mg.visitMaxs(2, 1);
mg.visitEnd();
// implement #getDelegateInvocationHandler
asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("getDelegateInvocationHandler"));
mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
mg.visitCode();
mg.loadThis();
mg.getField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, TYPE_INVOCATION_HANDLER);
mg.returnValue();
mg.visitMaxs(2, 1);
mg.visitEnd();
// implement #setDelegateMethods
asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("setDelegateMethods", java.lang.reflect.Method[].class));
mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
mg.visitCode();
mg.loadThis();
mg.loadArg(0);
mg.checkCast(TYPE_METHOD_ARRAY);
mg.putField(proxyType, FIELDNAME_DELEGATE_METHODS, TYPE_METHOD_ARRAY);
mg.returnValue();
mg.visitMaxs(2, 1);
mg.visitEnd();
// implement #getDelegateMethods
asmMethod = Method.getMethod(DeltaSpikeProxy.class.getDeclaredMethod("getDelegateMethods"));
mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw);
mg.visitCode();
mg.loadThis();
mg.getField(proxyType, FIELDNAME_DELEGATE_METHODS, TYPE_METHOD_ARRAY);
mg.returnValue();
mg.visitMaxs(2, 1);
mg.visitEnd();
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Unable to implement " + DeltaSpikeProxy.class.getName(), e);
}
}
use of org.objectweb.asm.commons.Method in project deltaspike by apache.
the class AsmDeltaSpikeProxyClassGenerator method defineMethod.
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType) {
Type methodType = Type.getType(method);
ArrayList<Type> exceptionsToCatch = new ArrayList<Type>();
for (Class<?> exception : method.getExceptionTypes()) {
if (!RuntimeException.class.isAssignableFrom(exception)) {
exceptionsToCatch.add(Type.getType(exception));
}
}
// push the method definition
int modifiers = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED) & method.getModifiers();
Method asmMethod = Method.getMethod(method);
GeneratorAdapter mg = new GeneratorAdapter(modifiers, asmMethod, null, getTypes(method.getExceptionTypes()), cw);
// copy annotations
for (Annotation annotation : method.getDeclaredAnnotations()) {
mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd();
}
mg.visitCode();
Label tryBlockStart = mg.mark();
mg.loadThis();
mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER);
mg.loadThis();
loadCurrentMethod(mg, method, methodType);
loadArguments(mg, method, methodType);
mg.invokeVirtual(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER, Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])"));
// cast the result
mg.unbox(methodType.getReturnType());
// build try catch
Label tryBlockEnd = mg.mark();
// push return
mg.returnValue();
// catch runtime exceptions and rethrow it
Label rethrow = mg.mark();
mg.visitVarInsn(Opcodes.ASTORE, 1);
mg.visitVarInsn(Opcodes.ALOAD, 1);
mg.throwException();
mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, Type.getInternalName(RuntimeException.class));
// catch checked exceptions and rethrow it
boolean throwableCatched = false;
if (!exceptionsToCatch.isEmpty()) {
rethrow = mg.mark();
mg.visitVarInsn(Opcodes.ASTORE, 1);
mg.visitVarInsn(Opcodes.ALOAD, 1);
mg.throwException();
// catch declared exceptions and rethrow it...
for (Type exceptionType : exceptionsToCatch) {
if (exceptionType.getClassName().equals(Throwable.class.getName())) {
throwableCatched = true;
}
mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName());
}
}
// if throwable isn't alreached cachted, catch it and wrap it with an UndeclaredThrowableException and throw it
if (!throwableCatched) {
Type uteType = Type.getType(UndeclaredThrowableException.class);
Label wrapAndRethrow = mg.mark();
mg.visitVarInsn(Opcodes.ASTORE, 1);
mg.newInstance(uteType);
mg.dup();
mg.visitVarInsn(Opcodes.ALOAD, 1);
mg.invokeConstructor(uteType, Method.getMethod("void <init>(java.lang.Throwable)"));
mg.throwException();
mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, wrapAndRethrow, Type.getInternalName(Throwable.class));
}
// finish the method
mg.endMethod();
mg.visitMaxs(12, 12);
mg.visitEnd();
}
use of org.objectweb.asm.commons.Method in project Payara by payara.
the class ProviderImplGenerator method generateConstructor.
private void generateConstructor(ClassWriter cw, String generatedClassName, FlashlightProbeProvider provider) {
Method m = Method.getMethod("void <init> ()");
GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
gen.loadThis();
gen.invokeConstructor(Type.getType(Object.class), m);
Type probeRegType = Type.getType(ProbeRegistry.class);
Type probeType = Type.getType(FlashlightProbe.class);
gen.loadThis();
for (FlashlightProbe probe : provider.getProbes()) {
gen.dup();
String fieldName = "_flashlight_" + probe.getProbeName();
gen.push(probe.getId());
gen.invokeStatic(probeRegType, Method.getMethod("org.glassfish.flashlight.provider.FlashlightProbe getProbeById(int)"));
gen.visitFieldInsn(Opcodes.PUTFIELD, generatedClassName, fieldName, probeType.getDescriptor());
}
gen.pop();
// return the value from constructor
gen.returnValue();
gen.endMethod();
}
use of org.objectweb.asm.commons.Method in project Payara by payara.
the class BtraceClientGenerator method generateConstructor.
private static void generateConstructor(ClassWriter cw) {
Method m = Method.getMethod("void <init> ()");
GeneratorAdapter gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
gen.loadThis();
gen.invokeConstructor(Type.getType(Object.class), m);
// return the value from constructor
gen.returnValue();
gen.endMethod();
}
use of org.objectweb.asm.commons.Method in project robolectric by robolectric.
the class ClassInstrumentor method instrumentConstructor.
/**
* Constructors are instrumented as follows:
*
* <ul>
* <li>Code other than a call to the superclass constructor is moved to a new method named
* {@code __constructor__} with the same signature.
* <li>The constructor is modified to call {@link ClassHandler#initializing(Object)} (or {@link
* ClassHandler#getShadowCreator(Class)} for {@code invokedynamic} JVMs).
* <li>The constructor is modified to then call {@link ClassHandler#methodInvoked(String,
* boolean, Class)} (or {@link ClassHandler#findShadowMethodHandle(Class, String,
* MethodType, boolean)} for {@code invokedynamic} JVMs) with the method name {@code
* __constructor__} and the same parameter types.
* </ul>
*
* Note that most code in the constructor will not be executed unless the {@link ClassHandler}
* arranges for it to happen.
*
* <p>Given a constructor like this:
*
* <pre>
* public ThisClass(String name, int size) {
* super(name, someStaticMethod());
* this.size = size;
* }
* </pre>
*
* ... generates code like this:
*
* <pre>
* private $$robo$$__constructor__(String name, int size) {
* this.size = size;
* }
*
* private __constructor__(String name, int size) {
* Plan plan = RobolectricInternals.methodInvoked(
* "pkg/ThisClass/__constructor__(Ljava/lang/String;I)V", true, ThisClass.class);
* if (plan != null) {
* try {
* plan.run(this, new Object[] {name, size});
* } catch (Throwable t) {
* throw RobolectricInternals.cleanStackTrace(t);
* }
* } else {
* $$robo$$__constructor__(name, size);
* }
* }
*
* public ThisClass(String name, int size) {
* super(name, someStaticMethod());
* $$robo$init();
* }
* </pre>
*
* @param method the constructor to instrument
*/
private void instrumentConstructor(MutableClass mutableClass, MethodNode method) {
makeMethodPrivate(method);
InsnList callSuper = extractCallToSuperConstructor(mutableClass, method);
method.name = directMethodName(mutableClass, ShadowConstants.CONSTRUCTOR_METHOD_NAME);
mutableClass.addMethod(redirectorMethod(mutableClass, method, ShadowConstants.CONSTRUCTOR_METHOD_NAME));
String[] exceptions = exceptionArray(method);
MethodNode initMethodNode = new MethodNode(method.access, "<init>", method.desc, method.signature, exceptions);
makeMethodPublic(initMethodNode);
RobolectricGeneratorAdapter generator = new RobolectricGeneratorAdapter(initMethodNode);
initMethodNode.instructions = callSuper;
generator.loadThis();
generator.invokeVirtual(mutableClass.classType, new Method(ROBO_INIT_METHOD_NAME, "()V"));
generateClassHandlerCall(mutableClass, method, ShadowConstants.CONSTRUCTOR_METHOD_NAME, generator);
generator.endMethod();
mutableClass.addMethod(initMethodNode);
}
Aggregations