Search in sources :

Example 46 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class ListWrapperPatcher method initClass.

@InitMethod
static void initClass(CtClass c) throws CannotCompileException, NotFoundException {
    CtMethod flushMethod = c.getDeclaredMethod("flush");
    flushMethod.insertAfter(BrowserSimulatorImpl.class.getName() + ".get().fireLoopEnd();");
}
Also used : CtMethod(javassist.CtMethod) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 47 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method findPatchMethod.

private CtMethod findPatchMethod(CtMethod m) throws Exception {
    for (CtMethod patchMethod : patchMethods) {
        PatchMethod annotation = (PatchMethod) patchMethod.getAnnotation(PatchMethod.class);
        String methodName = (annotation.value().length() > 0) ? annotation.value() : patchMethod.getName();
        if (m.getName().equals(methodName) && hasCompatibleSignature(m, patchMethod)) {
            return patchMethod;
        }
    }
    return null;
}
Also used : PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod) CtMethod(javassist.CtMethod)

Example 48 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getInitMethod.

private CtMethod getInitMethod(Set<CtClass> patchClasses) {
    List<CtMethod> initMethods = new ArrayList<CtMethod>();
    for (CtClass patchClass : patchClasses) {
        for (CtMethod ctMethod : patchClass.getDeclaredMethods()) {
            if (ctMethod.hasAnnotation(InitMethod.class)) {
                if (!Modifier.isStatic(ctMethod.getModifiers())) {
                    throw new GwtTestPatchException("@" + InitMethod.class.getSimpleName() + " has to be static : '" + ctMethod.getLongName() + "'");
                }
                try {
                    if (ctMethod.getParameterTypes().length != 1 || ctMethod.getParameterTypes()[0] != GwtClassPool.getCtClass(CtClass.class)) {
                        throw new GwtTestPatchException("@" + InitMethod.class.getName() + " method must have one and only one parameter of type '" + CtClass.class.getName() + "'");
                    }
                } catch (NotFoundException e) {
                    // should never happen
                    throw new GwtTestPatchException(e);
                }
                initMethods.add(ctMethod);
            }
        }
    }
    CtMethod initMethod = getMethodToUse(initMethods, InitMethod.class);
    if (initMethod != null) {
        initMethod.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
    }
    return initMethod;
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) InitMethod(com.googlecode.gwt.test.patchers.InitMethod) CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod)

Example 49 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getNewBody.

public String getNewBody(CtMethod m) throws Exception {
    CtMethod patchMethod = findPatchMethod(m);
    if (patchMethod == null) {
        return null;
    }
    processedMethods.add(patchMethod);
    // construction of the redirection code
    StringBuilder buffer = new StringBuilder();
    buffer.append("{");
    buffer.append("return ");
    buffer.append(patchMethod.getDeclaringClass().getName() + "." + patchMethod.getName());
    buffer.append("(");
    boolean append = false;
    if (!Modifier.isStatic(m.getModifiers())) {
        buffer.append("this");
        append = true;
    }
    for (int i = 0; i < m.getParameterTypes().length; i++) {
        if (append) {
            buffer.append(", ");
        }
        int j = i + 1;
        buffer.append("$" + j);
        append = true;
    }
    buffer.append(");");
    buffer.append("}");
    return buffer.toString();
}
Also used : CtMethod(javassist.CtMethod)

Example 50 with CtMethod

use of javassist.CtMethod in project gwt-test-utils by gwt-test-utils.

the class DirectoryTestReader method initTestMethods.

private void initTestMethods(Class<?> clazz, CsvDirectory csvDirectory) throws Exception {
    testMethods = new ArrayList<Method>();
    CtClass newClazz = GwtClassPool.get().makeClass(clazz.getName() + ".generated" + System.nanoTime());
    newClazz.setSuperclass(GwtClassPool.getCtClass(clazz));
    List<String> methodList = new ArrayList<String>();
    for (Entry<String, List<List<String>>> entry : tests.entrySet()) {
        String fileAbsolutePath = entry.getKey();
        String methodName = getTestName(fileAbsolutePath, csvDirectory.extension());
        CtMethod m = new CtMethod(CtClass.voidType, methodName, new CtClass[0], newClazz);
        methodList.add(methodName);
        m.setBody("launchTest(\"" + StringEscapeUtils.escapeJava(fileAbsolutePath) + "\");");
        newClazz.addMethod(m);
    }
    generatedClazz = newClazz.toClass(getClass().getClassLoader(), null);
    for (String methodName : methodList) {
        Method m = generatedClazz.getMethod(methodName);
        testMethods.add(m);
    }
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method) 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