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

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

Introduction

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

Prototype

void commit(TreeLogger logger);

Source Link

Usage

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

License:Open Source License

public String create(TreeLogger logger, GeneratorContext context) {
    PrintWriter printWriter = this.getPrintWriter(logger, context);
    if (printWriter == null) {
        return this.proxyModelQualifiedName;
    }//from  ww w . j  a v  a  2s.co  m

    this.collectImports();

    SourceWriter srcWriter = this.getSourceWriter(printWriter, context);

    srcWriter.indent();
    srcWriter.println();
    this.generateCreateService(srcWriter);
    srcWriter.println();
    this.generateServiceImplementation(logger, srcWriter);
    this.generateServiceImplementationWithCallback(srcWriter);

    srcWriter.outdent();
    srcWriter.commit(logger);
    return this.proxyModelQualifiedName;
}

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);/*from   w  w  w .  jav  a 2s  .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);//from   www .ja v  a 2 s.  co  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:io.reinert.requestor.rebind.JsonAutoBeanGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext ctx, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = ctx.getTypeOracle();
    assert typeOracle != null;

    JClassType intfType = typeOracle.findType(typeName);
    if (intfType == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }// w  w w.  ja  v  a 2  s  .c om

    if (intfType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, intfType.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }

    // TODO: check if type was already generated and reuse it
    TreeLogger typeLogger = logger.branch(TreeLogger.ALL, "Generating Json SerDes powered by AutoBeans...",
            null);
    final SourceWriter sourceWriter = getSourceWriter(typeLogger, ctx, intfType);

    if (sourceWriter != null) {
        sourceWriter.println();

        ArrayDeque<JClassType> annotatedTypes = new ArrayDeque<JClassType>();
        ArrayList<Json> jsonAnnotations = new ArrayList<Json>();

        final ArrayDeque<String> allTypesAndWrappers = new ArrayDeque<String>();

        for (JClassType type : typeOracle.getTypes()) {
            Json annotation = type.getAnnotation(Json.class);
            if (annotation != null && type.isInterface() != null) {
                annotatedTypes.add(type);
                jsonAnnotations.add(annotation);

                final String listWrapperTypeName = generateListWrapperInterface(sourceWriter, type);
                sourceWriter.println();

                final String setWrapperTypeName = generateSetWrapperInterface(sourceWriter, type);
                sourceWriter.println();

                // Add current type name for single de/serialization
                allTypesAndWrappers.add(type.getQualifiedSourceName());
                allTypesAndWrappers.add(listWrapperTypeName);
                allTypesAndWrappers.add(setWrapperTypeName);
            }
        }

        final ArrayDeque<String> serdesFields = new ArrayDeque<String>();
        final ArrayDeque<String> providerFields = new ArrayDeque<String>();

        if (!allTypesAndWrappers.isEmpty()) {
            generateFactoryInterface(sourceWriter, allTypesAndWrappers);
            sourceWriter.println();

            generateFactoryField(sourceWriter);
            sourceWriter.println();

            int i = 0;
            for (JClassType annotatedType : annotatedTypes) {
                final String providerFieldName = generateProviderField(sourceWriter, annotatedType);
                providerFields.add(providerFieldName);

                final String serdesFieldName = generateSerdesClassAndField(logger, typeOracle, sourceWriter,
                        annotatedType, jsonAnnotations.get(i++));
                serdesFields.add(serdesFieldName);
            }
        }

        generateFields(sourceWriter);
        generateConstructor(sourceWriter, serdesFields, providerFields);
        generateMethods(sourceWriter);

        sourceWriter.commit(typeLogger);
    }

    return typeName + "Impl";
}

From source file:io.reinert.requestor.rebind.JsonGwtJacksonGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext ctx, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = ctx.getTypeOracle();
    assert typeOracle != null;

    JClassType intfType = typeOracle.findType(typeName);
    if (intfType == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }/*ww w .j  a va  2s. c om*/

    if (intfType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, intfType.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }

    // TODO: check if type was already generated and reuse it
    TreeLogger typeLogger = logger.branch(TreeLogger.ALL, "Generating Json SerDes powered by Gwt Jackson...",
            null);
    final SourceWriter sourceWriter = getSourceWriter(typeLogger, ctx, intfType);

    if (sourceWriter != null) {
        sourceWriter.println();

        final ArrayList<String> serdes = new ArrayList<String>();
        for (JClassType type : typeOracle.getTypes()) {
            Json annotation = type.getAnnotation(Json.class);
            if (annotation != null) {
                serdes.add(generateSerdes(sourceWriter, type, annotation));
            }
        }

        generateFields(sourceWriter);
        generateConstructor(sourceWriter, serdes);
        generateMethods(sourceWriter);

        sourceWriter.commit(typeLogger);
    }

    return typeName + "Impl";
}

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
    }//from ww  w  .ja va2s.  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.officefloor.plugin.gwt.comet.generate.CometAdapterMapGenerator.java

