Example usage for com.google.gwt.user.rebind SourceWriter outdent

List of usage examples for com.google.gwt.user.rebind SourceWriter outdent

Introduction

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

Prototype

void outdent();

Source Link

Usage

From source file:org.tiwonk.gwt.rest.generator.ClientResourceGenerator.java

License:Apache License

private void saveMethodReturnsVoid(GeneratorInfo info, SourceWriter writer, JMethod method)
        throws UnableToCompleteException {

    String callbackParam = info.asyncCallbackParam.getName();
    String resourceParam = info.resourceParam.getName();
    String methodName = info.httpMethod.toString();

    // RequestBuilder request = new RequestBuilder(RequestBuilder.POST, getResourceURL());
    writer.println();/*from   w  w  w. j  a va 2  s.c o  m*/
    writer.indent();
    writer.println("RequestBuilder request = new RequestBuilder(RequestBuilder." + methodName
            + ", getResourceURL());");
    writer.indent();
    // request.setHeader("Content-Type", "application/vnd.example.v1.person");
    String consumes = info.consumes(method);
    if (consumes != null) {
        writer.print("request.setHeader(\"Content-Type\", \"");
        writer.print(consumes);
        writer.println("\");");
    }

    String produces = info.produces(method);
    if (produces != null) {
        writer.print("request.setHeader(\"Accept\", \"");
        writer.print(produces);
        writer.println("\");");
    }

    // JSONObject json = new JSONObject(person);
    // String payload = json.toString();
    writer.println();
    writer.print("JSONObject json = new JSONObject(");
    writer.print(resourceParam);
    writer.println(");");
    writer.println("String payload = json.toString();");

    //    try {
    //     request.sendRequest(payload, new RequestCallback() {
    writer.println();
    writer.println("try {");
    writer.indent();
    writer.println("request.sendRequest(payload, new RequestCallback() {");

    //    @Override
    //    public void onResponseReceived(Request request, Response response) {
    //      
    //      if (
    //        response.getStatusCode() != Response.SC_CREATED &&
    //        response.getStatusCode() != Response.SC_OK
    //      ) {
    //        onError(request, new ResourceAccessException(response));
    //        return;
    //      }
    //      callback.onSuccess(null);
    //    }
    writer.println();
    writer.println("@Override");
    writer.println("public void onResponseReceived(Request request, Response response) {");
    writer.indent();
    writer.println("if (");
    writer.println("  response.getStatusCode() != Response.SC_CREATED &&");
    writer.println("  response.getStatusCode() != Response.SC_OK");
    writer.println(") {");
    writer.println("  onError(request, new ResourceAccessException(response));");
    writer.println("  return;");
    writer.println("}");
    writer.println();
    writer.print(callbackParam);
    writer.println(".onSuccess(null);");
    writer.outdent();
    writer.println("}");

    //    @Override
    //    public void onError(Request request, Throwable e) {
    //      callback.onFailure(e);
    //    }
    //  });
    writer.println();
    writer.println("@Override");
    writer.println("public void onError(Request request, Throwable e) {");
    writer.print("  ");
    writer.print(callbackParam);
    writer.println(".onFailure(e);");
    writer.println("}");
    writer.outdent();
    writer.println("});");
    writer.outdent();

    //  } catch (RequestException e) {
    //    callback.onFailure(e);
    //  }
    writer.println();
    writer.println("} catch (RequestException e) {");
    writer.print("  ");
    writer.print(callbackParam);
    writer.println(".onFailure(e);");
    writer.println("}");
    writer.outdent();

}

From source file:org.tiwonk.gwt.rest.generator.ClientResourceGenerator.java

License:Apache License

