Search in sources :

Example 26 with Method

use of org.objectweb.asm.commons.Method in project deltaspike by apache.

the class AsmProxyClassGenerator method defineSuperAccessorMethod.

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();
    // 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();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 27 with Method

use of org.objectweb.asm.commons.Method in project deltaspike by apache.

the class AsmProxyClassGenerator method defineMethod.

private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Class manualInvocationHandlerClass) {
    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();
    loadCurrentMethod(mg, method, methodType);
    loadArguments(mg, method, methodType);
    // invoke our ProxyInvocationHandler
    mg.invokeStatic(Type.getType(manualInvocationHandlerClass), Method.getMethod("Object staticInvoke(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(10, 10);
    mg.visitEnd();
}
Also used : Type(org.objectweb.asm.Type) ArrayList(java.util.ArrayList) Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) Annotation(java.lang.annotation.Annotation)

Example 28 with Method

use of org.objectweb.asm.commons.Method in project deltaspike by apache.

the class AsmProxyClassGenerator method defineDelegateInvocationHandlerConstructor.

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();
    // 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();
}
Also used : Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 29 with Method

use of org.objectweb.asm.commons.Method in project cdap by caskdata.

the class HttpHandlerGenerator method generateConstructor.

/**
   * Generates the constructor. The constructor generated has signature {@code (DelegatorContext, MetricsContext)}.
   */
private void generateConstructor(TypeToken<? extends HttpServiceHandler> delegateType, ClassWriter classWriter) {
    Method constructor = Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class);
    String signature = Signatures.getMethodSignature(constructor, TypeToken.of(void.class), getContextType(delegateType), TypeToken.of(MetricsContext.class));
    // Constructor(DelegatorContext, MetricsContext)
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, signature, null, classWriter);
    // super(context, metricsContext);
    mg.loadThis();
    mg.loadArg(0);
    mg.loadArg(1);
    mg.invokeConstructor(Type.getType(AbstractHttpHandlerDelegator.class), Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class));
    mg.returnValue();
    mg.endMethod();
}
Also used : MetricsContext(co.cask.cdap.api.metrics.MetricsContext) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method)

Example 30 with Method

use of org.objectweb.asm.commons.Method in project cdap by caskdata.

the class HttpHandlerGenerator method generateLogger.

/**
   * Generates a static Logger field for logging and a static initialization block to initialize the logger.
   */
private void generateLogger(Type classType, ClassWriter classWriter) {
    // private static final Logger LOG = LoggerFactory.getLogger(classType);
    classWriter.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, "LOG", Type.getType(Logger.class).getDescriptor(), null, null);
    Method init = Methods.getMethod(void.class, "<clinit>");
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_STATIC, init, null, null, classWriter);
    mg.visitLdcInsn(classType);
    mg.invokeStatic(Type.getType(LoggerFactory.class), Methods.getMethod(Logger.class, "getLogger", Class.class));
    mg.putStatic(classType, "LOG", Type.getType(Logger.class));
    mg.returnValue();
    mg.endMethod();
}
Also used : GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) Method(org.objectweb.asm.commons.Method) LoggerFactory(org.slf4j.LoggerFactory) Logger(org.slf4j.Logger)

Aggregations

Method (org.objectweb.asm.commons.Method)32 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)24 Type (org.objectweb.asm.Type)16 Label (org.objectweb.asm.Label)8 ClassWriter (org.objectweb.asm.ClassWriter)7 MethodVisitor (org.objectweb.asm.MethodVisitor)7 IOException (java.io.IOException)6 ClassReader (org.objectweb.asm.ClassReader)5 Schema (co.cask.cdap.api.data.schema.Schema)4 Set (java.util.Set)3 ClassVisitor (org.objectweb.asm.ClassVisitor)3 TypeToken (com.google.common.reflect.TypeToken)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)1 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)1 Encoder (co.cask.cdap.common.io.Encoder)1 Sets (com.google.common.collect.Sets)1 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)1 InterceptorType (com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorType)1 File (java.io.File)1