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:org.gwt.json.serialization.InternalSerializerGenerator.java

License:Apache License

public String generateSerializers(SourceWriter writer) throws NotFoundException {

    Set<JField> candidates = AccessorResolveUtil.resolveFieldCandidates(jClassType);

    List<FieldInfo> resolvedFields = new ArrayList<FieldInfo>();
    for (JField jField : candidates) {
        resolvedFields.add(AccessorResolveUtil.resolveAccessor(jField, jClassType));
    }// w w w .  ja  v a2 s. c  om

    String serializerName = jClassType.getName().replaceAll("\\.", "\\$") + "_SerializableImpl";
    writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<"
            + jClassType.getQualifiedSourceName() + "> { ");
    writer.indent();
    writer.println("public " + serializerName + "(){}");

    writer.println("public JSONValue serialize(" + jClassType.getQualifiedSourceName() + " obj) {");
    writer.indent();
    writer.println("JSONObject jsonObj = new JSONObject();");
    writer.println("String type;");
    writer.println("String fieldName;");
    for (FieldInfo fieldInfo : resolvedFields) {

        JParameterizedType parametrizedType = fieldInfo.getjField().getType().isParameterized();
        generateGenericTypeSerializer(fieldInfo,
                parametrizedType != null ? parametrizedType.getTypeArgs() : null, writer);
    }
    writer.println("return jsonObj;");
    writer.outdent();
    writer.println("} ");
    writer.println();

    writer.println("public " + jClassType.getQualifiedSourceName()
            + " deSerialize(JSONValue jsonObj) throws JSONException {");
    writer.indent();
    writer.println("JSONObject obj = jsonObj.isObject();");
    writer.println(jClassType.getQualifiedSourceName() + " response = new "
            + jClassType.getQualifiedSourceName() + "();");
    writer.println("String type;");
    writer.println("String fieldName;");
    for (FieldInfo fieldInfo : resolvedFields) {
        JParameterizedType parametrizedType = fieldInfo.getjField().getType().isParameterized();
        generateGenericTypeDeSerializer(fieldInfo,
                parametrizedType != null ? parametrizedType.getTypeArgs() : null, writer);
    }
    writer.println("return response;");
    writer.outdent();
    writer.println("}");

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

    return serializerName;
}

From source file:org.gwt.json.serialization.InternalSerializerGenerator.java

License:Apache License

private void generateGenericTypeDeSerializer(FieldInfo fieldInfo, JClassType[] types, SourceWriter writer)
        throws NotFoundException {
    String fieldType = GenericGenerationUtils.resolveType(fieldInfo.getjField().getType());
    writer.println("type = \"" + fieldType + "\";");
    writer.println("fieldName = \"" + fieldInfo.getjField().getName() + "\";");
    writer.println("JsonGenericTypeSerializer<" + fieldType + "> " + fieldInfo.getjField().getName()
            + "Serializer = getSerializerForType(type);");
    writer.println("response." + fieldInfo.getSetAccessorName() + "(" + fieldInfo.getjField().getName()
            + "Serializer.deSerialize(obj.get(fieldName), "
            + GenericGenerationUtils.getGenericTypesParam(fieldInfo.getjField().getType(), types) + "));");
    if (fieldInfo.getjField().getType().isArray() != null) {
        registerArraySerializer(fieldInfo.getjField().getType().isArray());
    }/*from w  ww .j av  a 2  s.c  o  m*/
}

From source file:org.gwt.json.serialization.InternalSerializerGenerator.java

License:Apache License

private void generateGenericTypeSerializer(FieldInfo fieldInfo, JClassType[] types, SourceWriter writer)
        throws NotFoundException {
    /*/* ww  w . jav  a 2  s.c o  m*/
    * example:
    * type = "java.lang.String";
    * fieldName = "name";
    * JsonGenericTypeSerializer<java.lang.String> nameSerializer = getSerializerForType(type);
    * jsonObj.put(fieldName, nameSerializer.serialize(obj.getName(), null));
    *
    */
    String fieldType = GenericGenerationUtils.resolveType(fieldInfo.getjField().getType());
    writer.println("type = \"" + fieldType + "\";");
    writer.println("fieldName = \"" + fieldInfo.getjField().getName() + "\";");
    writer.println("JsonGenericTypeSerializer<" + fieldType + "> " + fieldInfo.getjField().getName()
            + "Serializer = getSerializerForType(type);");
    writer.println("jsonObj.put(fieldName, " + fieldInfo.getjField().getName() + "Serializer.serialize(obj."
            + fieldInfo.getGetAccessorName() + "(), "
            + GenericGenerationUtils.getGenericTypesParam(fieldInfo.getjField().getType(), types) + "));");

    if (fieldInfo.getjField().getType().isArray() != null) {
        registerArraySerializer(fieldInfo.getjField().getType().isArray());
    }
}

