Search in sources :

Example 71 with CtMethod

use of javassist.CtMethod in project drill by apache.

the class GuavaPatcher method patchStopwatch.

/**
   * Makes Guava stopwatch look like the old version for compatibility with hbase-server (for test purposes).
   */
private static void patchStopwatch() throws Exception {
    ClassPool cp = ClassPool.getDefault();
    CtClass cc = cp.get("com.google.common.base.Stopwatch");
    // Expose the constructor for Stopwatch for old libraries who use the pattern new Stopwatch().start().
    for (CtConstructor c : cc.getConstructors()) {
        if (!Modifier.isStatic(c.getModifiers())) {
            c.setModifiers(Modifier.PUBLIC);
        }
    }
    // Add back the Stopwatch.elapsedMillis() method for old consumers.
    CtMethod newmethod = CtNewMethod.make("public long elapsedMillis() { return elapsed(java.util.concurrent.TimeUnit.MILLISECONDS); }", cc);
    cc.addMethod(newmethod);
    // Load the modified class instead of the original.
    cc.toClass();
    logger.info("Google's Stopwatch patched for old HBase Guava version.");
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) CtConstructor(javassist.CtConstructor)

Example 72 with CtMethod

use of javassist.CtMethod in project drill by apache.

the class GuavaPatcher method patchCloseables.

private static void patchCloseables() throws Exception {
    ClassPool cp = ClassPool.getDefault();
    CtClass cc = cp.get("com.google.common.io.Closeables");
    // Add back the Closeables.closeQuietly() method for old consumers.
    CtMethod newmethod = CtNewMethod.make("public static void closeQuietly(java.io.Closeable closeable) { try{closeable.close();}catch(Exception e){} }", cc);
    cc.addMethod(newmethod);
    // Load the modified class instead of the original.
    cc.toClass();
    logger.info("Google's Closeables patched for old HBase Guava version.");
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod)

Example 73 with CtMethod

use of javassist.CtMethod 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)

Example 74 with CtMethod

use of javassist.CtMethod in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

public static String[] getParameterNames(Class<?> clazz, Method method) {
    CtMethod ctMethod;
    try {
        ctMethod = CtClassUtil.getMethod(clazz, method);
    } catch (NotFoundException e) {
        Throwable error = e.getCause();
        if (error instanceof RuntimeException) {
            throw (RuntimeException) error;
        }
        if (error instanceof Exception) {
            throw new RuntimeException(error.getMessage(), error);
        }
        throw new RuntimeException(e.getMessage(), e);
    }
    String[] names;
    try {
        names = getParameterNames(ctMethod);
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // System.err.println("getParameterNames methodName:" + method.toGenericString() + " names:" + StringUtils.join(names, ", "));
    return names;
}
Also used : NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod) NotFoundException(javassist.NotFoundException)

Example 75 with CtMethod

use of javassist.CtMethod in project leopard by tanhaichao.

the class CtClassUtil method getMethod.

public static CtMethod getMethod(Class<?> clazz, Method method) throws NotFoundException {
    CtClass ctClass = CtClassUtil.getClass(clazz);
    Class<?>[] types = method.getParameterTypes();
    CtMethod ctMethod;
    if (types == null) {
        ctMethod = ctClass.getDeclaredMethod(method.getName());
    } else {
        CtClass[] params = new CtClass[types.length];
        for (int i = 0; i < params.length; i++) {
            params[i] = CtClassUtil.getClass(types[i]);
        }
        ctMethod = ctClass.getDeclaredMethod(method.getName(), params);
    }
    return ctMethod;
}
Also used : CtClass(javassist.CtClass) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Aggregations

CtMethod (javassist.CtMethod)76 CtClass (javassist.CtClass)46 NotFoundException (javassist.NotFoundException)23 CannotCompileException (javassist.CannotCompileException)20 ClassPool (javassist.ClassPool)18 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2