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

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

Introduction

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

Prototype

void print(String s);

Source Link

Usage

From source file:fr.putnami.pwt.core.service.rebind.ServiceBinderCreator.java

License:Open Source License

private void writeCommandParam(SourceWriter srcWriter, JMethod method,
        Collection<CallbackMethod> callbackSuccess, JParameter callbackParameter) {
    boolean lazy = method.getAnnotation(LazyCommand.class) != null;
    boolean quiet = method.getAnnotation(QuietCommand.class) != null;

    srcWriter.print("CommandParam commandParam = new CommandParam(");
    srcWriter.print("%s, ", lazy);
    srcWriter.print("%s, ", quiet);
    srcWriter.print("this.serializer, ");
    srcWriter.print("Lists.newArrayList(Arrays.asList(");
    int i = 0;/*from w ww. j a v a 2 s.c om*/
    for (JParameter parameter : method.getParameters()) {
        if (!parameter.equals(callbackParameter)) {
            if (i++ > 0) {
                srcWriter.print(", ");
            }
            srcWriter.print("$%d_%s", i, parameter.getName());
        }
    }
    srcWriter.println(")), ");
    srcWriter.indent();
    srcWriter.println("Lists.newArrayList(");
    srcWriter.indent();
    i = 0;
    if (callbackParameter != null) {
        srcWriter.print("$%d_%s", method.getParameters().length, callbackParameter.getName());
        i++;
    }

    if (callbackSuccess != null) {
        for (CallbackMethod callbackMethod : callbackSuccess) {
            if (i++ > 0) {
                srcWriter.print(", ");
            }
            srcWriter.println("new CallbackAdapter<%s>(){", this.typeAsString(method.getReturnType(), true));
            srcWriter.indent();
            if (callbackMethod.successMethodName != null) {
                srcWriter.println("public void onSuccess(%s result){",
                        this.typeAsString(method.getReturnType(), true));
                srcWriter.indent();
                srcWriter.println("getHandler().%s(result);", callbackMethod.successMethodName);
                srcWriter.outdent();
                srcWriter.println("}");
            }
            if (callbackMethod.failureMethodName != null) {
                srcWriter.println("public void onFailure(Throwable caught){",
                        this.typeAsString(method.getReturnType(), true));
                srcWriter.indent();
                srcWriter.println("getHandler().%s(caught);", callbackMethod.failureMethodName);
                srcWriter.outdent();
                srcWriter.println("}");
            }
            srcWriter.outdent();
            srcWriter.print("}");
        }
    }
    srcWriter.outdent();
    srcWriter.println("));");
    srcWriter.outdent();
}

From source file:fr.putnami.pwt.plugin.rest.rebind.RestServiceBinderCreator.java

License:Open Source License

private void writeMethodCallback(SourceWriter srcWriter, JMethod method,
        Collection<CallbackMethod> callbackSuccess, JParameter callbackParam) {
    boolean quiet = method.getAnnotation(QuietCommand.class) != null;

    srcWriter.print("CompositeMethodCallback compositeCallback = new CompositeMethodCallback(");
    srcWriter.print("%s, ", quiet);
    srcWriter.println("Lists.newArrayList(");
    srcWriter.indent();/*w  w w.  j  a  va2 s  .c  o m*/
    int i = 0;
    if (callbackParam != null) {
        srcWriter.print("$%d_%s", method.getParameters().length, callbackParam);
        i++;
    }

    if (callbackSuccess != null) {
        for (CallbackMethod callbackMethod : callbackSuccess) {
            if (i++ > 0) {
                srcWriter.print(", ");
            }
            srcWriter.println("new MethodCallbackAdapter<%s>(){",
                    this.typeAsString(method.getReturnType(), true));
            srcWriter.indent();
            if (callbackMethod.successMethodName != null) {
                srcWriter.println("public void onSuccess(Method method, %s result){",
                        this.typeAsString(method.getReturnType(), true));
                srcWriter.indent();
                srcWriter.println("getHandler().%s(result);", callbackMethod.successMethodName);
                srcWriter.outdent();
                srcWriter.println("}");
            }
            if (callbackMethod.failureMethodName != null) {
                srcWriter.println("public void onFailure(Method method, Throwable caught){",
                        this.typeAsString(method.getReturnType(), true));
                srcWriter.indent();
                srcWriter.println("getHandler().%s(caught);", callbackMethod.failureMethodName);
                srcWriter.outdent();
                srcWriter.println("}");
            }
            srcWriter.outdent();
            srcWriter.print("}");
        }
    }
    srcWriter.outdent();
    srcWriter.println("));");
}

