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:cc.alcina.framework.entity.gwtsynth.ClientReflectionGenerator.java

License:Apache License

private void writeAnnotations(TreeLogger logger, GeneratorContext context, List<JAnnotationType> jAnns,
        ClassSourceFileComposerFactory crf, boolean initial) throws Exception {
    for (JAnnotationType type : jAnns) {
        Class<? extends Annotation> annClass = forName(type);
        String implementationName = type.getName() + "_Impl";
        ann2impl.put(annClass, type.getPackage().getName() + "." + implementationName);
    }/*w w  w  . jav  a 2s .c  o m*/
    for (JAnnotationType type : jAnns) {
        Class<? extends Annotation> annClass = forName(type);
        String implementationName = type.getName() + "_Impl";
        String implFQN = type.getPackage().getName() + "." + implementationName;
        crf.addImport(implFQN);
        ClassSourceFileComposerFactory annf = new ClassSourceFileComposerFactory(type.getPackage().getName(),
                implementationName);
        annf.addImport(Annotation.class.getCanonicalName());
        annf.addImport(type.getQualifiedSourceName());
        annf.addImplementedInterface(type.getName());
        List<Method> declaredMethods = new ArrayList<Method>(Arrays.asList(annClass.getDeclaredMethods()));
        Collections.sort(declaredMethods, ToStringComparator.INSTANCE);
        StringBuffer constrParams = new StringBuffer();
        boolean first = true;
        for (Method method : declaredMethods) {
            Class<?> returnType = method.getReturnType();
            addImport(annf, returnType);
            addImport(crf, returnType);
        }
        PrintWriter printWriter = context.tryCreate(logger, packageName, implementationName);
        // if calling from a non-initial module, we just want to add imports
        // without rewriting (indeed, we can't...) the annotation impls
        if (printWriter != null) {
            SourceWriter sw = createWriter(annf, printWriter);
            for (Method method : declaredMethods) {
                Class returnType = method.getReturnType();
                String rn = returnType.getSimpleName();
                String mn = method.getName();
                StringBuffer sb = new StringBuffer();
                writeLiteral(method.getDefaultValue(), returnType, sb, true);
                sw.println(String.format("private %s %s = %s;", rn, mn, sb.toString()));
                sw.println(String.format("public %s %s(){return %s;}", rn, mn, mn));
                sw.println(String.format("public %s _set%s(%s %s){this.%s = %s;return this;}",
                        implementationName, mn, rn, mn, mn, mn));
                sw.println();
            }
            sw.println();
            sw.println("public Class<? extends Annotation> annotationType() {");
            sw.indentln(String.format("return %s.class;", annClass.getSimpleName()));
            sw.println("}");
            sw.println();
            sw.println(String.format("public %s (){}", implementationName));
            sw.outdent();
            sw.println("}");
            commit(context, logger, printWriter);
        }
    }
}

