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);

Source Link

Usage

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 {/* w  ww. ja v  a2 s  .c o  m*/
        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;
}

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();/* w w  w  . j  a  v a 2 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 writeMethod(TreeLogger logger, MethodWrapper method, SourceWriter writer) {
    JType ptype = this.resolveType(method.getDeclaringType());

    writer.println("new Method(){ ");
    writer.indent();//from  ww  w  .java 2  s. c  om
    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:cz.muni.ucn.opsi.wui.gwt.rebind.beanModel.BeanModelGenerator.java

License:Open Source License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    oracle = context.getTypeOracle();/*from w w  w  .jav a 2  s .c  om*/
    beanModelMarkerType = oracle.findType(BeanModelMarker.class.getName());
    beanModelTagType = oracle.findType(BeanModelTag.class.getName());

    try {
        // final all beans and bean markers
        beans = new ArrayList<JClassType>();
        JClassType[] types = oracle.getTypes();
        for (JClassType type : types) {
            if (isBeanMarker(type)) {
                beans.add(getMarkerBean(type));
            } else if (isBean(type)) {
                beans.add(type);
            }
        }

        final String genPackageName = BeanModelLookup.class.getPackage().getName();
        final String genClassName = "BeanModelLookupImpl";

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName,
                genClassName);
        composer.setSuperclass(BeanModelLookup.class.getCanonicalName());
        composer.addImport(BeanModelFactory.class.getName());
        composer.addImport(Map.class.getName());
        composer.addImport(FastMap.class.getName());

        PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName);

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

            sw.println("private Map<String, BeanModelFactory> m;");

            sw.println("public BeanModelFactory getFactory(String n) {");
            sw.indent();
            //        sw.println("String n = b.getName();");
            sw.println("if (m == null) {");
            sw.indentln("m = new FastMap<BeanModelFactory>();");
            sw.println("}");
            sw.println("if (m.get(n) == null) {");
            sw.indent();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < beans.size(); i++) {
                JClassType bean = beans.get(i);
                String name = createBean(bean, logger, context);
                String factory = createFactory(bean, name, logger, context);

                if (i > 0) {
                    sw.print(" else ");
                }
                sw.println("if (" + bean.getQualifiedSourceName() + ".CLASS_NAME.equals(n)) {");
                sw.indentln("m" + i + "();");

                sb.append("private void m" + i + "() {\n");
                sb.append(
                        "  m.put(" + bean.getQualifiedSourceName() + ".CLASS_NAME, new " + factory + "());\n");
                sb.append("}\n");

                sw.print("}");
            }
            sw.outdent();
            sw.println("}");
            sw.println("return m.get(n);");
            sw.outdent();
            sw.println("}");

            sw.println(sb.toString());
            sw.commit(logger);
        }

        return composer.getCreatedClassName();

    } catch (Exception e) {
        logger.log(TreeLogger.ERROR, "Class " + typeName + " not found.", e);
        throw new UnableToCompleteException();
    }

}

From source file:cz.muni.ucn.opsi.wui.gwt.rebind.beanModel.BeanModelGenerator.java

License:Open Source License

protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) {
    sw.println("public <X> X get(String s) {");

    sw.println("if (allowNestedValues && NestedModelUtil.isNestedProperty(s)) {");
    sw.indentln("return (X)NestedModelUtil.getNestedValue(this, s);");
    sw.println("}");

    for (JMethod method : getters) {
        JClassType returnType = method.getReturnType().isClassOrInterface();
        String s = method.getName();
        String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get or

        sw.println("if (s.equals(\"" + p + "\")) {");
        sw.println("Object value = ((" + typeName + ")bean)." + s + "();");

        try {/*  w w w.  j  a va2s  .c o m*/
            if (returnType != null && returnType.isAssignableTo(oracle.getType(List.class.getName()))
                    && returnType.isParameterized() != null) {
                JParameterizedType type = returnType.isParameterized();
                JClassType[] params = type.getTypeArgs();
                if (beans.contains(params[0])) {
                    sw.println("if (value != null) {");
                    sw.indent();
                    sw.println("java.util.List list = (java.util.List)value;");
                    sw.println("java.util.List list2 = " + BeanModelLookup.class.getCanonicalName()
                            + ".get().getFactory(" + params[0].getQualifiedSourceName()
                            + ".CLASS_NAME).createModel((java.util.Collection) list);");
                    sw.outdent();
                    sw.println("return (X) list2;");
                    sw.println("}");
                }
            } else {
                // swap returnType as generic types were not matching
                // (beans.contains(returnType))
                if (returnType != null) {
                    String t = returnType.getQualifiedSourceName();
                    if (t.indexOf("extends") == -1) {
                        returnType = oracle.getType(t);
                    }
                }
                if (beans.contains(returnType)) {
                    sw.println("if (value != null) {");
                    sw.println("    BeanModel nestedModel = nestedModels.get(s);");
                    sw.println("    if (nestedModel != null) {");
                    sw.println("      Object bean = nestedModel.getBean();");
                    sw.println("      if (!bean.equals(value)){");
                    sw.println("        nestedModel = null;");
                    sw.println("      }");
                    sw.println("    }");
                    sw.println("    if (nestedModel == null) {");
                    sw.println("        nestedModel = " + BeanModelLookup.class.getCanonicalName()
                            + ".get().getFactory(" + returnType.getQualifiedSourceName()
                            + ".CLASS_NAME).createModel(value);");
                    sw.println("        nestedModels.put(s, nestedModel);");
                    sw.println("    }");
                    sw.println("    return (X)processValue(nestedModel);");
                    sw.println("}");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        sw.println("return (X)processValue(value);");
        sw.println("}");
    }
    sw.println("return super.get(s);");
    sw.println("}");
}

From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java

License:Apache License

/**
 * @param beanWriter//from  w  w w.j a v  a  2s  . c o  m
 * @param beanModel 
 */
private void writeAsMethod(SourceWriter beanWriter, ObservableBeanModel beanModel) {
    beanWriter.println("public %s as() {", beanModel.getType().getQualifiedSourceName());
    beanWriter.indentln("return shim;");
    beanWriter.println("}");
    beanWriter.println();
}

From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java

License:Apache License

/**
 * @param beanWriter/*from w w  w.  java  2 s . co  m*/
 * @param beanModel
 */
private void writeConstructor(SourceWriter beanWriter, ObservableBeanModel beanModel) {
    beanWriter.println("public %s(%s observed) {", beanModel.getSimpleImplementationTypeName(),
            beanModel.getType().getName());
    beanWriter.indentln("super(observed);");
    beanWriter.println("}");
    beanWriter.println();
}

From source file:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

protected void writeEnsureInjected(SourceWriter sw) {
    sw.println("private boolean injected;");
    sw.println("public boolean ensureInjected() {");
    sw.indent();/*  w w  w.  j  av a  2  s  .  com*/
    sw.println("if (!injected) {");
    sw.indentln("injected = true;");
    sw.indentln(StyleInjector.class.getName() + ".inject(getText());");
    sw.indentln("return true;");
    sw.println("}");
    sw.println("return false;");
    sw.outdent();
    sw.println("}");
}

From source file:org.example.modular_mvp.base.rebind.ExtensionManagerGenerator.java

License:Open Source License

private void generateClass(TreeLogger logger, GeneratorContext context, String packageName, String className,
        List<ExtensionDefinition> extensions) {
    PrintWriter pw = context.tryCreate(logger, packageName, className);
    if (pw == null) {
        return;//from   w  w  w. j  a  v a  2s.  co m
    }

    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);

    // imports
    composerFactory.addImport(ArrayList.class.getCanonicalName());
    composerFactory.addImport(List.class.getCanonicalName());
    composerFactory.addImport(ExtensionDefinition.class.getCanonicalName());
    composerFactory.addImport(ExtensionManager.class.getCanonicalName());

    // interface
    composerFactory.addImplementedInterface(ExtensionManager.class.getCanonicalName());

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

    // begin class definition
    sw.indent();

    // fields
    sw.println("private final List<NavigatorItem> items = new ArrayList<NavigatorItem>();");

    // constructor
    sw.println("public " + className + "() {");
    sw.indent();
    for (ExtensionDefinition extension : extensions) {
        for (ExtensionDefinition.NavigatorItem item : extension.value()) {
            sw.println("items.add(new NavigatorItem(\"%s\", \"%s\"));", escape(item.name()),
                    escape(item.token()));
        }
    }
    sw.outdent();
    sw.println("}");

    // methods
    // getNavigatorItems
    sw.println("public List<NavigatorItem> getNavigatorItems() {");
    sw.indentln("return items;");
    sw.println("}");

    // close it out
    sw.outdent();
    sw.println("}");

    context.commit(logger, pw);
}

From source file:org.opencms.gwt.rebind.rpc.CmsRpcProxyCreator.java

License:Apache License

/**
 * Generates a method to check if a given RPC method has to be synchronized.<p> 
 * /*  ww  w .  j  a  v  a2 s. co  m*/
 * @param srcWriter the source write to generate the code with
 * @param syncMethToAsyncMethMap the method map
 */
protected void generateSyncOverride(SourceWriter srcWriter, Map<JMethod, JMethod> syncMethToAsyncMethMap) {

    srcWriter.println("@Override");
    srcWriter.println("public boolean isSync(String methodName) {");

    JMethod[] syncMethods = serviceIntf.getOverridableMethods();
    for (JMethod syncMethod : syncMethods) {
        JMethod asyncMethod = syncMethToAsyncMethMap.get(syncMethod);
        if (!asyncMethod.isAnnotationPresent(SynchronizedRpcRequest.class)) {
            continue;
        }
        srcWriter.indentln(
                "if (methodName.equals(\"" + getProxySimpleName() + "." + syncMethod.getName() + "\")) {");
        srcWriter.indentln("return true;");
        srcWriter.indentln("}");
    }
    srcWriter.indentln("return false;");
    srcWriter.println("}");
}