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

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

Introduction

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

Prototype

public String getDescriptor() 

Source Link

Document

Returns the descriptor of the method described by this object.

Usage

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

License:Apache License

private void weaveOnReturnAdvice(int opcode, Advice advice, Method onReturnAdvice) {
    boolean leaveReturnValueOnStack = onReturnAdvice.getReturnType().getSort() == Type.VOID;
    if (onReturnAdvice.getArgumentTypes().length > 0) {
        // @BindReturn must be the first argument to @OnReturn (if present)
        int startIndex = 0;
        AdviceParameter parameter = advice.onReturnParameters().get(0);
        switch (parameter.kind()) {
        case RETURN:
            loadNonOptionalReturnValue(opcode, parameter, leaveReturnValueOnStack);
            startIndex = 1;//from  www .  j ava 2s. c  om
            break;
        case OPTIONAL_RETURN:
            loadOptionalReturnValue(opcode, leaveReturnValueOnStack);
            startIndex = 1;
            break;
        default:
            // first argument is not @BindReturn
            break;
        }
        loadMethodParameters(advice.onReturnParameters(), startIndex, travelerLocals.get(advice),
                advice.adviceType(), OnReturn.class, true);
    }
    visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onReturnAdvice.getName(),
            onReturnAdvice.getDescriptor(), false);
}

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

License:Apache License

private void visitOnThrowAdvice(Advice advice) {
    Method onThrowAdvice = advice.onThrowAdvice();
    if (onThrowAdvice == null) {
        return;/*w w w  .j  a va  2s.c  om*/
    }
    Integer enabledLocal = enabledLocals.get(advice);
    Label onThrowBlockEnd = null;
    if (enabledLocal != null) {
        onThrowBlockEnd = new Label();
        loadLocal(enabledLocal);
        visitJumpInsn(IFEQ, onThrowBlockEnd);
    }
    if (onThrowAdvice.getArgumentTypes().length == 0) {
        visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onThrowAdvice.getName(),
                onThrowAdvice.getDescriptor(), false);
    } else {
        int startIndex = 0;
        if (advice.onThrowParameters().get(0).kind() == ParameterKind.THROWABLE) {
            // @BindThrowable must be the first argument to @OnThrow (if present)
            visitInsn(DUP);
            startIndex++;
        }
        loadMethodParameters(advice.onThrowParameters(), startIndex, travelerLocals.get(advice),
                advice.adviceType(), OnThrow.class, true);
        visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onThrowAdvice.getName(),
                onThrowAdvice.getDescriptor(), false);
    }
    if (onThrowBlockEnd != null) {
        visitLabel(onThrowBlockEnd);
    }
}

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

License:Apache License

private void visitOnAfterAdvice(Advice advice) {
    Method onAfterAdvice = advice.onAfterAdvice();
    if (onAfterAdvice == null) {
        return;/*from   www.  j  a v  a  2 s  .com*/
    }
    Integer enabledLocal = enabledLocals.get(advice);
    Label onAfterBlockEnd = null;
    if (enabledLocal != null) {
        onAfterBlockEnd = new Label();
        loadLocal(enabledLocal);
        visitJumpInsn(IFEQ, onAfterBlockEnd);
    }
    loadMethodParameters(advice.onAfterParameters(), 0, travelerLocals.get(advice), advice.adviceType(),
            OnAfter.class, true);
    visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onAfterAdvice.getName(),
            onAfterAdvice.getDescriptor(), false);
    if (onAfterBlockEnd != null) {
        visitLabel(onAfterBlockEnd);
    }
}

From source file:org.glowroot.weaving.WeavingClassVisitor.java

License:Apache License

