Example usage for com.google.gwt.user.rebind ClassSourceFileComposerFactory createSourceWriter

List of usage examples for com.google.gwt.user.rebind ClassSourceFileComposerFactory createSourceWriter

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind ClassSourceFileComposerFactory createSourceWriter.

Prototype

public SourceWriter createSourceWriter(PrintWriter printWriter) 

Source Link

Document

Creates an implementation of SourceWriter that can be used to write the innards of a class.

Usage

From source file:cc.alcina.framework.entity.gwtsynth.ClientReflectionGenerator.java

License:Apache License

private SourceWriter createWriter(ClassSourceFileComposerFactory factory, PrintWriter contextWriter) {
    PrintWriter writer = contextWriter;
    if (debug) {/* w w  w .ja v  a  2 s . c o m*/
        writer = new BiWriter(writer);
        wrappedWriters.put(contextWriter, (BiWriter) writer);
    }
    return factory.createSourceWriter(writer);
}

From source file:com.rhizospherejs.gwt.rebind.MappingWriter.java

License:Open Source License

private SourceWriter getSourceWriter() {
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);

    composerFactory.addImport("com.google.gwt.core.client.JavaScriptObject");
    composerFactory.addImport("com.rhizospherejs.gwt.client.RhizosphereKind");
    composerFactory.addImport("com.rhizospherejs.gwt.client.RhizosphereMapping");
    composerFactory.addImport("com.rhizospherejs.gwt.client.RhizosphereMetaModel");
    composerFactory.addImport("com.rhizospherejs.gwt.client.RhizosphereMetaModel.Attribute");
    composerFactory.addImport("com.rhizospherejs.gwt.client.bridge.ModelBridge");
    composerFactory.addImport("com.rhizospherejs.gwt.client.meta.MetaModelFactory");
    composerFactory.addImport("com.rhizospherejs.gwt.client.meta.AttributeDescriptor");
    composerFactory.addImport(ModelInspector.CUSTOM_ATTRIBUTES_INTERFACE);
    composerFactory.addImport(BridgeCapabilities.JSO_BUILDER_CLASS);
    composerFactory.addImport(BridgeCapabilities.METAMODEL_ATTRIBUTE_BUILDER_CLASS);

    composerFactory.addImplementedInterface("RhizosphereMapping<" + modelClassName + ">");

    return composerFactory.createSourceWriter(this.pw);
}