private void buildGetMethod(GeneratorInfo info, SourceWriter writer, JMethod method) {

    if (info.asyncType == null) {
        info.logger.log(TreeLogger.ERROR,
                "GET method must have a single parameter of type AsyncCallback<T> where T is the resource type.");
        return;//  www.j  ava 2s.co  m
    }

    String callbackParam = info.asyncCallbackParam.getName();

    String resourceSimpleName = info.asyncType.getSimpleSourceName();
    String contentType = info.produces(method);

    writer.indent();
    // RequestBuilder request = new RequestBuilder(RequestBuilder.GET, getResourceURL());
    writer.println("RequestBuilder request = new RequestBuilder(RequestBuilder.GET, getResourceURL());");

    // request.setHeader("Content-Type", "...");
    if (contentType != null) {
        writer.print("request.setHeader(\"Accept\", \"");
        writer.print(contentType);
        writer.println("\");");
    }

    /*
    request.setCallback(new RequestCallback() {
              
      @Override
      public void onResponseReceived(Request request, Response response) {
        if (response.getStatusCode() != Response.SC_OK) {
          onError(request, new ResourceAccessException(response));
          return;
        }
        JSONValue value = JSONParser.parseStrict(response.getText());
        Person entity = toPerson(value.isObject().getJavaScriptObject());
        callback.onSuccess(entity);
      }      
    */

    writer.println("request.setCallback(new RequestCallback() {");
    writer.indent();
    writer.println();
    writer.println("@Override");
    writer.println("public void onResponseReceived(Request request, Response response) {");
    writer.indent();
    writer.println("if (response.getStatusCode() != Response.SC_OK) {");
    writer.indent();
    writer.println("onError(request, new ResourceAccessException(response));");
    writer.println("return;");
    writer.outdent();
    writer.println("}");
    writer.println("JSONValue value = JSONParser.parseStrict(response.getText());");
    writer.print(resourceSimpleName);
    writer.print(" entity = ");
    writer.println("value.isObject().getJavaScriptObject().cast();");
    writer.print(callbackParam);
    writer.println(".onSuccess(entity);");

    writer.outdent();
    writer.println("}");

    /*/
      @Override
      public void onError(Request request, Throwable exception) {
        callback.onFailure(exception);
      }
    });
     */
    writer.println();
    writer.println("@Override");
    writer.println("public void onError(Request request, Throwable exception) {");
    writer.indent();
    writer.print(callbackParam);
    writer.println(".onFailure(exception);");
    writer.outdent();
    writer.println("}");
    writer.outdent();
    writer.println("});");

    /*
    try {
      request.send();
    } catch (RequestException e) {
      callback.onFailure(e);
    }
     */
    writer.println("try {");
    writer.println("  request.send();");
    writer.println("} catch (RequestException e) {");
    writer.print("  ");
    writer.print(callbackParam);
    writer.println(".onFailure(e);");
    writer.println("}");
    writer.outdent();

}

From source file:org.vaadin.alump.offlinebuilder.OfflineConnectorReflectionGenerator.java

License:Open Source License

/**
 * Generated method that returns new instances matching with given class names
 * @param allInstantiableClasses//  w w  w . j  a v a  2  s. com
 * @param sourceWriter
 */
private void generateFactoryMethod(List<JClassType> allInstantiableClasses, SourceWriter sourceWriter) {
    sourceWriter.println("public Object instantiate(String className) {");
    sourceWriter.indent();
    sourceWriter.println("if (className == null) { return null; }");
    for (JClassType classType : allInstantiableClasses) {
        if (classType.isAbstract()) {
            continue;
        }
        sourceWriter.println("else if (className.equals(\"" + classType.getQualifiedSourceName() + "\")) {");
        sourceWriter.indent();
        sourceWriter.println("return new " + classType.getQualifiedSourceName() + "( );");
        sourceWriter.outdent();
        sourceWriter.println("}");
    }
    sourceWriter.println("return null;");
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:org.vectomatic.dev.svg.impl.gen.ExternalSVGResourceGenerator.java

License:Apache License

@Override
public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method)
        throws UnableToCompleteException {
    String name = method.getName();

    SourceWriter sw = new StringSourceWriter();
    sw.println("new " + ExternalSVGResourcePrototype.class.getName() + "(");
    sw.indent();/*from  w w w.  j ava 2  s . c o  m*/
    sw.println('"' + name + "\",");
    // These are field names
    sw.println(externalSVGUrlIdent + ", " + externalSVGCacheIdent + ", ");
    sw.println(offsets.get(method.getName()).toString());
    sw.outdent();
    sw.print(")");

    return sw.toString();
}

From source file:org.vectomatic.dev.svg.impl.gen.SVGResourceGenerator.java

License:Open Source License

