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:com.gwtent.gen.LogableSourceCreator.java

License:Apache License

public String generate() throws UnableToCompleteException {
    JClassType classType;//  www  .j av  a2s  .  c  o m
    try {
        logger.log(Type.DEBUG, "Start generate UNIT for " + typeName + " in " + this.getClass().getName());
        Calendar start = Calendar.getInstance();

        classType = typeOracle.getType(typeName);
        if (genExclusion(classType)) {
            return null;
        }

        SourceWriter source = getSourceWriter(classType, isUseLog(), 6);

        if ((source != null)) {
            source.indent();
            createSource(source, classType);
            source.outdent();
            source.commit(logger);
        }

        logger.log(Type.DEBUG, "Code commited, Unit name: " + getUnitName(classType) + " Time:"
                + (start.getTimeInMillis() - Calendar.getInstance().getTimeInMillis()));

        return getUnitName(classType);
    } catch (Throwable e) {
        this.logger.log(Type.ERROR, e.getMessage(), e);
        throw new UnableToCompleteException();
    }
}

From source file:com.gwtplatform.dispatch.rebind.VelocityGenerator.java

License:Apache License

@Override
public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String typeName)
        throws UnableToCompleteException {
    logger = new Logger(treeLogger);
    typeOracle = generatorContext.getTypeOracle();
    type = getType(typeName);/*from   w w w  .ja va2  s . c o  m*/

    PrintWriter printWriter = tryCreatePrintWriter(generatorContext);
    if (printWriter == null) {
        return typeName + SUFFIX;
    }

    injector = Guice.createInjector(new RebindModule(logger, generatorContext));
    serializerProviderGenerator = injector.getInstance(SerializerProviderGenerator.class);
    generatorFactory = injector.getInstance(GeneratorFactory.class);

    registerPrimitiveTypes();
    generateRestServices();
    generateRestGinModule();
    generateSerializerProvider();

    ClassSourceFileComposerFactory composer = initComposer();
    SourceWriter sourceWriter = composer.createSourceWriter(generatorContext, printWriter);
    sourceWriter.indent();
    sourceWriter.println("@Override");
    sourceWriter.println("public void onModuleLoad() {}");
    sourceWriter.outdent();
    sourceWriter.commit(treeLogger);

    return typeName + SUFFIX;
}

From source file:com.gwtplatform.dispatch.rest.delegates.rebind.fallback.ResourceDelegateFallbackGenerator.java

License:Apache License

private void compose(JClassType type, JClassType resourceType, String packageName, String className,
        PrintWriter printWriter) {
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImplementedInterface(type.getName());
    composer.setSuperclass(resourceType.getQualifiedSourceName() + "Delegate");

    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
    sourceWriter.commit(logger);
}

From source file:com.gwtplatform.dispatch.rest.rebind.entrypoint.DefaultEntryPointGenerator.java

License:Apache License

private void generateFile() {
    ClassSourceFileComposerFactory composer = initComposer();
    SourceWriter sourceWriter = composer.createSourceWriter(getContext(), printWriter);
    sourceWriter.commit(getLogger());
}

From source file:com.gwtplatform.dispatch.rest.rebind.VelocityGenerator.java

License:Apache License

private void generateEntryPoint(TreeLogger treeLogger, GeneratorContext generatorContext,
        PrintWriter printWriter) {
    ClassSourceFileComposerFactory composer = initComposer();
    SourceWriter sourceWriter = composer.createSourceWriter(generatorContext, printWriter);
    sourceWriter.commit(treeLogger);
}

From source file:com.gwtplatform.mvp.rebind.AbstractGenerator.java

License:Apache License

protected void closeDefinition(SourceWriter sourceWriter) {
    sourceWriter.commit(getTreeLogger());
}