@RequiresNonNull("type")
private void addShim(ShimType shimType) {
    for (java.lang.reflect.Method reflectMethod : shimType.shimMethods()) {
        Method method = Method.getMethod(reflectMethod);
        Shim shim = reflectMethod.getAnnotation(Shim.class);
        checkNotNull(shim);// ww  w  .  j av  a2  s  . co m
        Method targetMethod = Method.getMethod(shim.value());
        MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, method.getName(), method.getDescriptor(), null, null);
        checkNotNull(mv);
        mv.visitCode();
        int i = 0;
        mv.visitVarInsn(ALOAD, i++);
        for (Type argumentType : method.getArgumentTypes()) {
            mv.visitVarInsn(argumentType.getOpcode(ILOAD), i++);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), targetMethod.getName(),
                targetMethod.getDescriptor(), false);
        mv.visitInsn(method.getReturnType().getOpcode(IRETURN));
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}

From source file:org.glowroot.weaving.WeavingMethodVisitor.java

License:Apache License

private void defineAndEvaluateEnabledLocalVar(Advice advice) {
    Integer enabledLocal = null;//from   w w w  . ja v  a 2  s .co m
    Method isEnabledAdvice = advice.isEnabledAdvice();
    if (isEnabledAdvice != null) {
        loadMethodParameters(advice.isEnabledParameters(), 0, -1, advice.adviceType(), IsEnabled.class, false);
        visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), isEnabledAdvice.getName(),
                isEnabledAdvice.getDescriptor(), false);
        enabledLocal = newLocal(Type.BOOLEAN_TYPE);
        enabledLocals.put(advice, enabledLocal);
        storeLocal(enabledLocal);
    }
    if (advice.pointcut().ignoreSelfNested()) {
        // originalAdviceFlowLocal must be defined/initialized outside of any code branches
        // since it is referenced later on in resetAdviceFlowIfNecessary()
        int adviceFlowHolderLocal = newLocal(adviceFlowHolderType);
        adviceFlowHolderLocals.put(advice, adviceFlowHolderLocal);
        visitInsn(ACONST_NULL);
        storeLocal(adviceFlowHolderLocal);

        int originalAdviceFlowLocal = newLocal(Type.BOOLEAN_TYPE);
        originalAdviceFlowLocals.put(advice, originalAdviceFlowLocal);
        visitInsn(ICONST_0);
        storeLocal(originalAdviceFlowLocal);

        Label setAdviceFlowBlockEnd = new Label();
        if (enabledLocal != null) {
            loadLocal(enabledLocal);
            visitJumpInsn(IFEQ, setAdviceFlowBlockEnd);
        } else {
            enabledLocal = newLocal(Type.BOOLEAN_TYPE);
            enabledLocals.put(advice, enabledLocal);
            // temporary initial value to help with Java 7 stack frames
            visitInsn(ICONST_0);
            storeLocal(enabledLocal);
        }
        visitFieldInsn(GETSTATIC, advice.adviceType().getInternalName(), "glowroot$advice$flow$outer$holder",
                adviceFlowOuterHolderType.getDescriptor());
        visitMethodInsn(INVOKEVIRTUAL, adviceFlowOuterHolderType.getInternalName(), "getInnerHolder",
                "()" + adviceFlowHolderType.getDescriptor(), false);
        visitInsn(DUP);
        storeLocal(adviceFlowHolderLocal);
        visitMethodInsn(INVOKEVIRTUAL, adviceFlowHolderType.getInternalName(), "isTop", "()Z", false);
        Label isTopBlockStart = new Label();
        visitInsn(DUP);
        storeLocal(originalAdviceFlowLocal);
        visitJumpInsn(IFNE, isTopBlockStart);
        // !isTop()
        visitInsn(ICONST_0);
        storeLocal(enabledLocal);
        visitJumpInsn(GOTO, setAdviceFlowBlockEnd);
        // enabled
        visitLabel(isTopBlockStart);
        loadLocal(adviceFlowHolderLocal);
        visitInsn(ICONST_0);
        // note that setTop() is only called if enabled is true, so it only needs to be reset
        // at the end of the advice if enabled is true
        visitMethodInsn(INVOKEVIRTUAL, adviceFlowHolderType.getInternalName(), "setTop", "(Z)V", false);
        visitInsn(ICONST_1);
        storeLocal(enabledLocal);
        visitLabel(setAdviceFlowBlockEnd);
    }
}

From source file:org.glowroot.weaving.WeavingMethodVisitor.java