From source file:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    oracle = context.getTypeOracle();/*ww w.  jav  a  2s  .  c  o  m*/
    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(Class b) {");
            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.getName().equals(n)) {");
                sw.indentln("m" + i + "();");

                sb.append("private void m" + i + "() {\n");
                sb.append("  m.put(" + bean.getQualifiedSourceName() + ".class.getName(), 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:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) {
    sw.println("public <X> X getPropertyAsString(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  ww .j  av a2 s.co 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).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).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.getFromCache(s);");
    sw.println("}");
}

From source file:com.ait.toolkit.rebind.BeanModelGenerator.java

License:Open Source License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    oracle = context.getTypeOracle();/* ww  w .j a  v a 2  s .  c om*/
    beanModelMarkerType = oracle.findType(BeanMarker.class.getName());
    beanModelTagType = oracle.findType(BeanTag.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 = BeanLookup.class.getPackage().getName();
        final String genClassName = "BeanLookupImpl";

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName,
                genClassName);
        composer.setSuperclass(BeanLookup.class.getCanonicalName());
        composer.addImport(BeanFactory.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, BeanFactory> m;");

            sw.println("public BeanFactory getFactory(Class b) {");
            sw.indent();
            sw.println("String n = b.getName();");
            sw.println("if (m == null) {");
            sw.indentln("m = new FastMap<BeanFactory>();");
            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.getName().equals(n)) {");
                sw.indentln("m" + i + "();");

                sb.append("private void m" + i + "() {\n");
                sb.append("  m.put(" + bean.getQualifiedSourceName() + ".class.getName(), 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:com.ait.toolkit.rebind.BeanModelGenerator.java

License:Open Source License

protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) {
    sw.println("public <X> X getPropertyAsString(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//from   ww w. j  a  v a 2  s. com

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

        try {
            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 = " + BeanLookup.class.getCanonicalName()
                            + ".get().getFactory(" + params[0].getQualifiedSourceName()
                            + ".class).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("    Bean 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 = " + BeanLookup.class.getCanonicalName()
                            + ".get().getFactory(" + returnType.getQualifiedSourceName()
                            + ".class).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.getFromCache(s);");
    sw.println("}");
}

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

License:Apache License

public void create(TreeLogger logger, GeneratorContext context) throws UnableToCompleteException {
    String typeName = this.serverType.getQualifiedSourceName();

    String packageName = getPackageName();
    String simpleName = getSimpleName();

    TypeOracle oracle = context.getTypeOracle();

    PrintWriter pw = context.tryCreate(logger, packageName, simpleName);
    if (pw == null) {
        return;//from   ww w . j a va  2  s  . com
    }
    JClassType serverType = oracle.findType(Name.getSourceNameForClass(Server.class));
    JClassType clientType = ModelUtils.findParameterizationOf(serverType, this.serverType)[1];

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

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

    //TODO move this check before the printwriter creation can fail, and allow the warn to be optional
    sw.println("public %1$s(%2$s errorHandler) {", simpleName,
            Name.getSourceNameForClass(ServerBuilder.ConnectionErrorHandler.class));
    RemoteServiceRelativePath path = this.serverType.getAnnotation(RemoteServiceRelativePath.class);
    if (path == null) {
        //         logger.log(Type.WARN, "@RemoteServiceRelativePath required on " + typeName + " to make a connection to the server without a ServerBuilder");
        //         throw new UnableToCompleteException();
        sw.indentln("super(null);");
        sw.indentln(
                "throw new RuntimeException(\"@RemoteServiceRelativePath annotation required on %1$s to make a connection without a path defined in ServerBuilder\");");
    } else {
        sw.indentln("super(errorHandler, "
                + "com.google.gwt.user.client.Window.Location.getProtocol().toLowerCase().startsWith(\"https\") ? \"wss://\": \"ws://\", "
                + "com.google.gwt.user.client.Window.Location.getHost(), \"%1$s\");", path.value());
    }
    sw.println("}");

    sw.println("public %1$s(%2$s errorHandler, String url) {", simpleName,
            Name.getSourceNameForClass(ServerBuilder.ConnectionErrorHandler.class));
    sw.indentln("super(errorHandler, url);");
    sw.println("}");

    //Find all types that may go over the wire
    // Collect the types the server will send to the client using the Client interface
    SerializableTypeOracleBuilder serverSerializerBuilder = new SerializableTypeOracleBuilder(logger, context);
    appendMethodParameters(logger, clientType, Client.class, serverSerializerBuilder);
    // Also add the wrapper object ClientInvocation
    serverSerializerBuilder.addRootType(logger, oracle.findType(ClientInvocation.class.getName()));
    serverSerializerBuilder.addRootType(logger, oracle.findType(ClientCallbackInvocation.class.getName()));

    // Collect the types the client will send to the server using the Server interface
    SerializableTypeOracleBuilder clientSerializerBuilder = new SerializableTypeOracleBuilder(logger, context);
    appendMethodParameters(logger, this.serverType, Server.class, clientSerializerBuilder);
    // Also add the ServerInvocation wrapper
    clientSerializerBuilder.addRootType(logger, oracle.findType(ServerInvocation.class.getName()));
    clientSerializerBuilder.addRootType(logger, oracle.findType(ServerCallbackInvocation.class.getName()));

    String tsName = simpleName + "_TypeSerializer";
    TypeSerializerCreator serializerCreator = new TypeSerializerCreator(logger,
            clientSerializerBuilder.build(logger), serverSerializerBuilder.build(logger), context,
            packageName + "." + tsName, tsName);
    serializerCreator.realize(logger);

    // Make the newly created Serializer available at runtime
    sw.println("protected %1$s __getSerializer() {", Serializer.class.getName());
    sw.indentln("return %2$s.<%1$s>create(%1$s.class);", tsName, GWT.class.getName());
    sw.println("}");

    // Build methods that call from the client to the server
    for (JMethod m : this.serverType.getInheritableMethods()) {
        if (isRemoteMethod(m, Server.class)) {
            printServerMethodBody(logger, context, sw, m);
        }
    }

    // Read incoming calls and dispatch them to the correct client method
    sw.println("protected void __invoke(String method, Object[] params) {");
    for (JMethod m : clientType.getInheritableMethods()) {
        if (isRemoteMethod(m, Client.class)) {
            JParameter[] params = m.getParameters();
            sw.println("if (method.equals(\"%1$s\") && params.length == %2$d) {", m.getName(), params.length);
            sw.indent();
            sw.println("getClient().%1$s(", m.getName());
            sw.indent();
            for (int i = 0; i < params.length; i++) {
                if (i != 0) {
                    sw.print(",");
                }
                sw.println("(%1$s)params[%2$d]", params[i].getType().getQualifiedSourceName(), i);
            }
            sw.outdent();
            sw.println(");");
            sw.outdent();
            sw.println("}");
        }
    }
    sw.println("}");

    sw.println("protected void __onError(Exception error) {");
    sw.println("}");

    sw.commit(logger);
}

From source file:com.emitrom.pilot.core.shared.rebind.BeanModelGenerator.java

License:Apache License

protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) {
    sw.println("public <X> X getPropertyAsString(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 {/*from w w w. j a  v a  2 s.c  om*/
            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 = " + BeanLookup.class.getCanonicalName()
                            + ".get().getFactory(" + params[0].getQualifiedSourceName()
                            + ".class).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("    Bean 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 = " + BeanLookup.class.getCanonicalName()
                            + ".get().getFactory(" + returnType.getQualifiedSourceName()
                            + ".class).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.getFromCache(s);");
    sw.println("}");
}

From source file:com.emitrom.touch4j.rebind.BeanModelGenerator.java

License:Open Source License

protected void createGetMethods(List<JMethod> getters, SourceWriter sw, String typeName) {
    sw.println("public <X> X getPropertyAsString(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//from www  . j av  a  2s  . c  om

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

        try {
            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).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).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.getFromCache(s);");
    sw.println("}");
}

From source file:com.extjs.gxt.ui.rebind.core.BeanModelGenerator.java

License:sencha.com 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 {//from  www. j a v  a2  s  .  co 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).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).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:com.gafactory.core.rebind.ValueProviderCreator.java

License:sencha.com license

@Override
protected void create(SourceWriter sw) throws UnableToCompleteException {
    sw.println("public static final %1$s INSTANCE = new %1$s();", getSimpleName());
    // @Override// ww w. ja  v a2s.  c  o  m
    sw.println("public %1$s getValue(%2$s object) {", getValueTypeName(), getObjectTypeName());
    String getter = getGetterExpression("object");
    if (getter == null) {
        if (readability == RequiredReadability.NEITHER || readability == RequiredReadability.SET) {
            // getter is not required, but log it if someone tries to call it
            getLogger().log(Type.DEBUG,
                    "Getter could not be found (and apparently not needed), writting a log message to indicate that this is probably an error.");
            sw.indentln("com.google.gwt.core.client.GWT.log(\"Getter was called on "
                    + supertypeToImplement.getName() + ", but no getter exists.\", new RuntimeException());");
            sw.indentln("return null;");
        } else {
            getLogger().log(Type.ERROR, "No getter can be found, unable to proceed");
            throw new UnableToCompleteException();
        }
    } else {
        sw.indentln("return %1$s;", getter);
    }
    sw.println("}");

    // @Override
    sw.println("public void setValue(%1$s object, %2$s value) {", getObjectTypeName(), getValueTypeName());
    String setter = getSetterExpression("object", "value");
    if (setter == null) {
        if (readability == RequiredReadability.NEITHER || readability == RequiredReadability.GET) {
            // setter is not required, but log it if someone tries to call it
            getLogger().log(Type.DEBUG,
                    "Setter could not be found (and apparently not needed), writing a log message to indicate that this is probably an error.");
            sw.indentln("com.google.gwt.core.client.GWT.log(\"Setter was called on "
                    + supertypeToImplement.getName() + ", but no setter exists.\", new RuntimeException());");
        } else {
            getLogger().log(Type.ERROR, "No setter can be found, unable to proceed");
            throw new UnableToCompleteException();
        }
    } else {
        sw.indentln("%1$s;", setter);
    }
    sw.println("}");

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

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