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

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

Introduction

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

Prototype

void indentln(String s, Object... args);

Source Link

Document

Emit a printf-style string.

Usage

From source file:com.colinalworth.gwt.websockets.rebind.ServerBuilderGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();
    JClassType toGenerate = oracle.findType(typeName).isInterface();

    if (toGenerate == null) {
        logger.log(Type.ERROR, "Error generating " + typeName
                + ", either not an interface, or cannot be reached from client code.");
        throw new UnableToCompleteException();
    }/*ww w  .j  a  v a 2  s  . com*/
    JClassType serverBuilderType = oracle.findType(ServerBuilder.class.getName());
    JClassType serverImplType = ModelUtils.findParameterizationOf(serverBuilderType, toGenerate)[0];

    // Build an impl so we can call it ourselves
    ServerCreator creator = new ServerCreator(serverImplType);
    creator.create(logger, context);

    String packageName = toGenerate.getPackage().getName();
    String simpleName = toGenerate.getName().replace('.', '_') + "_Impl";

    PrintWriter pw = context.tryCreate(logger, packageName, simpleName);
    if (pw == null) {
        return packageName + "." + simpleName;
    }

    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleName);
    factory.setSuperclass(Name.getSourceNameForClass(ServerBuilderImpl.class) + "<"
            + serverImplType.getQualifiedSourceName() + ">");
    factory.addImplementedInterface(typeName);

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

    RemoteServiceRelativePath path = serverImplType.getAnnotation(RemoteServiceRelativePath.class);
    if (path != null) {
        sw.println("public %1$s() {", simpleName);
        sw.indentln("setPath(\"%1$s\");", path.value());
        sw.println("}");
    }

    sw.println();
    // start method
    sw.println("public %1$s start() {", serverImplType.getQualifiedSourceName());
    sw.indent();
    sw.println("String url = getUrl();");
    sw.println("if (url == null) {");
    sw.indentln("return new %1$s(getErrorHandler());", creator.getQualifiedSourceName());
    sw.println("} else {");
    sw.indentln("return new %1$s(getErrorHandler(), url);", creator.getQualifiedSourceName());
    sw.println("}");

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

    sw.commit(logger);

    return factory.getCreatedClassName();

}

From source file:com.gafactory.core.rebind.LabelProviderCreator.java

License:sencha.com license

@Override
protected void create(SourceWriter sw) throws UnableToCompleteException {
    sw.println("public static final %1$s INSTANCE = new %1$s();", getSimpleName());

    // @Override/*from   w  w  w. j a  va 2s  .c  o  m*/
    sw.println("public String getLabel(%1$s item) {", getObjectTypeName());

    // if the value is string, just return a getter for the value
    sw.indentln("return %1$s;", getGetterExpression("item"));

    // else toString() it, or String.valueOf() if it is a primitive

    sw.println("}");
}

From source file:com.gafactory.core.rebind.ModelKeyProviderCreator.java

License:sencha.com license

@Override
protected void create(SourceWriter sw) throws UnableToCompleteException {
    sw.println("public static final %1$s INSTANCE = new %1$s();", getSimpleName());

    // @Override/*from w  ww .  j a va2s  .com*/
    sw.println("public String getKey(%1$s item) {", getObjectTypeName());

    // if the value is string, just return a getter for the value
    sw.indentln("return \"\" + %1$s;", getGetterExpression("item"));

    // else toString() it, or String.valueOf() if it is a primitive

    sw.println("}");
}

From source file:com.gafactory.core.rebind.PropertyAccessGenerator.java

License:sencha.com license

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    // make sure it is an interface
    TypeOracle oracle = context.getTypeOracle();

    propertyAccessInterface = oracle.findType(Name.getSourceNameForClass(PropertyAccess.class));
    modelKeyProviderInterface = oracle.findType(Name.getSourceNameForClass(ModelKeyProvider.class));
    valueProviderInterface = oracle.findType(Name.getSourceNameForClass(ValueProvider.class));
    labelProviderInterface = oracle.findType(Name.getSourceNameForClass(LabelProvider.class));
    JClassType toGenerate = oracle.findType(typeName).isInterface();
    if (toGenerate == null) {
        logger.log(TreeLogger.ERROR, typeName + " is not an interface type");
        throw new UnableToCompleteException();
    }/*from  www  .ja  v  a 2 s.  co m*/
    if (!toGenerate.isAssignableTo(propertyAccessInterface)) {
        logger.log(Type.ERROR, "This isn't a PropertyAccess subtype...");
        throw new UnableToCompleteException();
    }

    // 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;
    }

    // start making the class, with basic imports
    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
    factory.addImplementedInterface(typeName);
    SourceWriter sw = factory.createSourceWriter(context, pw);

    // for each method,
    for (JMethod m : toGenerate.getOverridableMethods()) {
        TreeLogger l = logger.branch(Type.DEBUG, "Building method: " + m.getReadableDeclaration());

        // no support for params at this time
        if (m.getParameters().length != 0) {
            l.log(Type.ERROR, "Method " + m.toString() + " must not have parameters.");
            throw new UnableToCompleteException();
        }

        // ask for the types that provide the property data
        JClassType ret = m.getReturnType().isClassOrInterface();
        final AbstractCreator c;
        if (ret.isAssignableTo(valueProviderInterface)) {
            c = new ValueProviderCreator(context, l, m);
        } else if (ret.isAssignableTo(modelKeyProviderInterface)) {
            c = new ModelKeyProviderCreator(context, l, m);
        } else if (ret.isAssignableTo(labelProviderInterface)) {
            c = new LabelProviderCreator(context, l, m);
        } else {
            logger.log(Type.ERROR, "Method uses a return type that cannot be generated");
            throw new UnableToCompleteException();
        }
        c.create();
        // build the method
        // public ValueProvider<T, V> name() { return NameValueProvider.instance;
        // }
        sw.println("public %1$s %2$s() {", m.getReturnType().getQualifiedSourceName(), m.getName());
        sw.indentln("return %1$s;", c.getInstanceExpression());
        sw.println("}");
    }

    sw.commit(logger);

    return factory.getCreatedClassName();
}

From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java

License:Apache License

private void writeReturnWrapper(SourceWriter sw, AutoBeanType type, AutoBeanMethod method)
        throws UnableToCompleteException {
    if (!method.isValueType() && !method.isNoWrap()) {
        JMethod jmethod = method.getMethod();
        JClassType returnClass = jmethod.getReturnType().isClassOrInterface();
        AutoBeanType peer = model.getPeer(returnClass);

        sw.println("if (toReturn != null) {");
        sw.indent();//  w  w  w  . jav a 2 s .  c o  m
        sw.println("if (%s.this.isWrapped(toReturn)) {", type.getSimpleSourceName());
        sw.indentln("toReturn = %s.this.getFromWrapper(toReturn);", type.getSimpleSourceName());
        sw.println("} else {");
        sw.indent();
        if (peer != null) {
            // toReturn = new FooAutoBean(getFactory(), toReturn).as();
            sw.println("toReturn = new %s(getFactory(), toReturn).as();", peer.getQualifiedSourceName());
        }
        sw.outdent();
        sw.println("}");

        sw.outdent();
        sw.println("}");
    }
    // Allow return values to be intercepted
    JMethod interceptor = type.getInterceptor();
    if (interceptor != null) {
        // toReturn = FooCategory.__intercept(FooAutoBean.this, toReturn);
        sw.println("toReturn = %s.%s(%s.this, toReturn);",
                interceptor.getEnclosingType().getQualifiedSourceName(), interceptor.getName(),
                type.getSimpleSourceName());
    }
}

From source file:com.google.web.bindery.requestfactory.gwt.rebind.RequestFactoryGenerator.java

License:Apache License

private void writeAutoBeanFactory(SourceWriter sw, Collection<EntityProxyModel> models,
        Collection<JEnumType> extraEnums) {
    if (!extraEnums.isEmpty()) {
        StringBuilder extraClasses = new StringBuilder();
        for (JEnumType enumType : extraEnums) {
            if (extraClasses.length() > 0) {
                extraClasses.append(",");
            }//from w  w  w  . j  a v  a  2  s .  c o  m
            extraClasses.append(enumType.getQualifiedSourceName()).append(".class");
        }
        sw.println("@%s({%s})", ExtraEnums.class.getCanonicalName(), extraClasses);
    }
    // Map in static implementations of EntityProxy methods
    sw.println("@%s({%s.class, %s.class, %s.class})", Category.class.getCanonicalName(),
            EntityProxyCategory.class.getCanonicalName(), ValueProxyCategory.class.getCanonicalName(),
            BaseProxyCategory.class.getCanonicalName());
    // Don't wrap our id type, because it makes code grungy
    sw.println("@%s(%s.class)", NoWrap.class.getCanonicalName(), EntityProxyId.class.getCanonicalName());
    sw.println("interface Factory extends %s {", AutoBeanFactory.class.getCanonicalName());
    sw.indent();

    for (EntityProxyModel proxy : models) {
        // AutoBean<FooProxy> com_google_FooProxy();
        sw.println("%s<%s> %s();", AutoBean.class.getCanonicalName(), proxy.getQualifiedSourceName(),
                proxy.getQualifiedSourceName().replace('.', '_'));
    }
    sw.outdent();
    sw.println("}");

    // public static final Factory FACTORY = GWT.create(Factory.class);
    sw.println("public static Factory FACTORY;", GWT.class.getCanonicalName());

    // Write public accessor
    sw.println("@Override public Factory getAutoBeanFactory() {");
    sw.indent();
    sw.println("if (FACTORY == null) {");
    sw.indentln("FACTORY = %s.create(Factory.class);", GWT.class.getCanonicalName());
    sw.println("}");
    sw.println("return FACTORY;");
    sw.outdent();
    sw.println("}");
}

From source file:com.google.web.bindery.requestfactory.gwt.rebind.RequestFactoryGenerator.java

License:Apache License

private void writeContextMethods(SourceWriter sw) {
    for (ContextMethod method : model.getMethods()) {
        // public FooService foo() {
        sw.println("public %s %s() {", method.getQualifiedSourceName(), method.getMethodName());
        // return new FooServiceImpl(this);
        sw.indentln("return new %s(this);", method.getQualifiedSourceName());
        sw.println("}");
    }/*w w  w.j  a  va  2 s.co m*/
}

From source file:com.google.web.bindery.requestfactory.gwt.rebind.RequestFactoryGenerator.java

License:Apache License

private void writeTypeMap(SourceWriter sw) {
    sw.println("private static final %1$s<String, Class<?>> tokensToTypes" + " = new %1$s<String, Class<?>>();",
            HashMap.class.getCanonicalName());
    sw.println("private static final %1$s<Class<?>, String> typesToTokens" + " = new %1$s<Class<?>, String>();",
            HashMap.class.getCanonicalName());
    sw.println("private static final %1$s<Class<?>> entityProxyTypes = new %1$s<Class<?>>();",
            HashSet.class.getCanonicalName());
    sw.println("private static final %1$s<Class<?>> valueProxyTypes = new %1$s<Class<?>>();",
            HashSet.class.getCanonicalName());
    sw.println("static {");
    sw.indent();/*from w  w  w.  ja va  2s.  com*/
    for (EntityProxyModel type : model.getAllProxyModels()) {
        // tokensToTypes.put("Foo", Foo.class);
        sw.println("tokensToTypes.put(\"%s\", %s.class);", OperationKey.hash(type.getQualifiedBinaryName()),
                type.getQualifiedSourceName());
        // typesToTokens.put(Foo.class, Foo);
        sw.println("typesToTokens.put(%s.class, \"%s\");", type.getQualifiedSourceName(),
                OperationKey.hash(type.getQualifiedBinaryName()));
        // fooProxyTypes.add(MyFooProxy.class);
        sw.println("%s.add(%s.class);",
                type.getType().equals(Type.ENTITY) ? "entityProxyTypes" : "valueProxyTypes",
                type.getQualifiedSourceName());
    }
    sw.outdent();
    sw.println("}");

    // Write instance methods
    sw.println("@Override public String getFactoryTypeToken() {");
    sw.indentln("return \"%s\";", model.getFactoryType().getQualifiedBinaryName());
    sw.println("}");
    sw.println("@Override protected Class getTypeFromToken(String typeToken) {");
    sw.indentln("return tokensToTypes.get(typeToken);");
    sw.println("}");
    sw.println("@Override protected String getTypeToken(Class type) {");
    sw.indentln("return typesToTokens.get(type);");
    sw.println("}");
    sw.println("@Override public boolean isEntityType(Class<?> type) {");
    sw.indentln("return entityProxyTypes.contains(type);");
    sw.println("}");
    sw.println("@Override public boolean isValueType(Class<?> type) {");
    sw.indentln("return valueProxyTypes.contains(type);");
    sw.println("}");
}