@Override
public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method)
        throws UnableToCompleteException {

    // Extract the SVG name from the @Source annotation
    URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method);
    if (resources.length != 1) {
        logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null);
        throw new UnableToCompleteException();
    }/*from w  w  w  .  j a  v  a  2s .  c  om*/
    URL resource = resources[0];

    // The SVGResource is implemented as an anonymous inner class
    // xxx = new SVGResource() {
    //   public OMSVGSVGElement getSvg() {
    //     return OMSVGParser.parse("...");
    //   }
    // };
    SourceWriter sw = new StringSourceWriter();
    sw.println("new " + SVGResource.class.getName() + "() {");
    sw.indent();

    // Convenience when examining the generated code.
    sw.println("// " + resource.toExternalForm());

    sw.println("public " + OMSVGSVGElement.class.getName() + " getSvg() {");
    sw.indent();
    String toWrite = Util.readURLAsString(resource);
    if (getValidated(method)) {
        SVGValidator.validate(toWrite, resource.toExternalForm(), logger, null);
    }
    sw.println("return " + OMSVGParser.class.getName() + ".parse(\"" + Generator.escape(toWrite) + "\");");
    sw.outdent();
    sw.println("}");

    sw.println("public String getName() {");
    sw.indent();
    sw.println("return \"" + method.getName() + "\";");
    sw.outdent();
    sw.println("}");

    sw.outdent();
    sw.println("}");

    return sw.toString();
}

From source file:sheath.rebind.SheathGenerator.java

License:Apache License

@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    JClassType toGenerate = oracle.findType(typeName).isInterface();
    if (toGenerate == null) {
        logger.log(TreeLogger.ERROR, typeName + " is not an interface type");
        throw new UnableToCompleteException();
    }/*from   www  .  ja v  a  2s  .co  m*/

    JClassType sheathType = oracle.findType(Sheath.class.getCanonicalName());
    if (!toGenerate.isAssignableTo(sheathType)) {
        logger.log(TreeLogger.ERROR, typeName + " is not assignable to " + Sheath.class.getCanonicalName());
        throw new UnableToCompleteException();
    }
    if (toGenerate.equals(sheathType)) {
        logger.log(TreeLogger.ERROR,
                "You must declare an interface that extends " + Sheath.class.getCanonicalName());
        throw new UnableToCompleteException();
    }

    // TODO: really generate incrementally!

    Class<?>[] moduleClasses = collectAllModules(logger, toGenerate);

    Map<String, String> injectableTypes = new LinkedHashMap<String, String>();
    Set<Class<?>> staticInjections = new LinkedHashSet<Class<?>>();
    for (Class<?> module : moduleClasses) {
        Module annotation = module.getAnnotation(Module.class);
        String moduleName = module.getCanonicalName();
        for (Class<?> key : annotation.injects()) {
            injectableTypes.put(key.getName(), moduleName);
        }
        for (Class<?> c : annotation.staticInjections()) {
            staticInjections.add(c);
        }
    }

    String packageName = toGenerate.getPackage().getName();
    String simpleSourceName = toGenerate.getName().replace('.', '_') + "Impl";
    PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName);
    if (pw == null) {
        return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING, packageName + "." + simpleSourceName);
    }

    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
    factory.setSuperclass(AbstractSheath.class.getCanonicalName());
    factory.addImplementedInterface(typeName);
    SourceWriter sw = factory.createSourceWriter(context, pw);

    // Linker
    sw.println("private static class Plugin extends AbstractSheathPlugin {");
    sw.indent();
    sw.println("@java.lang.Override");
    sw.println(
            "public native %1$s<?> getAtInjectBinding(%2$s key, %2$s className, boolean mustBeInjectable) /*-{",
            Binding.class.getCanonicalName(), String.class.getCanonicalName());
    sw.indent();
    sw.println("switch (className) {");
    LinkedHashMap<String, String> factoryMethodsToGenerate = new LinkedHashMap<String, String>();
    for (JClassType type : oracle.getTypes()) {
        // XXX: workaround for http://code.google.com/p/google-web-toolkit/issues/detail?id=6799
        // We really should use type.getName().endsWith("$$InjectAdapter") but GWT makes a JClassType
        // with name "InjectAdapter", thinking it's a nested class, and we lose the part before the
        // dollar. So in the mean time, look for the InjectAdapter in the classpath FOR EACH CLASS,
        // and we additionally generate a non-JSNI class to instantiate it, so that processing of the
        // class name is done by JDT rather than GWT's TypeOracle.
        String adapterName = type.getQualifiedSourceName() + "$$InjectAdapter";
        boolean found;
        try {
            Class.forName(adapterName, false, Thread.currentThread().getContextClassLoader());
            found = true;
        } catch (Throwable t) {
            found = false;
        }
        if (found /*&& type.isAssignableTo(oracle.findType(Binding.class.getCanonicalName()))*/) {
            String name = type.getQualifiedSourceName();
            String factoryName = "create_" + name.replace('.', '_') + "_InjectAdapter";
            factoryMethodsToGenerate.put(adapterName, factoryName);
            sw.println("case '%s': return this.@%s.Plugin::%s()();", name, factory.getCreatedClassName(),
                    factoryName);
        }
    }
    sw.println("default: return null;");
    sw.println("}");
    sw.outdent();
    sw.println("}-*/;");
    for (Map.Entry<String, String> factoryMethodToGenerate : factoryMethodsToGenerate.entrySet()) {
        sw.println();
        sw.println("private %s %s() {", factoryMethodToGenerate.getKey(), factoryMethodToGenerate.getValue());
        sw.indentln("return new %s();", factoryMethodToGenerate.getKey());
        sw.println("}");
    }
    sw.println();
    sw.println("@java.lang.Override");
    sw.println("public %s[] createStaticInjections() {", StaticInjection.class.getCanonicalName());
    sw.indent();
    sw.println("return new %s[] {", StaticInjection.class.getCanonicalName());
    sw.indent();
    for (Class<?> staticInjection : staticInjections) {
        sw.println("new %s$$StaticInjection(),", staticInjection.getName());
    }
    sw.outdent();
    sw.println("};");
    sw.outdent();
    sw.println("}");
    sw.outdent();
    sw.print("}");

    // Constructor
    sw.println();
    sw.println("public %s() {", simpleSourceName);
    sw.indent();
    sw.println("super(new Plugin(), ");
    sw.println("new %s<?>[] {", ModuleAdapter.class.getCanonicalName());
    sw.indent();
    for (Class<?> module : moduleClasses) {
        sw.println("new %s$$ModuleAdapter(),", module.getName());
    }
    sw.outdent();
    sw.println("});");
    sw.outdent();
    sw.outdent();
    sw.println("}");

    for (JMethod method : toGenerate.getOverridableMethods()) {
        // TODO: check arguments (number, injectable types)
        if (method.getParameterTypes().length != 1) {
            // could be injectStatics()
            continue;
        }
        JType toInject = method.getParameterTypes()[0];
        sw.println("@java.lang.Override");
        sw.println("public %s %s(%s instance) {", method.getReturnType().getParameterizedQualifiedSourceName(),
                method.getName(), toInject.getParameterizedQualifiedSourceName());
        sw.indent();
        sw.println("doInject(instance, \"members/%s\", %s.class);",
                toInject.getParameterizedQualifiedSourceName(),
                injectableTypes.get(toInject.getQualifiedBinaryName()));
        if (!JPrimitiveType.VOID.equals(method.getReturnType())) {
            sw.println("return arg;");
        }
        sw.outdent();
        sw.println("}");
    }

    sw.commit(logger);

    return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING, packageName + "." + simpleSourceName);
}

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

License:Open Source License

public static InjectionCallbackArtifact ensureAsyncInjected(TreeLogger logger, String packageName,
        String className, String outputClass, GeneratorContext ctx) throws UnableToCompleteException {

    GwtInjectionMap gwtInjectionMap = getInjectionMap(logger, ctx);
    InjectionCallbackArtifact artifact = gwtInjectionMap.getOrCreateArtifact(ctx, packageName, className);
    if (artifact.isTargetUnbound()) {
        artifact.bindTo(outputClass);//from  ww w .j  av  a  2  s  .  co  m
        ensureProviderClass(logger, packageName, artifact.getSimpleName(), artifact.getCanonicalName(),
                artifact.getBoundTarget(), ctx);
        logger = logger.branch(Type.TRACE,
                "Creating asynchronous callback for " + artifact.getCanonicalName() + " -> " + outputClass);
        String generatedName = InjectionUtils.generatedAsyncProviderName(artifact.getGeneratedName());
        String implPackage = artifact.getImplementationPackage();
        PrintWriter printWriter = ctx.tryCreate(logger, implPackage, generatedName);
        if (printWriter == null) {
            logger.log(Type.WARN,
                    "Could not create the source writer for " + implPackage + "." + generatedName);
            return artifact;
        }
        artifact.addCallback(implPackage + "." + generatedName + ".Deproxy");
        ctx.commitArtifact(logger, artifact);

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(implPackage,
                generatedName);
        composer.setPrivacy("public final");

        composer.addImport(com.google.gwt.core.client.GWT.class.getName());
        composer.addImport(RunAsyncCallback.class.getName());
        composer.addImport(Fifo.class.getName());
        composer.addImport(JsFifo.class.getName());
        composer.addImport(AsyncProxy.class.getName());
        composer.addImport(ApplyMethod.class.getName());
        composer.addImport(ReceivesValue.class.getName());
        composer.addImport(ReceiverAdapter.class.getCanonicalName());
        composer.addImport(artifact.getCanonicalName());

        String simpleName = artifact.getSimpleName();
        SourceWriter sw = composer.createSourceWriter(ctx, printWriter);

        sw.println();
        sw.println("static final class Callbacks implements ApplyMethod{");
        sw.indent();
        sw.println("public void apply(Object ... args){");
        sw.println("}");
        sw.outdent();
        sw.println("}");
        sw.println();

        sw.println("static final class Deproxy implements ReceivesValue<" + simpleName + ">{");
        sw.indent();
        sw.println("public final void set(final " + simpleName + " value){");
        sw.indentln("getter = new ReceiverAdapter<" + simpleName + ">(value);");
        sw.println("}");
        sw.outdent();
        sw.println("}");
        sw.println();

        sw.println("private static final class Proxy implements ReceivesValue<ReceivesValue<" + simpleName
                + ">>{");
        sw.indent();
        sw.println("public final void set(final ReceivesValue<" + simpleName + "> receiver){");
        sw.indent();

        sw.print("GWT.runAsync(");
        sw.println(artifact.getCanonicalName() + ".class,new Request(receiver));");

        sw.outdent();
        sw.println("}");
        sw.outdent();
        sw.println("}");
        sw.println();

        sw.println("private static final class Request");
        sw.indent();
        sw.println("extends AsyncProxy<" + simpleName + "> ");
        sw.println("implements RunAsyncCallback{");

        DefermentWriter defer = new DefermentWriter(sw);
        defer.setStrategy(DefermentStrategy.NONE);

        sw.println("private static final Fifo<ReceivesValue<" + simpleName + ">> pending =");
        sw.indentln("JsFifo.newFifo();");
        sw.println();

        sw.println("protected Request(ReceivesValue<" + simpleName + "> receiver){");
        sw.indentln("accept(receiver);");
        sw.println("}");
        sw.println();

        sw.println("@Override");
        sw.println("protected final Fifo<ReceivesValue<" + simpleName + ">> pending(){");
        sw.indentln("return pending;");
        sw.println("}");
        sw.println();

        sw.println("protected final void dispatch(){");
        sw.indentln("go();");
        sw.println("}");
        sw.println();

        sw.println("public final void onSuccess(){");
        sw.indent();
        defer.printStart();

        sw.println("final " + simpleName + " value = ");
        sw.print(packageName + "." + InjectionUtils.generatedProviderName(simpleName));
        sw.println(".theProvider.get();");
        sw.println();

        sw.print("final ApplyMethod callbacks = GWT.create(");
        sw.print(packageName + ".impl." + generatedName + ".Callbacks.class");
        sw.println(");");
        sw.println("callbacks.apply(value);");
        sw.println();

        sw.println("apply(value);");
        sw.outdent();
        sw.println("}");
        sw.outdent();
        defer.printFinish();
        sw.println("}");
        sw.println();

        sw.println("private static ReceivesValue<ReceivesValue<" + simpleName + ">> getter = new Proxy();");
        sw.println();

        sw.println("static void request(final ReceivesValue<" + simpleName + "> request){");
        sw.indentln("getter.set(request);");
        sw.println("}");
        sw.println();

        sw.println("static void go(){");
        sw.indentln("request(null);");
        sw.println("}");
        sw.println();

        sw.println("private " + generatedName + "(){}");

        sw.commit(logger);

    } else {
        assert artifact.getBoundTarget().equals(outputClass) : "The injection target "
                + artifact.getCanonicalName() + " was bound " + "to " + artifact.getBoundTarget()
                + ", but you tried to bind it again, " + "to a different class: " + outputClass;
    }
    return artifact;
}

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