From source file:org.gwt.json.serialization.SerializerGenerator.java

License:Apache License

protected void generateRegisterConstructor(Map<String, String> serializers, String simpleName,
        SourceWriter writer, TypeOracle typeOracle) {
    writer.println("public " + simpleName + "() {");
    writer.indent();//from w  ww  . j  a v  a 2s  .co m
    for (Map.Entry<String, String> entry : serializers.entrySet()) {
        writer.println("registerSerializer(\"" + entry.getKey() + "\", new " + entry.getValue() + "("
                + constructorArgsForType(entry.getValue(), typeOracle) + "));");
    }
    writer.outdent();
    writer.println("}");
}

From source file:org.jboss.as.console.rebind.extensions.SubsystemExtensionProcessorGenerator.java

License:Apache License

private void generateClass(TreeLogger logger, GeneratorContext context, String packageName, String className,
        List<SubsystemExtension> subsystemExtensions) {
    PrintWriter pw = context.tryCreate(logger, packageName, className);
    if (pw == null) {
        return;/*from  ww  w .java 2 s.co m*/
    }

    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);

    // imports
    composerFactory.addImport(ArrayList.class.getCanonicalName());
    composerFactory.addImport(LinkedHashMap.class.getCanonicalName());
    composerFactory.addImport(List.class.getCanonicalName());
    composerFactory.addImport(Map.class.getCanonicalName());
    composerFactory.addImport(SubsystemExtensionProcessor.class.getCanonicalName());
    composerFactory.addImport(SubsystemGroup.class.getCanonicalName());
    composerFactory.addImport(SubsystemGroupItem.class.getCanonicalName());
    composerFactory.addImport(Predicate.class.getCanonicalName());
    composerFactory.addImport(LHSNavTreeItem.class.getCanonicalName());

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

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

    // begin class definition
    sw.indent();

    // fields
    sw.println(
            "private final Map<String, SubsystemGroup> extensionGroups = new LinkedHashMap<String, SubsystemGroup>();");
    sw.println("private final List<Predicate> _runtimeMetricsExtensions = new ArrayList<Predicate>();");
    sw.println("private final List<Predicate> _runtimeOperationsExtensions = new ArrayList<Predicate>();");

    // constructor
    sw.println("public " + className + "() {");
    sw.indent();
    for (SubsystemExtension extension : subsystemExtensions) {
        for (SubsystemGroupDefinition groupDef : extension.groups()) {
            sw.println("SubsystemGroup group;");
            sw.println("if (extensionGroups.containsKey(\"%s\")) {", groupDef.name());
            sw.indentln("group = extensionGroups.get(\"%s\");", groupDef.name());
            sw.println("} else {");
            sw.indent();
            sw.println("group = new SubsystemGroup(\"%s\");", groupDef.name());
            sw.println("extensionGroups.put(group.getName(), group);");
            sw.outdent();
            sw.println("}");
            for (SubsystemItemDefinition itemDef : groupDef.items()) {
                sw.println("group.getItems().add(new SubsystemGroupItem(\"%s\", \"%s\", \"%s\"));",
                        itemDef.name(), extension.subsystem(), itemDef.presenter());
            }
        }
        for (SubsystemItemDefinition runtimeItemDef : extension.metrics()) {
            sw.println(
                    "_runtimeMetricsExtensions.add(new Predicate(\"%s\", new LHSNavTreeItem(\"%s\", \"%s\")));",
                    extension.subsystem(), runtimeItemDef.name(), runtimeItemDef.presenter());
        }
        for (SubsystemItemDefinition runtimeItemDef : extension.runtime()) {
            sw.println(
                    "_runtimeOperationsExtensions.add(new Predicate(\"%s\", new LHSNavTreeItem(\"%s\", \"%s\")));",
                    extension.subsystem(), runtimeItemDef.name(), runtimeItemDef.presenter());
        }
    }
    sw.outdent();
    sw.println("}");

    // methods
    // processProfileExtensions
    sw.println("public void processProfileExtensions(Map<String, SubsystemGroup> groups) {");
    sw.indent();
    sw.println("for (Map.Entry<String, SubsystemGroup> entry : extensionGroups.entrySet()) {");
    sw.indent();
    sw.println("if (groups.containsKey(entry.getKey())) {");
    sw.indent();
    sw.println("SubsystemGroup group = groups.get(entry.getKey());");
    sw.println("group.getItems().addAll(entry.getValue().getItems());");
    sw.outdent();
    sw.println("} else {");
    sw.indent();
    sw.println("SubsystemGroup group = entry.getValue();");
    sw.println("groups.put(group.getName(), group);");
    sw.outdent();
    sw.println("}");
    sw.outdent();
    sw.println("}");
    sw.outdent();
    sw.println("}");

    // getRuntimeMetricsExtensions
    sw.println("public List<Predicate> getRuntimeMetricsExtensions() {");
    sw.indentln("return _runtimeMetricsExtensions;");
    sw.println("}");

    // getRuntimeOperationsExtensions
    sw.println("public List<Predicate> getRuntimeOperationsExtensions() {");
    sw.indentln("return _runtimeOperationsExtensions;");
    sw.println("}");

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

    context.commit(logger, pw);
}

From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java

License:Open Source License

/**
 * Generate source code for new class. Class extends
 * <code>HashMap</code>.//from  www  .j a  v a2 s . c  om
 *
 * @param logger  Logger object
 * @param context Generator context
 */
private void generateClass(TreeLogger logger, GeneratorContext context) {

    // get print writer that receives the source code
    PrintWriter printWriter = context.tryCreate(logger, packageName, className);

    // print writer if null, source code has ALREADY been generated, return
    if (printWriter == null)
        return;

    // init composer, set class properties, create source writer
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);

    // Imports
    composerFactory.addImport("org.jboss.as.console.client.Console");
    composerFactory.addImport("org.jboss.as.console.client.widgets.forms.*");
    composerFactory.addImport("java.util.*");

    // Interfaces
    composerFactory.addImplementedInterface("org.jboss.as.console.client.widgets.forms.ApplicationMetaData");

    // SourceWriter
    SourceWriter sourceWriter = composerFactory.createSourceWriter(context, printWriter);

    // fields
    generateFields(sourceWriter);

    // ctor
    generateConstructor(logger, context, sourceWriter);

    // Methods
    generateMethods(sourceWriter);

    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");

    // commit generated class
    context.commit(logger, printWriter);
}

From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java

License:Open Source License

private void generateFields(SourceWriter sourceWriter) {
    sourceWriter.println(
            "private static Map<Class<?>, List<PropertyBinding>> registry = new HashMap<Class<?>,List<PropertyBinding>>();");
    sourceWriter.println(/*from w  ww. jav  a  2 s  .co  m*/
            "private static Map<Class<?>, AddressBinding> addressing= new HashMap<Class<?>, AddressBinding>();");
    sourceWriter.println("private static Map<Class<?>, Mutator> mutators = new HashMap<Class<?>, Mutator>();");
    sourceWriter.println(
            "private static Map<Class<?>, EntityFactory> factories = new HashMap<Class<?>, EntityFactory>();");
    sourceWriter.println("private static " + BEAN_FACTORY_NAME
            + " beanFactory = com.google.gwt.core.client.GWT.create(" + BEAN_FACTORY_NAME + ".class);");
}

From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java

License:Open Source License