From source file:com.gwtplatform.mvp.rebind.ProxyGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext ctx, String requestedClass)
        throws UnableToCompleteException {
    // Initialize dependencies
    TypeOracle oracle = ctx.getTypeOracle();
    classCollection = new ClassCollection(oracle);
    ginjectorInspector = new GinjectorInspector(classCollection, ctx, logger);
    presenterInspector = new PresenterInspector(oracle, logger, classCollection, ginjectorInspector);
    proxyOutputterFactory = new ProxyOutputterFactory(oracle, logger, classCollection, ginjectorInspector,
            presenterInspector);/*from   w  ww  . j  av  a2 s.c  o m*/

    // Find the requested class
    JClassType proxyInterface = oracle.findType(requestedClass);

    if (proxyInterface == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + requestedClass + "'", null);

        throw new UnableToCompleteException();
    }

    // If it's not an interface it's a custom user-made proxy class. Don't use generator.
    if (proxyInterface.isInterface() == null) {
        return null;
    }

    ginjectorInspector.init();
    if (!presenterInspector.init(proxyInterface)) {
        return null;
    }

    // Find the package, build the generated class name.
    JPackage interfacePackage = proxyInterface.getPackage();
    String packageName = interfacePackage == null ? "" : interfacePackage.getName();
    String implClassName = presenterInspector.getPresenterClassName() + proxyInterface.getSimpleSourceName()
            + "Impl";
    String generatedClassName = packageName + "." + implClassName;

    // Create the printWriter
    PrintWriter printWriter = ctx.tryCreate(logger, packageName, implClassName);
    if (printWriter == null) {
        // We've already created it, so nothing to do
        return generatedClassName;
    }

    try {
        ProxyOutputter proxyOutputter = proxyOutputterFactory.create(proxyInterface);

        // Start composing the class
        ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
                implClassName);
        proxyOutputter.initComposerFactory(composerFactory);

        // Get a source writer
        SourceWriter writer = composerFactory.createSourceWriter(ctx, printWriter);

        proxyOutputter.writeFields(writer);
        proxyOutputter.writeInnerClasses(writer);
        proxyOutputter.writeConstructor(writer, implClassName, true);
        proxyOutputter.writeMethods(writer);

        writer.commit(logger);

        return generatedClassName;
    } finally {
        printWriter.close();
    }
}

From source file:com.intrinsarc.backbone.gwt.generators.StateDispatcherGenerator.java

License:Apache License

public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    try {//from  ww w. j  a va 2  s  .c  o m
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType classType = typeOracle.getType(typeName);
        JParameterizedType parameterizedType = classType.getSuperclass().isParameterized();
        JClassType T = parameterizedType.getTypeArgs()[0];
        logger.log(TreeLogger.DEBUG, "Parameterised type = " + T.getName());

        String packageName = classType.getPackage().getName();
        String proxyClassName = classType.getSimpleSourceName() + "Dispatcher";
        String qualifiedProxyClassName = packageName + "." + proxyClassName;

        SourceWriter writer = getSourceWriter(logger, context, packageName, proxyClassName);
        if (writer != null) {
            writeToStringMethod(logger, proxyClassName, T, writer);
            writer.commit(logger);
        }
        return qualifiedProxyClassName;
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "Class '" + typeName + "' Not Found", e);
        throw new UnableToCompleteException();
    }
}

From source file:com.intrinsarc.backbone.gwt.generators.TerminalGenerator.java

License:Apache License

public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    try {// w w  w  . j a v  a2s .  c om
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType classType = typeOracle.getType(typeName);
        JParameterizedType parameterizedType = classType.getSuperclass().isParameterized();
        JClassType T = parameterizedType.getTypeArgs()[0];
        String packageName = classType.getPackage().getName();
        String proxyClassName = classType.getSimpleSourceName() + "Dispatcher";
        String qualifiedProxyClassName = packageName + "." + proxyClassName;

        SourceWriter writer = getSourceWriter(logger, context, packageName, proxyClassName);
        if (writer != null) {
            writeToStringMethod(logger, proxyClassName, T, writer);
            writer.commit(logger);
        }
        return qualifiedProxyClassName;
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "Class '" + typeName + "' Not Found", e);
        throw new UnableToCompleteException();
    }
}

From source file:com.msco.mil.server.com.sencha.gxt.explorer.rebind.SampleGenerator.java