License:Open Source License

public static boolean ensureProviderClass(TreeLogger logger, String packageName, String simpleName0,
        String canonical, String qualifiedSourceName, GeneratorContext ctx) {
    String simpleName = SourceUtil.toSourceName(simpleName0);
    String generatedName = InjectionUtils.generatedProviderName(simpleName);
    String cleanedCanonical = SourceUtil.toSourceName(canonical);
    logger.log(Type.DEBUG, "Creating provider for " + packageName + "." + generatedName);

    PrintWriter printWriter = ctx.tryCreate(logger, packageName, generatedName);
    if (printWriter == null) {
        logger.log(Type.TRACE, "Already generated " + generatedName);
        return false;
    }// w w w. j a  va2s  . co  m
    logger.log(Type.TRACE, "Newly Generating provider " + generatedName + " <- " + qualifiedSourceName);

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, generatedName);
    composer.setSuperclass(SingletonInitializer.class.getName() + "<" + simpleName0 + ">");
    composer.addImport(cleanedCanonical);
    composer.addImport(GWT.class.getName());
    composer.addImport(SingletonProvider.class.getName());

    SourceWriter sw = composer.createSourceWriter(ctx, printWriter);

    sw.println("@Override");
    sw.println("public " + simpleName + " initialValue(){");
    sw.indent();

    sw.print("return GWT.<" + cleanedCanonical + ">create(");
    sw.print(SourceUtil.toSourceName(qualifiedSourceName) + ".class");
    sw.println(");");

    sw.outdent();
    sw.println("}");
    sw.println();
    //now, print a static final provider instance
    sw.print("public static final SingletonProvider<");
    sw.print(simpleName0 + "> ");
    sw.print("theProvider = ");
    sw.println("new " + generatedName + "();");
    sw.commit(logger);
    return true;
}

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