From source file:gwt.ns.webworker.rebind.EmulatedWorkerFactoryGenerator.java

License:Apache License

@Override
public void generateWorkerFactory(TreeLogger logger, GeneratorContext context, JClassType sourceType,
        String genName, PrintWriter out, ModuleDef modDef, JClassType workerEntryType)
        throws UnableToCompleteException {

    ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(sourceType.getPackage().getName(),
            genName);/*  www.  j  a v  a  2  s  . com*/

    // imports and interface
    f.addImport(Worker.class.getName());
    f.addImport(WorkerImplProxy.class.getName());
    f.addImport(workerEntryType.getQualifiedSourceName());
    f.addImplementedInterface(sourceType.getName());

    // new generated source file
    SourceWriter sw = f.createSourceWriter(context, out);

    // @Override
    // public Worker createAndStart() {
    //  WorkerImplProxy proxy = new WorkerImplProxy(new RationalsWorker());
    //  return proxy;
    // }
    sw.println("@Override");
    sw.println("public Worker createAndStart() {");
    sw.indent();

    sw.print("WorkerImplProxy proxy = new WorkerImplProxy(new ");
    sw.print(workerEntryType.getSimpleSourceName());
    sw.println("());");
    sw.println("return proxy;");

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

    sw.commit(logger);
}

From source file:gwt.ns.webworker.rebind.NativeWorkerFactoryGenerator.java

License:Apache License

@Override
public void generateWorkerFactory(TreeLogger logger, GeneratorContext context, JClassType sourceType,
        String genName, PrintWriter out, ModuleDef modDef, JClassType workerEntryType)
        throws UnableToCompleteException {

    // native worker, so request worker compilation
    WorkerRequestArtifact request = new WorkerRequestArtifact(modDef.getCanonicalName(), modDef.getName());
    context.commitArtifact(logger, request);

    ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(sourceType.getPackage().getName(),
            genName);/*w w w  .j a v  a 2s. c  o m*/

    // imports and interface
    f.addImport(Worker.class.getName());
    f.addImport(WorkerImplNative.class.getName());
    f.addImplementedInterface(sourceType.getName());

    // new generated source file
    if (out != null) {
        SourceWriter sw = f.createSourceWriter(context, out);

        // @Override
        // public Worker createAndStart() {
        //   return WorkerImplNative.create("PLACEHOLDER_PATH"); }
        // Note: placeholder path will be replaced by linker
        sw.println("@Override");
        sw.println("public Worker createAndStart() {");
        sw.indent();
        sw.print("return WorkerImplNative.create(\"");
        sw.print(request.getRelativePlaceholderUrl());
        sw.println("\");");
        sw.outdent();
        sw.println("}");

        sw.commit(logger);
    }
}

From source file:net.officefloor.plugin.gwt.comet.generate.CometAdapterGenerator.java

