Search in sources :

Example 11 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class CtMethodJavaWriterTest method testExtractSignature_simple.

@Test
public void testExtractSignature_simple() throws CannotCompileException, NotFoundException {
    //GIVEN
    CtClass targetClass = ClassPool.getDefault().makeClass("Target" + TestCounter.testCounter++);
    CtMethod fooMethod = CtNewMethod.make("public void foo() { }", targetClass);
    targetClass.addMethod(fooMethod);
    //WHEN
    String extractSignature = signatureExtractor.createJavaSignature(fooMethod);
    //THEN
    assertEquals("public void foo()", extractSignature);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 12 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class CtMethodJavaWriterTest method testInvokeSuper_with_params.

@Test
public void testInvokeSuper_with_params() throws CannotCompileException, NotFoundException {
    //GIVEN
    CtClass targetClass = ClassPool.getDefault().makeClass("Target" + TestCounter.testCounter++);
    CtMethod fooMethod = CtNewMethod.make("public void foo(int a, String b) {}", targetClass);
    targetClass.addMethod(fooMethod);
    //WHEN
    String extractSignature = signatureExtractor.invokeSuper(fooMethod);
    //THEN
    assertEquals("super.foo(p0, p1);", extractSignature);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 13 with CtMethod

use of javassist.CtMethod in project afterburner by stephanenicolas.

the class CtMethodJavaWriterTest method testExtractSignature_with_return_type.

@Test
public void testExtractSignature_with_return_type() throws CannotCompileException, NotFoundException {
    //GIVEN
    CtClass targetClass = ClassPool.getDefault().makeClass("Target" + TestCounter.testCounter++);
    CtMethod fooMethod = CtNewMethod.make("public int foo() { return 0; }", targetClass);
    targetClass.addMethod(fooMethod);
    //WHEN
    String extractSignature = signatureExtractor.createJavaSignature(fooMethod);
    //THEN
    assertEquals("public int foo()", extractSignature);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 14 with CtMethod

use of javassist.CtMethod in project HikariCP by brettwooldridge.

the class JavassistProxyFactory method generateProxyClass.

/**
    *  Generate Javassist Proxy Classes
    */
private static <T> void generateProxyClass(Class<T> primaryInterface, String superClassName, String methodBody) throws Exception {
    String newClassName = superClassName.replaceAll("(.+)\\.(\\w+)", "$1.Hikari$2");
    CtClass superCt = classPool.getCtClass(superClassName);
    CtClass targetCt = classPool.makeClass(newClassName, superCt);
    targetCt.setModifiers(Modifier.FINAL);
    System.out.println("Generating " + newClassName);
    targetCt.setModifiers(Modifier.PUBLIC);
    // Make a set of method signatures we inherit implementation for, so we don't generate delegates for these
    Set<String> superSigs = new HashSet<>();
    for (CtMethod method : superCt.getMethods()) {
        if ((method.getModifiers() & Modifier.FINAL) == Modifier.FINAL) {
            superSigs.add(method.getName() + method.getSignature());
        }
    }
    Set<String> methods = new HashSet<>();
    Set<Class<?>> interfaces = getAllInterfaces(primaryInterface);
    for (Class<?> intf : interfaces) {
        CtClass intfCt = classPool.getCtClass(intf.getName());
        targetCt.addInterface(intfCt);
        for (CtMethod intfMethod : intfCt.getDeclaredMethods()) {
            final String signature = intfMethod.getName() + intfMethod.getSignature();
            // don't generate delegates for methods we override
            if (superSigs.contains(signature)) {
                continue;
            }
            // Ignore already added methods that come from other interfaces
            if (methods.contains(signature)) {
                continue;
            }
            // Track what methods we've added
            methods.add(signature);
            // Clone the method we want to inject into
            CtMethod method = CtNewMethod.copy(intfMethod, targetCt, null);
            String modifiedBody = methodBody;
            // If the super-Proxy has concrete methods (non-abstract), transform the call into a simple super.method() call
            CtMethod superMethod = superCt.getMethod(intfMethod.getName(), intfMethod.getSignature());
            if ((superMethod.getModifiers() & Modifier.ABSTRACT) != Modifier.ABSTRACT && !isDefaultMethod(intf, intfCt, intfMethod)) {
                modifiedBody = modifiedBody.replace("((cast) ", "");
                modifiedBody = modifiedBody.replace("delegate", "super");
                modifiedBody = modifiedBody.replace("super)", "super");
            }
            modifiedBody = modifiedBody.replace("cast", primaryInterface.getName());
            // Generate a method that simply invokes the same method on the delegate
            if (isThrowsSqlException(intfMethod)) {
                modifiedBody = modifiedBody.replace("method", method.getName());
            } else {
                modifiedBody = "{ return ((cast) delegate).method($$); }".replace("method", method.getName()).replace("cast", primaryInterface.getName());
            }
            if (method.getReturnType() == CtClass.voidType) {
                modifiedBody = modifiedBody.replace("return", "");
            }
            method.setBody(modifiedBody);
            targetCt.addMethod(method);
        }
    }
    targetCt.getClassFile().setMajorVersion(ClassFile.JAVA_7);
    targetCt.writeFile("target/classes");
}
Also used : CtClass(javassist.CtClass) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) HashSet(java.util.HashSet)

Example 15 with CtMethod

use of javassist.CtMethod in project HikariCP by brettwooldridge.

the class JavassistProxyFactory method modifyProxyFactory.

private static void modifyProxyFactory() throws Exception {
    System.out.println("Generating method bodies for com.zaxxer.hikari.proxy.ProxyFactory");
    String packageName = ProxyConnection.class.getPackage().getName();
    CtClass proxyCt = classPool.getCtClass("com.zaxxer.hikari.pool.ProxyFactory");
    for (CtMethod method : proxyCt.getMethods()) {
        switch(method.getName()) {
            case "getProxyConnection":
                method.setBody("{return new " + packageName + ".HikariProxyConnection($$);}");
                break;
            case "getProxyStatement":
                method.setBody("{return new " + packageName + ".HikariProxyStatement($$);}");
                break;
            case "getProxyPreparedStatement":
                method.setBody("{return new " + packageName + ".HikariProxyPreparedStatement($$);}");
                break;
            case "getProxyCallableStatement":
                method.setBody("{return new " + packageName + ".HikariProxyCallableStatement($$);}");
                break;
            case "getProxyResultSet":
                method.setBody("{return new " + packageName + ".HikariProxyResultSet($$);}");
                break;
            default:
                // unhandled method
                break;
        }
    }
    proxyCt.writeFile("target/classes");
}
Also used : CtClass(javassist.CtClass) ProxyConnection(com.zaxxer.hikari.pool.ProxyConnection) CtMethod(javassist.CtMethod)

Aggregations

CtMethod (javassist.CtMethod)70 CtClass (javassist.CtClass)42 CannotCompileException (javassist.CannotCompileException)20 NotFoundException (javassist.NotFoundException)20 ClassPool (javassist.ClassPool)16 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