use of javassist.CtField in project gwt-test-utils by gwt-test-utils.
the class GwtDotCreateProviderPatcher method initClass.
@InitMethod
static void initClass(CtClass ctClass) throws CannotCompileException {
CtField field = CtField.make("private Class " + BINDED_CLASS_FIELD + ";", ctClass);
ctClass.addField(field);
}
use of javassist.CtField in project gwt-test-utils by gwt-test-utils.
the class DeferredBindingModule method generatedAsyncProvider.
@SuppressWarnings("unchecked")
private Class<?> generatedAsyncProvider(String className, Key<?> providedKey) {
CtClass providedCtClass = GwtClassPool.getCtClass(providedKey.getTypeLiteral().getRawType());
CtClass c = GwtClassPool.get().makeClass(className);
c.addInterface(GwtClassPool.getCtClass(AsyncProvider.class));
try {
ClassFile classFile = c.getClassFile();
classFile.setVersionToJava5();
ConstPool constantPool = classFile.getConstPool();
CtField provider = CtField.make("private " + Provider.class.getName() + " provider;", c);
c.addField(provider);
FieldInfo fieldInfo = provider.getFieldInfo();
// Make it generic
SignatureAttribute signatureAttribute = new SignatureAttribute(fieldInfo.getConstPool(), "Lcom/google/inject/Provider<" + Descriptor.of(providedCtClass) + ">;");
fieldInfo.addAttribute(signatureAttribute);
AnnotationsAttribute attr = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
javassist.bytecode.annotation.Annotation a = new javassist.bytecode.annotation.Annotation(Inject.class.getName(), constantPool);
attr.setAnnotation(a);
provider.getFieldInfo().addAttribute(attr);
CtMethod get = CtMethod.make("public void get(" + AsyncCallback.class.getName() + " callback) { callback.onSuccess(provider.get()); }", c);
c.addMethod(get);
return c.toClass();
} catch (CannotCompileException e) {
throw new GwtTestGinException("Error while creating AsyncProvider subclass [" + className + "]", e);
}
}
use of javassist.CtField in project coprhd-controller by CoprHD.
the class DbSchemaChanger method addBeanProperty.
/**
* Add a bean property to the class i.e. add followings to a class:
* 1. a class field
* 2. a getter method
* 3. a setter method
*
* @param propertyName the bean property name
* @param propertyClazz the bean property type
* @param columnName the corresponding column name of the bean property
*/
public <T> DbSchemaChanger addBeanProperty(String propertyName, Class<T> propertyClazz, String columnName) throws Exception {
CtField f = new CtField(pool.get(propertyClazz.getName()), propertyName, cc);
cc.addField(f);
String getterMethodName = generateGetterMethodName(propertyName);
StringBuilder method = new StringBuilder("public ");
// return type
method.append(propertyClazz.getName());
method.append(" ");
method.append(getterMethodName);
method.append("() {\n return ");
method.append(propertyName);
method.append(";\n }");
log.info("Generate getter method = {}", method.toString());
CtMethod getter = CtNewMethod.make(method.toString(), cc);
cc.addMethod(getter);
String setterMethodName = generateSetterMethodName(propertyName);
method = new StringBuilder("public void ");
method.append(setterMethodName);
method.append("(");
method.append(propertyClazz.getName());
method.append(" ");
method.append(propertyName);
method.append(") {\n this.");
method.append(propertyName);
method.append(" = ");
method.append(propertyName);
method.append(";\n setChanged(\"");
method.append(columnName);
method.append("\");\n }");
log.info("Generate setter method = {}", method.toString());
CtMethod setter = CtNewMethod.make(method.toString(), cc);
cc.addMethod(setter);
dumpClassInfo();
return this;
}
use of javassist.CtField 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.CtField 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