Search in sources :

Example 1 with MethodVisitor

use of jodd.asm5.MethodVisitor in project jodd by oblac.

the class ProxettaAsmUtil method visitReturn.

// ---------------------------------------------------------------- return
/**
	 * Visits return opcodes.
	 */
public static void visitReturn(MethodVisitor mv, MethodInfo methodInfo, boolean isLast) {
    switch(methodInfo.getReturnOpcodeType()) {
        case 'V':
            if (isLast) {
                mv.visitInsn(POP);
            }
            mv.visitInsn(RETURN);
            break;
        case 'B':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(label);
                AsmUtil.byteValue(mv);
            }
            mv.visitInsn(IRETURN);
            break;
        case 'C':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(label);
                AsmUtil.charValue(mv);
            }
            mv.visitInsn(IRETURN);
            break;
        case 'S':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(label);
                AsmUtil.shortValue(mv);
            }
            mv.visitInsn(IRETURN);
            break;
        case 'I':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(label);
                AsmUtil.intValue(mv);
            }
            mv.visitInsn(IRETURN);
            break;
        case 'Z':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(ICONST_0);
                mv.visitInsn(IRETURN);
                mv.visitLabel(label);
                AsmUtil.booleanValue(mv);
            }
            mv.visitInsn(IRETURN);
            break;
        case 'J':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(LCONST_0);
                mv.visitInsn(LRETURN);
                mv.visitLabel(label);
                AsmUtil.longValue(mv);
            }
            mv.visitInsn(LRETURN);
            break;
        case 'F':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(FCONST_0);
                mv.visitInsn(FRETURN);
                mv.visitLabel(label);
                AsmUtil.floatValue(mv);
            }
            mv.visitInsn(FRETURN);
            break;
        case 'D':
            if (isLast) {
                mv.visitInsn(DUP);
                Label label = new Label();
                mv.visitJumpInsn(IFNONNULL, label);
                mv.visitInsn(POP);
                mv.visitInsn(DCONST_0);
                mv.visitInsn(DRETURN);
                mv.visitLabel(label);
                AsmUtil.doubleValue(mv);
            }
            mv.visitInsn(DRETURN);
            break;
        default:
            mv.visitInsn(ARETURN);
            break;
    }
}
Also used : Label(jodd.asm5.Label)

Example 2 with MethodVisitor

use of jodd.asm5.MethodVisitor in project jodd by oblac.

the class ProxettaClassBuilder method makeProxyConstructor.

/**
	 * Creates init method that simply calls all advice constructor methods in correct order.
	 * This created init method is called from each destination's constructor.
	 */
protected void makeProxyConstructor() {
    MethodVisitor mv = wd.dest.visitMethod(AsmUtil.ACC_PRIVATE | AsmUtil.ACC_FINAL, initMethodName, DESC_VOID, null, null);
    mv.visitCode();
    if (wd.adviceInits != null) {
        for (String name : wd.adviceInits) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, wd.thisReference, name, DESC_VOID, false);
        }
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : MethodVisitor(jodd.asm5.MethodVisitor)

Example 3 with MethodVisitor

use of jodd.asm5.MethodVisitor in project jodd by oblac.

the class ProxettaMethodBuilder method createProxyMethod.

/**
	 * Creates proxy methods over target method, For each matched proxy, new proxy method is created
	 * by taking advice bytecode and replaces usages of {@link jodd.proxetta.ProxyTarget}.
	 * <p>
	 * Invocation chain example: {@code name -> name$p0 -> name$p1 -> name$p4 -> super}.
	 */
