Example usage for org.objectweb.asm.commons Method Method

List of usage examples for org.objectweb.asm.commons Method Method

Introduction

In this page you can find the example usage for org.objectweb.asm.commons Method Method.

Prototype

public Method(final String name, final Type returnType, final Type[] argumentTypes) 

Source Link

Document

Constructs a new Method .

Usage

From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java

License:Apache License

/**
 * Add fields and generate constructor./*from w  w  w.  j a v a  2 s .com*/
 */
private void init() {
    for (final Field field : fields) {
        visitField(field.access, field.name, field.type.getDescriptor(), null, null).visitEnd();
    }
    final Method init = new Method("<init>", Type.VOID_TYPE, helper.getArgumentTypes());

    final GeneratorAdapter mgen = new GeneratorAdapter(0, init, null, Privilizer.EMPTY_TYPE_ARRAY, this);

    mgen.visitCode();
    final Label begin = mgen.mark();

    // invoke super constructor
    mgen.loadThis();
    mgen.invokeConstructor(Type.getType(Object.class), Method.getMethod("void <init> ()"));
    // assign remaining fields

    int arg = 0;
    for (final Field field : fields) {
        mgen.loadThis();
        mgen.loadArg(arg++);
        mgen.putField(action, field.name, field.type);
    }
    mgen.returnValue();
    final Label end = mgen.mark();

    // declare local vars
    mgen.visitLocalVariable("this", action.getDescriptor(), null, begin, end, 0);
    arg = 1;
    for (final Field field : fields) {
        mgen.visitLocalVariable("arg" + arg, field.type.getDescriptor(), null, begin, end, arg++);
    }
    mgen.endMethod();
}

From source file:org.apache.commons.weaver.privilizer.PrivilizingVisitor.java

License:Apache License

/**
 * Generates the instructions to push onto the stack whether there is a
 * security manager available./* w  w w.  ja v a  2  s  .c om*/
 * @param mgen to control
 */
private static void checkSecurityManager(final GeneratorAdapter mgen) {
    final Label setFalse = new Label();
    final Label done = new Label();
    mgen.invokeStatic(Type.getType(System.class),
            new Method("getSecurityManager", Type.getType(SecurityManager.class), Privilizer.EMPTY_TYPE_ARRAY));
    mgen.ifNull(setFalse);
    mgen.push(true);
    mgen.goTo(done);
    mgen.mark(setFalse);
    mgen.push(false);
    mgen.mark(done);
}

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 va2  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.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 ww  .  ja  v a  2 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.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();/* w w w  .ja  v  a2s  .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.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();//from w  ww  .j a  va 2 s .  c o  m

    // 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();
}

From source file:org.apache.deltaspike.proxy.util.AsmProxyClassGenerator.java

License:Apache License

private static void defineDelegateInvocationHandlerConstructor(ClassWriter cw, Type proxyType, Type superType,
        Type invocationHandlerType) {
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC,
            new Method("<init>", Type.VOID_TYPE, new Type[] { invocationHandlerType }), null, null, cw);

    mg.visitCode();/*from  w  w w . j a  v a 2s .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, invocationHandlerType);

    mg.returnValue();
    mg.endMethod();

    mg.visitEnd();
}

From source file:org.apache.twill.yarn.CustomClassLoader.java

License:Apache License

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    if (!CustomClassLoaderRunnable.GENERATED_CLASS_NAME.equals(name)) {
        return super.findClass(name);
    }//  w  w  w .ja va  2s . c  o m

    // Generate a class that look like this:
    //
    // public class Generated {
    //
    //   public void announce(ServiceAnnouncer announcer, String serviceName, int port) {
    //     announcer.announce(serviceName, port);
    //   }
    // }
    Type generatedClassType = Type
            .getObjectType(CustomClassLoaderRunnable.GENERATED_CLASS_NAME.replace('.', '/'));
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, generatedClassType.getInternalName(), null,
            Type.getInternalName(Object.class), null);

    // Generate the default constructor, which just call super();
    Method constructor = new Method("<init>", Type.VOID_TYPE, new Type[0]);
    GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, null, null, cw);
    mg.loadThis();
    mg.invokeConstructor(Type.getType(Object.class), constructor);
    mg.returnValue();
    mg.endMethod();

    // Generate the announce method
    Method announce = new Method("announce", Type.VOID_TYPE,
            new Type[] { Type.getType(ServiceAnnouncer.class), Type.getType(String.class), Type.INT_TYPE });
    mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, announce, null, null, cw);
    mg.loadArg(0);
    mg.loadArg(1);
    mg.loadArg(2);
    mg.invokeInterface(Type.getType(ServiceAnnouncer.class), new Method("announce",
            Type.getType(Cancellable.class), new Type[] { Type.getType(String.class), Type.INT_TYPE }));
    mg.pop();
    mg.returnValue();
    mg.endMethod();
    cw.visitEnd();

    byte[] byteCode = cw.toByteArray();
    return defineClass(CustomClassLoaderRunnable.GENERATED_CLASS_NAME, byteCode, 0, byteCode.length);
}

From source file:org.glowroot.agent.weaving.PluginClassRenamer.java

License:Apache License

private @Nullable Method hack(@Nullable Method method) {
    if (method == null) {
        return null;
    }//from   w w  w.  jav  a  2  s . c o m
    Type[] argumentTypes = method.getArgumentTypes();
    Type[] hackedArgumentTypes = new Type[argumentTypes.length];
    for (int i = 0; i < argumentTypes.length; i++) {
        hackedArgumentTypes[i] = hack(argumentTypes[i]);
    }
    return new Method(method.getName(), hack(method.getReturnType()), hackedArgumentTypes);
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

/**
 * Generates the instructions to box the top stack value. This value is
 * replaced by its boxed equivalent on top of the stack.
 *
 * @param type the type of the top stack value.
 */// w w  w. ja  va  2s  .co m
public void box(final Type type) {
    if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
        return;
    }
    if (type == Type.VOID_TYPE) {
        push((String) null);
    } else {
        Type boxed = type;
        switch (type.getSort()) {
        case Type.BYTE:
            boxed = BYTE_TYPE;
            break;
        case Type.BOOLEAN:
            boxed = BOOLEAN_TYPE;
            break;
        case Type.SHORT:
            boxed = SHORT_TYPE;
            break;
        case Type.CHAR:
            boxed = CHARACTER_TYPE;
            break;
        case Type.INT:
            boxed = INTEGER_TYPE;
            break;
        case Type.FLOAT:
            boxed = FLOAT_TYPE;
            break;
        case Type.LONG:
            boxed = LONG_TYPE;
            break;
        case Type.DOUBLE:
            boxed = DOUBLE_TYPE;
            break;
        }
        newInstance(boxed);
        if (type.getSize() == 2) {
            // Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o
            dupX2();
            dupX2();
            pop();
        } else {
            // p -> po -> opo -> oop -> o
            dupX1();
            swap();
        }
        invokeConstructor(boxed, new Method("<init>", Type.VOID_TYPE, new Type[] { type }));
    }
}