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.ait.toolkit.rebind.TemplateGenerator.java

License:Open Source License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    this.templatesInterface = oracle.findType(Name.getSourceNameForClass(Template.class));

    JClassType interfaceType;//  ww w . j  a  v a  2 s.  c  o m
    try {
        interfaceType = oracle.getType(typeName);
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }

    if (interfaceType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, typeName + " is not an interface type");
        throw new UnableToCompleteException();
    }
    if (!interfaceType.isAssignableTo(templatesInterface)) {
        logger.log(Type.ERROR, "This isn't a Template subtype...");
        throw new UnableToCompleteException();
    }

    String content = getTemplateContent(context, logger, interfaceType);
    String packageName = interfaceType.getPackage().getName();
    String className = "Tpl_For_" + interfaceType.getQualifiedSourceName().replace(".", "_") + "_Generated";

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImport(SafeHtml.class.getName());
    composer.addImport(SafeHtmlUtils.class.getName());
    composer.addImplementedInterface(Template.class.getName());

    PrintWriter pw = context.tryCreate(logger, packageName, className);

    if (pw != null) {
        SourceWriter sw = composer.createSourceWriter(context, pw);

        sw.println("  public SafeHtml getContent(){");
        sw.println("      return SafeHtmlUtils.fromSafeConstant(\"" + content + "\");");
        sw.println("  }");
        sw.println("");
        sw.println("");
        sw.println("  public SafeHtml getSafeContent(){");
        sw.println("      return SafeHtmlUtils.fromString(\"" + content + "\");");
        sw.println("  }");

        sw.commit(logger);
    }

    return composer.getCreatedClassName();

}

From source file:com.allen_sauer.gwt.log.rebind.LogMessageFormatterGenerator.java

License:Apache License

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

    JClassType userType;/*from w ww .  j  av a2s  .c  om*/
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    composerFactory.addImport(Date.class.getName());
    composerFactory.addImport(GWT.class.getName());
    composerFactory.addImport(LogUtil.class.getName());
    composerFactory.addImport(Duration.class.getName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String logPattern;
        try {
            ConfigurationProperty logPatternProperty = propertyOracle
                    .getConfigurationProperty(PROPERTY_LOG_PATTERN);
            List<String> values = logPatternProperty.getValues();
            logPattern = values.get(0);
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_LOG_PATTERN + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("private double BIG_BANG = Duration.currentTimeMillis();");

        sw.println();
        sw.println(
                "public String format(String logLevelText, String category, String message, Throwable throwable) {");
        sw.indent();
        sw.println("if (category == null) {");
        sw.indent();
        sw.println("category = \"<null category>\";");
        sw.outdent();
        sw.println("}");
        sw.println("if (message == null) {");
        sw.indent();
        sw.println("message = \"<null message>\";");
        sw.outdent();
        sw.println("}");
        sw.println(logPatternToCode(logPattern));
        sw.outdent();
        sw.println("}");

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.allen_sauer.gwt.log.rebind.RemoteLoggerConfigGenerator.java

License:Apache License

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

    JClassType userType;//from w w w  . ja  v a 2 s . co m
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String logUrl;
        try {
            ConfigurationProperty logPatternProperty = propertyOracle
                    .getConfigurationProperty(PROPERTY_LOG_URL);
            List<String> values = logPatternProperty.getValues();
            logUrl = values.get(0);
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_LOG_URL + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("public String serviceEntryPointUrl() {");
        sw.indent();

        if (logUrl == null) {
            sw.println("return null;");
        } else {
            sw.println("return \"" + logUrl.trim() + "\";");
        }

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

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.allen_sauer.gwt.voices.crowd.rebind.GwtUserAgentProviderGenerator.java

License:Apache License

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

    JClassType userType;// ww w  .  j a  va2  s. co m
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "OOPS", e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();

    JClassType remoteService = typeOracle.findType(typeName);
    if (remoteService == null) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type '" + typeName + "'", null);
        throw new UnableToCompleteException();
    }

    if (remoteService.isInterface() == null) {
        logger.log(TreeLogger.ERROR, remoteService.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName,
            className + "Impl");
    composerFactory.addImplementedInterface(remoteService.getQualifiedSourceName());

    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        PropertyOracle propertyOracle = context.getPropertyOracle();
        String userAgent;
        try {
            SelectionProperty userAgentProperty = propertyOracle.getSelectionProperty(logger,
                    PROPERTY_USER_AGENT);

            // ALWAYS RETURNS 'gecko'
            userAgent = userAgentProperty.getCurrentValue();
        } catch (BadPropertyValueException e) {
            logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_USER_AGENT + "'", e);
            throw new UnableToCompleteException();
        }

        sw.println();
        sw.println("public String getGwtUserAgent() {");
        sw.indent();

        if (userAgent == null) {
            sw.println("return null;");
        } else {
            sw.println("return \"" + userAgent + "\";");
        }

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

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.artemis.gwtref.gen.ReflectionCacheSourceCreator.java

License:Apache License

private void createProxy(JClassType type) {
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(type.getPackage().getName(),
            type.getSimpleSourceName() + "Proxy");
    PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
    if (printWriter == null) {
        return;//from   w  w w  .  java 2 s  .c o  m
    }
    SourceWriter writer = composer.createSourceWriter(context, printWriter);
    writer.commit(logger);
}

From source file:com.badlogic.gdx.backends.gwt.preloader.PreloaderBundleGenerator.java

License:Apache License

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;
    }//from w  w  w.ja  v  a2s. c  om
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
    sourceWriter.commit(logger);
    return packageName + "." + className;
}

From source file:com.cgxlib.xq.rebind.BrowserGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    PropertyOracle propOracle = context.getPropertyOracle();

    String ua = null;/* w w w. j  a  v  a 2 s .  c  o  m*/
    try {
        ua = propOracle.getSelectionProperty(logger, "user.agent").getCurrentValue();
    } catch (BadPropertyValueException e) {
        logger.log(TreeLogger.ERROR, "Can not resolve user.agent property", e);
        throw new UnableToCompleteException();
    }

    JClassType clz = oracle.findType(typeName);
    String pName = clz.getPackage().getName();
    String cName = clz.getName() + "_" + ua;

    PrintWriter pWriter = context.tryCreate(logger, pName, cName);

    if (pWriter != null) {
        ClassSourceFileComposerFactory cFact = new ClassSourceFileComposerFactory(pName, cName);
        cFact.setSuperclass(pName + "." + clz.getName());

        SourceWriter writer = cFact.createSourceWriter(context, pWriter);

        writer.println("protected boolean isWebkit() {return " + "safari".equals(ua) + ";}");
        writer.println("protected boolean isSafari() {return " + "safari".equals(ua) + ";}");
        writer.println("protected boolean isOpera() {return " + "opera".equals(ua) + ";}");
        writer.println("protected boolean isMozilla() {return " + ua.contains("gecko") + ";}");
        writer.println("protected boolean isMsie() {return " + ua.contains("ie") + ";}");
        writer.println("protected boolean isIe6() {return " + "ie6".equals(ua) + ";}");
        writer.println("protected boolean isIe8() {return " + "ie8".equals(ua) + ";}");
        writer.println("protected boolean isIe9() {return " + "ie9".equals(ua) + ";}");
        writer.println("protected boolean isIe10() {return " + "ie10".equals(ua) + ";}");
        writer.println("protected boolean isIe11() {return " + "gecko1_8".equals(ua) + ";}");
        writer.println("public String toString() {return \"Browser:\"" + " + \" webkit=\" + webkit"
                + " + \" mozilla=\" + mozilla" + " + \" opera=\" + opera" + " + \" msie=\" + msie"
                + " + \" ie6=\" + ie6" + " + \" ie8=\" + ie8" + " + \" ie9=\" + ie9" + ";}");
        writer.commit(logger);
    }

    return pName + "." + cName;
}

From source file:com.cgxlib.xq.rebind.JsniBundleGenerator.java

License:Apache License

public String generate(TreeLogger logger, GeneratorContext context, String requestedClass)
        throws UnableToCompleteException {

    TypeOracle oracle = context.getTypeOracle();
    JClassType clazz = oracle.findType(requestedClass);

    String packageName = clazz.getPackage().getName();
    String className = clazz.getName().replace('.', '_') + "_Impl";
    String fullName = packageName + "." + className;

    PrintWriter pw = context.tryCreate(logger, packageName, className);

    if (pw != null) {
        ClassSourceFileComposerFactory fact = new ClassSourceFileComposerFactory(packageName, className);
        if (clazz.isInterface() != null) {
            fact.addImplementedInterface(requestedClass);
        } else {//  w w w .  j av  a  2s .c o  m
            fact.setSuperclass(requestedClass);
        }

        SourceWriter sw = fact.createSourceWriter(context, pw);

        if (sw != null) {
            for (JMethod method : clazz.getMethods()) {
                LibrarySource librarySource = method.getAnnotation(LibrarySource.class);
                String value, prepend, postpend;
                String replace[];
                if (librarySource != null) {
                    value = librarySource.value();
                    prepend = librarySource.prepend();
                    postpend = librarySource.postpend();
                    replace = librarySource.replace();
                } else {
                    MethodSource methodSource = method.getAnnotation(MethodSource.class);
                    if (methodSource != null) {
                        value = methodSource.value();
                        prepend = methodSource.prepend();
                        postpend = methodSource.postpend();
                        replace = methodSource.replace();
                    } else {
                        continue;
                    }
                }
                try {
                    // Read the javascript content
                    String content = getContent(logger, packageName.replace(".", "/"), value);

                    // Adjust javascript so as we can introduce it in a JSNI comment block without
                    // breaking java syntax.
                    String jsni = parseJavascriptSource(content);

                    for (int i = 0; i < replace.length - 1; i += 2) {
                        jsni = jsni.replaceAll(replace[i], replace[i + 1]);
                    }

                    pw.println(method.toString().replace("abstract", "native") + "/*-{");
                    pw.println(prepend);
                    pw.println(jsni);
                    pw.println(postpend);
                    pw.println("}-*/;");
                } catch (Exception e) {
                    logger.log(TreeLogger.ERROR,
                            "Error parsing javascript source: " + value + " " + e.getMessage());
                    throw new UnableToCompleteException();
                }
            }
        }
        sw.commit(logger);
    }

    return fullName;
}