private void generateConstructor(TreeLogger logger, GeneratorContext context, SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();/*from  www .j av  a 2  s  . c  o  m*/
    sourceWriter.println("super();");
    /*    sourceWriter.println("String label = \"\";");
    sourceWriter.println("Class<?> listType = null;");
    sourceWriter.println("String subgroup = \"\";");
    sourceWriter.println("String localTabName = \"\";");
    sourceWriter.println("String[] acceptedValues = null;"); */

    try {
        Class<?> beanFactoryClass = getClass().getClassLoader().loadClass(BEAN_FACTORY_NAME);

        int idx = 0;

        for (Method method : beanFactoryClass.getMethods()) {
            Type returnType = method.getGenericReturnType();
            if (!(returnType instanceof ParameterizedType))
                continue;

            ParameterizedType type = (ParameterizedType) returnType;
            Type[] typeArguments = type.getActualTypeArguments();
            if (!(typeArguments[0] instanceof Class))
                continue;

            Class beanTypeClass = (Class) typeArguments[0];

            //sourceWriter.println(beanTypeClass.getSimpleName() + "_" + idx + "();");
            sourceWriter.println(beanTypeClass.getSimpleName() + "();");

            idx++;
        }

    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Failed to load " + BEAN_FACTORY_NAME);
    }

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

From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java

License:Open Source License

private void generateMethods(SourceWriter sourceWriter) {

    sourceWriter.println("public List<PropertyBinding> getBindingsForType(Class<?> type) { ");
    sourceWriter.indent();//from  www  .  j a  va  2  s .c  om
    sourceWriter.println("return registry.get(type);");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public BeanMetaData getBeanMetaData(Class<?> type) { ");
    sourceWriter.indent();
    sourceWriter.println("return new BeanMetaData(type, addressing.get(type), registry.get(type));");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public FormMetaData getFormMetaData(Class<?> type) { ");
    sourceWriter.indent();
    sourceWriter.println("return new FormMetaData(type, registry.get(type));");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public Mutator getMutator(Class<?> type) { ");
    sourceWriter.indent();
    sourceWriter.println("return mutators.get(type);");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public <T> EntityFactory<T> getFactory(Class<T> type) {");
    sourceWriter.indent();
    sourceWriter.println("return factories.get(type);");
    sourceWriter.outdent();
    sourceWriter.println("}");

    generateMethodMethods(sourceWriter);
}

From source file:org.jboss.as.console.rebind.forms.ApplicationMetaDataGenerator.java

License:Open Source License

private void generateMethodMethods(SourceWriter sourceWriter) {

    try {// w  ww.  ja v  a 2s  . c  om
        Class<?> beanFactoryClass = getClass().getClassLoader().loadClass(BEAN_FACTORY_NAME);

        int idx = 0;

        for (Method method : beanFactoryClass.getMethods()) {

            Type returnType = method.getGenericReturnType();
            if (returnType instanceof ParameterizedType) {
                ParameterizedType type = (ParameterizedType) returnType;
                Type[] typeArguments = type.getActualTypeArguments();

                if (typeArguments[0] instanceof Class) {
                    Class beanTypeClass = (Class) typeArguments[0];
                    //sourceWriter.println("public void " + beanTypeClass.getSimpleName() + "_" + idx + "() {");
                    sourceWriter.println("public void " + beanTypeClass.getSimpleName() + "() {");
                    sourceWriter.indent();
                    sourceWriter.println("Class<?> listType = null;");
                    sourceWriter.println("String label = \"\";");
                    sourceWriter.println("String subgroup = \"\";");
                    sourceWriter.println("String tabName = \"\";");
                    sourceWriter.println("String[] acceptedValues = null;");
                    sourceWriter.println("");

                    sourceWriter.println("registry.put(" + beanTypeClass.getName()
                            + ".class, new ArrayList<PropertyBinding>());");

                    // --------------------------------
                    // Mutator

                    sourceWriter.println(
                            "Mutator mut_" + idx + " = new Mutator<" + beanTypeClass.getName() + ">();");
                    sourceWriter
                            .println("mutators.put(" + beanTypeClass.getName() + ".class , mut_" + idx + ");");

                    // -----------------------------
                    // PropertyBinding

                    List<PropBindingDeclarations> bindings = mapProperties(beanTypeClass);

                    for (PropBindingDeclarations binding : bindings) {
                        BindingDeclaration bindDecl = binding.getBindingDeclaration();
                        FormItemDeclaration formDecl = binding.getFormItemDeclaration();

                        if (bindDecl.skip())
                            continue;

                        if (!bindDecl.listType().equals("")) {
                            sourceWriter.println("listType = " + bindDecl.listType() + ".class;");
                        }

                        if (formDecl.localLabel().equals("")) {
                            sourceWriter.println("label = \"" + formDecl.label() + "\";");
                        } else {
                            sourceWriter.println("label = Console.CONSTANTS." + formDecl.localLabel() + "();");
                        }

                        if (!"".equals(formDecl.subgroup())) {
                            sourceWriter.println("subgroup = \"" + formDecl.subgroup() + "\";");
                        } else {
                            sourceWriter.println("subgroup = \"\";");
                        }

                        if ("CUSTOM".equals(formDecl.tabName()))
                            sourceWriter.println("tabName = \"CUSTOM\";"); // will be set programmatically
                        else if ("".equals(formDecl.localTabName()))
                            sourceWriter.println("tabName = \"" + formDecl.tabName() + "\";");
                        else
                            sourceWriter
                                    .println("tabName = Console.CONSTANTS." + formDecl.localTabName() + "();");

                        sourceWriter.println(
                                "acceptedValues = " + makeStringArrayString(formDecl.acceptedValues()) + ";");
                        sourceWriter.println("registry.get(" + beanTypeClass.getName() + ".class).add(");
                        sourceWriter.indent();
                        sourceWriter.println("new PropertyBinding(\"" + bindDecl.getJavaName() + "\", \""
                                + bindDecl.getDetypedName() + "\", \"" + bindDecl.getJavaTypeName()
                                + "\", listType, this, " + bindDecl.key() + ", " + bindDecl.expr() + ", "
                                + bindDecl.writeUndefined() + ", \"" + formDecl.defaultValue() + "\", label, "
                                + formDecl.required() + ", \"" + formDecl.formItemTypeForEdit() + "\", \""
                                + formDecl.formItemTypeForAdd() + "\", subgroup, tabName, " + formDecl.order()
                                + ", acceptedValues)");
                        sourceWriter.outdent();
                        sourceWriter.println(");");

                        // create and register setters
                        sourceWriter.println("mut_" + idx + ".register(\"" + bindDecl.getJavaName()
                                + "\", new Setter<" + beanTypeClass.getName() + ">() {\n"
                                + "public void invoke(" + bindDecl.getBeanClassName()
                                + " entity, Object value) {\n" + "entity.set" + bindDecl.getPropertyName()
                                + "((" + bindDecl.getJavaTypeName() + ")value);\n" + "}\n" + "});\n");

                        // create and register getters

                        String prefix = "get";
                        if (bindDecl.getJavaTypeName().equals("java.lang.Boolean"))
                            prefix = "is";

                        sourceWriter.println(
                                "mut_" + idx + ".register(\"" + bindDecl.getJavaName() + "\", new Getter<"
                                        + beanTypeClass.getName() + ">() {\n" + "public Object invoke("
                                        + bindDecl.getBeanClassName() + " entity) {\n" + "   return entity."
                                        + prefix + bindDecl.getPropertyName() + "();\n" + "}\n" + "});\n");

                    }

                    // -----------------------------
                    // AddressBinding

                    AddressDeclaration addr = parseAddress(beanTypeClass);

                    sourceWriter.println("AddressBinding addr_" + idx + " = new AddressBinding();");
                    sourceWriter.println(
                            "addressing.put(" + beanTypeClass.getName() + ".class , addr_" + idx + ");");

                    for (String[] token : addr.getAddress()) {
                        sourceWriter
                                .println("addr_" + idx + ".add(\"" + token[0] + "\", \"" + token[1] + "\");");
                    }

                    // -----------------------------
                    // Factory lookup
                    sourceWriter.println("factories.put(" + beanTypeClass.getName()
                            + ".class, new EntityFactory<" + beanTypeClass.getName() + ">() {\n" + "public "
                            + beanTypeClass.getName() + " create() {\n" + "return beanFactory."
                            + method.getName() + "().as();\n" + "}\n" + "});\n");

                    sourceWriter.println("// ---- End " + beanTypeClass.getName() + " ----");

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

            idx++;

        }

    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Failed to load " + BEAN_FACTORY_NAME);
    }
}