License:Apache License

private void invokeOnBefore(Advice advice, @Nullable Integer travelerLocal) {
    Method onBeforeAdvice = advice.onBeforeAdvice();
    if (onBeforeAdvice == null) {
        return;//from  w  ww  .  j  a  va 2 s.  co  m
    }
    Integer enabledLocal = enabledLocals.get(advice);
    Label onBeforeBlockEnd = null;
    if (enabledLocal != null) {
        onBeforeBlockEnd = new Label();
        loadLocal(enabledLocal);
        visitJumpInsn(IFEQ, onBeforeBlockEnd);
    }
    loadMethodParameters(advice.onBeforeParameters(), 0, -1, advice.adviceType(), OnBefore.class, false);
    visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onBeforeAdvice.getName(),
            onBeforeAdvice.getDescriptor(), false);
    if (travelerLocal != null) {
        storeLocal(travelerLocal);
    }
    if (onBeforeBlockEnd != null) {
        visitLabel(onBeforeBlockEnd);
    }
}

From source file:org.glowroot.weaving.WeavingMethodVisitor.java

License:Apache License

private void visitOnThrowAdvice() {
    for (Advice advice : Lists.reverse(advisors)) {
        Method onThrowAdvice = advice.onThrowAdvice();
        if (onThrowAdvice == null) {
            continue;
        }/* ww w  .  j a v a2 s.c om*/
        Integer enabledLocal = enabledLocals.get(advice);
        Label onThrowBlockEnd = null;
        if (enabledLocal != null) {
            onThrowBlockEnd = new Label();
            loadLocal(enabledLocal);
            visitJumpInsn(IFEQ, onThrowBlockEnd);
        }
        if (onThrowAdvice.getArgumentTypes().length == 0) {
            visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onThrowAdvice.getName(),
                    onThrowAdvice.getDescriptor(), false);
        } else {
            int startIndex = 0;
            if (advice.onThrowParameters().get(0).kind() == ParameterKind.THROWABLE) {
                // @BindThrowable must be the first argument to @OnThrow (if present)
                visitInsn(DUP);
                startIndex++;
            }
            loadMethodParameters(advice.onThrowParameters(), startIndex, travelerLocals.get(advice),
                    advice.adviceType(), OnThrow.class, true);
            visitMethodInsn(INVOKESTATIC, advice.adviceType().getInternalName(), onThrowAdvice.getName(),
                    onThrowAdvice.getDescriptor(), false);
        }
        if (onThrowBlockEnd != null) {
            visitLabel(onThrowBlockEnd);
        }
    }
}

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

License:Open Source License

/**
 * Generates an invoke method instruction.
 *
 * @param opcode the instruction's opcode.
 * @param type the class in which the method is defined.
 * @param method the method to be invoked.
 *//* ww w .java2 s  . com*/
private void invokeInsn(final int opcode, final Type type, final Method method) {
    String owner = type.getSort() == Type.ARRAY ? type.getDescriptor() : type.getInternalName();
    visitMethodInsn(opcode, owner, method.getName(), method.getDescriptor());
}

From source file:org.jruby.anno.IndyBinder.java

License:LGPL