License:Open Source License

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

    final String COMET_LISTENER_CLASS_NAME = CometSubscriber.class.getName();

    // Determine if the CometListener
    if (COMET_LISTENER_CLASS_NAME.equals(typeName)) {
        logger.log(Type.DEBUG, "Not generating for " + typeName);
        return typeName; // use CometListener as is
    }/*w  w  w  .  ja va  2  s. c  o  m*/

    // Obtain the type
    TypeOracle oracle = context.getTypeOracle();
    JClassType type = oracle.findType(typeName);
    if (type == null) {
        logger.log(Type.ERROR, "Can not find " + JClassType.class.getSimpleName() + " for " + typeName);
        throw new UnableToCompleteException();
    }

    // Ensure type has correct signature
    JMethod[] methods = type.getMethods();
    if (methods.length != 1) {
        logger.log(Type.ERROR,
                "Interface " + type.getQualifiedSourceName()
                        + " must only have one method declared as the interface is marked with "
                        + CometSubscriber.class.getSimpleName());
        throw new UnableToCompleteException();
    }
    JMethod method = methods[0];

    // Ensure method has only one parameter
    JParameter[] parameters = method.getParameters();
    JParameter eventParameter = null;
    JParameter matchKeyParameter = null;
    switch (parameters.length) {
    case 2:
        matchKeyParameter = parameters[1];
    case 1:
        eventParameter = parameters[0];
    case 0:
        break;
    default:
        // Too many parameters
        logger.log(Type.ERROR, "Interface method " + type.getQualifiedSourceName() + "." + method.getName()
                + " must have no more than two parameters (event and match key) as the interface is marked with "
                + CometSubscriber.class.getSimpleName());
        throw new UnableToCompleteException();
    }

    // Ensure no throws on the method
    if (method.getThrows().length != 0) {
        logger.log(Type.ERROR,
                "Interface method " + type.getQualifiedSourceName() + "." + method.getName()
                        + " must not throw exceptions as the interface is marked with "
                        + CometSubscriber.class.getSimpleName());
        throw new UnableToCompleteException();
    }

    // Obtain details to generate adapter class
    String packageName = type.getPackage().getName();
    String simpleName = type.getSimpleSourceName() + "Adapter";
    String qualifiedName = packageName + "." + simpleName;
    String methodName = method.getName();
    logger.log(Type.TRACE, "Generating " + CometSubscriber.class.getSimpleName() + " Adapter for " + typeName
            + " [resulting class " + qualifiedName + "]");

    // Generate the adapter
    ClassSourceFileComposerFactory adapter = new ClassSourceFileComposerFactory(packageName, simpleName);
    adapter.addImplementedInterface(CometAdapter.class.getName());
    adapter.addImport(CometAdapter.class.getName());
    adapter.addImport(OfficeFloorComet.class.getName());
    PrintWriter src = context.tryCreate(logger, packageName, simpleName);
    if (src == null) {
        logger.log(Type.TRACE, qualifiedName + " for adapting " + CometSubscriber.class.getSimpleName()
                + " already generated.");
        return qualifiedName; // should already exist
    }
    SourceWriter writer = adapter.createSourceWriter(context, src);

    // Provide handleEvent method
    writer.println("@Override");
    writer.println("public void handleEvent(Object handler, Object event, Object matchKey) {");
    writer.print("    ((" + typeName + ") handler)." + methodName + "(");
    if (eventParameter != null) {
        writer.print("(" + eventParameter.getType().getQualifiedSourceName() + ") event");
    }
    if (matchKeyParameter != null) {
        writer.print(", (" + matchKeyParameter.getType().getQualifiedSourceName() + ") matchKey");
    }
    writer.println(");");
    writer.println("}");
    writer.println();

    // Provide createPublisher method
    writer.println("@Override");
    writer.println("public Object createPublisher() {");
    writer.println("    return new " + typeName + "() {");
    writer.println("        @Override");
    writer.print("        public void " + methodName + "(");
    if (eventParameter != null) {
        writer.print(eventParameter.getType().getQualifiedSourceName() + " event");
    }
    if (matchKeyParameter != null) {
        writer.print(", " + matchKeyParameter.getType().getQualifiedSourceName() + " matchKey");
    }
    writer.print(") {");
    writer.println("            OfficeFloorComet.publish(" + typeName + ".class, "
            + (eventParameter == null ? "null" : "event") + ", "
            + (matchKeyParameter == null ? "null" : "matchKey") + ");");
    writer.println("        }");
    writer.println("    };");
    writer.println("}");

    writer.commit(logger);

    // Return adapter
    return qualifiedName;
}

