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

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

Introduction

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

Prototype

void println(String s);

Source Link

Usage

From source file:com.smartgwt.rebind.BeanProperty.java

License:Open Source License

public void writeMultipleConstructor(SourceWriter source, boolean addComma) {
    final String beanClassName = beanClass.getSimpleBeanClassName();

    source.println("new BeanPropertyMultiple<" + beanClassName + "> (\"" + getName() + "\",");
    source.indent();/*from ww w .  j av  a  2s .c  o  m*/

    if (getters.size() == 0) {
        source.println("null, // no getters");
    } else {
        source.println("new BeanMethod[] {");
        source.indent();

        Iterator<BeanMethod> iterator = getters.iterator();
        while (iterator.hasNext()) {
            BeanMethod getter = iterator.next();
            getter.writeNew(source, beanClassName, getMethodIndex(getter), null, iterator.hasNext());
        }

        source.outdent();
        source.println("},");
    }

    if (setters.size() == 0) {
        source.println("null // no setters");
    } else {
        source.println("new BeanMethod[] {");
        source.indent();

        Iterator<BeanMethod> iterator = setters.iterator();
        while (iterator.hasNext()) {
            BeanMethod setter = iterator.next();
            setter.writeNew(source, beanClassName, null, getMethodIndex(setter), iterator.hasNext());
        }

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

    source.outdent();
    source.println(")" + (addComma ? "," : ""));
}

From source file:com.smartgwt.rebind.BeanValueType.java

License:Open Source License

public void writeRegisterValueType(SourceWriter source, TreeLogger logger, GeneratorContext context) {
    final String registerValueTypeStaticMethodName;
    if (findType(EnumValueType.class).equals(beanValueType)) {
        registerValueTypeStaticMethodName = "registerEnumValueType";
    } else if (findType(JsoValueType.class).equals(beanValueType)) {
        registerValueTypeStaticMethodName = "registerJsoValueType";
    } else if (findType(OtherValueType.class).equals(beanValueType)) {
        registerValueTypeStaticMethodName = "registerOtherValueType";
    } else if (findType(ValueEnumValueType.class).equals(beanValueType)) {
        registerValueTypeStaticMethodName = "registerValueEnumValueType";
    } else {//from  w w w  .j  a  v  a  2  s.co m
        registerValueTypeStaticMethodName = "registerValueType";
    }
    source.println(
            // We import all the pre-written BeanValueType classes, but we need to 
            // use the qualified name of ones that we are generating ...
            (requiresGeneration ? getQualifiedFactoryName() : getSimpleFactoryName()) + "."
                    + registerValueTypeStaticMethodName + "(" +
                    // Will take either class literal, empty array, or neither (but not both)
                    (constructorTakesClassLiteral ? getQualifiedValueTypeLiteral() : "") +
                    // Add the empty array parameter if we are an array ...
                    (constructorTakesEmptyArray
                            ? "new " + getQualifiedGenericName().replace("[]", "[0]") + "[0]"
                            : "")
                    + ");");

    // If we have a component type, we'll need to make sure to register that as well.
    if (componentValueType != null) {
        componentValueType.writeRegisterValueType(source, logger, context);
    }

    // Make sure we're generated if we are referenced
    if (requiresGeneration) {
        generateFactory(logger, context);
    }
}

From source file:com.smartgwt.rebind.BeanValueType.java

License:Open Source License

public String generateFactory(TreeLogger logger, GeneratorContext context) {
    final String packageName = getFactoryPackage();
    final String factoryName = getSimpleFactoryName();
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, factoryName);

    composer.addImport(com.smartgwt.client.bean.BeanValueType.class.getCanonicalName());
    composer.addImport(com.google.gwt.core.client.JavaScriptObject.class.getCanonicalName());

    // Import our valueType, but without the [] designation
    composer.addImport(getQualifiedTypeName().replace("[]", ""));

    composer.addImport(beanValueType.getQualifiedSourceName());
    composer.setSuperclass(beanValueType.getSimpleSourceName() + "<" + getSimpleGenericName() + ">");

    PrintWriter printWriter = context.tryCreate(logger, packageName, factoryName);
    if (printWriter != null) {
        SourceWriter source = composer.createSourceWriter(context, printWriter);

        source.println("// This class lovingly generated by com.smartgwt.rebind.BeanValueType\n");
        source.println("public static void registerValueType () {");

        source.indent();//  w  w  w  .ja  v  a 2s  . c o m
        source.println("// We check first to see if it's already registered, to avoid\n"
                + "// constructing the singleton over and over again. This will\n"
                + "// be called multiple times as various BeanFactories initialize\n" + "// themselves.");
        source.println("if (BeanValueType.getBeanValueType(" + getSimpleValueTypeLiteral() + ") == null) {");

        source.indent();
        source.println("BeanValueType.registerBeanValueType(new " + getSimpleFactoryName() + "());");

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

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

        source.println("@Override public Class<" + getSimpleTypeName() + "> getValueType () {");
        source.indent();
        source.println("return " + getSimpleTypeName() + ".class;");
        source.outdent();
        source.println("}\n");

        source.println("@Override public boolean isAssignableFrom (Object value) {");
        source.indent();
        source.println("return value == null || value instanceof " + getSimpleTypeName() + ";");
        source.outdent();
        source.println("}");

        if (componentType != null) {
            source.println("\nprivate " + getSimpleTypeName() + " emptyArray = new "
                    + componentType.getSimpleSourceName() + "[0];");
            source.println("\n@Override public " + getSimpleTypeName() + " emptyArray () {");
            source.indent();
            source.println("return emptyArray;");
            source.outdent();
            source.println("}");
        }

        if (scClassName != null) {
            source.println("\n@Override public String getScClassName () {");
            source.indent();
            source.println("return \"" + scClassName + "\";");
            source.outdent();
            source.println("}");
        }

        // Try to write a newInstance function that takes a JavaScriptObject
        if (isAbstract) {
            // If the type is abstract, our only hope is if it has a static getOrCreateRef method
            if (hasGetOrCreateRef) {
                source.println("\n@Override public " + getSimpleTypeName()
                        + " newInstance (JavaScriptObject jsObject) {");
                source.indent();
                source.println("return " + getSimpleTypeName() + ".getOrCreateRef(jsObject);");
                source.outdent();
                source.println("}");
            }
        } else {
            if (jsObjConstructor != null) {
                // If it has the right kind of constructor, then use that
                source.println("\n@Override public " + getSimpleTypeName()
                        + " newInstance (JavaScriptObject jsObject) {");
                source.indent();
                source.println("return new " + getSimpleTypeName() + "(jsObject);");
                source.outdent();
                source.println("}");
            } else if (hasSetJavaScriptObject) {
                // Custom subclasses likely won't have the constructor, but may have a a setJavaScriptObject method
                source.println("\n@Override public " + getSimpleTypeName()
                        + " newInstance (JavaScriptObject jsObject) {");
                source.indent();
                source.println(getSimpleTypeName() + " value = new " + getSimpleTypeName() + "();");
                source.println("value.setJavaScriptObject(jsObject);");
                source.println("return value;");
                source.outdent();
                source.println("}");
            } else if (hasGetOrCreateRef) {
                // And may as well fall back to getOrCreateRef if it exists
                source.println("\n@Override public " + getSimpleTypeName()
                        + " newInstance (JavaScriptObject jsObject) {");
                source.indent();
                source.println("return " + getSimpleTypeName() + ".getOrCreateRef(jsObject);");
                source.outdent();
                source.println("}");
            }
        }

        source.commit(logger);
    }

    return composer.getCreatedClassName();
}

