List of usage examples for org.objectweb.asm.commons Method getMethod
public static Method getMethod(final String method)
From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java
License:Apache License
/** * Generate impl method.//from w w w . j a v a2s.c o m */ private void impl() { final Method run = Method.getMethod("Object run()"); final GeneratorAdapter mgen = new GeneratorAdapter(Opcodes.ACC_PUBLIC, run, null, exceptions, this); for (final Field field : fields) { mgen.loadThis(); mgen.getField(action, field.name, field.type); } mgen.invokeStatic(owner.target, helper); if (methd.getReturnType().getSort() < Type.ARRAY) { mgen.valueOf(methd.getReturnType()); } mgen.returnValue(); mgen.endMethod(); }
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
private static void defineConstructor(ClassWriter cw, Type proxyType, Type superType) { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, new Method("<init>", Type.VOID_TYPE, new Type[] {}), null, null, cw); mg.visitCode();// ww w. j a v a2 s . c o m // invoke super constructor mg.loadThis(); mg.invokeConstructor(superType, Method.getMethod("void <init> ()")); mg.returnValue(); mg.endMethod(); mg.visitEnd(); }
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
private static void definePartialBeanProxyMethods(ClassWriter cw, Type proxyType, Type invocationHandlerType) { try {// w w w .j av a2s .c o m // implement #setRedirectInvocationHandler Method asmMethod = Method.getMethod(PartialBeanProxy.class .getDeclaredMethod("setRedirectInvocationHandler", InvocationHandler.class)); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw); mg.visitCode(); mg.loadThis(); mg.loadArg(0); mg.checkCast(invocationHandlerType); mg.putField(proxyType, FIELDNAME_HANDLER, invocationHandlerType); mg.returnValue(); mg.visitMaxs(2, 1); mg.visitEnd(); // implement #getRedirectInvocationHandler asmMethod = Method.getMethod(PartialBeanProxy.class.getDeclaredMethod("getRedirectInvocationHandler")); mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, asmMethod, null, null, cw); mg.visitCode(); mg.loadThis(); mg.getField(proxyType, FIELDNAME_HANDLER, invocationHandlerType); mg.returnValue(); mg.visitMaxs(2, 1); mg.visitEnd(); } catch (NoSuchMethodException e) { throw new IllegalStateException("Unable to implement " + PartialBeanProxy.class.getName(), e); } }
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType, Type invocationHandlerType, Type superType, boolean callInvocationHandler) { Type methodType = Type.getType(method); Type[] exceptionTypes = getTypes(method.getExceptionTypes()); // 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, exceptionTypes, cw); // copy annotations for (Annotation annotation : method.getDeclaredAnnotations()) { mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd(); }// w ww . j a va 2s. c o m mg.visitCode(); Label tryBlockStart = mg.mark(); mg.loadThis(); loadCurrentMethod(mg, method, methodType); loadArguments(mg, method, methodType); // invoke our ProxyInvocationHandler mg.invokeStatic(Type.getType(ManualInvocationHandler.class), Method.getMethod("Object staticInvoke(Object, java.lang.reflect.Method, Object[])")); // cast the result mg.unbox(methodType.getReturnType()); Label tryBlockEnd = mg.mark(); // push return mg.returnValue(); boolean throwableCatched = false; // catch ProceedOriginalRuntimeException Label proceedOriginal = mg.mark(); if (callInvocationHandler) { // call stored InvocationHandler mg.loadThis(); mg.getField(proxyType, FIELDNAME_HANDLER, invocationHandlerType); mg.loadThis(); loadCurrentMethod(mg, method, methodType); loadArguments(mg, method, methodType); mg.invokeVirtual(invocationHandlerType, Method.getMethod("Object invoke(Object, java.lang.reflect.Method, Object[])")); mg.unbox(methodType.getReturnType()); mg.returnValue(); } else { // call super method mg.loadThis(); mg.loadArgs(); mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(), Type.getMethodDescriptor(method), false); mg.returnValue(); } mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, proceedOriginal, Type.getInternalName(ProceedOriginalMethodException.class)); // catch declared exceptions if (exceptionTypes.length > 0) { Label rethrow = mg.mark(); mg.visitVarInsn(Opcodes.ASTORE, 1); mg.visitVarInsn(Opcodes.ALOAD, 1); mg.throwException(); // catch declared exceptions and rethrow it... for (Type exceptionType : exceptionTypes) { if (exceptionType.getClassName().equals(Throwable.class.getName())) { throwableCatched = true; } mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName()); } } if (!throwableCatched) { // catch Throwable and wrap it with a UndeclaredThrowableException 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(10, 10); mg.visitEnd(); }
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
/** * Generates:/* w w w. j av a2s. com*/ * <pre> * Method method = * method.getDeclaringClass().getMethod("methodName", new Class[] { args... }); * </pre> * @param mg * @param method * @param methodType */ private static void loadCurrentMethod(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType) { mg.push(Type.getType(method.getDeclaringClass())); mg.push(method.getName()); // create the Class[] mg.push(methodType.getArgumentTypes().length); mg.newArray(TYPE_CLASS); // push parameters into array for (int i = 0; i < methodType.getArgumentTypes().length; i++) { // keep copy of array on stack mg.dup(); // push index onto stack mg.push(i); mg.push(methodType.getArgumentTypes()[i]); mg.arrayStore(TYPE_CLASS); } // invoke getMethod() with the method name and the array of types mg.invokeVirtual(TYPE_CLASS, Method.getMethod("java.lang.reflect.Method getDeclaredMethod(String, Class[])")); }
From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java
License:Apache License
private static void defineDefaultConstructor(ClassWriter cw, Type proxyType, Type superType) { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, new Method("<init>", Type.VOID_TYPE, new Type[] {}), null, null, cw); mg.visitCode();/*from w w w. j av a 2 s . c om*/ // invoke super constructor mg.loadThis(); mg.invokeConstructor(superType, Method.getMethod("void <init> ()")); mg.returnValue(); mg.endMethod(); mg.visitEnd(); }
From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java
License:Apache License
private static void defineDeltaSpikeProxyMethods(ClassWriter cw, Type proxyType) { try {/* ww w . ja va 2s.c o m*/ // 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); } }
From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java
License:Apache License
private static void defineSuperAccessorMethod(ClassWriter cw, java.lang.reflect.Method method, Type superType, String superAccessorMethodSuffix) { Method originalAsmMethod = Method.getMethod(method); Method newAsmMethod = new Method(method.getName() + superAccessorMethodSuffix, originalAsmMethod.getReturnType(), originalAsmMethod.getArgumentTypes()); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, newAsmMethod, null, null, cw); mg.visitCode();//www. j a va 2 s. co m // call super method mg.loadThis(); mg.loadArgs(); mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(), Type.getMethodDescriptor(method), false); mg.returnValue(); // finish the method mg.endMethod(); mg.visitMaxs(10, 10); mg.visitEnd(); }
From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java
License:Apache License
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)); }/*www. j a v a 2s . com*/ } // 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(); }
From source file:org.apache.deltaspike.proxy.impl.AsmProxyClassGenerator.java
License:Apache License
private static void defineDelegateInvocationHandlerConstructor(ClassWriter cw, Type proxyType, Type superType, Type delegateInvocationHandlerType) { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, new Method("<init>", Type.VOID_TYPE, new Type[] { delegateInvocationHandlerType }), null, null, cw); mg.visitCode();// w w w. j av a 2 s . c om // invoke super constructor mg.loadThis(); mg.invokeConstructor(superType, Method.getMethod("void <init> ()")); // set invocation handler mg.loadThis(); mg.loadArg(0); mg.putField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, delegateInvocationHandlerType); mg.returnValue(); mg.endMethod(); mg.visitEnd(); }