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);
}
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations