Search in sources :

Example 6 with SourceWriter

use of com.google.gwt.user.rebind.SourceWriter in project gwt-test-utils by gwt-test-utils.

the class MyGenerator method generate.

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
    String packageName = "com.slazzer";
    String className = "MyGeneratedClass";
    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, className);
    PrintWriter pw = context.tryCreate(logger, packageName, className);
    if (pw != null) {
        factory.addImplementedInterface(IGenerateWith.class.getCanonicalName());
        factory.addImport(GWT.class.getCanonicalName());
        factory.addImport(JavaScriptObject.class.getCanonicalName());
        factory.addImport(HashMap.class.getCanonicalName());
        SourceWriter writer = factory.createSourceWriter(context, pw);
        writer.println("public String getMessage() {");
        writer.println("   return \"generated with MyGenerator class\";");
        writer.println("}");
        writer.commit(logger);
    }
    return factory.getCreatedClassName();
}
Also used : ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) GWT(com.google.gwt.core.client.GWT) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) HashMap(java.util.HashMap) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Example 7 with SourceWriter

use of com.google.gwt.user.rebind.SourceWriter in project libgdx by libgdx.

the class PreloaderBundleGenerator method createDummyClass.

private String createDummyClass(TreeLogger logger, GeneratorContext context) {
    String packageName = "com.badlogic.gdx.backends.gwt.preloader";
    String className = "PreloaderBundleImpl";
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImplementedInterface(packageName + ".PreloaderBundle");
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);
    if (printWriter == null) {
        return packageName + "." + className;
    }
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
    sourceWriter.commit(logger);
    return packageName + "." + className;
}
Also used : ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Example 8 with SourceWriter

use of com.google.gwt.user.rebind.SourceWriter in project gwtphonegap by dankurka.

the class PhoneGapLogValueGenerator method generate.

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
    PropertyOracle propertyOracle = context.getPropertyOracle();
    ConfigurationProperty property = null;
    int value = 100;
    try {
        property = propertyOracle.getConfigurationProperty("phonegap.logging.maxentries");
        List<String> values = property.getValues();
        if (values.size() < 1) {
            logger.log(TreeLogger.WARN, "can not resolve phonegap.logging.maxentries variable - defaulting to 100");
        } else {
            String stringValue = values.get(0);
            try {
                value = Integer.parseInt(stringValue);
            } catch (Exception e) {
                logger.log(TreeLogger.WARN, "can not prase phonegap.logging.maxentries variable - value: '" + stringValue + "' - defaulting to 100");
            }
        }
    } catch (BadPropertyValueException e) {
        logger.log(TreeLogger.WARN, "can not resolve phonegap.logging.maxentries variable - defaulting to 100", e);
    }
    JClassType classType = null;
    try {
        classType = context.getTypeOracle().getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "can not find type: '" + typeName + "'", e);
        throw new UnableToCompleteException();
    }
    String packageName = classType.getPackage().getName();
    String simpleName = classType.getSimpleSourceName() + "_" + value;
    String fullName = packageName + "." + simpleName;
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);
    composer.addImplementedInterface(typeName);
    composer.addImport(typeName);
    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    if (printWriter == null) {
        return fullName;
    }
    SourceWriter writer = composer.createSourceWriter(context, printWriter);
    writer.println("public int getMaxEntries() {");
    writer.println("return " + value + ";");
    writer.println("}");
    writer.commit(logger);
    return fullName;
}
Also used : ConfigurationProperty(com.google.gwt.core.ext.ConfigurationProperty) ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) NotFoundException(com.google.gwt.core.ext.typeinfo.NotFoundException) SourceWriter(com.google.gwt.user.rebind.SourceWriter) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) BadPropertyValueException(com.google.gwt.core.ext.BadPropertyValueException) NotFoundException(com.google.gwt.core.ext.typeinfo.NotFoundException) JClassType(com.google.gwt.core.ext.typeinfo.JClassType) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) BadPropertyValueException(com.google.gwt.core.ext.BadPropertyValueException) PropertyOracle(com.google.gwt.core.ext.PropertyOracle) PrintWriter(java.io.PrintWriter)

Example 9 with SourceWriter

use of com.google.gwt.user.rebind.SourceWriter in project rstudio by rstudio.

the class StaticDataResourceGenerator method createAssignment.

@Override
public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException {
    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();
    }
    URL resource = resources[0];
    String outputUrlExpression = context.deploy(resource, null, true);
    SourceWriter sw = new StringSourceWriter();
    // Write the expression to create the subtype.
    sw.println("new " + StaticDataResource.class.getName() + "() {");
    sw.indent();
    // Convenience when examining the generated code.
    sw.println("// " + resource.toExternalForm());
    sw.println("public String getUrl() {");
    sw.indent();
    sw.println("return " + outputUrlExpression + ";");
    sw.outdent();
    sw.println("}");
    sw.println("public com.google.gwt.safehtml.shared.SafeUri getSafeUri() {");
    sw.indent();
    sw.println("return new org.rstudio.core.client.SafeUriStringImpl(" + outputUrlExpression + ");");
    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();
}
Also used : UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) StaticDataResource(org.rstudio.core.client.resources.StaticDataResource) StringSourceWriter(com.google.gwt.user.rebind.StringSourceWriter) SourceWriter(com.google.gwt.user.rebind.SourceWriter) URL(java.net.URL) StringSourceWriter(com.google.gwt.user.rebind.StringSourceWriter)

Example 10 with SourceWriter

use of com.google.gwt.user.rebind.SourceWriter in project rstudio by rstudio.

the class ImageResourceInfo method generate.

/**
    * Generates the impl class and returns its name.
    */
public String generate() throws Exception {
    ImageResourceInfo images = generateImageBundle();
    simpleName_ = bundleType_.getName().replace('.', '_') + "__Impl";
    PrintWriter printWriter = context_.tryCreate(logger_, packageName_, simpleName_);
    if (printWriter != null) {
        // always null only when printWriter is also null.
        assert images != null;
        ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName_, simpleName_);
        factory.setSuperclass(bundleType_.getName());
        factory.addImport("org.rstudio.core.client.command.AppCommand");
        factory.addImport("org.rstudio.core.client.command.MenuCallback");
        factory.addImport("org.rstudio.core.client.command.ShortcutManager");
        factory.addImport("org.rstudio.core.client.resources.ImageResource2x");
        SourceWriter writer = factory.createSourceWriter(context_, printWriter);
        emitConstructor(writer, images);
        emitCommandFields(writer);
        emitMenus(writer);
        emitShortcuts(writer);
        emitCommandAccessors(writer);
        // Close the class and commit it
        writer.outdent();
        writer.println("}");
        context_.commit(logger_, printWriter);
    }
    return packageName_ + "." + simpleName_;
}
Also used : ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Aggregations

SourceWriter (com.google.gwt.user.rebind.SourceWriter)12 ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)11 PrintWriter (java.io.PrintWriter)11 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)4 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)4 GWT (com.google.gwt.core.client.GWT)3 TypeOracle (com.google.gwt.core.ext.typeinfo.TypeOracle)3 NotFoundException (com.google.gwt.core.ext.typeinfo.NotFoundException)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 BadPropertyValueException (com.google.gwt.core.ext.BadPropertyValueException)1 ConfigurationProperty (com.google.gwt.core.ext.ConfigurationProperty)1 PropertyOracle (com.google.gwt.core.ext.PropertyOracle)1 JMethod (com.google.gwt.core.ext.typeinfo.JMethod)1 Resource (com.google.gwt.dev.resource.Resource)1 ClientBundleWithLookup (com.google.gwt.resources.client.ClientBundleWithLookup)1 DataResource (com.google.gwt.resources.client.DataResource)1 ImageResource (com.google.gwt.resources.client.ImageResource)1 ResourcePrototype (com.google.gwt.resources.client.ResourcePrototype)1 TextResource (com.google.gwt.resources.client.TextResource)1 StringSourceWriter (com.google.gwt.user.rebind.StringSourceWriter)1