From source file:com.cgxlib.xq.rebind.JsonBuilderGenerator.java

License:Apache License

public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String requestedClass)
        throws UnableToCompleteException {

    oracle = generatorContext.getTypeOracle();
    JClassType clazz = oracle.findType(requestedClass);

    jsonBuilderType = oracle.findType(JsonBuilder.class.getName());
    settingsType = oracle.findType(IsProperties.class.getName());
    stringType = oracle.findType(String.class.getName());
    jsType = oracle.findType(JavaScriptObject.class.getName());
    listType = oracle.findType(List.class.getName());
    functionType = oracle.findType(Function.class.getName());
    jsonFactoryType = oracle.findType(JsonFactory.class.getName());

    String t[] = generateClassName(clazz);

    boolean isFactory = clazz.isAssignableTo(jsonFactoryType);

    SourceWriter sw = getSourceWriter(treeLogger, generatorContext, t[0], t[1], isFactory, requestedClass);
    if (sw != null) {
        if (isFactory) {
            generateCreateMethod(sw, treeLogger);
        } else {/*  w w w  .j a  v  a 2 s .  c o  m*/
            Set<String> attrs = new HashSet<String>();
            for (JMethod method : clazz.getInheritableMethods()) {
                String methName = method.getName();
                // skip method from JsonBuilder
                if (jsonBuilderType.findMethod(method.getName(), method.getParameterTypes()) != null
                        || settingsType.findMethod(method.getName(), method.getParameterTypes()) != null) {
                    continue;
                }

                Name nameAnnotation = method.getAnnotation(Name.class);
                String name = nameAnnotation != null ? nameAnnotation.value()
                        : methName.replaceFirst("^(get|set)", "");
                if (nameAnnotation == null) {
                    name = name.substring(0, 1).toLowerCase() + name.substring(1);
                }
                attrs.add(name);
                generateMethod(sw, method, name, treeLogger);
            }
            generateFieldNamesMethod(sw, attrs, treeLogger);
            generateToJsonMethod(sw, t[3], treeLogger);
        }
        sw.commit(treeLogger);
    }
    return t[2];
}

From source file:com.cgxlib.xq.rebind.LazyGenerator.java

License:Apache License

public String generate(TreeLogger treeLogger, GeneratorContext generatorContext, String requestedClass)
        throws UnableToCompleteException {
    TypeOracle oracle = generatorContext.getTypeOracle();
    lazyType = oracle.findType("com.cgxlib.xq.client.Lazy");
    lazyBaseType = oracle.findType("com.cgxlib.xq.client.LazyBase");

    assert lazyType != null : "Can't find Lazy interface type";
    assert lazyBaseType != null : "Can't find LazyBase interface type";

    JClassType requestedType = oracle.findType(requestedClass);
    JClassType targetType = null;//  w  w  w  .  java 2 s .  co m
    JClassType nonLazyType = null;

    for (JClassType inf : requestedType.getImplementedInterfaces()) {
        if (inf.isAssignableTo(lazyType)) {
            nonLazyType = inf.isParameterized().getTypeArgs()[0];
            targetType = inf.isParameterized().getTypeArgs()[1];
            break;
        }
    }

    if (targetType == null)
        return null;

    assert targetType != null : "Parameter of Lazy<T> not found";
    String genClass = targetType.getPackage().getName() + "." + targetType.getSimpleSourceName()
            + getImplSuffix();

    SourceWriter sw = getSourceWriter(treeLogger, generatorContext, requestedType.getPackage().getName(),
            targetType.getSimpleSourceName() + getImplSuffix(), targetType.getQualifiedSourceName());
    if (sw != null) {
        generatePreamble(sw, nonLazyType.getQualifiedSourceName(), treeLogger);
        sw.indent();
        for (JMethod method : targetType.getMethods()) {
            generateMethod(sw, method, nonLazyType.getQualifiedSourceName(), genClass, treeLogger);
        }
        sw.outdent();
        generateDoneMethod(sw, nonLazyType, treeLogger);
        sw.commit(treeLogger);
    }

    return genClass;
}