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.totsp.gwittir.rebind.beans.IntrospectorGenerator.java

License:Open Source License

private void writeBeanDescriptor(TreeLogger logger, BeanResolver info, MethodWrapper[] methods,
        SourceWriter writer) {
    writer.println("new BeanDescriptor() { ");
    writer.indent();/*from w  w w .j  a v a2 s . c  o  m*/
    writer.println("private HashMap lookup;");
    writer.println("private Property[] properties;");
    writer.println("public Property[] getProperties(){");
    writer.indent();

    {
        writer.println("if( this.properties != null ) ");
        writer.indentln("return this.properties;");
        writer.println("this.properties = new Property[" + (info.getProperties().size()) + "];");

        Collection pds = info.getProperties().values();
        String[] propertyNames = new String[pds.size()];
        logger.log(TreeLogger.SPAM, "" + (pds == null), null);

        if (pds != null) {
            int i = 0;

            for (Iterator it = pds.iterator(); it.hasNext(); i++) {
                RProperty p = (RProperty) it.next();
                propertyNames[i] = p.getName();
                writer.println("{");
                writer.indent();

                writer.print("Method readMethod = ");

                if (p.getReadMethod() == null) {
                    writer.println("null;");
                } else {
                    writer.println(this.packageName + "." + this.methodsImplementationName + ".METHOD_"
                            + +this.find(methods, p.getReadMethod()) + ";");
                }

                writer.print("Method writeMethod = ");

                if (p.getWriteMethod() == null) {
                    writer.println("null;");
                } else {
                    writer.println(this.packageName + "." + this.methodsImplementationName + ".METHOD_"
                            + +this.find(methods, p.getWriteMethod()) + ";");
                }

                logger.log(TreeLogger.DEBUG, p.getName() + " " + p.getType().getQualifiedSourceName(), null);

                JType ptype = this.resolveType(p.getType());

                logger.log(TreeLogger.DEBUG, p.getName() + " (Erased) " + ptype.getQualifiedSourceName(), null);
                writer.println("this.properties[" + (i) + "] = new Property( \"" + p.getName() + "\", "
                        + ((p.getType() != null) ? ptype.getQualifiedSourceName() : "Object")
                        + ".class,  readMethod, writeMethod );");
                writer.outdent();
                writer.println("}");
            }
        }

        writer.println("return this.properties;");
    }

    writer.outdent();
    writer.println("} //end getProperties()");
    writer.println("public Property getProperty( String name ) {");
    writer.indent();
    //TODO Rewrite this to a nested if loop using the propertyNames parameter.
    writer.println("Property p = null;");
    writer.println("if( this.lookup != null ) {");
    writer.indentln("p = (Property) lookup.get(name); ");
    writer.println("} else {");
    writer.indent();
    writer.println("this.lookup = new HashMap();");
    writer.println("Property[] props = this.getProperties(); ");
    writer.println("for( int i=0; i < props.length; i++ ) {");
    writer.indent();
    writer.println("this.lookup.put( props[i].getName(), props[i] );");
    writer.outdent();
    writer.println("}");
    writer.println("p = (Property) this.lookup.get(name);");
    writer.outdent();
    writer.println("}");
    writer.println("if( p == null ) throw new RuntimeException(\"Couldn't find property \"+name+\" for "
            + info.getType().getQualifiedSourceName() + "\");");
    writer.println("else return p;");
    writer.outdent();
    writer.println("}");

    writer.outdent();
    writer.print("}");
}

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

License:Open Source License

private void writeIntrospectables(TreeLogger logger, List introspectables, MethodWrapper[] methods,
        SourceWriter writer) {
    for (Iterator it = introspectables.iterator(); it.hasNext();) {
        BeanResolver bean = (BeanResolver) it.next();

        logger.branch(TreeLogger.DEBUG, "Introspecting: " + bean.getType().getQualifiedSourceName(), null);

        try {/*from   w  w w .  j av  a 2 s  .  co  m*/
            if (bean.getProperties().size() == 0) {
                continue;
            }

            writer.print("private static BeanDescriptor ");
            writer.print(bean.getType().getQualifiedSourceName().replaceAll("\\.", "_"));

            writer.println(" = null;");
        } catch (Exception e) {
            logger.log(TreeLogger.ERROR, "Unable to introspect class. Is class a bean?", e);
        }
    }
}

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

License:Open Source License