public void processMethodDeclarationMulti(List<ExecutableElement> methods) {
    Handle[] handles = new Handle[5];
    List<ExecutableElementDescriptor> descs = new ArrayList<>();
    boolean meta = false;
    boolean isStatic = false;
    JRubyMethod anno = null;//from  w w w . j  a  v a 2  s  .c o m
    int min = Integer.MAX_VALUE;
    int max = 0;

    Map<Handle, ExecutableElementDescriptor> handleToDesc = new HashMap<>();

    for (ExecutableElement method : methods) {
        anno = method.getAnnotation(JRubyMethod.class);
        ExecutableElementDescriptor desc = new ExecutableElementDescriptor(method);
        descs.add(desc);

        if (anno != null && mv != null) {
            isStatic |= desc.isStatic;
            CharSequence qualifiedName = desc.declaringClassName;

            boolean hasContext = desc.hasContext;
            boolean hasBlock = desc.hasBlock;

            StringBuilder buffer = new StringBuilder(method.getReturnType().toString()).append(" foo(");
            boolean first = true;
            for (VariableElement parameter : method.getParameters()) {
                if (!first)
                    buffer.append(',');
                first = false;
                buffer.append(parameter.asType().toString());
            }
            buffer.append(')');

            Handle handle = new Handle(isStatic ? H_INVOKESTATIC : H_INVOKEVIRTUAL,
                    qualifiedName.toString().replace('.', '/'), method.getSimpleName().toString(),
                    Method.getMethod(buffer.toString()).getDescriptor());

            int handleOffset = calculateHandleOffset(method.getParameters().size(), anno.required(),
                    anno.optional(), anno.rest(), isStatic, hasContext, hasBlock);

            handles[handleOffset] = handle;
            handleToDesc.put(handle, desc);

            meta |= anno.meta();

            int specificArity = -1;
            if (desc.optional == 0 && !desc.rest) {
                if (desc.required == 0) {
                    if (desc.actualRequired <= 3) {
                        specificArity = desc.actualRequired;
                    }
                } else if (desc.required >= 0 && desc.required <= 3) {
                    specificArity = desc.required;
                }
            }

            if (specificArity != -1) {
                if (specificArity < min)
                    min = specificArity;
                if (specificArity > max)
                    max = specificArity;
            } else {
                if (desc.required < min)
                    min = desc.required;
                if (desc.rest)
                    max = Integer.MAX_VALUE;
                if (desc.required + desc.optional > max)
                    max = desc.required + desc.optional;
            }
        }
    }

    int implClass = meta ? SINGLETONCLASS : CLASS;

    mv.newobj("org/jruby/internal/runtime/methods/HandleMethod");
    mv.dup();

    mv.aload(implClass);
    mv.getstatic(p(Visibility.class), anno.visibility().name(), ci(Visibility.class));
    mv.ldc(encodeSignature(0, 0, 0, 0, 0, true, false));
    mv.ldc(true);
    mv.ldc(anno.notImplemented());

    DescriptorInfo info = new DescriptorInfo(descs);

    mv.ldc(info.getParameterDesc());

    mv.ldc(min);
    mv.ldc(max);

    for (int i = 0; i < 5; i++) {
        if (handles[i] != null) {
            mv.ldc(handles[i]);

            adaptHandle(handleToDesc.get(handles[i]), implClass);
        } else {
            mv.aconst_null();
        }
    }

    Method handleInit = Method.getMethod(
            "void foo(org.jruby.RubyModule, org.jruby.runtime.Visibility, long, boolean, boolean, java.lang.String, int, int, java.util.concurrent.Callable, java.util.concurrent.Callable, java.util.concurrent.Callable, java.util.concurrent.Callable, java.util.concurrent.Callable)");
    mv.invokespecial("org/jruby/internal/runtime/methods/HandleMethod", "<init>", handleInit.getDescriptor());

    mv.astore(BASEMETHOD);

    generateMethodAddCalls(methods.get(0), anno);
}

From source file:org.kjots.json.object.ClassVisitor.java

License:Apache License

/**
 * Visit a method./*from  w ww.  ja va2s. co m*/
 * <p>
 * This is a convenience method that is equivalent to the following:
 * <pre>
 *   visitMethod(access, method.getName(), method.getDescriptor(), signature, exceptions))
 * </pre>
 * <p>
 * This method will add the given method to set of implemented methods.
 *
 * @param access The access flags.
 * @param method The method.
 * @param signature The signature.
 * @param exceptions The exception.
 * @return The method visitor.
 * @see org.objectweb.asm.ClassVisitor#visitMethod(int, String, String, String, String[])
 */
public MethodVisitor visitMethod(int access, Method method, String signature, String[] exceptions) {
    MethodVisitor methodVisitor = new MethodVisitor(this.asmClassVisitor.visitMethod(access, method.getName(),
            method.getDescriptor(), signature, exceptions));

    this.implementedMethods.add(method);

    return methodVisitor;
}