License:Open Source License

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

    // 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();
    }//from w ww  .j  av a  2  s  . c om

    // Obtain details to generate adapter class
    String packageName = type.getPackage().getName();
    String simpleName = type.getSimpleSourceName() + "Impl";
    String qualifiedName = packageName + "." + simpleName;
    logger.log(Type.TRACE, "Generating " + CometAdapterMap.class.getSimpleName() + " implementation");

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

    // Generate the map
    ClassSourceFileComposerFactory adapter = new ClassSourceFileComposerFactory(packageName, simpleName);
    adapter.addImplementedInterface(CometAdapterMap.class.getName());
    adapter.addImport(CometAdapterMap.class.getName());
    adapter.addImport(CometAdapter.class.getName());
    adapter.addImport(Map.class.getName());
    adapter.addImport(HashMap.class.getName());
    adapter.addImport(GWT.class.getName());
    PrintWriter src = context.tryCreate(logger, packageName, simpleName);
    if (src == null) {
        logger.log(Type.WARN, qualifiedName + " for " + CometAdapterMap.class.getSimpleName()
                + " implementation already generated.");
        return qualifiedName; // should already exist
    }
    SourceWriter writer = adapter.createSourceWriter(context, src);
    writer.println("@Override");
    writer.println("public Map<Class<?>, " + CometAdapter.class.getSimpleName() + "> getMap() {");
    writer.println("    Map<Class<?>, " + CometAdapter.class.getSimpleName() + "> map = new HashMap<Class<?>, "
            + CometAdapter.class.getSimpleName() + ">();");

    // Add all CometListener types to the map
    for (JClassType check : oracle.getTypes()) {

        // Determine if a CometListener type
        if (!(check.getFlattenedSupertypeHierarchy().contains(cometListenerType))) {
            logger.log(Type.DEBUG,
                    "Type " + check.getQualifiedSourceName() + " not included in map for " + simpleName);
            continue; // ignore non CometListener type
        }

        // Ensure not the actual CometListener type
        if (cometListenerType.getQualifiedSourceName().equals(check.getQualifiedSourceName())) {
            logger.log(Type.DEBUG,
                    "Not including " + check.getQualifiedSourceName() + " in map for " + simpleName);
            continue; // ignore the CometListener
        }

        // Add to map
        String serviceTypeName = check.getQualifiedSourceName();
        logger.log(Type.TRACE, "Including " + serviceTypeName + " in map for " + simpleName);
        writer.println("    map.put(" + serviceTypeName + ".class, (" + CometAdapter.class.getSimpleName()
                + ") GWT.create(" + serviceTypeName + ".class));");
    }

    writer.println("    return map;");
    writer.println("}");

    // Committing
    logger.log(Type.TRACE,
            "Committing " + CometAdapterMap.class.getSimpleName() + " implementation to have type created");
    writer.commit(logger);
    logger.log(Type.TRACE, CometAdapterMap.class.getSimpleName() + " created");

    // Return adapter
    return qualifiedName;
}

From source file:net.sf.gilead.proxy.gwt.AbstractGwtProxyGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    try {//  w w w  .j a v a 2  s . c o m
        // Get target class
        //
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType requestedClass = typeOracle.getType(typeName);

        // Do not generate proxy for a class already implementing
        // ILightEntity interface
        if (isLazyPojo(requestedClass) == true) {
            // LOGGER is not compatible from GWT 1.4 to GWT 1.5 !
            // logger.log(TreeLogger.INFO,
            // requestedClass.getClass().getName() +
            // " is already a lazy pojo : proxy not needed.",
            // null);
            return null;
        }

        // Read additional data information
        //
        if (_additionalCode == null) {
            _additionalCode = AdditionalCodeReader.readFromFile(_additionalCodePath);
        }

        // Compute proxy information
        //
        String packageName = requestedClass.getPackage().getName();
        String proxyClassName = requestedClass.getSimpleSourceName() + _additionalCode.getSuffix();
        String qualifiedProxyClassName = packageName + "." + proxyClassName;
        String className = requestedClass.getName();

        // LOGGER is not compatible from GWT 1.4 to GWT 1.5 !
        // logger.log(TreeLogger.INFO,
        // "Generating proxy " + qualifiedProxyClassName +
        // " for class " + className,
        // null);

        // Create source writer
        //
        SourceWriter sourceWriter = getSourceWriter(logger, context, packageName, proxyClassName, className,
                _additionalCode);
        if (sourceWriter != null) {
            generateProxy(logger, sourceWriter, _additionalCode);
            sourceWriter.commit(logger);

            // LOGGER is not compatible from GWT 1.4 to GWT 1.5 !
            // logger.log(TreeLogger.INFO, "Proxy generation OK", null);
        }
        return qualifiedProxyClassName;
    } catch (Exception ex) {
        // LOGGER is not compatible from GWT 1.4 to GWT 1.5 !
        // logger.log(TreeLogger.ERROR, "Proxy generation error", ex);

        throw new UnableToCompleteException();
    }
}

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  .c  o 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.util.gwt.base.rebind.AbstractIncrementalGenerator.java

License:Apache License

/**
 * This method performs the actual generation of the {@link Class} to rebind.
 * /* w ww . j av a  2  s . c o m*/
 * @param inputType is the {@link JClassType} reflecting the input-type that triggered the generation via
 *        {@link com.google.gwt.core.client.GWT#create(Class)}.
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 * @return the fully qualified name of the generated class.
 */
protected String generate(JClassType inputType, TreeLogger logger, GeneratorContext context) {

    String packageName = inputType.getPackage().getName();
    String simpleName = createClassName(inputType);
    logger.log(TreeLogger.DEBUG, getClass().getSimpleName() + ": Generating " + simpleName);
    ClassSourceFileComposerFactory sourceComposerFactory = new ClassSourceFileComposerFactory(packageName,
            simpleName);
    // Import statements
    generateImportStatements(inputType, logger, sourceComposerFactory, context);

    // Class declaration
    generateClassDeclaration(inputType, logger, sourceComposerFactory, context);

    PrintWriter writer = context.tryCreate(logger, packageName, simpleName);
    if (writer != null) {
        SourceWriter sourceWriter = sourceComposerFactory.createSourceWriter(context, writer);

        generateClassContents(inputType, logger, sourceWriter, simpleName, context);
        sourceWriter.commit(logger);
    }
    return sourceComposerFactory.getCreatedClassName();
}