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

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

Introduction

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

Prototype

public static Method getMethod(final String method) 

Source Link

Document

Returns a Method corresponding to the given Java method declaration.

Usage

From source file:org.evosuite.runtime.instrumentation.RegisterObjectForDeterministicHashCodeVisitor.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    // We don't use the AdviceAdapter here because this is not properly initialised if the constructor is
    // exited with an exception
    if (opcode == Opcodes.RETURN) {
        loadThis();//from  w  w  w.  j ava  2  s .co  m
        invokeStatic(Type.getType(org.evosuite.runtime.System.class),
                Method.getMethod("void registerObjectForIdentityHashCode(Object)"));
    }
    super.visitInsn(opcode);
}

From source file:org.evosuite.stubs.StubClassVisitor.java

License:Open Source License

private void insertReturnCast(GeneratorAdapter mg, Method m) {
    if (m.getReturnType().getSort() == Type.OBJECT) {
        mg.checkCast(m.getReturnType());
    } else if (m.getReturnType().getSort() == Type.ARRAY) {
        if (m.getReturnType().getElementType().getSort() == Type.OBJECT) {
            // TODO: String? Other arrays?
            try {
                java.lang.reflect.Method copyMethod = System.class.getMethod("arraycopy",
                        new Class<?>[] { Object.class, int.class, Object.class, int.class, int.class });

                // Object 1
                mg.dup(); // O1, O1
                mg.arrayLength(); // O1, O1.length
                int arrayLengthPos = mg.newLocal(Type.INT_TYPE);
                mg.storeLocal(arrayLengthPos);

                mg.loadLocal(arrayLengthPos);
                mg.newArray(m.getReturnType().getElementType());
                int newArrayPos = mg.newLocal(m.getReturnType());
                mg.storeLocal(newArrayPos);

                mg.push(0); // O1, 0
                mg.loadLocal(newArrayPos); // O1, 0, O2
                mg.push(0); // O1, 0, O2, 0
                mg.loadLocal(arrayLengthPos); // O1, 0, O2, 0, O1.length               
                mg.invokeStatic(Type.getType(System.class), Method.getMethod(copyMethod));
                mg.loadLocal(newArrayPos);
                mg.checkCast(m.getReturnType());
            } catch (Exception e) {
                System.out.println("Screw that");
            }/*from  w  w  w . j a v a  2  s  . c o  m*/
            // mg.checkCast(m.getReturnType());
        }
    }
}

From source file:org.evosuite.testcase.ConstructorStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override/* w  ww. ja va 2s .  c o  m*/
public void getBytecode(GeneratorAdapter mg, Map<Integer, Integer> locals, Throwable exception) {
    logger.debug("Invoking constructor");
    Label start = mg.newLabel();
    Label end = mg.newLabel();

    // if(exception != null)
    mg.mark(start);

    mg.newInstance(Type.getType(retval.getVariableClass()));
    mg.dup();
    int num = 0;
    for (VariableReference parameter : parameters) {
        parameter.loadBytecode(mg, locals);
        if (constructor.getConstructor().getParameterTypes()[num].isPrimitive()) {
            if (parameter.getGenericClass().isWrapperType()) {
                mg.unbox(Type.getType(parameter.getGenericClass().getUnboxedType()));
            } else if (!parameter.getGenericClass().isPrimitive()) {
                Class<?> parameterClass = new GenericClass(constructor.getParameterTypes()[num]).getBoxedType();
                Type parameterType = Type.getType(parameterClass);
                mg.checkCast(parameterType);
                mg.unbox(Type.getType(constructor.getConstructor().getParameterTypes()[num]));
            }

            if (!constructor.getParameterTypes()[num].equals(parameter.getVariableClass())) {
                logger.debug("Types don't match - casting {} to {}", parameter.getVariableClass().getName(),
                        constructor.getConstructor().getParameterTypes()[num].getName());
                mg.cast(Type.getType(parameter.getVariableClass()),
                        Type.getType(constructor.getConstructor().getParameterTypes()[num]));
            }
        } else if (parameter.getVariableClass().isPrimitive()) {
            mg.box(Type.getType(parameter.getVariableClass()));
        }
        num++;
    }
    mg.invokeConstructor(Type.getType(retval.getVariableClass()),
            Method.getMethod(constructor.getConstructor()));
    logger.debug("Storing result");
    retval.storeBytecode(mg, locals);

    // if(exception != null) {
    mg.mark(end);
    Label l = mg.newLabel();
    mg.goTo(l);
    // mg.catchException(start, end,
    // Type.getType(getExceptionClass(exception)));
    mg.catchException(start, end, Type.getType(Throwable.class));
    mg.pop(); // Pop exception from stack
    if (!retval.isVoid()) {
        Class<?> clazz = retval.getVariableClass();
        if (clazz.equals(boolean.class))
            mg.push(false);
        else if (clazz.equals(char.class))
            mg.push(0);
        else if (clazz.equals(int.class))
            mg.push(0);
        else if (clazz.equals(short.class))
            mg.push(0);
        else if (clazz.equals(long.class))
            mg.push(0L);
        else if (clazz.equals(float.class))
            mg.push(0.0F);
        else if (clazz.equals(double.class))
            mg.push(0.0);
        else if (clazz.equals(byte.class))
            mg.push(0);
        else if (clazz.equals(String.class))
            mg.push("");
        else
            mg.visitInsn(Opcodes.ACONST_NULL);

        retval.storeBytecode(mg, locals);
    }
    mg.mark(l);
    // }

}

