Search in sources :

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

Example 37 with CtMethod

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

the class AbstractRemoteServiceServletPatcher method addMockedGetServletConfigMethod.

@InitMethod
static void addMockedGetServletConfigMethod(CtClass c) throws CannotCompileException {
    StringBuilder sb = new StringBuilder();
    sb.append("public javax.servlet.ServletConfig getServletConfig() { return ");
    sb.append(AbstractRemoteServiceServletPatcher.class.getName()).append(".ensureServletMockProvider(this, \"getServletConfig()\").getMockedConfig(this); }");
    CtMethod m = CtMethod.make(sb.toString(), c);
    c.addMethod(m);
}
Also used : CtMethod(javassist.CtMethod) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 38 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class PersistentAttributesHelper method inferTypeName.

/**
	 * Consistent with hasAnnotation()
	 */
private static String inferTypeName(CtClass ctClass, String attributeName) {
    AccessType classAccessType = getAccessTypeOrNull(ctClass);
    CtField field = findFieldOrNull(ctClass, attributeName);
    CtMethod getter = findGetterOrNull(ctClass, attributeName);
    if (classAccessType == AccessType.FIELD || (field != null && getAccessTypeOrNull(field) == AccessType.FIELD)) {
        return field == null ? null : inferFieldTypeName(field);
    }
    if (classAccessType == AccessType.PROPERTY || (getter != null && getAccessTypeOrNull(getter) == AccessType.PROPERTY)) {
        return getter == null ? null : inferMethodTypeName(getter);
    }
    String found = (getter == null ? null : inferMethodTypeName(getter));
    if (found == null && field != null) {
        return inferFieldTypeName(field);
    }
    return found;
}
Also used : CtField(javassist.CtField) AccessType(javax.persistence.AccessType) CtMethod(javassist.CtMethod)

Example 39 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class PersistentAttributesHelper method getAnnotation.

public static <T extends Annotation> T getAnnotation(CtClass ctClass, String attributeName, Class<T> annotation) {
    AccessType classAccessType = getAccessTypeOrNull(ctClass);
    CtField field = findFieldOrNull(ctClass, attributeName);
    CtMethod getter = findGetterOrNull(ctClass, attributeName);
    if (classAccessType == AccessType.FIELD || (field != null && getAccessTypeOrNull(field) == AccessType.FIELD)) {
        return field == null ? null : getAnnotationOrNull(field, annotation);
    }
    if (classAccessType == AccessType.PROPERTY || (getter != null && getAccessTypeOrNull(getter) == AccessType.PROPERTY)) {
        return getter == null ? null : getAnnotationOrNull(getter, annotation);
    }
    T found = (getter == null ? null : getAnnotationOrNull(getter, annotation));
    if (found == null && field != null) {
        return getAnnotationOrNull(field, annotation);
    }
    return found;
}
Also used : CtField(javassist.CtField) AccessType(javax.persistence.AccessType) CtMethod(javassist.CtMethod)

Example 40 with CtMethod

use of javassist.CtMethod in project hibernate-orm by hibernate.

the class MethodWriter method addGetter.

/* --- */
public static CtMethod addGetter(CtClass target, String field, String name) {
    CtField actualField = null;
    try {
        actualField = target.getField(field);
        log.debugf("Writing getter method [%s] into [%s] for field [%s]", name, target.getName(), field);
        CtMethod method = CtNewMethod.getter(name, target.getField(field));
        target.addMethod(method);
        return method;
    } catch (CannotCompileException cce) {
        try {
            // Fall back to create a getter from delegation.
            CtMethod method = CtNewMethod.delegator(CtNewMethod.getter(name, actualField), target);
            target.addMethod(method);
            return method;
        } catch (CannotCompileException ignored) {
            String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
            throw new EnhancementException(msg, cce);
        }
    } catch (NotFoundException nfe) {
        String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
        throw new EnhancementException(msg, nfe);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) 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