From source file:com.smartgwt.rebind.BeanValueTypeFactoryGenerator.java

License:Open Source License

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

    final String genPackageName = metaFactoryType.getPackage().getName();
    final String genClassName = metaFactoryType.getSimpleSourceName() + "Impl";

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.addImplementedInterface(typeName);
    composer.addImport(com.smartgwt.client.bean.BeanValueType.class.getCanonicalName());
    composer.addImport("com.smartgwt.client.bean.types.*");

    PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName);
    if (printWriter != null) {
        SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
        sourceWriter.println("// This class lovingly generated by "
                + BeanValueTypeFactoryGenerator.class.getCanonicalName() + "\n");

        StringBuilder functions = new StringBuilder();

        // Our constructor ... will be called by GWT.create()
        sourceWriter.println(genClassName + " () {");
        sourceWriter.indent();//from  w ww.  j a va2 s .  c o m

        JClassType beanValueTypeClass = typeOracle
                .findType(com.smartgwt.client.bean.BeanValueType.class.getCanonicalName()).isClass();

        // Iterate over the methods defined on the interface
        for (JMethod method : metaFactoryType.getMethods()) {
            if (method.getParameters().length != 0) {
                logger.log(Type.ERROR, typeName + "::" + method.getName() + " should have no parameters.");
                throw new UnableToCompleteException();
            }

            JParameterizedType returnType = method.getReturnType().isParameterized();
            if (returnType == null) {
                logger.log(Type.ERROR,
                        typeName + "::" + method.getName() + " has a non-parameterized return type.");
                throw new UnableToCompleteException();
            }

            if (returnType.getBaseType() != beanValueTypeClass) {
                logger.log(Type.ERROR, typeName + "::" + method.getName()
                        + " does not have BeanValueType<> as its return type.");
                throw new UnableToCompleteException();
            }

            JClassType[] typeArgs = returnType.getTypeArgs();
            if (typeArgs.length != 1) {
                logger.log(Type.ERROR, typeName + "::" + method.getName()
                        + " should have a return type with one parameterized type.");
                throw new UnableToCompleteException();
            }

            BeanValueType beanValueType = new BeanValueType(typeArgs[0], typeOracle);

            // Write the function to register the value type. Note that a side-effect
            // is that the factory for the value type is actually generated!
            beanValueType.writeRegisterValueType(sourceWriter, logger, context);

            // And we'll need to generate the function!
            functions.append("\n\n@Override public BeanValueType<" + beanValueType.getQualifiedTypeName() + "> "
                    + method.getName() + "() {\n  " + "return (BeanValueType<"
                    + beanValueType.getQualifiedTypeName() + ">) BeanValueType.getBeanValueType("
                    + beanValueType.getQualifiedValueTypeLiteral() + ");\n}");
        }

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

        sourceWriter.println(functions.toString());
        sourceWriter.commit(logger);
    }

    return composer.getCreatedClassName();
}

From source file:com.smartgwt.rebind.CanvasMetaBeanFactoryGenerator.java

License:Open Source License

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

    final String genPackageName = "com.smartgwt.client.bean";
    final String genClassName = "CanvasMetaBeanFactoryImpl";

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.addImplementedInterface(BeanFactory.CanvasMetaFactory.class.getCanonicalName());

    PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName);
    if (printWriter != null) {
        SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
        sourceWriter.println("// This class lovingly generated by "
                + CanvasMetaBeanFactoryGenerator.class.getCanonicalName() + "\n");

        // Our constructor ... will be called by GWT.create()
        sourceWriter.println(genClassName + " () {");
        sourceWriter.indent();/*from  www  .ja  va 2  s.c o m*/

        for (JClassType classType : oracle.getTypes()) {
            if (classType.isAssignableTo(canvasType) && isEligibleForGeneration(classType)) {
                BeanClass beanClass = new BeanClass(classType);
                beanClass.generateFactory(logger, context);

                // We have to instantiate the factory to register it in the BeanFactory static API
                sourceWriter.println(beanClass.getQualifiedFactoryName() + ".create(false);");
            }
        }

        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.commit(logger);
    }

    return composer.getCreatedClassName();
}

From source file:com.smartgwt.rebind.FormItemMetaBeanFactoryGenerator.java

License:Open Source License

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

    final String genPackageName = "com.smartgwt.client.bean";
    final String genClassName = "FormItemMetaBeanFactoryImpl";

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.addImplementedInterface(BeanFactory.FormItemMetaFactory.class.getCanonicalName());

    PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName);
    if (printWriter != null) {
        SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
        sourceWriter.println("// This class lovingly generated by "
                + FormItemMetaBeanFactoryGenerator.class.getCanonicalName() + "\n");

        // Our constructor ... will be called by GWT.create()
        sourceWriter.println(genClassName + " () {");
        sourceWriter.indent();//from  ww  w .  j ava  2  s. c om

        for (JClassType classType : oracle.getTypes()) {
            if (classType.isAssignableTo(formItemType) && isEligibleForGeneration(classType)) {
                BeanClass beanClass = new BeanClass(classType);
                beanClass.generateFactory(logger, context);

                // We have to instantiate the factory to register it in the BeanFactory static API
                sourceWriter.println(beanClass.getQualifiedFactoryName() + ".create(false);");
            }
        }

        sourceWriter.outdent();
        sourceWriter.println("}");
        sourceWriter.commit(logger);
    }

    return composer.getCreatedClassName();
}

From source file:com.smartgwt.rebind.MetaBeanFactoryGenerator.java

