Example usage for org.objectweb.asm Opcodes RETURN

List of usage examples for org.objectweb.asm Opcodes RETURN

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes RETURN.

Prototype

int RETURN

To view the source code for org.objectweb.asm Opcodes RETURN.

Click Source Link

Usage

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createNoArgConstructor(final String enhancingClassName, final String enhancedClassName,
        final String descEnhancer, final ClassWriter cw) {
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, Enhancer.CONSTRUCTOR_INIT,
            Enhancer.makeDescription(Void.TYPE), null, null);
    mv.visitCode();//  www .j a va  2  s .  com

    final Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, enhancingClassName, Enhancer.CONSTRUCTOR_INIT,
            Enhancer.makeDescription(Void.TYPE));

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitInsn(Opcodes.ACONST_NULL);
    mv.visitFieldInsn(Opcodes.PUTFIELD, enhancedClassName, Enhancer.FIELD_ENHANCED_SESSION,
            Enhancer.DESCRIPTOR_SESSION);

    mv.visitInsn(Opcodes.RETURN);

    final Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable(Enhancer.THIS, descEnhancer, null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

private static void createOverrriddenMethod(final String enhancingClassName, final String enhancedClassName,
        final String descEnhancer, final ClassWriter cw, Method method) {
    final String methodDescription = Enhancer.makeDescription(method.getReturnType(),
            method.getParameterTypes());

    // TODO Exception types
    for (int i = 0; i < method.getExceptionTypes().length; i++) {
    }/* w  w w.  j  av a 2s.c  o m*/

    final MethodVisitor mv = cw.visitMethod(method.getModifiers(), method.getName(), methodDescription, null,
            null);
    mv.visitCode();

    // this.__enhanced_$$__check();
    final Label lCheck = new Label();
    mv.visitLabel(lCheck);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, enhancedClassName, Enhancer.METHOD_ENHANCED_CHECK,
            Enhancer.makeDescription(Void.TYPE));

    mv.visitVarInsn(Opcodes.ALOAD, 0); // load this

    // infer the method parameters
    for (int i = 0, r = 1; i < method.getParameterTypes().length; i++, r++) {
        final Class<?> paramClass = method.getParameterTypes()[i];
        mv.visitVarInsn(Enhancer.getLoadType(paramClass), r); // load parameter

        if ((paramClass == Double.TYPE) || (paramClass == Long.TYPE)) {
            r++;
        }
    }

    // super.method(...);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, enhancingClassName, method.getName(), methodDescription);

    if (method.getReturnType() != Void.TYPE) {
        // non void return
        mv.visitInsn(Enhancer.getReturnType(method.getReturnType()));
    } else {
        // void return
        mv.visitInsn(Opcodes.RETURN);
    }

    final Label lOut = new Label();
    mv.visitLabel(lOut);
    Enhancer.registerLocals(descEnhancer, method, mv, lCheck, lOut);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.boretti.drools.integration.drools5.DroolsClassVisitor.java

License:Open Source License

@Override
public void visitEnd() {
    FieldVisitor fv = null;/*ww w.ja va 2 s  .c  om*/
    if (isNeedChangeForBoth()) {
        fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_NAME, Type.BOOLEAN_TYPE.getDescriptor(), null,
                null);
        if (fv != null) {
            AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
            AnnotationVisitor value = av.visitArray("value");
            value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
            value.visitEnd();
            av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
            av.visitEnd();
            fv.visitEnd();
        }
    }
    if (isNeedChangeForField()) {
        fv = super.visitField(Opcodes.ACC_PRIVATE, DROOLS_FIELD_RULE,
                Type.getType(RuleBase.class).getDescriptor(), null, null);
        if (fv != null) {
            AnnotationVisitor av = fv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
            AnnotationVisitor value = av.visitArray("value");
            value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
            value.visitEnd();
            av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
            av.visitEnd();
            fv.visitEnd();
        }
        MethodVisitor mv = super.visitMethod(Opcodes.ACC_PRIVATE, DROOLS_METHOD_RUN, "()V", null, null);
        AnnotationVisitor av = mv.visitAnnotation(Type.getType(Generated.class).getDescriptor(), true);
        AnnotationVisitor value = av.visitArray("value");
        value.visit("", "Generated by Drools5IntegrationHelper Maven plugin");
        value.visitEnd();
        av.visit("date", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz").format(current));
        av.visitEnd();
        mv.visitCode();
        Label start = new Label();
        mv.visitLabel(start);
        Label doIt = new Label();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME,
                Type.BOOLEAN_TYPE.getDescriptor());
        mv.visitJumpInsn(Opcodes.IFEQ, doIt);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitLabel(doIt);
        mv.visitFrame(Opcodes.F_SAME, 1, new Object[] { Type.getObjectType(me).getInternalName() }, 0,
                new Object[] {});
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_RULE,
                Type.getType(RuleBase.class).getDescriptor());
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(RuleBase.class).getInternalName(),
                "newStatelessSession", "()Lorg/drools/StatelessSession;");
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn(FIELD_NAME_LOGGER);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(Object.class).getInternalName(), "getClass",
                "()Ljava/lang/Class;");
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getType(org.apache.log4j.Logger.class).getInternalName(),
                "getLogger", "(Ljava/lang/Class;)Lorg/apache/log4j/Logger;");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(),
                "setGlobal", "(Ljava/lang/String;Ljava/lang/Object;)V");
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getType(StatelessSession.class).getInternalName(),
                "execute", "(Ljava/lang/Object;)V");
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(me).getInternalName(), DROOLS_FIELD_NAME,
                Type.BOOLEAN_TYPE.getDescriptor());
        mv.visitInsn(Opcodes.RETURN);
        Label end = new Label();
        mv.visitLabel(end);
        mv.visitLocalVariable("this", Type.getObjectType(me).getDescriptor(), null, start, end, 0);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    super.visitEnd();
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addTraceReturn() {

    InsnList il = this.mn.instructions;

    Iterator<AbstractInsnNode> it = il.iterator();
    while (it.hasNext()) {
        AbstractInsnNode abstractInsnNode = it.next();

        switch (abstractInsnNode.getOpcode()) {
        case Opcodes.RETURN:
            il.insertBefore(abstractInsnNode, getVoidReturnTraceInstructions());
            break;
        case Opcodes.IRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
        case Opcodes.ARETURN:
        case Opcodes.DRETURN:
            il.insertBefore(abstractInsnNode, getReturnTraceInstructions());
        }//www. j  a va2  s.  c  om
    }
}