From source file:com.sencha.gxt.core.rebind.BindingPropertyGenerator.java

License:sencha.com license

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

    JClassType toGenerate = oracle.findType(typeName).isInterface();
    if (toGenerate == null) {
        logger.log(Type.ERROR, typeName + " is not an interface");
        throw new UnableToCompleteException();
    }/*  ww  w. jav  a  2 s .  com*/

    PropertyName annotation = toGenerate.getAnnotation(PropertyName.class);
    if (annotation == null) {
        logger.log(Type.ERROR, "Cannot generate with a @PropertyName anntation on the type");
        throw new UnableToCompleteException();
    }

    String propertyName = annotation.value();
    SelectionProperty property;
    String value;
    try {
        property = context.getPropertyOracle().getSelectionProperty(logger, propertyName);
        value = property.getCurrentValue();
    } catch (BadPropertyValueException e) {
        logger.log(Type.ERROR, "Error occured loading property: ", e);
        throw new UnableToCompleteException();
    }
    String packageName = toGenerate.getPackage().getName();
    String simpleSourceName = toGenerate.getName().replace('.', '_') + "_" + value;
    PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName);
    if (pw == null) {
        return packageName + "." + simpleSourceName;
    }

    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
    factory.addImplementedInterface(typeName);
    SourceWriter sw = factory.createSourceWriter(context, pw);

    for (JMethod method : toGenerate.getMethods()) {
        if (method.getReturnType().isPrimitive() != JPrimitiveType.BOOLEAN && !method.getReturnType().isClass()
                .getQualifiedSourceName().equals(Name.getSourceNameForClass(Boolean.class))) {
            logger.log(Type.ERROR, "Methods must return boolean or Boolean");
            throw new UnableToCompleteException();
        }
        sw.println("%1$s {", method.getReadableDeclaration(false, true, true, true, true));

        PropertyValue val = method.getAnnotation(PropertyValue.class);
        if (val == null) {
            logger.log(Type.ERROR, "Method must have a @PropertyValue annotation");
            throw new UnableToCompleteException();
        }

        if (!property.getPossibleValues().contains(val.value()) && val.warn()) {
            logger.log(Type.WARN,
                    "Value '" + val
                            + "' is not present in the current set of possible values for selection property "
                            + propertyName);
        }
        sw.indentln("return %1$b;", val.value().equals(value));

        sw.println("}");
    }

    sw.commit(logger);

    return factory.getCreatedClassName();
}

From source file:com.sencha.gxt.data.rebind.ValueProviderCreator.java

License:sencha.com license

@Override
protected void create(SourceWriter sw) throws UnableToCompleteException {
    sw.println("public static final %1$s INSTANCE = new %1$s();", getSimpleName());
    // @Override/*from  w w w  .j  av a 2s  .  c o  m*/
    sw.println("public %1$s getValue(%2$s object) {", getValueTypeName(), getObjectTypeName());
    appendGetterBody(sw, "object");
    sw.println("}");

    // @Override
    sw.println("public void setValue(%1$s object, %2$s value) {", getObjectTypeName(), getValueTypeName());
    appendSetterBody(sw, "object", "value");
    sw.println("}");

    // @Override
    sw.println("public String getPath() {");
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (String p : path) {
        if (!first) {
            sb.append(".");
        }
        sb.append(p);

        first = false;
    }
    sw.indentln("return \"%1$s\";", sb.toString());
    sw.println("}");
}