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:cz.muni.ucn.opsi.wui.gwt.rebind.beanModel.BeanModelGenerator.java

License:Open Source License

protected String createBean(JClassType bean, TreeLogger logger, GeneratorContext context) throws Exception {
    final String genPackageName = bean.getPackage().getName();
    final String genClassName = "BeanModel_" + bean.getQualifiedSourceName().replace(".", "_");

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName, genClassName);
    composer.setSuperclass(BeanModelE.class.getCanonicalName());
    composer.addImport(BeanModelE.class.getName());
    composer.addImport(BeanModel.class.getName());
    composer.addImport(NestedModelUtil.class.getName());
    PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName);

    if (pw != null) {
        List<JMethod> getters = findGetters(bean);
        List<JMethod> setters = findSetters(bean);
        SourceWriter sw = composer.createSourceWriter(context, pw);

        sw.println("public " + genClassName + "(){");
        for (JMethod method : getters) {
            String s = method.getName();
            String p = lowerFirst(s.substring(s.startsWith("g") ? 3 : 2)); // get or
            // is
            sw.println("beanProperties.add(\"" + p + "\");");
        }/*  w w w. j  av a  2  s.  c o m*/
        sw.println("}");

        createGetMethods(getters, sw, bean.getQualifiedSourceName());
        createSetMethods(setters, sw, bean.getQualifiedSourceName());

        // delegate equals to bean
        sw.println("public boolean equals(Object obj) {");
        sw.println("  if (obj instanceof " + "BeanModel" + ") {");
        sw.println("    obj = ((BeanModel)obj).getBean();");
        sw.println("  }");
        sw.println("  if (bean == null && obj == null) {");
        sw.println("      return true;");
        sw.println("  }");
        sw.println("  if (bean == null && obj != null) {");
        sw.println("      return false;");
        sw.println("  }");
        sw.println("  return bean.equals(obj);");
        sw.println("}");

        // delegate hashCode to bean
        sw.println("public int hashCode(){");
        sw.println("  return bean.hashCode();");
        sw.println("}");

        sw.commit(logger);
    }
    return composer.getCreatedClassName();
}

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 {/*from w w  w  . jav  a 2 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_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.ws.rebind.filter.serialization.GWTSerializerGenerator.java

License:Open Source License

/**
 * Generate any fields required by the proxy.
 *///ww w.ja v a2  s  .c om
private void generateFields(SourceWriter srcWriter, SerializableTypeOracle serializableTypeOracle,
        String serializationPolicyStrongName, String remoteServiceInterfaceName,
        JClassType serializerInterface) {
    // Initialize a field with binary name of the remote service interface
    srcWriter.println(
            "private static final String INTERFACE_NAME = " + "\"" + remoteServiceInterfaceName + "\";");
    srcWriter.println(
            "private static final String SERIALIZATION_POLICY =\"" + serializationPolicyStrongName + "\";");
    String typeSerializerName = SerializationUtils.getTypeSerializerQualifiedName(serializerInterface);
    srcWriter.println(
            "private static final " + typeSerializerName + " SERIALIZER = new " + typeSerializerName + "();");
    srcWriter.println();
}

From source file:de.csenk.gwt.ws.rebind.filter.serialization.GWTSerializerGenerator.java

License:Open Source License

/**
 * Generate the proxy constructor and delegate to the superclass constructor
 * using the default address for the/*from www. j  av a 2s .  c om*/
 * {@link com.google.gwt.user.client.rpc.RemoteService RemoteService}.
 */
private void generateContructor(SourceWriter srcWriter, JClassType serializerInterface,
        Map<JType, String> typeStrings) {
    srcWriter.println("public " + getImplementationSimpleName(serializerInterface) + "() {");
    srcWriter.indent();
    srcWriter.println("super(GWT.getModuleBaseURL(),");
    srcWriter.indent();
    srcWriter.println("SERIALIZATION_POLICY, ");
    srcWriter.println("SERIALIZER);");
    srcWriter.outdent();

    srcWriter.println();
    for (Entry<JType, String> typeEntry : typeStrings.entrySet()) {
        srcWriter.print("typeStringMap.put(\"");
        srcWriter.print(typeEntry.getKey().getQualifiedSourceName() + "\", \"");
        srcWriter.print(typeEntry.getValue());
        srcWriter.println("\");");
    }

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

From source file:de.knightsoftnet.navigation.rebind.VersionInfoGenerator.java

License:Apache License

@Override
public final String generate(final TreeLogger plogger, final GeneratorContext pcontext, final String ptypeName)
        throws UnableToCompleteException {
    try {/*  w  w w  .  j ava  2  s  .  co m*/
        final JClassType classType = pcontext.getTypeOracle().getType(ptypeName);

        // Here you would retrieve the metadata based on typeName for this class
        final SourceWriter src = this.getSourceWriter(classType, pcontext, plogger);

        if (src != null) {
            final ResourceBundle bundle = ResourceBundle.getBundle("Version");
            final String version = bundle.getString("application.version");
            final String buildTimeString = bundle.getString("build.timestamp");
            final String copyright = bundle.getString("application.copyright");
            final String author = bundle.getString("application.author");

            src.println("@Override");
            src.println("public final SafeHtml getCopyrightText() {");
            src.println("  return SafeHtmlUtils.fromString(\" " + copyright + "\");");
            src.println("}");

            src.println("@Override");
            src.println("public final SafeHtml getVersionNumber() {");
            src.println("  return SafeHtmlUtils.fromString(\"" + version + "\");");
            src.println("}");

            src.println("@Override");
            src.println("public final SafeHtml getVersionDate() {");
            src.println(
                    "  return SafeHtmlUtils.fromString(this.parseAndFormatDate(\"" + buildTimeString + "\"));");
            src.println("}");

            src.println("@Override");
            src.println("public final SafeHtml getAuthor() {");
            src.println("  return SafeHtmlUtils.fromString(\"" + author + "\");");
            src.println("}");

            src.commit(plogger);

            System.out.println("Generating for: " + ptypeName);
        }
        return ptypeName + "Generated";
    } catch (final NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.knightsoftnet.validators.rebind.BeanHelperCache.java

License:Apache License

/**
 * Write an Empty Interface implementing
 * {@link de.knightsoftnet.validators.client.impl.GwtSpecificValidator} with Generic parameter of
 * the bean type./*from w  w  w. j  a  v a  2s .  c  om*/
 */
private void writeInterface(final GeneratorContext context, final TreeLogger logger, final BeanHelper bean) {
    final PrintWriter pw = context.tryCreate(logger, bean.getPackage(), bean.getValidatorName());
    if (pw != null) {
        final TreeLogger interfaceLogger = logger.branch(TreeLogger.TRACE,
                "Creating the interface for " + bean.getFullyQualifiedValidatorName());

        final ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(bean.getPackage(),
                bean.getValidatorName());
        factory.addImplementedInterface(
                GwtSpecificValidator.class.getCanonicalName() + " <" + bean.getTypeCanonicalName() + ">");
        factory.addImport(GWT.class.getCanonicalName());
        factory.makeInterface();
        final SourceWriter sw = factory.createSourceWriter(context, pw);

        // static MyValidator INSTANCE = GWT.create(MyValidator.class);
        sw.print("static ");
        sw.print(bean.getValidatorName());
        sw.print(" INSTANCE = GWT.create(");
        sw.print(bean.getValidatorName());
        sw.println(".class);");

        sw.commit(interfaceLogger);
        pw.close();
    }
}

From source file:de.knightsoftnet.validators.rebind.GwtReflectGetterGenerator.java

License:Apache License

@Override
public final String generate(final TreeLogger plogger, final GeneratorContext pcontext, final String ptypeName)
        throws UnableToCompleteException {
    try {/*from   w  w  w . j av  a 2  s  .c  o m*/
        plogger.log(TreeLogger.DEBUG, "Start generating for " + ptypeName + ".");

        final JClassType classType = pcontext.getTypeOracle().getType(ptypeName);
        final TypeOracle typeOracle = pcontext.getTypeOracle();
        assert typeOracle != null;
        final JClassType reflectorType = this.findType(plogger, typeOracle, ptypeName);

        // here you would retrieve the metadata based on typeName for this class
        final SourceWriter src = this.getSourceWriter(classType, pcontext, plogger);

        // generator is called more then once in a build, with second call, we don't get
        // a writer and needn't generate the class once again
        if (src != null) {
            final Class<?>[] classes = this.getBeans(plogger, reflectorType);

            src.println("@Override");
            src.println("public final Object getProperty(final Object pbean, final String pname)"
                    + " throws NoSuchMethodException, ReflectiveOperationException {");

            src.println("  if (pbean == null) {");
            src.println("    throw new NoSuchMethodException(\"A null object has no getters\");");
            src.println("  }");
            src.println("  if (pname == null) {");
            src.println("    throw new NoSuchMethodException(\"No method to get property for null\");");
            src.println("  }");
            src.println(StringUtils.EMPTY);

            for (final Class<?> clazz : classes) {
                final String className = clazz.getName();
                plogger.log(TreeLogger.DEBUG, "Generating getter reflections for class " + className);

                // Describe the bean properties
                final PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(clazz);

                src.println("  if (pbean.getClass() == " + className + ".class) {");
                src.println("    switch (pname) {");

                // for all getters generate a case and return entry
                for (final PropertyDescriptor property : properties) {

                    final Method readMethod = property.getReadMethod();
                    final String name = property.getName();
                    if (readMethod == null) {
                        continue; // If the property cannot be read
                    }
                    plogger.log(TreeLogger.DEBUG, "Add getter for property " + name);

                    // Invoke the getter on the bean
                    src.println("      case \"" + name + "\":");
                    src.println("        return ((" + className + ") pbean)." + readMethod.getName() + "();");
                }

                src.println("      default:");
                src.println("        throw new NoSuchMethodException(\"Class " + className
                        + " has no getter for porperty \" + pname);");
                src.println("    }");
                src.println("  }");
            }
            src.println("  throw new ReflectiveOperationException(\"Class \" + "
                    + "pbean.getClass().getName() + \" is not reflected\");");
            src.println("}");

            plogger.log(TreeLogger.DEBUG, "End of generating reached");

            src.commit(plogger);
        }
        return this.getClassPackage(classType) + "." + this.getClassName(classType);
    } catch (final NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

protected void writeUnsafeNativeLongIfNeeded(final SourceWriter sw, final JType jtype) {
    if (JPrimitiveType.LONG.equals(jtype)) {
        // @com.google.gwt.core.client.UnsafeNativeLong
        sw.print("@");
        sw.println(UnsafeNativeLong.class.getCanonicalName());
    }//from ww w.ja  v a 2s. c om
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeBeanDescriptor(final SourceWriter sw) {
    final BeanDescriptor beanDescriptor = this.beanHelper.getBeanDescriptor();

    // private final GwtBeanDescriptor <MyBean> beanDescriptor =
    sw.print("private final ");
    sw.print(GwtBeanDescriptor.class.getCanonicalName());
    sw.print("<" + this.beanHelper.getTypeCanonicalName() + ">");
    sw.println(" beanDescriptor = ");
    sw.indent();/*ww  w.  j a  v  a 2  s  .  c o m*/
    sw.indent();

    // GwtBeanDescriptorImpl.builder(Order.class)
    sw.print(GwtBeanDescriptorImpl.class.getCanonicalName());
    sw.println(".builder(" + this.beanHelper.getTypeCanonicalName() + ".class)");
    sw.indent();
    sw.indent();

    // .setConstrained(true)
    sw.println(".setConstrained(" + beanDescriptor.isBeanConstrained() + ")");

    int count = 0;
    for (final ConstraintDescriptor<?> constraint : beanDescriptor.getConstraintDescriptors()) {
        if (this.areConstraintDescriptorGroupsValid(constraint)) {
            // .add(c0)
            sw.println(".add(" + this.constraintDescriptorVar("this", count) + ")");
            count++;
        }
    }

    // .put("myProperty", myProperty_pd)
    for (final PropertyDescriptor p : beanDescriptor.getConstrainedProperties()) {
        sw.print(".put(\"");
        sw.print(p.getPropertyName());
        sw.print("\", ");
        sw.print(p.getPropertyName());
        sw.println("_pd)");
    }

    // .setBeanMetadata(beanMetadata)
    sw.println(".setBeanMetadata(beanMetadata)");

    // .build();
    sw.println(".build();");
    sw.outdent();
    sw.outdent();
    sw.outdent();
    sw.outdent();
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeBeanMetadata(final SourceWriter sw) throws UnableToCompleteException {
    // private final BeanMetadata beanMetadata =
    sw.println("private final BeanMetadata beanMetadata =");
    sw.indent();/*from  w  ww .  j a  va  2 s. com*/
    sw.indent();

    // new BeanMetadata(
    sw.println("new " + BeanMetadata.class.getSimpleName() + "(");
    sw.indent();
    sw.indent();

    // <<bean class>>, <<default group seq class 1>>, <<default group seq class 2>>, ...
    final Class<?> beanClazz = this.beanHelper.getClazz();
    sw.print(asLiteral(beanClazz));
    final GroupSequence groupSeqAnnotation = beanClazz.getAnnotation(GroupSequence.class);
    final List<Class<?>> groupSequence = new ArrayList<>();
    if (groupSeqAnnotation == null) {
        groupSequence.add(beanClazz);
    } else {
        groupSequence.addAll(Arrays.asList(groupSeqAnnotation.value()));
    }
    boolean groupSequenceContainsDefault = false;
    for (final Class<?> group : groupSequence) {
        sw.println(",");
        if (group.getName().equals(beanClazz.getName())) {
            sw.print(asLiteral(Default.class));
            groupSequenceContainsDefault = true;
        } else if (group.getName().equals(Default.class.getName())) {
            throw error(this.logger, "'Default.class' cannot appear in default group sequence list.");
        } else {
            sw.print(asLiteral(group));
        }
    }
    if (!groupSequenceContainsDefault) {
        throw error(this.logger,
                beanClazz.getName() + " must be part of the redefined default group " + "sequence.");
    }

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