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.gwt.beansbinding.core.rebind.BeanPropertyDescriptorGenerator.java

License:Apache License

private void write(SourceWriter w, JClassType _type, JClassType[] types) {
    w.println("public static void setupBeanInfo() throws " + IntrospectionException.class.getName());
    w.println("{");
    w.indent();
    w.println("GwtBeanInfo beanInfo;");
    for (JClassType type : types) {
        Collection<Property> properties = lookupJavaBeanPropertyAccessors(type);
        w.println("\n// " + type.getName());
        w.println("beanInfo = new GwtBeanInfo();");
        for (Property property : properties) {
            w.print("beanInfo.addPropertyDescriptor( ");
            writePropertyDescriptor(w, type, property.name, property.propertyType, property.getter,
                    property.setter);/*from w ww .j a v  a 2 s  .c o  m*/
            w.println(" );");
        }
        w.println("GwtIntrospector.setBeanInfo( " + type.getQualifiedSourceName() + ".class, beanInfo );");
    }
    w.outdent();
    w.println("}");

    // automatically invoke setupBeanInfo()
    w.println("static {");
    w.indent();
    w.println(
            "try{setupBeanInfo();}catch(Exception ex){com.google.gwt.user.client.Window.alert(ex.getMessage());}");
    w.outdent();
    w.println("}");
}

From source file:org.gwt.beansbinding.core.rebind.BeanPropertyDescriptorGenerator.java

License:Apache License

private void writePropertyDescriptor(SourceWriter sw, JClassType type, String propertyName, String propertyType,
        JMethod getter, JMethod setter) {
    sw.print("new PropertyDescriptor( \"" + propertyName + "\", " + propertyType + ".class, ");
    if (getter != null) {
        sw.println("new Method() ");
        sw.println("{");
        sw.indent();
        sw.println("public Object invoke( Object bean, Object... args )");
        sw.println("{");
        sw.indent();//  ww w  . j  a  v a  2 s .  com
        sw.println("return ( (" + type.getQualifiedSourceName() + ") bean)." + getter.getName() + "();");
        sw.outdent();
        sw.println("}");
        sw.outdent();
        sw.print("}, ");
    } else {
        sw.print("null, ");
    }
    if (setter != null) {
        sw.println("new Method() ");
        sw.println("{");
        sw.indent();
        sw.println("public Object invoke( Object bean, Object... args )");
        sw.println("{");
        sw.indent();
        JType argType = setter.getParameters()[0].getType().getErasedType();
        String argTypeName;
        if (argType.isPrimitive() != null) {
            argTypeName = argType.isPrimitive().getQualifiedBoxedSourceName();
        } else {
            argTypeName = argType.getQualifiedSourceName();
        }
        sw.println("( (" + type.getQualifiedSourceName() + ") bean)." + setter.getName() + "( (" + argTypeName
                + ") args[0] );");
        sw.println("return null;");
        sw.outdent();
        sw.println("}");
        sw.outdent();
        sw.print("} )");
    } else {
        sw.print("null )");
    }
}

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

License:Apache License

public String generateSerializers(SourceWriter writer) throws NotFoundException {
    String serializerName = jArrayType.getQualifiedSourceName().replaceAll("\\.", "\\$").replaceAll("\\[\\]",
            "_1") + "_SerializableImpl";
    writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<"
            + jArrayType.getQualifiedSourceName() + "> { ");
    writer.indent();
    writer.println("public " + serializerName + "(){}");

    writer.println("public JSONValue serialize(" + jArrayType.getQualifiedSourceName() + " obj) {");
    writer.indent();//from  w  w w  .  ja  v a2s .  c  o m
    writer.println("if (obj == null) return JSONNull.getInstance();");
    writer.println("JSONArray jsonArray = new JSONArray();");
    writer.println("for (int i=0; i< obj.length; i++) {");
    writer.indent();
    JType componentType = jArrayType.getComponentType();
    String fieldType = GenericGenerationUtils.resolveType(componentType);
    if (componentType.isArray() != null) {
        writer.println(
                "jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], null));");
        registerArraySerializer(componentType.isArray());
    } else {
        JParameterizedType parametrizedType = componentType.isParameterized();
        writer.println("jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], "
                + GenericGenerationUtils.getGenericTypesParam(componentType,
                        parametrizedType != null ? parametrizedType.getTypeArgs() : null)
                + "));");
    }
    writer.outdent();
    writer.println("}");

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

    writer.println("public " + jArrayType.getQualifiedSourceName()
            + " deSerialize(JSONValue jsonObj) throws JSONException {");
    writer.indent();

    writer.println("if (jsonObj == null) return null;");

    writer.println("JSONArray arr = jsonObj.isArray();");
    writer.println(jArrayType.getQualifiedSourceName() + " response = new "
            + insertAsFirstArray(jArrayType.getComponentType().getQualifiedSourceName(), "[arr.size()]") + ";");
    writer.println("JsonGenericTypeSerializer<" + fieldType + "> serializer = getSerializerForType(\""
            + fieldType + "\");");
    writer.println("for (int i=0; i<arr.size(); i++) {");
    writer.indent();
    if (componentType.isArray() != null) {
        writer.print("response[i] = serializer.deSerialize(arr.get(i), null);");
        registerArraySerializer(componentType.isArray());
    } else {
        JParameterizedType parametrizedType = componentType.isParameterized();
        writer.print("response[i] = serializer.deSerialize(arr.get(i), "
                + GenericGenerationUtils.getGenericTypesParam(componentType,
                        parametrizedType != null ? parametrizedType.getTypeArgs() : null)
                + ");");
    }
    writer.outdent();
    writer.println("}");
    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

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));
    }//from   w  ww  . ja v  a2 s. co m

    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.SerializerGenerator.java

License:Apache License

protected void generateRegisterConstructor(Map<String, String> serializers, String simpleName,
        SourceWriter writer, TypeOracle typeOracle) {
    writer.println("public " + simpleName + "() {");
    writer.indent();
    for (Map.Entry<String, String> entry : serializers.entrySet()) {
        writer.println("registerSerializer(\"" + entry.getKey() + "\", new " + entry.getValue() + "("
                + constructorArgsForType(entry.getValue(), typeOracle) + "));");
    }/*from  ww w  .  java  2  s  .  c  o  m*/
    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   w w w .j  a va  2  s. c o  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

private void generateConstructor(TreeLogger logger, GeneratorContext context, SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();
    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 {//w w  w. j  a va 2  s.  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))
                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();
    sourceWriter.println("return registry.get(type);");
    sourceWriter.outdent();//from www. ja  v a 2 s . co  m
    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 {/*from  www  . j a v  a 2  s .c o  m*/
        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);
    }
}

From source file:org.jboss.as.console.rebind.ProductConfigGenerator.java

License:Open Source License

private void generateConstructor(TreeLogger logger, GeneratorContext context, SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();
    sourceWriter.println("super();");

    // TODO/*  www. ja v  a  2  s . c  o  m*/

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