Search in sources :

Example 21 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 22 with MethodInfo

use of javassist.bytecode.MethodInfo in project reflections by ronmamo.

the class MethodParameterNamesScanner method scan.

@Override
public void scan(Object cls) {
    final MetadataAdapter md = getMetadataAdapter();
    for (Object method : md.getMethods(cls)) {
        String key = md.getMethodFullKey(cls, method);
        if (acceptResult(key)) {
            LocalVariableAttribute table = (LocalVariableAttribute) ((MethodInfo) method).getCodeAttribute().getAttribute(LocalVariableAttribute.tag);
            int length = table.tableLength();
            //skip this
            int i = Modifier.isStatic(((MethodInfo) method).getAccessFlags()) ? 0 : 1;
            if (i < length) {
                List<String> names = new ArrayList<String>(length - i);
                while (i < length) names.add(((MethodInfo) method).getConstPool().getUtf8Info(table.nameIndex(i++)));
                getStore().put(key, Joiner.on(", ").join(names));
            }
        }
    }
}
Also used : MetadataAdapter(org.reflections.adapters.MetadataAdapter) ArrayList(java.util.ArrayList) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute)

Example 23 with MethodInfo

use of javassist.bytecode.MethodInfo in project pinpoint by naver.

the class JavassistClass method hasEnclosingMethod.

@Override
public boolean hasEnclosingMethod(String methodName, String... parameterTypes) {
    CtBehavior behavior;
    try {
        behavior = ctClass.getEnclosingBehavior();
    } catch (NotFoundException ignored) {
        return false;
    }
    if (behavior == null) {
        return false;
    }
    final MethodInfo methodInfo = behavior.getMethodInfo2();
    if (!methodInfo.getName().equals(methodName)) {
        return false;
    }
    final String jvmSignature = JavaAssistUtils.javaTypeToJvmSignature(parameterTypes);
    if (methodInfo.getDescriptor().startsWith(jvmSignature)) {
        return true;
    }
    return false;
}
Also used : CtBehavior(javassist.CtBehavior) NotFoundException(javassist.NotFoundException) MethodInfo(javassist.bytecode.MethodInfo)

Example 24 with MethodInfo

use of javassist.bytecode.MethodInfo in project pinpoint by naver.

the class JavassistEngineTest method getTransformByteCode.

public byte[] getTransformByteCode() {
    try {
        final ClassPool pool = new ClassPool(true);
        final CtClass ctClass = pool.get(mock);
        final ConstPool constPool = ctClass.getClassFile2().getConstPool();
        MethodInfo info = new MethodInfo(constPool, "transformMethod", "()V");
        final CtMethod newMethod = CtMethod.make(info, ctClass);
        ctClass.addMethod(newMethod);
        return ctClass.toBytecode();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}
Also used : CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) ClassPool(javassist.ClassPool) MethodInfo(javassist.bytecode.MethodInfo) CtMethod(javassist.CtMethod)

Example 25 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)

Aggregations

MethodInfo (javassist.bytecode.MethodInfo)54 Bytecode (javassist.bytecode.Bytecode)28 BadBytecode (javassist.bytecode.BadBytecode)19 CodeIterator (javassist.bytecode.CodeIterator)18 CodeAttribute (javassist.bytecode.CodeAttribute)17 ConstPool (javassist.bytecode.ConstPool)17 ClassFile (javassist.bytecode.ClassFile)12 LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)10 DuplicateMemberException (javassist.bytecode.DuplicateMemberException)9 List (java.util.List)7 ClassPool (javassist.ClassPool)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 DataOutputStream (java.io.DataOutputStream)6 IOException (java.io.IOException)6 CtMethod (javassist.CtMethod)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4