From source file:org.fuin.units4j.analyzer.MCAMethod.java

License:Open Source License

/**
 * Constructor with class and method.//from w w w  . j  a  v a  2 s . c om
 * 
 * @param className
 *            Full qualified class name.
 * @param methodSignature
 *            Method signature.
 */
public MCAMethod(final String className, final String methodSignature) {
    super();
    if (className == null) {
        throw new IllegalArgumentException("Argument 'className' canot be NULL");
    }
    if (methodSignature == null) {
        throw new IllegalArgumentException("Argument 'methodSignature' canot be NULL");
    }
    this.className = className;
    this.methodSignature = methodSignature;
    this.method = Method.getMethod(methodSignature);
}

From source file:org.fuin.units4j.analyzer.MCAMethod.java

License:Open Source License

/**
 * Constructor with ASM arguments.// www . ja  v  a 2 s  . c o m
 * 
 * @param asmClassName
 *            ASM class name with '/'.
 * @param asmMethodName
 *            Method name.
 * @param asmMethodDescr
 *            ASM method description.
 */
public MCAMethod(final String asmClassName, final String asmMethodName, final String asmMethodDescr) {

    if (asmClassName == null) {
        throw new IllegalArgumentException("Argument 'asmClassName' canot be NULL");
    }
    if (asmMethodName == null) {
        throw new IllegalArgumentException("Argument 'asmMethodName' canot be NULL");
    }
    if (asmMethodDescr == null) {
        throw new IllegalArgumentException("Argument 'asmMethodDescr' canot be NULL");
    }

    this.className = asmClassName.replace('/', '.');
    final int p = asmMethodDescr.indexOf(')');
    if (p < 0) {
        throw new IllegalStateException("Couldn't find closing bracket: " + asmMethodDescr);
    }
    final StringBuilder sb = new StringBuilder("(");
    final Type[] argTypes = Type.getArgumentTypes(asmMethodDescr);
    if (argTypes != null) {
        for (int i = 0; i < argTypes.length; i++) {
            if (i > 0) {
                sb.append(", ");
            }
            sb.append(argTypes[i].getClassName());
        }
    }
    sb.append(")");
    final String returnType = Type.getReturnType(asmMethodDescr).getClassName();
    this.methodSignature = returnType + " " + asmMethodName + sb;
    this.method = Method.getMethod(methodSignature);
}

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

License:Apache License

private void initIsEnabledAdvice(Class<?> adviceClass, java.lang.reflect.Method method)
        throws AdviceConstructionException {
    checkState(!hasIsEnabledAdvice,/*from   w w w . ja  v a  2s . c  o m*/
            "@Pointcut '" + adviceClass.getName() + "' has more than one @IsEnabled method");
    Method asmMethod = Method.getMethod(method);
    checkState(asmMethod.getReturnType().getSort() == Type.BOOLEAN, "@IsEnabled method must return boolean");
    builder.isEnabledAdvice(asmMethod);
    List<AdviceParameter> parameters = getAdviceParameters(method.getParameterAnnotations(),
            method.getParameterTypes(), isEnabledBindAnnotationTypes, IsEnabled.class);
    builder.addAllIsEnabledParameters(parameters);
    hasIsEnabledAdvice = true;
}

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

