Search in sources :

Example 1 with AsyncProvider

use of com.google.gwt.inject.client.AsyncProvider in project gwt-test-utils by gwt-test-utils.

the class DeferredBindingModule method generatedAsyncProvider.

@SuppressWarnings("unchecked")
private Class<Object> 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)

Aggregations

AsyncProvider (com.google.gwt.inject.client.AsyncProvider)1 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1 CannotCompileException (javassist.CannotCompileException)1 CtClass (javassist.CtClass)1 CtField (javassist.CtField)1 CtMethod (javassist.CtMethod)1 javassist.bytecode (javassist.bytecode)1