License:Open Source License

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

    final String genPackageName = getFactoryPackage(metaFactoryType);
    final String genClassName = getSimpleFactoryName(metaFactoryType);

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.addImplementedInterface(typeName);
    composer.addImport(com.smartgwt.client.bean.BeanFactory.class.getCanonicalName());

    PrintWriter printWriter = context.tryCreate(logger, genPackageName, genClassName);
    if (printWriter != null) {
        SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
        sourceWriter.println("// This class lovingly generated by "
                + MetaBeanFactoryGenerator.class.getCanonicalName() + "\n");

        StringBuilder functions = new StringBuilder();

        // Our constructor ... will be called by GWT.create()
        sourceWriter.println(genClassName + " () {");
        sourceWriter.indent();/*from  w w w .ja va  2  s .co  m*/

        JClassType beanFactoryType = typeOracle.findType(BeanFactory.class.getCanonicalName()).isClass();
        JClassType baseWidgetType = typeOracle.findType(BaseWidget.class.getCanonicalName()).isClass();
        JClassType dataClassType = typeOracle.findType(DataClass.class.getCanonicalName()).isClass();

        // Iterate over the methods defined on the interface
        for (JMethod method : metaFactoryType.getMethods()) {
            if (method.getParameters().length != 0) {
                logger.log(Type.ERROR, typeName + "::" + method.getName() + " should have no parameters.");
                throw new UnableToCompleteException();
            }

            JParameterizedType returnType = method.getReturnType().isParameterized();
            if (returnType == null) {
                logger.log(Type.ERROR,
                        typeName + "::" + method.getName() + " has a non-parameterized return type.");
                throw new UnableToCompleteException();
            }

            if (returnType.getBaseType() != beanFactoryType) {
                logger.log(Type.ERROR, typeName + "::" + method.getName()
                        + " does not have BeanFactory<> as its return type.");
                throw new UnableToCompleteException();
            }

            JClassType[] typeArgs = returnType.getTypeArgs();
            if (typeArgs.length != 1) {
                logger.log(Type.ERROR, typeName + "::" + method.getName()
                        + " should have a return type with one parameterized type.");
                throw new UnableToCompleteException();
            }

            JClassType beanClassType = typeArgs[0];
            if (!baseWidgetType.isAssignableFrom(beanClassType)
                    && !dataClassType.isAssignableFrom(beanClassType)) {
                logger.log(Type.ERROR, typeName + "::" + method.getName()
                        + ": for now, factories can only be created for Canvas or DataClass and subclasses.");
                throw new UnableToCompleteException();
            }

            BeanClass beanClass = new BeanClass(beanClassType);
            beanClass.generateFactory(logger, context);

            // We have to instantiate the factory to register it in the BeanFactory static API
            sourceWriter.println(beanClass.getQualifiedFactoryName() + ".create(false);");

            // And we'll need to generate the function!
            functions.append("\n\n@Override public BeanFactory<" + beanClassType.getQualifiedSourceName() + "> "
                    + method.getName() + "() {\n  " + "return (BeanFactory<"
                    + beanClassType.getQualifiedSourceName() + ">) BeanFactory.getFactory("
                    + beanClassType.getQualifiedSourceName() + ".class);\n}");
        }

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

        sourceWriter.println(functions.toString());
        sourceWriter.commit(logger);
    }

    return composer.getCreatedClassName();
}

From source file:com.threerings.gwt.rebind.MessagesLookupGenerator.java

License:Open Source License

protected void generateClass(TreeLogger logger, GeneratorContext ctx) throws NotFoundException {
    PrintWriter printWriter = ctx.tryCreate(logger, _packageName, _className);
    if (printWriter == null) {
        // if this is null, the class was already generated for another module so we don't need
        // to generate it again
        return;//from   w  w w  . j a v a  2  s .  c o  m
    }

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(_packageName, _className);
    composer.setSuperclass(_typeName);

    SourceWriter code = composer.createSourceWriter(ctx, printWriter);
    code.println("protected static final " + _using + " msg = com.google.gwt.core.client.GWT.create(" + _using
            + ".class);");

    code.println("private " + _className + "() { }"); // TODO: omit?
    generateLookupMethod(ctx, code);

    // close generated class
    code.outdent();
    code.println("}");

    // commit generated class
    ctx.commit(logger, printWriter);
}

From source file:com.threerings.gwt.rebind.MessagesLookupGenerator.java

License:Open Source License

protected void generateLookupMethod(GeneratorContext ctx, SourceWriter code) throws NotFoundException {
    JMethod[] methods = ctx.getTypeOracle().getType(_using).getMethods();
    code.println("@Override public String fetch (String key, Object... params) {");
    code.indent();/*from  w  ww .  ja  va2  s. co m*/
    code.println("int length = (params != null) ? params.length : 0;");
    for (JMethod method : methods) {
        String s = method.getName();
        int jpi = method.getParameters().length;
        code.println("if (key.equals(\"" + s + "\") && length >= " + jpi + ") {");
        code.indent();
        code.print("return msg." + s + "(");
        for (int jj = 0; jj < jpi; jj++) {
            if (jj > 0) {
                code.print(", ");
            }
            code.print("params[" + jj + "].toString()");
        }
        code.println(");"); // end return
        code.outdent();
        code.println("}"); // end if
    }

    code.println("return \"Invalid key: \" + key + \" with \" + length + \" params.\";");

    // end function
    code.outdent();
    code.println("}");
}

