Search in sources :

Example 11 with MethodInfo

use of javassist.bytecode.MethodInfo in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

/**
 * 获取方法的参数名称.
 *
 * @param ctMethod
 * @return
 * @throws NotFoundException
 */
public static String[] getParameterNames(CtMethod ctMethod) throws NotFoundException {
    MethodInfo methodInfo = ctMethod.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    // logger.info("methodInfo.getConstPool().getSize():");
    LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    // String[] names = new String[attribute.tableLength() - 1];
    String[] paramNames = new String[ctMethod.getParameterTypes().length];
    int pos = 0;
    if (true) {
        int size = attribute.tableLength();
        if (size > 0) {
            String[] names = new String[size - 1];
            for (int i = 0; i < names.length; i++) {
                names[i] = attribute.variableName(i);
                if ("this".equals(names[i])) {
                    pos = i + 1;
                    break;
                }
            }
        // logger.info(methodInfo.getName() + " pos:" + pos + " allNames:" + StringUtils.join(names, ", "));
        }
    }
    // logger.info(methodInfo.getName() + " pos:" + pos);
    for (int i = 0; i < paramNames.length; i++) {
        // paramNames[i] = attribute.variableName(i + 1);
        try {
            paramNames[i] = attribute.variableName(i + pos);
        // logger.info("paramNames[" + i + "]:" + paramNames[i]);
        } catch (RuntimeException e) {
            throw e;
        }
    }
    // System.err.println("paramNames:" + StringUtils.join(paramNames));
    return paramNames;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute)

Example 12 with MethodInfo

use of javassist.bytecode.MethodInfo in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

/**
 * 获取方法的参数名称.
 *
 * @param ctMethod
 * @return
 * @throws NotFoundException
 */
public static String[] getParameterNames(CtMethod ctMethod) throws NotFoundException {
    MethodInfo methodInfo = ctMethod.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    // logger.info("methodInfo.getConstPool().getSize():");
    LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    // String[] names = new String[attribute.tableLength() - 1];
    String[] paramNames = new String[ctMethod.getParameterTypes().length];
    int pos = 0;
    if (true) {
        int size = attribute.tableLength();
        if (size > 0) {
            String[] names = new String[size - 1];
            for (int i = 0; i < names.length; i++) {
                names[i] = attribute.variableName(i);
                if ("this".equals(names[i])) {
                    pos = i + 1;
                    break;
                }
            }
        // logger.info(methodInfo.getName() + " pos:" + pos + " allNames:" + StringUtils.join(names, ", "));
        }
    }
    // logger.info(methodInfo.getName() + " pos:" + pos);
    for (int i = 0; i < paramNames.length; i++) {
        // paramNames[i] = attribute.variableName(i + 1);
        try {
            paramNames[i] = attribute.variableName(i + pos);
        // logger.info("paramNames[" + i + "]:" + paramNames[i]);
        } catch (RuntimeException e) {
            throw e;
        }
    }
    // System.err.println("paramNames:" + StringUtils.join(paramNames));
    return paramNames;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute)

Example 13 with MethodInfo

use of javassist.bytecode.MethodInfo in project audit4j-core by audit4j.

the class AnnotationDB method scanMethods.

/**
 * Scanns both the method and its parameters for annotations.
 *
 * @param cf
 */
protected void scanMethods(ClassFile cf) {
    List<ClassFile> methods = cf.getMethods();
    if (methods == null)
        return;
    for (Object obj : methods) {
        MethodInfo method = (MethodInfo) obj;
        if (scanMethodAnnotations) {
            AnnotationsAttribute visible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag);
            AnnotationsAttribute invisible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag);
            if (visible != null)
                populate(visible.getAnnotations(), cf.getName());
            if (invisible != null)
                populate(invisible.getAnnotations(), cf.getName());
        }
        if (scanParameterAnnotations) {
            ParameterAnnotationsAttribute paramsVisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag);
            ParameterAnnotationsAttribute paramsInvisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag);
            if (paramsVisible != null && paramsVisible.getAnnotations() != null) {
                for (Annotation[] anns : paramsVisible.getAnnotations()) {
                    populate(anns, cf.getName());
                }
            }
            if (paramsInvisible != null && paramsInvisible.getAnnotations() != null) {
                for (Annotation[] anns : paramsInvisible.getAnnotations()) {
                    populate(anns, cf.getName());
                }
            }
        }
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) ParameterAnnotationsAttribute(javassist.bytecode.ParameterAnnotationsAttribute) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) MethodInfo(javassist.bytecode.MethodInfo) Annotation(javassist.bytecode.annotation.Annotation)

Aggregations

MethodInfo (javassist.bytecode.MethodInfo)13 ConstPool (javassist.bytecode.ConstPool)6 CodeAttribute (javassist.bytecode.CodeAttribute)5 LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)5 ClassPool (javassist.ClassPool)4 CtClass (javassist.CtClass)3 Bytecode (javassist.bytecode.Bytecode)3 CtMethod (javassist.CtMethod)2 NotFoundException (javassist.NotFoundException)2 BadBytecode (javassist.bytecode.BadBytecode)2 CodeIterator (javassist.bytecode.CodeIterator)2 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CtBehavior (javassist.CtBehavior)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 AttributeInfo (javassist.bytecode.AttributeInfo)1 ClassFile (javassist.bytecode.ClassFile)1 ParameterAnnotationsAttribute (javassist.bytecode.ParameterAnnotationsAttribute)1 StackMapTable (javassist.bytecode.StackMapTable)1