From source file:net.sf.mmm.service.impl.gwt.rpc.client.rebind.RemoteInvocationServiceCallerGenerator.java

License:Apache License

/**
 * {@inheritDoc}// w ww .  j  a va2  s . c o  m
 */
@Override
protected void generateClassContents(JClassType inputType, TreeLogger logger, SourceWriter sourceWriter,
        String simpleName, GeneratorContext context) {

    generateSourcePublicConstructorDeclaration(sourceWriter, simpleName);

    // generate CDI constructor
    // sourceWriter.print("@");
    // sourceWriter.println(Inject.class.getSimpleName());
    // sourceWriter.print("public ");
    // sourceWriter.print(simpleName);
    // sourceWriter.print("(");
    // sourceWriter.print(RemoteInvocationGenericServiceGwtAsync.class.getSimpleName());
    // sourceWriter.print(" genericService");
    // sourceWriter.println(") {");
    // sourceWriter.indent();
    // sourceWriter.println("super(genericService);");

    // generate service clients and register in constructor...
    TypeOracle typeOracle = context.getTypeOracle();
    JClassType dabayServiceType = typeOracle.findType(RemoteInvocationService.class.getName());
    for (JClassType type : typeOracle.getTypes()) {
        if ((type.isAssignableTo(dabayServiceType)) && (!type.equals(dabayServiceType))
                && (type.isInterface() != null)) {
            sourceWriter.print("registerService(");
            sourceWriter.print(type.getQualifiedSourceName());
            sourceWriter.print(".class, new ");
            sourceWriter.print(generateServiceClient(type, inputType.getPackage().getName(), logger, context));
            sourceWriter.println("());");
        }
    }
    generateSourceCloseBlock(sourceWriter);
}

From source file:net.sf.mmm.service.impl.gwt.rpc.client.rebind.RemoteInvocationServiceCallerGenerator.java

License:Apache License

/**
 * This method generates a service-client implementation of a {@link RemoteInvocationService}-interface
 * given by <code>serviceInterface</code>.
 * //from  w w w  .  j  av  a 2  s.  co m
 * @param serviceInterface is the {@link RemoteInvocationService}-interface.
 * @param packageName is the {@link Package#getName() package name}.
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 * @return the qualified name of the generated class.
 */
protected String generateServiceClient(JClassType serviceInterface, String packageName, TreeLogger logger,
        GeneratorContext context) {

    String simpleName = serviceInterface.getName() + "ClientImpl";
    logger.log(TreeLogger.INFO, getClass().getSimpleName() + ": Generating " + simpleName);
    ClassSourceFileComposerFactory sourceComposerFactory = new ClassSourceFileComposerFactory(packageName,
            simpleName);

    // imports
    sourceComposerFactory.addImport(RemoteInvocationService.class.getName());
    sourceComposerFactory.addImport(Serializable.class.getName());
    sourceComposerFactory.addImport(GenericRemoteInvocationRpcCall.class.getName());
    sourceComposerFactory.addImport(AbstractRemoteInvocationServiceClient.class.getName());

    sourceComposerFactory.addImplementedInterface(serviceInterface.getQualifiedSourceName());
    sourceComposerFactory.setSuperclass(AbstractRemoteInvocationServiceClient.class.getSimpleName());
    PrintWriter writer = context.tryCreate(logger, packageName, simpleName);
    if (writer != null) {
        SourceWriter sourceWriter = sourceComposerFactory.createSourceWriter(context, writer);
        // generate constructor
        sourceWriter.print("public ");
        sourceWriter.print(simpleName);
        sourceWriter.println("() {");
        sourceWriter.indent();
        sourceWriter.println("super();");
        sourceWriter.outdent();
        sourceWriter.println("}");

        // generate service-interface methods to implement
        for (JMethod method : serviceInterface.getOverridableMethods()) {

            generateServiceClientMethod(serviceInterface, sourceWriter, method);
        }

        sourceWriter.commit(logger);
    }
    return sourceComposerFactory.getCreatedClassName();
}