License:Apache License

private void initOnBeforeAdvice(Class<?> adviceClass, java.lang.reflect.Method method)
        throws AdviceConstructionException {
    checkState(!hasOnBeforeAdvice,/*from  www  .  j  a  v a2s .c  o  m*/
            "@Pointcut '" + adviceClass.getName() + "' has more than one @OnBefore method");
    Method onBeforeAdvice = Method.getMethod(method);
    builder.onBeforeAdvice(onBeforeAdvice);
    List<AdviceParameter> parameters = getAdviceParameters(method.getParameterAnnotations(),
            method.getParameterTypes(), onBeforeBindAnnotationTypes, OnBefore.class);
    builder.addAllOnBeforeParameters(parameters);
    if (onBeforeAdvice.getReturnType().getSort() != Type.VOID) {
        builder.travelerType(onBeforeAdvice.getReturnType());
    }
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnBeforeAdvice = true;
}

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

License:Apache License

private void initOnReturnAdvice(Class<?> adviceClass, java.lang.reflect.Method method)
        throws AdviceConstructionException {
    checkState(!hasOnReturnAdvice,// w ww. j a  v  a  2  s  . c om
            "@Pointcut '" + adviceClass.getName() + "' has more than one @OnReturn method");
    List<AdviceParameter> parameters = getAdviceParameters(method.getParameterAnnotations(),
            method.getParameterTypes(), onReturnBindAnnotationTypes, OnReturn.class);
    for (int i = 1; i < parameters.size(); i++) {
        checkState(parameters.get(i).kind() != ParameterKind.RETURN,
                "@BindReturn must be the first argument to @OnReturn");
        checkState(parameters.get(i).kind() != ParameterKind.OPTIONAL_RETURN,
                "@BindOptionalReturn must be the first argument to @OnReturn");
    }
    builder.onReturnAdvice(Method.getMethod(method));
    builder.addAllOnReturnParameters(parameters);
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnReturnAdvice = true;
}

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

License:Apache License

private void initOnThrowAdvice(Class<?> adviceClass, java.lang.reflect.Method method)
        throws AdviceConstructionException {
    checkState(!hasOnThrowAdvice,//from  w w  w  . ja va  2  s  . c o  m
            "@Pointcut '" + adviceClass.getName() + "' has more than one @OnThrow method");
    List<AdviceParameter> parameters = getAdviceParameters(method.getParameterAnnotations(),
            method.getParameterTypes(), onThrowBindAnnotationTypes, OnThrow.class);
    for (int i = 1; i < parameters.size(); i++) {
        checkState(parameters.get(i).kind() != ParameterKind.THROWABLE,
                "@BindThrowable must be the first argument to @OnThrow");
    }
    Method asmMethod = Method.getMethod(method);
    checkState(asmMethod.getReturnType().getSort() == Type.VOID, "@OnThrow method must return void (for now)");
    builder.onThrowAdvice(asmMethod);
    builder.addAllOnThrowParameters(parameters);
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnThrowAdvice = true;
}

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

License:Apache License

private void initOnAfterAdvice(Class<?> adviceClass, java.lang.reflect.Method method)
        throws AdviceConstructionException {
    checkState(!hasOnAfterAdvice,//from  w w  w . java 2 s .co m
            "@Pointcut '" + adviceClass.getName() + "' has more than one @OnAfter method");
    Method asmMethod = Method.getMethod(method);
    checkState(asmMethod.getReturnType().getSort() == Type.VOID, "@OnAfter method must return void");
    builder.onAfterAdvice(asmMethod);
    List<AdviceParameter> parameters = getAdviceParameters(method.getParameterAnnotations(),
            method.getParameterTypes(), onAfterBindAnnotationTypes, OnAfter.class);
    builder.addAllOnAfterParameters(parameters);
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnAfterAdvice = true;
}