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

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

Introduction

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

Prototype

void indent();

Source Link

Usage

From source file:org.pentaho.mantle.rebind.EventBusUtilGenerator.java

License:Open Source License

private void generateConstructor(SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();
    sourceWriter.println("super();");
    sourceWriter.outdent();/*from   ww  w.j  av  a  2s  .com*/
    sourceWriter.println("}");
}

From source file:org.pentaho.ui.xul.gwt.generators.EventHandlerGenerator.java

License:Open Source License

private void generateMethods(SourceWriter sourceWriter) {

    sourceWriter.println("public void execute(String method, Object[] args) { ");
    sourceWriter.indent();

    try {/*w  ww. jav  a 2  s.c om*/
        JClassType classType = typeOracle.getType(typeName);

        do {
            for (JMethod m : classType.getMethods()) {
                String methodName = m.getName();

                if (!m.isPublic()) {
                    continue;
                }

                sourceWriter.println("if(method.equals(\"" + methodName + "\")){");
                sourceWriter.indent();

                boolean firstParam = true;
                // method call
                sourceWriter.print("handler." + methodName + "(");
                int argPos = 0;
                for (JParameter param : m.getParameters()) {
                    if (!firstParam) {
                        sourceWriter.print(", ");
                    } else {
                        firstParam = false;
                    }
                    sourceWriter.print("(" + boxPrimative(param.getType()) + ") args[" + argPos + "]");
                    argPos++;
                }
                sourceWriter.print(");");
                // end method call

                sourceWriter.println("return;");
                sourceWriter.outdent();
                sourceWriter.println("}");
            }
        } while ((classType = classType.getSuperclass()).getSimpleSourceName().equals("Object") == false);

    } catch (Exception e) {

        // record to logger that Map generation threw an exception
        logger.log(TreeLogger.ERROR, "PropertyMap ERROR!!!", e);

    }
    sourceWriter.println(
            "System.err.println(\"ERROR: method '\" + method + \"' not annotated with EventMethod.\");");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println(handlerClassName + " handler;");

    sourceWriter.println("public void setHandler(XulEventHandler handler) { ");
    sourceWriter.indent();
    sourceWriter.println("this.handler = (" + handlerClassName + ") handler;");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public XulEventHandler getHandler() { ");
    sourceWriter.indent();
    sourceWriter.println("return this.handler;");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public String getName() { ");
    sourceWriter.indent();
    sourceWriter.println("return this.handler.getName();");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public Object getData() { ");
    sourceWriter.indent();
    sourceWriter.println("return null;");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public void setData(Object o) { ");
    sourceWriter.println("}");

}

From source file:org.pentaho.ui.xul.gwt.generators.EventHandlerGenerator.java

License:Open Source License

private void generateConstructor(SourceWriter sourceWriter) {

    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();
    sourceWriter.println("super();");

    sourceWriter.outdent();/*w ww . j  a  v  a2 s.c  o  m*/
    sourceWriter.println("}");

}

From source file:org.pentaho.ui.xul.gwt.generators.TypeControllerGenerator.java

License:Open Source License

private void generateConstructor(SourceWriter sourceWriter) {

    sourceWriter.println(//  w  w w. j  av a2  s.co  m
            "public Map<String, GwtBindingMethod> wrappedTypes = new HashMap<String, GwtBindingMethod>();");

    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();
    sourceWriter.println("super();");

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

}

From source file:org.pentaho.ui.xul.gwt.generators.TypeControllerGenerator.java

License:Open Source License

private void writeMethods(SourceWriter sourceWriter) {
    sourceWriter.println("public GwtBindingMethod findGetMethod(Object obj, String propertyName){");
    sourceWriter.indent();

    sourceWriter.println(//w ww  .j a  va 2s  . c  o m
            "GwtBindingMethod retVal = findMethod(obj,\"get\"+propertyName.substring(0,1).toUpperCase()"
                    + "+propertyName.substring(1));");

    sourceWriter.println("if(retVal == null){");
    sourceWriter.indent();
    sourceWriter
            .println("retVal = findMethod(obj,\"is\"+propertyName.substring(0,1).toUpperCase()+propertyName."
                    + "substring(1));");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("return retVal;");

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

    sourceWriter.println("public GwtBindingMethod findSetMethod(Object obj, String propertyName){");
    sourceWriter.indent();

    sourceWriter.println("return findMethod(obj,\"set\"+propertyName.substring(0,1).toUpperCase()+propertyName"
            + ".substring(1));");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public GwtBindingMethod findMethod(Object obj, String propertyName){");
    sourceWriter.indent();

    sourceWriter.println("return findOrCreateMethod(obj.getClass().getName(), propertyName);");
    sourceWriter.outdent();
    sourceWriter.println("}");

    createFindMethod(sourceWriter);
}

From source file:org.pentaho.ui.xul.gwt.generators.TypeControllerGenerator.java

License:Open Source License

private void createFindMethod(SourceWriter sourceWriter) {

    // We create more than one findMethod as the body of one method would be too large. This is the int that we
    // increment to add to the name
    // i.e. findMethod0()
    int methodNum = 0;

    // This int keeps track of how many methods are generated. When it gets to 200 we create a new findMethodX()
    // and chain it to the previous.
    int methodCount = 0;

    sourceWriter.println("private GwtBindingMethod findOrCreateMethod(String obj, String methodName){ ");
    sourceWriter.indent();

    sourceWriter.println("GwtBindingMethod newMethod;");

    // dummy first condition, rest are "else if". Keeps us from having conditional logic.
    sourceWriter.println("if(false){ }");

    for (JClassType type : implementingTypes) {

        // close last method, chain it to a new one.
        if (methodCount > 200) {
            sourceWriter.println("return findOrCreateMethod" + (methodNum) + "(obj, methodName);");
            sourceWriter.println("}");

            sourceWriter.println("private GwtBindingMethod findOrCreateMethod" + (methodNum++)
                    + "(String obj, String methodName){ ");
            sourceWriter.println("GwtBindingMethod newMethod;");
            // dummy first condition, rest are "else if". Keeps us from having conditional logic.
            sourceWriter.println("if(false){ }");

            methodCount = 0;//from   w  w  w .  j  a  va 2  s .c  o m
        }

        String keyRoot = generateTypeKey(type);

        // if(type.isAbstract()){
        // System.out.println("abstract");
        // continue;
        // }

        // determine if there are any methods that are bindable before writing out conditional for class

        JClassType loopType = type;
        boolean hasBindableMethods = false;
        JClassType eventSourceType = null;
        try {
            eventSourceType = typeOracle.getType("org.pentaho.ui.xul.XulEventSource");
        } catch (NotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        //CHECKSTYLE IGNORE Indentation FOR NEXT 1 LINES
        outer: while (loopType.getSuperclass() != null
                && loopType.getSimpleSourceName().equals("Object") == false
                && loopType.isAssignableTo(eventSourceType)) {

            for (JMethod m : loopType.getMethods()) {
                if (m.isPublic() && m.getAnnotation(Bindable.class) != null) {
                    hasBindableMethods = true;
                    break outer;
                }
            }
            loopType = loopType.getSuperclass();
        }
        if (hasBindableMethods == false) {
            continue;
        }
        sourceWriter.println("else if(obj.equals(\"" + type.getQualifiedSourceName() + "\")){ ");
        try {

            loopType = type;
            sourceWriter.indent();

            // Loop over class heirarchy and generate methods for every object that is declared a XulEventSource
            while (loopType.getSuperclass() != null && loopType.getSimpleSourceName().equals("Object") == false
                    && loopType.isAssignableTo(eventSourceType)) {
                String superName = generateTypeKey(loopType);

                boolean first = true;
                for (JMethod m : loopType.getMethods()) {
                    methodCount++;
                    if (!m.isPublic() || m.getAnnotation(Bindable.class) == null) {
                        continue;
                    }

                    sourceWriter.println(
                            (first ? "" : "else ") + "if(methodName.equals(\"" + m.getName() + "\")){ ");
                    if (first) {
                        first = false;
                    }
                    sourceWriter.indent();

                    String methodName = m.getName();

                    // check to see if we've already processed this classes' method. Point to that class instead.
                    if (generatedMethods.contains((superName + "_" + methodName).toLowerCase())
                            && type != loopType) {

                        sourceWriter.println("return findOrCreateMethod(\"" + superName + "\", methodName);");

                    } else {
                        // See if it's already been created and cached. If so, return that.
                        String keyName = (keyRoot + "_" + methodName).toLowerCase();

                        sourceWriter.println("GwtBindingMethod found = wrappedTypes.get(\"" + keyName + "\");");
                        sourceWriter.println("if(found != null){");
                        sourceWriter.indent();
                        sourceWriter.println("return found;");
                        sourceWriter.outdent();
                        sourceWriter.println("} else {");
                        sourceWriter.indent();

                        // Not cached, create a new instance and put it in the cache.
                        sourceWriter.println("newMethod = new GwtBindingMethod(){");

                        sourceWriter.println(
                                "public Object invoke(Object obj, Object[] args) throws XulException { ");
                        sourceWriter.indent();
                        sourceWriter.println("try{");
                        sourceWriter.println(loopType.getQualifiedSourceName() + " target = ("
                                + loopType.getQualifiedSourceName() + ") obj;");

                        JParameter[] params = m.getParameters();
                        String argList = "";
                        int pos = 0;
                        for (JParameter param : params) {
                            if (pos > 0) {
                                argList += ", ";
                            }
                            argList += "(" + getTypeName(param.getType()) + ") args[" + pos + "]";
                            pos++;
                        }

                        if (isVoidReturn(m.getReturnType())) {
                            sourceWriter.println("target." + methodName + "(" + argList + ");");
                            sourceWriter.println("return null;");
                        } else {
                            sourceWriter.println("return " + boxReturnType(m) + " target." + methodName + "("
                                    + argList + ");");
                        }

                        sourceWriter.println(
                                "}catch(Exception e){ e.printStackTrace(); throw new XulException(\"error with "
                                        + type.getQualifiedSourceName() + "\"+e.getMessage());}");
                        sourceWriter.println("}");

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

                        // Add it to the HashMap cache as type and decendant type if available.
                        sourceWriter.println("wrappedTypes.put((\"" + keyName + "\"), newMethod);");
                        if (keyRoot.equals(superName) == false) {
                            sourceWriter.println("wrappedTypes.put((\"" + keyName + "\"), newMethod);");
                        }
                        generatedMethods.add((keyRoot + "_" + methodName).toLowerCase());
                        generatedMethods.add((superName + "_" + methodName).toLowerCase());

                        sourceWriter.println("return newMethod;");

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

                    sourceWriter.println("}");

                }

                // go up a level in the heirarchy and check again.
                loopType = loopType.getSuperclass();
            }
            sourceWriter.outdent();
            sourceWriter.println("}");

        } catch (Exception e) {

            // record to logger that Map generation threw an exception
            logger.log(TreeLogger.ERROR, "PropertyMap ERROR!!!", e);

        }

    }

    sourceWriter.outdent();

    // This is the end of the line, if not found return null.
    sourceWriter.println("return null;");
    sourceWriter.println("}");
}

From source file:org.rapla.rest.gwtjsonrpc.rebind.ProxyCreator.java

License:Apache License

private void generateProxyConstructor(@SuppressWarnings("unused") final TreeLogger logger,
        final SourceWriter w) {
    final RemoteServiceRelativePath relPath = svcInf.getAnnotation(RemoteServiceRelativePath.class);
    if (relPath != null) {
        w.println();//from  ww  w. j  a  v a 2 s.  com
        w.println("public " + getProxySimpleName() + "() {");
        w.indent();
        w.println("setServiceEntryPoint(GWT.getModuleBaseURL() + \"" + relPath.value() + "\");");
        w.outdent();
        w.println("}");
    }
}

From source file:org.rapla.rest.gwtjsonrpc.rebind.ProxyCreator.java

License:Apache License

private void generateProxyCallCreator(final TreeLogger logger, final SourceWriter w)
        throws UnableToCompleteException {
    String callName = getJsonCallClassName(logger);
    w.println();/*from  ww w.j  a va2 s  .c o m*/
    w.println("@Override");
    w.print("protected <T> ");
    w.print(callName);
    w.print("<T> newJsonCall(final AbstractJsonProxy proxy, ");
    w.print("final String methodName, final String reqData, ");
    w.println("final ResultDeserializer<T> ser) {");
    w.indent();

    w.print("return new ");
    w.print(callName);
    w.println("<T>(proxy, methodName, reqData, ser);");

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

From source file:org.rapla.rest.gwtjsonrpc.rebind.ProxyCreator.java

License:Apache License

private void generateProxyMethod(@SuppressWarnings("unused") final TreeLogger logger, final JMethod method,
        final SourceWriter w) {
    final JParameter[] params = method.getParameters();
    final JType callback = method.getReturnType();// params[params.length - 1];
    JType resultType = callback;//from w  w  w.j  a  v  a  2 s . co  m
    //    final JClassType resultType =
    //        callback.isParameterized().getTypeArgs()[0];
    final String[] serializerFields = new String[params.length];
    String resultField = "";

    w.println();
    for (int i = 0; i < params.length /*- 1*/; i++) {
        final JType pType = params[i].getType();
        if (SerializerCreator.needsTypeParameter(pType)) {
            serializerFields[i] = "serializer_" + instanceField++;
            w.print("private static final ");
            if (pType.isArray() != null)
                w.print(serializerCreator.serializerFor(pType));
            else
                w.print(JsonSerializer.class.getName());
            w.print(" ");
            w.print(serializerFields[i]);
            w.print(" = ");
            serializerCreator.generateSerializerReference(pType, w, false);
            w.println(";");
        }
    }
    JClassType parameterizedResult = null;
    if (resultType.isParameterized() != null) {
        resultField = "serializer_" + instanceField++;
        w.print("private static final ");
        w.print(ResultDeserializer.class.getName());
        w.print(" ");
        w.print(resultField);
        w.print(" = ");
        parameterizedResult = resultType.isParameterized().getTypeArgs()[0];
        serializerCreator.generateSerializerReference(parameterizedResult, w, false);
        w.println(";");
    }

    w.print("public ");
    w.print(method.getReturnType().getQualifiedSourceName());
    w.print(" ");
    w.print(method.getName());
    w.print("(");
    boolean needsComma = false;
    final NameFactory nameFactory = new NameFactory();
    for (int i = 0; i < params.length; i++) {
        final JParameter param = params[i];

        if (needsComma) {
            w.print(", ");
        } else {
            needsComma = true;
        }

        final JType paramType = param.getType().getErasedType();
        w.print(paramType.getQualifiedSourceName());
        w.print(" ");

        nameFactory.addName(param.getName());
        w.print(param.getName());
    }

    w.println(") {");
    w.indent();

    if (returnsCallbackHandle(method)) {
        w.print("return new ");
        w.print(CallbackHandle.class.getName());
        w.print("(");
        if (SerializerCreator.needsTypeParameter(resultType)) {
            w.print(resultField);
        } else {
            deserializerCreator.generateDeserializerReference(resultType, w);
        }
        w.print(", " + "null" // callback.getName()
        );
        w.println(");");
        w.outdent();
        w.println("}");
        return;
    }

    //    final HostPageCache hpc = method.getAnnotation(HostPageCache.class);
    //    if (hpc != null) {
    //      final String objName = nameFactory.createName("cached");
    //      w.print("final JavaScriptObject " + objName + " = ");
    //      w.print(AbstractJsonProxy.class.getName());
    //      w.print(".");
    //      w.print(hpc.once() ? "hostPageCacheGetOnce" : "hostPageCacheGetMany");
    //      w.println("(\"" + hpc.name() + "\");");
    //      w.println("if (" + objName + " != null) {");
    //      w.indent();
    //      w.print(JsonUtil.class.getName());
    //      w.print(".invoke(");
    //      if (SerializerCreator.needsTypeParameter(resultType)) {
    //        w.print(resultField);
    //      } else {
    //        deserializerCreator.generateDeserializerReference(resultType, w);
    //      }
    //     // w.print(", " + callback.getName());
    //      w.print(", " + "null");
    //      w.print(", " + objName);
    //      w.println(");");
    //      w.println("return;");
    //      w.outdent();
    //      w.println("}");
    //    }

    final String reqDataStr;
    if (params.length == 0) {
        reqDataStr = "\"[]\"";
    } else {
        final String reqData = nameFactory.createName("reqData");
        w.println("final StringBuilder " + reqData + " = new StringBuilder();");
        needsComma = false;
        w.println(reqData + ".append('[');");
        for (int i = 0; i < params.length; i++) {
            if (needsComma) {
                w.println(reqData + ".append(\",\");");
            } else {
                needsComma = true;
            }

            final JType pType = params[i].getType();
            final String pName = params[i].getName();
            if (pType == JPrimitiveType.CHAR || SerializerCreator.isBoxedCharacter(pType)) {
                w.println(reqData + ".append(\"\\\"\");");
                w.println(reqData + ".append(" + JsonSerializer.class.getSimpleName() + ".escapeChar(" + pName
                        + "));");
                w.println(reqData + ".append(\"\\\"\");");
            } else if ((SerializerCreator.isJsonPrimitive(pType) || SerializerCreator.isBoxedPrimitive(pType))
                    && !SerializerCreator.isJsonString(pType)) {
                w.println(reqData + ".append(" + pName + ");");
            } else {
                w.println("if (" + pName + " != null) {");
                w.indent();
                if (SerializerCreator.needsTypeParameter(pType)) {
                    w.print(serializerFields[i]);
                } else {
                    serializerCreator.generateSerializerReference(pType, w, false);
                }
                w.println(".printJson(" + reqData + ", " + pName + ");");
                w.outdent();
                w.println("} else {");
                w.indent();
                w.println(reqData + ".append(" + JsonSerializer.class.getName() + ".JS_NULL);");
                w.outdent();
                w.println("}");
            }
        }
        w.println(reqData + ".append(']');");
        reqDataStr = reqData + ".toString()";
    }

    String resultClass = futureResultClassName;
    if (parameterizedResult != null) {
        resultClass += "<" + parameterizedResult.getQualifiedSourceName() + ">";
    }
    w.println(resultClass + " result = new " + resultClass + "();");
    w.print("doInvoke(");
    w.print("\"" + method.getName() + "\"");
    w.print(", " + reqDataStr);
    w.print(", ");
    if (resultType.isParameterized() != null) {
        w.print(resultField);
    } else {
        deserializerCreator.generateDeserializerReference(resultType, w);
    }

    //w.print(", " + callback.getName());
    w.print(", result");

    w.println(");");
    w.println("return result;");

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

From source file:org.rapla.rest.gwtjsonrpc.rebind.ResultDeserializerCreator.java

License:Apache License

private void generateFromResult(SourceWriter w) {
    final String ctn = componentType.getQualifiedSourceName();

    w.println("@Override");
    w.print("public " + ctn + "[] ");
    w.println("fromResult(JavaScriptObject responseObject) {");
    w.indent();

    w.print("final " + ctn + "[] tmp = new " + ctn);
    w.println("[getResultSize(responseObject)];");

    w.println("serializer.fromJson(getResult(responseObject), tmp);");
    w.println("return tmp;");
    w.outdent();//from  w  ww .jav  a  2s.co m

    w.println("}");
}