public void createProxyMethod(final TargetMethodData td) {
    final ProxyAspectData aspectData = td.getProxyData();
    int access = td.msign.getAccessFlags();
    access &= ~ACC_NATIVE;
    access &= ~ACC_ABSTRACT;
    access = ProxettaAsmUtil.makePrivateFinalAccess(access);
    final MethodVisitor mv = wd.dest.visitMethod(access, td.methodName(), td.msign.getDescription(), null, null);
    mv.visitCode();
    //*** VISIT ADVICE - called for each aspect and each method
    aspectData.getAdviceClassReader().accept(new EmptyClassVisitor() {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            if (!name.equals(executeMethodName)) {
                return null;
            }
            return new HistoryMethodAdapter(mv) {

                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (owner.equals(aspectData.adviceReference)) {
                        // [F5]
                        owner = wd.thisReference;
                        name = adviceFieldName(name, aspectData.aspectIndex);
                    }
                    super.visitFieldInsn(opcode, owner, name, desc);
                }

                @Override
                public void visitVarInsn(int opcode, int var) {
                    var += (var == 0 ? 0 : td.msign.getAllArgumentsSize());
                    if (proxyInfoRequested) {
                        proxyInfoRequested = false;
                        if (opcode == ASTORE) {
                            ProxyTargetReplacement.info(mv, td.msign, var);
                        }
                    }
                    // [F1]
                    super.visitVarInsn(opcode, var);
                }

                @Override
                public void visitIincInsn(int var, int increment) {
                    var += (var == 0 ? 0 : td.msign.getAllArgumentsSize());
                    // [F1]
                    super.visitIincInsn(var, increment);
                }

                @Override
                public void visitInsn(int opcode) {
                    if (opcode == ARETURN) {
                        visitReturn(mv, td.msign, true);
                        return;
                    }
                    if (traceNext) {
                        if ((opcode == POP) || (opcode == POP2)) {
                            // [F3] - invoke invoked without assignment
                            return;
                        }
                    }
                    super.visitInsn(opcode);
                }

                @SuppressWarnings({ "ParameterNameDiffersFromOverriddenParameter" })
                @Override
                public void visitMethodInsn(int opcode, String string, String mname, String mdesc, boolean isInterface) {
                    if ((opcode == INVOKEVIRTUAL) || (opcode == INVOKEINTERFACE) || (opcode == INVOKESPECIAL)) {
                        if (string.equals(aspectData.adviceReference)) {
                            string = wd.thisReference;
                            mname = adviceMethodName(mname, aspectData.aspectIndex);
                        }
                    } else if (opcode == INVOKESTATIC) {
                        if (string.equals(aspectData.adviceReference)) {
                            string = wd.thisReference;
                            mname = adviceMethodName(mname, aspectData.aspectIndex);
                        } else if (string.endsWith('/' + TARGET_CLASS_NAME)) {
                            if (isInvokeMethod(mname, mdesc)) {
                                // [R7]
                                if (td.isLastMethodInChain()) {
                                    if (!wd.isWrapper()) {
                                        // PROXY
                                        loadSpecialMethodArguments(mv, td.msign);
                                        mv.visitMethodInsn(INVOKESPECIAL, wd.superReference, td.msign.getMethodName(), td.msign.getDescription(), isInterface);
                                    } else {
                                        // WRAPPER
                                        mv.visitVarInsn(ALOAD, 0);
                                        mv.visitFieldInsn(GETFIELD, wd.thisReference, wd.wrapperRef, wd.wrapperType);
                                        loadVirtualMethodArguments(mv, td.msign);
                                        if (wd.wrapInterface) {
                                            mv.visitMethodInsn(INVOKEINTERFACE, wd.wrapperType.substring(1, wd.wrapperType.length() - 1), td.msign.getMethodName(), td.msign.getDescription(), true);
                                        } else {
                                            mv.visitMethodInsn(INVOKEVIRTUAL, wd.wrapperType.substring(1, wd.wrapperType.length() - 1), td.msign.getMethodName(), td.msign.getDescription(), isInterface);
                                        }
                                    }
                                    // [F4]
                                    prepareReturnValue(mv, td.msign, aspectData.maxLocalVarOffset);
                                    traceNext = true;
                                } else {
                                    // calls next proxy method
                                    loadSpecialMethodArguments(mv, td.msign);
                                    mv.visitMethodInsn(INVOKESPECIAL, wd.thisReference, td.nextMethodName(), td.msign.getDescription(), isInterface);
                                    visitReturn(mv, td.msign, false);
                                }
                                return;
                            }
                            if (isArgumentsCountMethod(mname, mdesc)) {
                                // [R2]
                                ProxyTargetReplacement.argumentsCount(mv, td.msign);
                                return;
                            }
                            if (isArgumentTypeMethod(mname, mdesc)) {
                                // [R3]
                                int argIndex = this.getArgumentIndex();
                                ProxyTargetReplacement.argumentType(mv, td.msign, argIndex);
                                return;
                            }
                            if (isArgumentMethod(mname, mdesc)) {
                                // [R4]
                                int argIndex = this.getArgumentIndex();
                                ProxyTargetReplacement.argument(mv, td.msign, argIndex);
                                return;
                            }
                            if (isSetArgumentMethod(mname, mdesc)) {
                                // [R5]
                                int argIndex = this.getArgumentIndex();
                                checkArgumentIndex(td.msign, argIndex);
                                mv.visitInsn(POP);
                                storeMethodArgumentFromObject(mv, td.msign, argIndex);
                                return;
                            }
                            if (isCreateArgumentsArrayMethod(mname, mdesc)) {
                                // [R6]
                                ProxyTargetReplacement.createArgumentsArray(mv, td.msign);
                                return;
                            }
                            if (isCreateArgumentsClassArrayMethod(mname, mdesc)) {
                                // [R11]
                                ProxyTargetReplacement.createArgumentsClassArray(mv, td.msign);
                                return;
                            }
                            if (isTargetMethod(mname, mdesc)) {
                                // [R9.1]
                                mv.visitVarInsn(ALOAD, 0);
                                return;
                            }
                            if (isTargetClassMethod(mname, mdesc)) {
                                // [R9]
                                ProxyTargetReplacement.targetClass(mv, td.msign);
                                //ProxyTargetReplacement.targetClass(mv, wd.superReference);
                                return;
                            }
                            if (isTargetMethodNameMethod(mname, mdesc)) {
                                // [R10]
                                ProxyTargetReplacement.targetMethodName(mv, td.msign);
                                return;
                            }
                            if (isTargetMethodSignatureMethod(mname, mdesc)) {
                                ProxyTargetReplacement.targetMethodSignature(mv, td.msign);
                                return;
                            }
                            if (isTargetMethodDescriptionMethod(mname, mdesc)) {
                                ProxyTargetReplacement.targetMethodDescription(mv, td.msign);
                                return;
                            }
                            if (isInfoMethod(mname, mdesc)) {
                                // we are NOT replacing info() here! First, we need to figure out
                                // what is the operand for the very next ASTORE instructions
                                // since we need to create an object and store it in this
                                // register - and reuse it, in replacement code.
                                //ProxyTargetReplacement.info(mv, td.msign);
                                proxyInfoRequested = true;
                                return;
                            }
                            if (isReturnTypeMethod(mname, mdesc)) {
                                // [R11]
                                ProxyTargetReplacement.returnType(mv, td.msign);
                                return;
                            }
                            if (isReturnValueMethod(mname, mdesc)) {
                                castToReturnType(mv, td.msign);
                                return;
                            }
                            if (isTargetMethodAnnotationMethod(mname, mdesc)) {
                                String[] args = getLastTwoStringArguments();
                                // pop current two args
                                mv.visitInsn(POP);
                                mv.visitInsn(POP);
                                ProxyTargetReplacement.targetMethodAnnotation(mv, td.msign, args);
                                return;
                            }
                            if (isTargetClassAnnotationMethod(mname, mdesc)) {
                                String[] args = getLastTwoStringArguments();
                                // pop current two args
                                mv.visitInsn(POP);
                                mv.visitInsn(POP);
                                ProxyTargetReplacement.targetClassAnnotation(mv, td.msign.getClassInfo(), args);
                                return;
                            }
                        }
                    }
                    super.visitMethodInsn(opcode, string, mname, mdesc, isInterface);
                }
            };
        }
    }, 0);
}
Also used : EmptyClassVisitor(jodd.asm.EmptyClassVisitor) EmptyMethodVisitor(jodd.asm.EmptyMethodVisitor) MethodVisitor(jodd.asm5.MethodVisitor)

Example 4 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class Cmp2Generator method createEjbStore.

public void createEjbStore() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "ejbStore", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 5 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class Cmp2Generator method createEjbActivate.

public void createEjbActivate() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "ejbActivate", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm5.MethodVisitor)27 MethodVisitor (jodd.asm5.MethodVisitor)6 ClassWriter (org.apache.xbean.asm5.ClassWriter)5 Label (org.apache.xbean.asm5.Label)5 Method (java.lang.reflect.Method)2 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)2 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)2 Label (jodd.asm5.Label)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 MethodAdapter (jodd.asm.MethodAdapter)1 FieldVisitor (jodd.asm5.FieldVisitor)1 Type (jodd.asm5.Type)1 ProxettaException (jodd.proxetta.ProxettaException)1 Type (org.apache.xbean.asm5.Type)1