private void writeMethod(TreeLogger logger, MethodWrapper method, SourceWriter writer) {
    JType ptype = this.resolveType(method.getDeclaringType());

    writer.println("new Method(){ ");
    writer.indent();//from  ww w. j av  a2 s .  c  o  m
    writer.println("public String getName() {");
    writer.indentln("return \"" + method.getBaseMethod().getName() + "\";");
    writer.println(" }");
    writer.println("public Object invoke( Object target, Object[] args ) throws Exception {");
    writer.indent();
    writer.println(ptype.getQualifiedSourceName() + " casted =");
    writer.println("(" + ptype.getQualifiedSourceName() + ") target;");
    logger.log(TreeLogger.SPAM, "Method: " + method.getBaseMethod().getName() + " "
            + method.getBaseMethod().getReturnType().getQualifiedSourceName(), null);

    if (!(method.getBaseMethod().getReturnType().isPrimitive() == JPrimitiveType.VOID)) {
        writer.print("return ");
    }

    JType type = this.resolveType(method.getBaseMethod().getReturnType());

    boolean boxed = this.box(type, writer);
    writer.print("casted." + method.getBaseMethod().getName() + "(");

    if (method.getBaseMethod().getParameters() != null) {
        for (int j = 0; j < method.getBaseMethod().getParameters().length; j++) {
            JType arg = this.resolveType(method.getBaseMethod().getParameters()[j].getType());

            this.unbox(arg, "args[" + j + "]", writer);

            if (j != (method.getBaseMethod().getParameters().length - 1)) {
                writer.print(", ");
            }
        }
    }

    writer.print(")");

    if (boxed) {
        writer.print(")");
    }

    writer.println(";");

    if (method.getBaseMethod().getReturnType().getQualifiedSourceName().equals("void")) {
        writer.println("return null;");
    }

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

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

License:Open Source License

private void writeResolver(List introspectables, SourceWriter writer) {
    writer.println("public Class resolveClass(Object object){");
    writer.indent();//from   www. j  a  va2 s  . c  o m

    for (Iterator it = introspectables.iterator(); it.hasNext();) {
        BeanResolver type = (BeanResolver) it.next();
        writer.println("if( object instanceof " + type.getType().getQualifiedSourceName() + " ) return "
                + type.getType().getQualifiedSourceName() + ".class;");
    }

    writer.println("throw new RuntimeException( \"Object \"+object+\"could not be resolved.\" );");
    writer.outdent();
    writer.println("}");
}

From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java

License:Open Source License

public void writeReader(SourceWriter writer, RProperty prop) {
    if (prop.getWriteMethod() == null) {
        return;/*from ww  w . jav  a2  s .  co  m*/
    }

    JSONField field = prop.getReadMethod() == null ? null
            : prop.getReadMethod().getBaseMethod().getAnnotation(JSONField.class);
    JSONOmit omit = prop.getReadMethod() == null ? null
            : prop.getReadMethod().getBaseMethod().getAnnotation(JSONOmit.class);
    System.out.println(prop.getName() + " omit " + omit + " field " + field);
    if (omit != null) {
        return;
    }

    String fieldName = (field == null) ? prop.getName() : field.value();

    try {
        writer.println("if(root.containsKey(\"" + fieldName + "\")){");

        if (prop.getType().isPrimitive() == null) {
            writer.println("if(root.get(\"" + fieldName + "\").isNull() != null) {");
            writer.println(this.setterPrefix(prop) + "null);");
            writer.println("} else {");
        }

        if (prop.getType().isArray() != null) {
            JArrayType arrayType = prop.getType().isArray();
            JType paramType = arrayType.getComponentType();
            writer.println("JSONArray array = root.get(\"" + fieldName + "\").isArray();");
            writer.println(paramType.getQualifiedSourceName() + "[] value = new "
                    + paramType.getQualifiedSourceName() + "[ array.size() ];");
            writer.println("for(int i=0; i<array.size(); i++){");
            writer.indent();

            writer.println(" value[i] = " + this.fromType(paramType, "array.get(i)") + ";");

            writer.outdent();
            writer.println("}"); //endfor
            writer.println(this.setterPrefix(prop) + " value );");
        } else if (prop.getType() instanceof JClassType
                && ((JClassType) prop.getType()).isAssignableTo(this.collectionType)) {
            // get the parameter type
            JClassType propType = (JClassType) prop.getType();
            JType paramType = propType.asParameterizationOf((JGenericType) this.collectionType)
                    .getTypeArgs()[0];
            writer.println("JSONArray array = root.get(\"" + fieldName + "\").isArray();");
            writer.println(propType.getParameterizedQualifiedSourceName() + " col = "
                    + this.newCollectionExpression(propType) + ";");
            writer.println("for(int i=0; i<array.size(); i++){");
            writer.indent();

            writer.println(" col.add(" + this.fromType(paramType, "array.get(i)") + ");");

            writer.outdent();
            writer.println("}"); //endfor
            writer.println(this.setterPrefix(prop) + " col );");
        } else {
            writer.println(
                    setterPrefix(prop) + fromType(prop.getType(), "root.get(\"" + fieldName + "\")") + ");");
        }

        if (prop.getType().isPrimitive() == null) {
            writer.println("}"); //end null else
        }

        writer.println("}"); //end contains key
    } catch (Exception e) {
        System.out.println("Exception on prop " + prop);
        throw new RuntimeException(e);
    }
}

From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java

License:Open Source License

private void writeClassSerializer(TreeLogger logger, GeneratorContext context, BeanResolver type) {
    logger.log(Type.INFO, "Creating JSON Serializer for " + type.getType());

    String classTypeName = type.getType().getSimpleSourceName() + "_JSONCodec";
    ClassSourceFileComposerFactory mcf = new ClassSourceFileComposerFactory(
            type.getType().getPackage().getName(), classTypeName);
    mcf.addImport(JSONParser.class.getCanonicalName());
    mcf.addImport(JSONObject.class.getCanonicalName());
    mcf.addImport(JSONArray.class.getCanonicalName());
    mcf.addImport(JSONBoolean.class.getCanonicalName());
    mcf.addImport(JSONNumber.class.getCanonicalName());
    mcf.addImport(JSONString.class.getCanonicalName());
    mcf.addImport(JSONNull.class.getCanonicalName());
    mcf.addImport(GWT.class.getCanonicalName());
    mcf.addImport(SerializationException.class.getCanonicalName());
    mcf.addImplementedInterface(//from ww  w .ja v  a 2s .co m
            JSONCodec.class.getCanonicalName() + "<" + type.getType().getQualifiedSourceName() + ">");

    PrintWriter printWriter = context.tryCreate(logger, type.getType().getPackage().getName(), classTypeName);

    if (printWriter == null) {
        logger.log(Type.INFO, "Already genned " + classTypeName);

        return;
    }

    SourceWriter writer = mcf.createSourceWriter(context, printWriter);

    writeDeserializer(writer, type);
    writeSerializer(writer, type);

    HashSet<BeanResolver> childrenCopy = new HashSet<BeanResolver>(this.children);
    this.children.clear();

    for (BeanResolver child : childrenCopy) {
        writer.println(" private static final " + JSONCodec.class.getCanonicalName() + "<"
                + child.getType().getQualifiedSourceName() + "> CODEC_"
                + child.getType().getQualifiedSourceName().replaceAll("\\.", "_") + " = GWT.create("
                + child.getType().getQualifiedSourceName() + "_JSONCodec.class);");
        writeClassSerializer(logger, context, child);
    }

    writer.println(" public String getMimeType() { return MIME_TYPE; }");
    writer.println("}"); // close the class

    context.commit(logger, printWriter);
}

From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java

License:Open Source License

private void writeDeserializer(SourceWriter writer, BeanResolver r) {
    writer.println("public " + r.toString() + " deserialize(String data) throws SerializationException { ");
    writer.indent();//from www  . j a  v a  2  s  .  com
    writer.println("try {");
    writer.indent();
    writer.println("JSONObject root = JSONParser.parse(data).isObject(); ");
    writer.println(" return this.deserializeFromJSONObject(root);");
    writer.println("} catch (Exception e) { ");
    writer.indent();
    writer.println("throw new SerializationException(e);");
    writer.outdent();
    writer.println("}");
    writer.println("}");

    writer.println("public " + r.toString()
            + " deserializeFromJSONObject(JSONObject root) throws SerializationException {");
    writer.indent();
    writer.println(" if(root == null) return null;");
    writer.println("try {");
    writer.indent();

    writer.println(r.getType().getQualifiedSourceName() + " destination = new "
            + r.getType().getQualifiedSourceName() + "();");

    for (RProperty p : r.getProperties().values()) {
        if (p.getWriteMethod() != null) {
            writeReader(writer, p);
        }
    }

    writer.println(" return destination;");
    writer.outdent();
    writer.println("} catch (Exception e) { ");
    writer.indent();
    writer.println("throw new SerializationException(e);");
    writer.outdent();
    writer.println("}");
    writer.outdent();
    writer.println("}");
}

From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java

License:Open Source License

private void writeSerializer(SourceWriter writer, BeanResolver r) {
    writer.println("public JSONObject serializeToJSONObject( " + r.getType().getQualifiedSourceName()
            + " source ) throws SerializationException { ");
    writer.indent();/*from   w ww. j a va2s  . c o m*/
    writer.println(" JSONObject destination = new JSONObject();");

    for (RProperty prop : r.getProperties().values()) {
        if (prop.getName().equals("class") || prop.getReadMethod() == null) {
            continue;
        }

        JSONField field = prop.getReadMethod().getBaseMethod().getAnnotation(JSONField.class);
        JSONOmit omit = prop.getReadMethod().getBaseMethod().getAnnotation(JSONOmit.class);
        System.out.println(" ws \t " + prop.getName() + " "
                + prop.getReadMethod().getBaseMethod().getEnclosingType()
                + prop.getReadMethod().getBaseMethod().getReadableDeclaration() + " " + omit + " " + field);
        if (omit != null) {
            continue;
        }

        String fieldName = (field == null) ? prop.getName() : field.value();

        if (prop.getReadMethod() != null) {
            JClassType classType = prop.getType().isClassOrInterface();
            JArrayType arrayType = prop.getType().isArray();
            System.out.println(prop.getName() + "  ArrayType " + arrayType + " :: "
                    + ((arrayType == null ? "" : "" + arrayType.getComponentType())));
            if ((classType != null) && (classType.isAssignableTo(this.collectionType)) || arrayType != null) {
                JType subType = (arrayType != null) ? arrayType.getComponentType()
                        : classType.asParameterizationOf(this.collectionType.isGenericType()).getTypeArgs()[0];
                writer.println();
                writer.println(
                        " if( source." + prop.getReadMethod().getBaseMethod().getName() + "() == null ){");
                writer.println("destination.put(\"" + fieldName + "\", JSONNull.getInstance());");
                writer.println(" } else { ");
                writer.println("int i=0; JSONArray value = new JSONArray();");
                writer.println("for( " + subType.getQualifiedSourceName() + " o : source."
                        + prop.getReadMethod().getBaseMethod().getName() + "()){");
                writer.println("   value.set(i++, " + toType(subType, " o ") + ");");
                writer.println("}");
                writer.println("destination.put(\"" + fieldName + "\", value);"); //TODO JSONField
                writer.println("}");
            } else {
                writer.print("destination.put( \"" + fieldName + "\", "); //TODO JSONField
                writer.print(toType(prop.getType(),
                        " source." + prop.getReadMethod().getBaseMethod().getName() + "() ") + ");");
            }
        }
    }

    writer.outdent();
    writer.println("return destination;");
    writer.println("}");

    writer.println("public String serialize(" + r.getType().getQualifiedSourceName()
            + " source ) throws SerializationException { ");
    writer.println("   return serializeToJSONObject(source).toString();");
    writer.println("}");
}

From source file:com.totsp.gwittir.serial.json.rebind.JSONCodecGenerator.java

License:Open Source License

private void writeTopSerializer(TreeLogger logger, GeneratorContext context, JClassType typeFor,
        JClassType subType) {/*  ww w.  j  a va 2  s .c  o  m*/
    ClassSourceFileComposerFactory mcf = new ClassSourceFileComposerFactory(typeFor.getPackage().getName(),
            typeFor.getSimpleSourceName() + "_Impl");
    mcf.addImplementedInterface(typeFor.getParameterizedQualifiedSourceName());
    mcf.setSuperclass(subType.getParameterizedQualifiedSourceName() + "_JSONCodec");

    PrintWriter printWriter = context.tryCreate(logger, typeFor.getPackage().getName(),
            typeFor.getSimpleSourceName() + "_Impl");

    if (printWriter == null) {
        logger.log(Type.INFO, "Already genned " + typeFor.getSimpleSourceName() + "_Impl");

        return;
    }

    SourceWriter writer = mcf.createSourceWriter(context, printWriter);
    writer.println(" public String getMimeType() { return MIME_TYPE; }");
    writer.println("}");
    context.commit(logger, printWriter);
}

From source file:com.vaadin.server.widgetsetutils.AcceptCriteriaFactoryGenerator.java

License:Apache License

/**
 * Generate source code for WidgetMapImpl
 * // ww w.jav a 2  s . c  om
 * @param logger
 *            Logger object
 * @param context
 *            Generator context
 */
private void generateClass(TreeLogger logger, GeneratorContext context) {
    // get print writer that receives the source code
    PrintWriter printWriter = null;
    printWriter = context.tryCreate(logger, packageName, className);
    // print writer if null, source code has ALREADY been generated,
    // return (WidgetMap is equal to all permutations atm)
    if (printWriter == null) {
        return;
    }
    logger.log(Type.INFO, "Detecting available criteria ...");
    Date date = new Date();

    // init composer, set class properties, create source writer
    ClassSourceFileComposerFactory composer = null;
    composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImport("com.google.gwt.core.client.GWT");
    composer.setSuperclass("com.vaadin.client.ui.dd.VAcceptCriterionFactory");
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);

    // generator constructor source code
    generateInstantiatorMethod(sourceWriter, context, logger);
    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");
    // commit generated class
    context.commit(logger, printWriter);
    logger.log(Type.INFO, "Done. (" + (new Date().getTime() - date.getTime()) / 1000 + "seconds)");

}