License:sencha.com license

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    // Get access to metadata about the type to be generated
    TypeOracle oracle = context.getTypeOracle();
    JClassType toGenerate = oracle.findType(typeName).isClass();

    // Get the name of the new type
    String packageName = toGenerate.getPackage().getName();
    String simpleSourceName = toGenerate.getName().replace('.', '_') + "Impl";
    PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName);
    if (pw == null) {
        return packageName + "." + simpleSourceName;
    }/*from   w w  w .ja  v a2s.  c  o m*/

    // Generate an HTML file resource for every example and write the source
    JClassType[] types = oracle.getTypes();

    // Build a ResourceOracle capable of reading java files
    sourceOracle = new ResourceOracleImpl(logger.branch(Type.DEBUG, "Gathering sources"));

    // Clean up these prefixes to not have filters
    PathPrefixSet prefixes = ((ResourceOracleImpl) context.getResourcesOracle()).getPathPrefixes();
    sourceOracle.setPathPrefixes(new PathPrefixSet());
    for (PathPrefix p : prefixes.values()) {
        sourceOracle.getPathPrefixes().add(new PathPrefix(p.getPrefix(), null));
    }
    ResourceOracleImpl.refresh(logger, sourceOracle);

    // Load the header and footer HTML content
    try {
        String slashyPackageName = getClass().getPackage().getName().replace('.', '/');
        javaHeader = Utility.getFileFromClassPath(slashyPackageName + "/header.html");
        footer = Utility.getFileFromClassPath(slashyPackageName + "/footer.html");
    } catch (IOException e) {
        logger.log(Type.ERROR, "Header or Footer failed to be read", e);
        throw new UnableToCompleteException();
    }

    // Find all examples, annotated with @Detail
    Set<ExampleDetailModel> examples = new HashSet<ExampleDetailModel>();
    Map<String, List<ExampleDetailModel>> hierarchy = new HashMap<String, List<ExampleDetailModel>>();

    Set<SourceModel> exampleSources = new HashSet<SourceModel>();
    for (JClassType type : types) {
        Example.Detail detail = type.getAnnotation(Example.Detail.class);
        if (detail != null) {
            ExampleDetailModel example = new ExampleDetailModel(logger, context, type, detail);

            // Collect sources to be built into html
            exampleSources.addAll(example.getAllSources());

            List<ExampleDetailModel> exampleList = hierarchy.get(detail.category());
            if (exampleList == null) {
                exampleList = new ArrayList<ExampleDetailModel>();
                hierarchy.put(detail.category(), exampleList);
            }
            examples.add(example);
            exampleList.add(example);
        }
    }

    // Sort folders, sort within those folders
    List<String> folders = new ArrayList<String>(hierarchy.keySet());
    Collections.sort(folders);
    for (List<ExampleDetailModel> contents : hierarchy.values()) {
        Collections.sort(contents);
    }

    // Actually build source for each type
    for (SourceModel type : exampleSources) {
        TreeLogger l = logger.branch(Type.DEBUG, "Writing HTML file for " + type.getName());

        // attempt to create the output file
        if (type.getType() == FileType.JAVA) {
            writeTypeToHtml(l, context, type.getJClassType());
        } else {
            writeFileToHtml(l, context, type.getPath());
        }
    }

    // Start making the class, with basic imports
    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
    factory.setSuperclass(typeName);
    factory.addImport(Name.getSourceNameForClass(Category.class));
    factory.addImport(Name.getSourceNameForClass(ImageResource.class));
    factory.addImport(Name.getSourceNameForClass(GWT.class));
    factory.addImport(Name.getSourceNameForClass(Example.class));
    factory.addImport(Name.getSourceNameForClass(Source.class));
    factory.addImport(Name.getSourceNameForClass(Source.FileType.class));
    SourceWriter sw = factory.createSourceWriter(context, pw);

    // Write the ctor
    sw.println("public %1$s() {", simpleSourceName);
    sw.indent();
    // Declare variables that will be used
    sw.println("Category c;");
    sw.println("ImageResource icon;");
    sw.println("Example e;");
    sw.println("Source dir;");
    Set<String> names = new HashSet<String>();
    Map<JClassType, String> bundles = new HashMap<JClassType, String>();
    for (String folder : folders) {
        // TODO escape name
        sw.println("c = new Category(\"%1$s\");", folder);
        for (ExampleDetailModel example : hierarchy.get(folder)) {
            // make sure the bundle to be used exists
            if (!bundles.containsKey(example.getClientBundleType())) {
                String bundleName = getNextName("bundle", names);
                sw.println("%1$s %2$s = GWT.create(%1$s.class);",
                        example.getClientBundleType().getQualifiedSourceName(), bundleName);

                bundles.put(example.getClientBundleType(), bundleName);
            }

            // write out the example, adding it to the current category
            writeExample(sw, bundles.get(example.getClientBundleType()), example);
        }
        sw.println("categories.add(c);");
    }
    sw.outdent();
    sw.println("}");// end ctor

    sw.commit(logger);

    return factory.getCreatedClassName();
}