License:Open Source License

/**
 * @throws ClassNotFoundException/*from  w ww.  j a va 2  s . com*/
 */
public static RebindResult execImpl(TreeLogger logger, GeneratorContext context, JClassType type)
        throws ClassNotFoundException, UnableToCompleteException {
    logger.log(Type.TRACE, "Async Inject For " + type.getSimpleSourceName());
    PlatformSet platforms = CurrentGwtPlatform.getPlatforms(context);
    JClassType targetType = type;
    String simpleName = type.getSimpleSourceName();
    SingletonOverride winningOverride = null;
    JClassType winningType = null;
    for (JClassType subtype : type.getSubtypes()) {
        if (winningType == null) {
            SingletonDefault singletonDefault = subtype.getAnnotation(SingletonDefault.class);
            if (singletonDefault != null && platforms.isAllowedType(subtype)) {
                winningType = subtype;
                continue;
            }
        }
        SingletonOverride override = subtype.getAnnotation(SingletonOverride.class);
        if (override != null) {
            logger.log(Type.DEBUG, "Got subtype " + subtype + " : " + " - prodMode: " + context.isProdMode());
            if (platforms.isAllowedType(subtype)) {
                if (winningOverride != null) {
                    if (winningOverride.priority() > override.priority())
                        continue;
                }
                winningOverride = override;
                winningType = subtype;
            }
        }
    }
    if (winningType == null) {
        winningType = targetType;//no matches, resort to instantiate the class sent.
        //TODO sanity check here, or at least log a warning
    }
    String generatedName = "AsyncFor_" + simpleName;
    String packageName = type.getPackage().getName();
    ensureAsyncInjected(logger, packageName, type.getName(), winningType.getQualifiedBinaryName(), context);
    //    ensureCallbackClass(logger, packageName,type.getQualifiedSourceName(),type.getSimpleSourceName(), winningType.getQualifiedSourceName(), context);
    packageName = packageName + ".impl";
    PrintWriter printWriter = context.tryCreate(logger, packageName, generatedName);
    if (printWriter == null) {
        return new RebindResult(RebindMode.USE_EXISTING, packageName + "." + generatedName);
    }
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, generatedName);
    composer.setPrivacy("public final");
    composer.addImplementedInterface("ReceivesValue<ReceivesValue<" + simpleName + ">>");
    composer.addImport(type.getQualifiedSourceName());
    composer.addImport(ReceivesValue.class.getName());

    SourceWriter sw = composer.createSourceWriter(context, printWriter);
    sw.println();

    sw.indent();

    sw.println("@Override");
    sw.println("public final void set(ReceivesValue<" + simpleName + "> x) {");
    sw.indent();

    //      sw.println(packageName+"."+generatedCallbackName(simpleName)+".deferred.push(x);");
    //      sw.println(packageName+"."+generatedCallbackName(simpleName)+".go();");

    sw.outdent();
    sw.println("}");
    sw.outdent();

    sw.commit(logger);

    return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING, packageName + "." + generatedName);

}

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);
                }/*  w  ww. j a v  a  2  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);
}