From source file:org.webinit.gwt.rebind.InterfaceProxyGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {

    // retrieve the type oracle
    TypeOracle oracle = context.getTypeOracle();
    assert oracle != null;

    // get the implementation name from the type name
    String packageName;//from  w ww.  jav  a  2s .  c  om
    String implementationName;
    if (typeName.lastIndexOf('.') == -1) {
        packageName = "";
        implementationName = typeName + IMPLEMENTATION_SUFFIX;
    } else {
        packageName = typeName.substring(0, typeName.lastIndexOf('.'));
        implementationName = typeName.substring(typeName.lastIndexOf('.') + 1) + IMPLEMENTATION_SUFFIX;
    }

    JClassType sourceClass = oracle.findType(typeName);

    if (sourceClass == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    // checking done, get the source writers

    ClassSourceFileComposerFactory oocf = new ClassSourceFileComposerFactory(packageName, implementationName);

    // if the source class is a Class, adds it as a superclass
    if (sourceClass.isClass() != null) {
        oocf.setSuperclass(typeName);
    }
    if (sourceClass.isInterface() != null) {
        oocf.addImplementedInterface(sourceClass.getQualifiedSourceName());
        JClassType[] implementedInterfaces = sourceClass.getImplementedInterfaces();
        for (JClassType intf : implementedInterfaces) {
            oocf.addImplementedInterface(intf.getQualifiedSourceName());
        }
    }

    oocf.addImplementedInterface(ProxyInterface.class.getCanonicalName());

    // go ahead to write source
    PrintWriter printWriter = context.tryCreate(logger, packageName, implementationName);

    if (null == printWriter) {
        return packageName + "." + implementationName;
    }

    logger.log(TreeLogger.INFO, "Generating " + packageName + "." + implementationName + " for " + typeName);

    SourceWriter sourceWriter = oocf.createSourceWriter(printWriter);

    sourceWriter.println("public String getWIInterfaceProxyGeneratorVersion() {");
    sourceWriter.println("\treturn \"" + VERSION + "\";");
    sourceWriter.println("}");

    for (JClassType intf : sourceClass.getImplementedInterfaces()) {
        JClassType targetClass = oracle.findType(intf.getQualifiedSourceName() + PROXY_SUFFIX);

        // the proxy implementation found
        if (targetClass != null) {
            String proxyObjectName = PROXY_OBJECT_PREFIX + intf.getSimpleSourceName();
            writeProxyObject(logger, targetClass.getQualifiedSourceName(), proxyObjectName, sourceWriter);
            // write methods
            JMethod[] methods = intf.getMethods();
            for (JMethod method : methods) {

                // do not write out the method if it is a tag method.
                if (method.isAnnotationPresent(SkipProxy.class)) {
                    logger.log(TreeLogger.DEBUG, "Skip " + method.getReadableDeclaration()
                            + " because it's skipped in the interface.");
                    continue;
                }

                // test target class's eligibility
                final JMethod targetMethod = getMethod(targetClass, method);

                if (targetMethod != null && targetMethod.isAnnotationPresent(SkipProxy.class)) {
                    logger.log(TreeLogger.DEBUG, "Skip " + method.getReadableDeclaration()
                            + " because it's skipped in the proxy implementation.");
                    continue;
                }

                // test source class's eligibility
                final JMethod sourceMethod = getMethod(sourceClass, method);
                if (sourceMethod != null && sourceMethod.isAnnotationPresent(OverrideProxy.class)) {
                    logger.log(TreeLogger.DEBUG, "Skip " + method.getReadableDeclaration()
                            + " because it's overriden in the source class.");
                    continue;
                }

                // notify empty implementation
                if (targetMethod != null && targetMethod.isAnnotationPresent(EmptyImplementation.class))
                    logger.log(TreeLogger.TRACE,
                            "Empty implementation of " + method.getReadableDeclaration() + " in "
                                    + targetClass.getQualifiedSourceName() + ".\n"
                                    + "Please @OverrideProxy in proper classes.");

                writeMethod(logger, method, proxyObjectName, sourceWriter);
            }
        }
    }

    sourceWriter.println("}");
    context.commit(logger, printWriter);
    return packageName + "." + implementationName;
}

From source file:org.webinit.gwt.rebind.ObservableObjectGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    // retrieve the type oracle
    TypeOracle oracle = context.getTypeOracle();
    assert oracle != null;

    // get the implementation name from the type name
    String implementationName;//from w  ww .j  a v a  2  s  .c o  m
    if (typeName.lastIndexOf('.') == -1) {
        implementationName = typeName + implementationSuffix;
    } else {
        implementationName = typeName.substring(typeName.lastIndexOf('.') + 1) + implementationSuffix;
    }

    logger.log(TreeLogger.DEBUG, "Generating for " + typeName);

    // try to get java.lang.Object
    try {
        objectType_ = oracle.getType("java.lang.Object");
    } catch (NotFoundException nfe) {
        logger.log(TreeLogger.ERROR, typeName, nfe);
        return null;
    }

    JClassType sourceClass = oracle.findType(typeName);

    if (sourceClass == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    // if target class is an interface and it has one or more unknown methods,
    // we cannot implement this interface because we cannot implement unknown methods.
    if (sourceClass.isInterface() != null) {
        JClassType targetInterface = oracle
                .findType(org.webinit.gwt.client.Observable.class.getCanonicalName());
        JMethod[] targetMethods = targetInterface.getMethods();
        JMethod[] methods = sourceClass.getMethods();
        int known = 0;
        for (JMethod method : methods) {
            for (JMethod targetMethod : targetMethods) {
                if (method.equals(targetMethod)) {
                    known++;
                }
            }
        }
        if (known < methods.length) {
            logger.log(TreeLogger.ERROR,
                    "Unable to implement/extend " + typeName + ", because it has unknown methods.");
            throw new UnableToCompleteException();
        }

    }
    // target class is a class,
    // if it has unknown abstract methods, we cannot implement it.
    // if it has methods with the same signature as the observable interface, we cannot implement it.
    else {
        JClassType targetInterface = oracle
                .findType(org.webinit.gwt.client.Observable.class.getCanonicalName());
        JMethod[] targetMethods = targetInterface.getMethods();

        JMethod[] methods = sourceClass.getMethods();
        for (JMethod method : methods) {
            for (JMethod targetMethod : targetMethods) {
                if (!method.equals(targetMethod) && method.isAbstract()) {
                    logger.log(TreeLogger.ERROR,
                            "Unable to implement/extend " + typeName + ", because it has unknown method");
                    throw new UnableToCompleteException();
                }
                if (method.getReadableDeclaration().equals(targetMethod.getReadableDeclaration())
                        && !method.isAbstract()) {
                    logger.log(TreeLogger.ERROR, "Source class contains a conflict method " + method.getName());
                    throw new UnableToCompleteException();
                }
            }
        }
    }

    // checking done, get the source writers

    ClassSourceFileComposerFactory oocf = new ClassSourceFileComposerFactory(packageName, implementationName);

    // if the source class is a Class, adds it as a superclass
    if (sourceClass.isClass() != null) {
        oocf.setSuperclass(typeName);
    }
    if (sourceClass.isInterface() != null) {
        oocf.addImplementedInterface(sourceClass.getQualifiedSourceName());
        JClassType[] implementedInterfaces = sourceClass.getImplementedInterfaces();
        for (JClassType intf : implementedInterfaces) {
            oocf.addImplementedInterface(intf.getQualifiedSourceName());
        }
    }

    // add imports
    oocf.addImport("java.util.Map");
    oocf.addImport("java.util.Set");
    oocf.addImport("java.util.List");
    oocf.addImport("java.util.HashMap");
    oocf.addImport("java.util.HashSet");
    oocf.addImport("java.util.ArrayList");

    PrintWriter printWriter = context.tryCreate(logger, packageName, implementationName);
    SourceWriter sourceWriter = oocf.createSourceWriter(printWriter);

    writeObservableObjectClass(logger, sourceWriter);
    context.commit(logger, printWriter);

    return packageName + "." + implementationName;
}

From source file:xapi.dev.generators.RunAsyncInjectionGenerator.java

License:Open Source License

@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    Iterable<InjectionCallbackArtifact> artifacts = getInjectionMap(logger, context).getArtifacts();
    //        ctx.getArtifacts().find(InjectionCallbackArtifact.class);
    if (typeName.endsWith(".Callbacks")) {
        typeName = typeName.substring(0, typeName.length() - 10);
        logger.log(Type.INFO, "" + typeName);
        for (InjectionCallbackArtifact artifact : artifacts) {
            if (artifact.getAsyncInjectionName().equals(typeName)) {
                //we have our injectable artifact!
                int packageIndex = typeName.lastIndexOf('.');
                String packageName = typeName.substring(0, packageIndex);
                String generatedName = "RunAsync" + typeName.substring(packageIndex + 12);

                typeName = packageName + "." + generatedName;
                logger.log(Type.INFO, "WIN " + typeName + " <- " + artifact);

                PrintWriter printWriter = context.tryCreate(logger, packageName, generatedName);
                if (printWriter == null) {
                    logger.log(Type.TRACE, "Re-Using existing " + typeName);
                    return new RebindResult(RebindMode.USE_EXISTING, typeName);
                }//from www.  ja v a2 s  .c  o m
                ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName,
                        generatedName);
                composer.addImplementedInterface(ApplyMethod.class.getName());
                composer.setPrivacy("public final");
                composer.addImport(GWT.class.getName());
                composer.addImport(ApplyMethod.class.getName());
                composer.addImport(ProvidesValue.class.getName());
                composer.addImport(ReceivesValue.class.getName());
                composer.addImport(Runnable.class.getName());
                composer.addImport(Fifo.class.getName());
                composer.addImport(JsFifo.class.getName());
                composer.addImport(Timer.class.getName());
                composer.addImport(Scheduler.class.getName());
                composer.addImport(ScheduledCommand.class.getCanonicalName());

                SourceWriter sw = composer.createSourceWriter(printWriter);
                sw.println("private static final Fifo<Object> callbacks;");
                sw.println("static{");
                sw.indent();
                sw.println("callbacks = JsFifo.newFifo();");
                for (String callback : artifact.getCallbacks()) {
                    sw.println("callbacks.give(GWT.create(" + callback + ".class));");
                }
                sw.outdent();
                sw.println("}");

                sw.println("@SuppressWarnings({\"unchecked\", \"rawtypes\"})");
                sw.println("public void apply(final Object ... args){");

                DefermentWriter defer = new DefermentWriter(sw);
                defer.printStart();//used to optionally push callbacks into new execution block

                //for now, we are only sending the service object as parameter
                sw.println("  Object service = args[0], callback;");
                sw.println("  while(!callbacks.isEmpty()){");
                sw.indent();
                sw.println("  callback = callbacks.take();");
                sw.println("  if (callback instanceof ReceivesValue){");
                sw.println("    ((ReceivesValue)callback).set(service);");
                sw.println("  }");
                sw.println("  else if (callback instanceof ApplyMethod){");
                sw.println("    ((ApplyMethod)callback).apply(args);");
                sw.println("  }");
                sw.println("  else if (callback instanceof ProvidesValue){");
                sw.println("    ((ProvidesValue<ReceivesValue>)callback).get().set(service);");
                sw.println("  }");
                sw.println("  else if (callback instanceof Runnable){");
                sw.println("    ((Runnable)callback).run();");
                sw.println("  }");
                sw.println("}");
                sw.outdent();

                defer.printFinish();

                sw.println("}");
                //          sw.println("}");
                sw.commit(logger);
                context.commit(logger, printWriter);
                return new RebindResult(RebindMode.USE_ALL_NEW, typeName);
            }
        }
    }
    logger.log(Type.INFO, "No callback class found for " + typeName + "; returning untransformed.");
    return new RebindResult(RebindMode.USE_EXISTING, typeName);
}