From source file:org.cacheonix.impl.transformer.ByteInstructionTest.java

License:LGPL

/**
 * Test method for {@link ByteInstruction#getReturnCode(String desc)} .
 *//*  ww  w . j a v  a  2s .  c  o m*/
public void testGetReturnCode() {

    String desc = "(IF)V";
    int ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.RETURN);

    desc = "(IF)I";
    ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.IRETURN);

    desc = "(IF)D";
    ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.DRETURN);

    desc = "(IF)Ljava/lang/Object;";
    ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.ARETURN);

}

From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java

License:Apache License

private static void genConstructor(ClassWriter cw, final String superClass) {
    MethodVisitor mv;// w w w. ja v  a  2  s . co  m
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superClass, "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java

License:Apache License

private static void genConstructor(ClassWriter cw, final String superClass, String internalName) {
    MethodVisitor mv;/*from  w  w  w.  j  av a2 s .  c  o m*/
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            "(Lorg/codehaus/groovy/runtime/callsite/CallSite;Lgroovy/lang/MetaClassImpl;Lgroovy/lang/MetaMethod;[Ljava/lang/Class;Ljava/lang/reflect/Constructor;)V",
            null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superClass, "<init>",
            "(Lorg/codehaus/groovy/runtime/callsite/CallSite;Lgroovy/lang/MetaClassImpl;Lgroovy/lang/MetaMethod;[Ljava/lang/Class;)V",
            false);

    mv.visitVarInsn(Opcodes.ALOAD, 5);
    mv.visitFieldInsn(Opcodes.PUTSTATIC, internalName, "__constructor__", "Ljava/lang/reflect/Constructor;");

    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.coldswap.transformer.ClInitTransformer.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   www  .  j a  v a 2s  .  co m
public byte[] transform(ClassLoader classLoader, String s, Class<?> aClass, ProtectionDomain protectionDomain,
        byte[] bytes) throws IllegalClassFormatException {
    if (s != null && !"".equals(s)) {
        for (String pack : ClassUtil.skipTransforming) {
            if (s.startsWith(pack)) {
                return bytes;
            }
        }

        ClassNode cn = new ClassNode(Opcodes.ASM5);
        ClassReader cr = new ClassReader(bytes);
        ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        // create adapter for method insertion.
        cr.accept(cn, 0);
        // insert <clinit>V if it is not inserted
        List methods = cn.methods;
        boolean clInitFound = false;
        for (MethodNode methodNode : (List<MethodNode>) methods) {
            if ("<clinit>".equals(methodNode.name)) {
                clInitFound = true;
            }
        }

        if (!clInitFound) {
            MethodNode mn = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
            InsnList insnList = mn.instructions;
            insnList.add(new LabelNode());
            insnList.add(new InsnNode(Opcodes.RETURN));
            cn.methods.add(mn);
        }

        cn.accept(cw);
        byte[] toRet = cw.toByteArray();
        if (toRet != null) {
            logger.info("Successful transformation!");
            return toRet;
        } else {
            logger.severe("Could not transform class");
            return bytes;
        }
    }
    return bytes;
}

From source file:org.coldswap.util.ByteCodeGenerator.java

License:Open Source License

/**
 * Creates a new class containing the new static field.
 *
 * @param classNode        containing the old class.
 * @param fieldNode        containing the old field.
 * @param initInstructions a list of instructions that goes into <clinit>.
 * @param className        the name of the new class to be generated.
 * @return an array of bytes which builds the new class.
 */// www.  j  a v a 2s . c  o m
@SuppressWarnings("unchecked")
public static byte[] newFieldClass(ClassNode classNode, FieldNode fieldNode, InsnList initInstructions,
        String className) {
    ClassNode newClass = new ClassNode();
    newClass.version = classNode.version;
    newClass.access = Opcodes.ACC_PUBLIC;
    newClass.signature = "L" + className + ";";
    newClass.name = className;
    newClass.superName = "java/lang/Object";
    newClass.fields.add(
            new FieldNode(fieldNode.access, fieldNode.name, fieldNode.desc, fieldNode.desc, fieldNode.value));
    if (initInstructions != null) {
        if (initInstructions.size() > 0) {
            MethodNode mn = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
            InsnList il = mn.instructions;
            il.add(new LabelNode());
            il.add(initInstructions);
            il.add(new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name, fieldNode.desc));
            il.add(new InsnNode(Opcodes.RETURN));
            newClass.methods.add(mn);
        }
    }

    ClassWriter newCWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    newClass.accept(newCWriter);
    return newCWriter.toByteArray();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionConstructorGenerator.java

License:Apache License

private static void createConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/*ww w . j  a  va2 s . co m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}