From source file:net.sf.mmm.service.impl.gwt.rpc.client.rebind.RemoteInvocationServiceCallerGenerator.java

License:Apache License

/**
 * This method generates the implementation of a method for a service-client.
 * /*  w w  w .ja v  a  2s  . com*/
 * @param serviceInterface is the {@link RemoteInvocationService}-interface.
 * @param sourceWriter is the {@link SourceWriter}.
 * @param method is the {@link JMethod} to generate.
 */
private void generateServiceClientMethod(JClassType serviceInterface, SourceWriter sourceWriter,
        JMethod method) {

    // generate method declaration...
    sourceWriter.print("public ");
    JType returnType = method.getReturnType();
    sourceWriter.print(returnType.getQualifiedSourceName());
    sourceWriter.print(" ");
    sourceWriter.print(method.getName());
    sourceWriter.print("(");
    String separator = "";
    JParameter[] parameters = method.getParameters();
    for (JParameter parameter : parameters) {
        if (separator.length() == 0) {
            separator = ", ";
        } else {
            sourceWriter.print(separator);
        }
        sourceWriter.print(parameter.getType().getQualifiedSourceName());
        sourceWriter.print(" ");
        sourceWriter.print(parameter.getName());
    }
    sourceWriter.println("){");

    // generate method body...
    sourceWriter.indent();

    // generate statement for argument array
    sourceWriter.print(Serializable.class.getSimpleName());
    sourceWriter.print("[] _arguments = new ");
    sourceWriter.print(Serializable.class.getSimpleName());
    sourceWriter.print("[");
    sourceWriter.print(Integer.toString(parameters.length));
    sourceWriter.println("];");

    String[] signatureArray = new String[parameters.length];
    // fill in arguments
    for (int i = 0; i < parameters.length; i++) {
        // assign argument statement
        JParameter parameter = parameters[i];
        sourceWriter.print("_arguments[");
        sourceWriter.print(Integer.toString(i));
        sourceWriter.print("] = ");
        sourceWriter.print(parameter.getName());
        sourceWriter.println(";");

        // assign argument type for signature
        signatureArray[i] = parameter.getType().getQualifiedSourceName();
    }

    // generate statement to create call
    sourceWriter.print(GenericRemoteInvocationRpcCall.class.getSimpleName());
    sourceWriter.print(" _call = new ");
    sourceWriter.print(GenericRemoteInvocationRpcCall.class.getSimpleName());
    sourceWriter.print("(");
    sourceWriter.print(serviceInterface.getQualifiedSourceName());
    sourceWriter.print(".class.getName(), \"");
    sourceWriter.print(method.getName());
    sourceWriter.print("\", ");
    sourceWriter.print(Integer.toString(GenericRemoteInvocationRpcCall.getSignature(signatureArray)));
    sourceWriter.println(", _arguments);");

    // add recorded call
    sourceWriter.print("addCall(_call, ");
    sourceWriter.print(returnType.getQualifiedSourceName());
    sourceWriter.println(".class);");

    // generate dummy return statement
    JPrimitiveType primitiveReturnType = returnType.isPrimitive();
    if (primitiveReturnType == null) {
        sourceWriter.println("return null;");
    } else {
        switch (primitiveReturnType) {
        case VOID:
            // nothing to return
            break;
        case BOOLEAN:
            sourceWriter.println("return false;");
            break;
        case BYTE:
        case DOUBLE:
        case FLOAT:
        case INT:
        case LONG:
        case SHORT:
            sourceWriter.println("return 0;");
            break;
        case CHAR:
            sourceWriter.println("return ' ';");
            break;
        default:
            throw new IllegalCaseException(JPrimitiveType.class, primitiveReturnType);
        }
    }
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:net.sf.mmm.util.gwt.base.rebind.AbstractIncrementalGenerator.java

License:Apache License

/**
 * This method generates the source code for a public method or constructor including the opening brace and
 * indentation./*from   w  ww .j  a  va  2  s . co  m*/
 * 
 * @param sourceWriter is the {@link SourceWriter}.
 * @param returnType is the return type of the method.
 * @param methodName is the name of the method (or the {@link Class#getSimpleName() simple class name} for a
 *        constructor}.
 * @param arguments is the source line with the arguments to the method or constructor.
 * @param override - <code>true</code> if an {@link Override} annotation shall be added.
 */
protected final void generateSourcePublicMethodDeclaration(SourceWriter sourceWriter, String returnType,
        String methodName, String arguments, boolean override) {

    if (override) {
        sourceWriter.println("@Override");
    }
    sourceWriter.print("public ");
    if (returnType != null) {
        sourceWriter.print(returnType);
        sourceWriter.print(" ");
    }
    sourceWriter.print(methodName);
    sourceWriter.print("(");
    sourceWriter.print(arguments);
    sourceWriter.println(") {");
    sourceWriter.indent();
}

From source file:net.sf.mmm.util.nls.impl.rebind.AbstractNlsBundleGenerator.java

License:Apache License

/**
 * Generates the implementation of {@link NlsBundleWithLookup#getMessage(String, Map)}.
 * // w  w  w  . j av  a2 s  .  co  m
 * @param sourceWriter is the {@link SourceWriter}.
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 * @param method is the {@link NlsBundleWithLookup#getMessage(String, Map)}-method to generate an
 *        implementation for.
 * @param methods is the list of all declared methods of the bundle. Likely to be ignored but may be used to
 *        generate a switch statement with delegations.
 */
protected void generateLookupMethod(SourceWriter sourceWriter, TreeLogger logger, GeneratorContext context,
        JMethod method, JMethod[] methods) {

    generateSourcePublicMethodDeclaration(sourceWriter, method);

    sourceWriter.print("String ");
    sourceWriter.print(VARIABLE_MESSAGE);
    sourceWriter.println("= null;");
    boolean addElse = false;
    for (JMethod currentMethod : methods) {
        if (!isLookupMethod(currentMethod)) {
            if (addElse) {
                sourceWriter.print(" else ");
            } else {
                addElse = true;
            }
            sourceWriter.print("if (methodName.equals(\"");
            String methodName = currentMethod.getName();
            sourceWriter.print(methodName);
            sourceWriter.println("\")) {");
            sourceWriter.indent();
            generateMethodMessageBlock(sourceWriter, logger, context, methodName);
            sourceWriter.outdent();
            sourceWriter.print("}");
        } else {
            assert (method == currentMethod);
        }
    }
    // methodName not found/present?
    sourceWriter.println();
    sourceWriter.print("if (");
    sourceWriter.print(VARIABLE_MESSAGE);
    sourceWriter.println("== null) {");
    sourceWriter.indent();
    sourceWriter.println("return null;");
    sourceWriter.outdent();
    sourceWriter.println("}");

    // generate message depending on arguments
    sourceWriter.print("if ((");
    sourceWriter.print(VARIABLE_ARGUMENTS);
    sourceWriter.print(" == null) || (");
    sourceWriter.print(VARIABLE_ARGUMENTS);
    sourceWriter.println(".isEmpty())) {");
    sourceWriter.indent();
    generateCreateMessageBlock(sourceWriter, false);
    sourceWriter.outdent();
    sourceWriter.println("} else {");
    sourceWriter.indent();
    generateCreateMessageBlock(sourceWriter, true);
    sourceWriter.outdent();
    sourceWriter.println("}");

    generateSourceCloseBlock(sourceWriter);
}