Search in sources :

Example 31 with CtField

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);
}
Also used : CtField(javassist.CtField) InitMethod(com.googlecode.gwt.test.patchers.InitMethod)

Example 32 with CtField

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);
    }
}
Also used : javassist.bytecode(javassist.bytecode) AsyncProvider(com.google.gwt.inject.client.AsyncProvider) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) CannotCompileException(javassist.CannotCompileException) AsyncProvider(com.google.gwt.inject.client.AsyncProvider) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod)

Example 33 with CtField

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;
}
Also used : CtField(javassist.CtField) CtMethod(javassist.CtMethod)

Example 34 with CtField

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;
}
Also used : CtField(javassist.CtField) AccessType(javax.persistence.AccessType) CtMethod(javassist.CtMethod)

Example 35 with CtField

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);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Aggregations

CtField (javassist.CtField)76 CtClass (javassist.CtClass)47 CtMethod (javassist.CtMethod)27 CannotCompileException (javassist.CannotCompileException)24 NotFoundException (javassist.NotFoundException)22 ClassPool (javassist.ClassPool)20 CtConstructor (javassist.CtConstructor)15 Test (org.junit.Test)12 ClassFile (javassist.bytecode.ClassFile)9 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ConstPool (javassist.bytecode.ConstPool)4 SMethod (org.bimserver.shared.meta.SMethod)4 SParameter (org.bimserver.shared.meta.SParameter)4 InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)3 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3