From source file:com.totsp.gwittir.rebind.beans.IntrospectorGenerator.java

License:Open Source License

public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    //.println("Introspector Generate.");
    try {/*from ww w  . ja  va2 s . c  om*/
        this.objectType = context.getTypeOracle().getType("java.lang.Object");
    } catch (NotFoundException ex) {
        logger.log(TreeLogger.ERROR, typeName, ex);

        return null;
    }

    List<BeanResolver> introspectables = this.getIntrospectableTypes(logger, context.getTypeOracle());

    MethodWrapper[] methods = this.findMethods(logger, introspectables);

    ClassSourceFileComposerFactory mcf = new ClassSourceFileComposerFactory(this.packageName,
            this.methodsImplementationName);
    mcf.addImport(com.totsp.gwittir.client.beans.Method.class.getCanonicalName());

    PrintWriter methodsPrintWriter = context.tryCreate(logger, this.packageName,
            this.methodsImplementationName);

    if (methodsPrintWriter != null) {
        SourceWriter methodsWriter = mcf.createSourceWriter(context, methodsPrintWriter);
        this.writeMethods(logger, methods, methodsWriter);
        methodsWriter.println("}");
        context.commit(logger, methodsPrintWriter);
    }

    ClassSourceFileComposerFactory cfcf = new ClassSourceFileComposerFactory(this.packageName,
            this.implementationName);
    cfcf.addImplementedInterface(typeName);
    cfcf.addImport("java.util.HashMap");
    cfcf.addImport(com.totsp.gwittir.client.beans.Method.class.getCanonicalName());
    cfcf.addImport(com.totsp.gwittir.client.beans.Property.class.getCanonicalName());
    cfcf.addImport(com.totsp.gwittir.client.beans.BeanDescriptor.class.getCanonicalName());

    PrintWriter printWriter = context.tryCreate(logger, packageName, implementationName);

    if (printWriter == null) {
        //.println( "Introspector Generate skipped.");
        return packageName + "." + implementationName;
    }

    SourceWriter writer = cfcf.createSourceWriter(context, printWriter);
    this.writeIntrospectables(logger, introspectables, methods, writer);
    this.writeResolver(introspectables, writer);

    writer.println(
            "private HashMap<Class,BeanDescriptor> beanDescriptorLookup = new HashMap<Class,BeanDescriptor>();");
    writer.println();
    writer.println("public BeanDescriptor getDescriptor( Object object ){ ");
    writer.indent();
    writer.println(
            "if( object == null ) throw new NullPointerException(\"Attempt to introspect null object\");");
    writer.println("if( object instanceof " + SelfDescribed.class.getCanonicalName()
            + " ) return ((SelfDescribed)object).__descriptor();");
    writer.println("BeanDescriptor descriptor = beanDescriptorLookup.get(object.getClass());");
    writer.println("if (descriptor!=null){");
    writer.indentln("return descriptor;");
    writer.outdent();
    writer.println("}");

    writer.println("descriptor=_getDescriptor(object);");
    writer.println("beanDescriptorLookup.put(object.getClass(),descriptor);");
    writer.println("return descriptor;");
    writer.outdent();
    writer.println("}");

    writer.println("private BeanDescriptor _getDescriptor( Object object ){ ");
    writer.indent();

    for (BeanResolver resolver : introspectables) {
        writer.println("if( object instanceof " + resolver.getType().getQualifiedSourceName() + " ) {");
        writer.indent();

        String name = resolver.getType().getQualifiedSourceName().replaceAll("\\.", "_");
        logger.log(TreeLogger.DEBUG, "Writing : " + name, null);
        writer.print("return " + name + " == null ? " + name + " = ");
        this.writeBeanDescriptor(logger, resolver, methods, writer);
        writer.print(": " + name + ";");
        writer.outdent();
        writer.println("}");
    }

    writer.println(" throw new IllegalArgumentException(\"Unknown type\" + object.getClass() ); ");
    writer.outdent();
    writer.println("}");
    writer.outdent();
    writer.println("}");

    context.commit(logger, printWriter);

    //.println( "Introspector Generate completed.");
    return packageName + "." + implementationName;
}