Search in sources :

Example 6 with MethodInfo

use of javassist.bytecode.MethodInfo in project hibernate-orm by hibernate.

the class BulkAccessorFactory method addDefaultConstructor.

/**
	 * Declares a constructor that takes no parameter.
	 *
	 * @param classfile The class descriptor
	 *
	 * @throws CannotCompileException Indicates trouble with the underlying Javassist calls
	 */
private void addDefaultConstructor(ClassFile classfile) throws CannotCompileException {
    final ConstPool constPool = classfile.getConstPool();
    final String constructorSignature = "()V";
    final MethodInfo constructorMethodInfo = new MethodInfo(constPool, MethodInfo.nameInit, constructorSignature);
    final Bytecode code = new Bytecode(constPool, 0, 1);
    // aload_0
    code.addAload(0);
    // invokespecial
    code.addInvokespecial(BulkAccessor.class.getName(), MethodInfo.nameInit, constructorSignature);
    // return
    code.addOpcode(Opcode.RETURN);
    constructorMethodInfo.setCodeAttribute(code.toCodeAttribute());
    constructorMethodInfo.setAccessFlags(AccessFlag.PUBLIC);
    classfile.addMethod(constructorMethodInfo);
}
Also used : ConstPool(javassist.bytecode.ConstPool) MethodInfo(javassist.bytecode.MethodInfo) Bytecode(javassist.bytecode.Bytecode)

Example 7 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 8 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 9 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 10 with MethodInfo

use of javassist.bytecode.MethodInfo in project yyl_example by Relucent.

the class GetMethodParamNameTest method getMethodParamNames.

/**
	 * 获得参数名 (JDK 自带类 ,接口方法和抽象方法无法正确获取参数名)
	 */
private static String[] getMethodParamNames(final Method method) throws NotFoundException {
    final String methodName = method.getName();
    final Class<?>[] methodParameterTypes = method.getParameterTypes();
    final int methodParameterCount = methodParameterTypes.length;
    final String className = method.getDeclaringClass().getName();
    final boolean isStatic = Modifier.isStatic(method.getModifiers());
    final String[] methodParametersNames = new String[methodParameterCount];
    ClassPool pool = ClassPool.getDefault();
    CtClass ctClass = pool.get(className);
    CtClass[] ctTypes = new CtClass[methodParameterTypes.length];
    for (int i = 0; i < methodParameterCount; i++) {
        ctTypes[i] = pool.get(methodParameterTypes[i].getName());
    }
    CtMethod ctMethod = ctClass.getDeclaredMethod(methodName, ctTypes);
    MethodInfo methodInfo = ctMethod.getMethodInfo();
    CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
    LocalVariableAttribute attribute = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
    //如果是静态方法,第一个参数就是方法参数,非静态方法,则第一个参数是 this ,然后才是方法的参数
    if (attribute != null) {
        int variableCount = isStatic ? methodParameterCount : methodParameterCount + 1;
        for (int index = 0; index < variableCount; index++) {
            int methodParameterIndex = isStatic ? index : index - 1;
            if (0 <= methodParameterIndex && methodParameterIndex < methodParameterCount) {
                methodParametersNames[methodParameterIndex] = attribute.variableName(index);
            }
        }
    }
    return methodParametersNames;
}
Also used : ClassPool(javassist.ClassPool) CtClass(javassist.CtClass) CodeAttribute(javassist.bytecode.CodeAttribute) CtClass(javassist.CtClass) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute) CtMethod(javassist.CtMethod)

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