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.jboss.as.console.rebind.ProductConfigGenerator.java

License:Open Source License

/**
 * Generate source code for new class. Class extends
 * <code>HashMap</code>./*from   ww w .ja  va2 s . c o m*/
 *
 * @param logger  Logger object
 * @param context Generator context
 */
private void generateClass(TreeLogger logger, GeneratorContext context) throws Throwable {

    // 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.ProductConfig");

    composerFactory.addImport("java.util.*");

    // Interfaces
    composerFactory.addImplementedInterface("org.jboss.as.console.client.ProductConfig");

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

    // fields
    generateFields(sourceWriter);

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

    // Methods
    generateMethods(sourceWriter, context);

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

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

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();/*from   w w w.jav  a2  s .  c o  m*/
    sourceWriter.println("super();");

    // TODO

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

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

License:Open Source License

private void generateMethods(SourceWriter sourceWriter, GeneratorContext context) throws Throwable {

    PropertyOracle propertyOracle = context.getPropertyOracle();
    String consoleProfileProperty = propertyOracle.getConfigurationProperty("console.profile").getValues()
            .get(0);// w  w  w  . j av  a 2 s  .  c  om

    if (null == consoleProfileProperty)
        throw new BadPropertyValueException("Missing configuration property 'console.profile'!");

    String prodVersionProperty = propertyOracle.getConfigurationProperty("console.product.version").getValues()
            .get(0);

    String consoleProductVersion = (prodVersionProperty != null) ? prodVersionProperty : "";

    String devHostProperty = propertyOracle.getConfigurationProperty("console.dev.host").getValues().get(0);

    String consoleDevHost = (devHostProperty != null) ? devHostProperty : "127.0.0.1";

    // most of the config attributes are by default empty
    // they need be overriden by custom gwt.xml descriptor on a project/product level

    sourceWriter.println("public String getProductTitle() { ");
    sourceWriter.indent();
    sourceWriter.println("return \"\";");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public String getProductVersion() { ");
    sourceWriter.indent();
    sourceWriter.println("return \"" + consoleProductVersion + "\";");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public String getCoreVersion() { ");
    sourceWriter.indent();
    sourceWriter.println("return org.jboss.as.console.client.Build.VERSION;");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public String getDevHost() { ");
    sourceWriter.indent();
    sourceWriter.println("return \"" + devHostProperty + "\";");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public ProductConfig.Profile getProfile() { ");
    sourceWriter.indent();
    if ("eap".equals(consoleProfileProperty))
        sourceWriter.println("return ProductConfig.Profile.EAP;");
    else
        sourceWriter.println("return ProductConfig.Profile.JBOSS;");
    sourceWriter.outdent();
    sourceWriter.println("}");

}

From source file:org.jboss.bpm.console.rebind.WorkspaceGenerator.java

License:Open Source License

/**
 * Generate source code for new class. Class extends
 * <code>HashMap</code>./*from  w  ww  . ja  v  a  2  s . c  o m*/
 *
 * @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 = null;
    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.bpm.console.client.ApplicationContext");
    composerFactory.addImport("org.jboss.bpm.console.client.Workspace");
    composerFactory.addImport("org.jboss.bpm.console.client.WorkspaceLauncher");

    // Interfaces
    composerFactory.addImplementedInterface("org.jboss.bpm.console.client.WorkspaceLauncher");

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

    // Methods
    generateMethods(sourceWriter);

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

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

From source file:org.jboss.bpm.console.rebind.WorkspaceGenerator.java

License:Open Source License

private void generateMethods(SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public void launch(Workspace workspace) { ");
    sourceWriter.indent();//from  ww  w  .j av  a  2 s.c o  m

    InputStream in = getClass().getClassLoader().getResourceAsStream(WORKSPACE_CONFIG);

    if (null == in) {
        in = getClass().getClassLoader().getResourceAsStream(WORKSPACE_CONFIG_DEFAULT);
        if (null == in)
            throw new RuntimeException(
                    "Cannot find '" + WORKSPACE_CONFIG + "' or '" + WORKSPACE_CONFIG_DEFAULT + "'");
    }

    try {
        //use buffering, reading one line at a time
        //FileReader always assumes default encoding is OK!
        BufferedReader input = new BufferedReader(new InputStreamReader(in));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {

                // ignore comments and empty lines
                if (line.equals("") || line.startsWith("#"))
                    continue;

                sourceWriter.println("workspace.addEditor( new " + line + "(), false );");
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        throw new RuntimeException("Error reading '" + WORKSPACE_CONFIG + "'");
    }

    // end constructor source generation
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:org.jboss.errai.bus.rebind.ExtensionProxyGenerator.java

License:Apache License

/**
 * Generate source code for new class. Class extends
 * <code>HashMap</code>./*from w w w.  ja  v a 2s. co m*/
 *
 * @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,

    if (printWriter == null)
        return;

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

    composer.addImplementedInterface(ExtensionsLoader.class.getName());
    composer.addImport(JSONValue.class.getName());
    composer.addImport(JSONString.class.getName());
    composer.addImport(JSONNumber.class.getName());
    composer.addImport(JSONBoolean.class.getName());
    composer.addImport(JSONObject.class.getName());

    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);

    // generator constructor source code
    generateExtensions(context, logger, sourceWriter);
    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");

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

From source file:org.jboss.errai.bus.rebind.ExtensionProxyGenerator.java

License:Apache License

private void generateExtensions(GeneratorContext context, TreeLogger logger, SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();/*from   w w w  .j a  va  2s.c om*/
    sourceWriter.println("super();");
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("public void initExtensions(final " + MessageBus.class.getName() + " bus) { ");
    sourceWriter.outdent();

    MetaDataScanner scanner = ScannerSingleton.getOrCreateInstance();
    new BusClientConfigGenerator().generate(context, logger, sourceWriter, scanner, typeOracle);

    Set<Class<?>> extensionComponents = scanner.getTypesAnnotatedWith(ExtensionComponent.class);

    for (Class<?> cls : extensionComponents) {
        if (ExtensionGenerator.class.isAssignableFrom(cls)) {
            try {

                ExtensionGenerator generator = cls.asSubclass(ExtensionGenerator.class).newInstance();
                generator.generate(context, logger, sourceWriter, scanner, typeOracle);
            } catch (Exception e) {
                throw new RuntimeException("Could not load extension generator: " + cls.getName(), e);
            }
        }
    }

    // end constructor source generation
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:org.jboss.errai.widgets.rebind.WidgetMappingsGenerator.java

License:Apache License

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,

    if (printWriter == null) {
        return;/*from ww  w. jav a2s.  com*/
    }

    // init composer, set class properties, create source writer
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImplementedInterface(typeName);
    composer.addImport(Widget.class.getName());

    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);

    // generator constructor source code
    generateExtensions(context, logger, sourceWriter);
    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");

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

From source file:org.jboss.errai.widgets.rebind.WidgetMappingsGenerator.java

License:Apache License

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

    sourceWriter.println("public void mapAll(final " + strTargetType + " widget) { ");
    sourceWriter.outdent();//  w  w  w . jav a  2 s . c o  m

    try {
        JClassType widgetMapper = typeOracle.getType(CollectionWidgetMapper.class.getName());

        for (JField currField : targetClass.getFields()) {
            if (currField.isAnnotationPresent(WidgetMapper.class)
                    && widgetMapper.isAssignableFrom(currField.getType().isClassOrInterface())) {
                WidgetMapper mf = currField.getAnnotation(WidgetMapper.class);

                JField widgetField = targetClass.getField(mf.value());
                String varName = widgetField.getName() + "Mapper";

                JClassType binderField = currField.getType().isClassOrInterface();
                JParameterizedType pType = binderField.isParameterized();

                if (pType == null) {
                    RuntimeException e = new RuntimeException(
                            "Field '" + currField.getName() + "' must be parameterized");
                    logger.log(TreeLogger.Type.ERROR, e.getMessage(), e);
                    throw e;
                }

                // The last type arg shall always be our domain object type per this spec.
                JClassType jEntityTarget = pType.getTypeArgs()[pType.getTypeArgs().length - 1];
                String strTypeParms = generateTypeParmString(pType);

                List<JField> fieldsToMap = new LinkedList<JField>();

                /**
                 * If an EntityFields annotatio is present, then we discriminate on those fields.  Otherwise
                 * we capture all fields by default.
                 */
                if (currField.isAnnotationPresent(EntityFields.class)) {
                    EntityFields ef = currField.getAnnotation(EntityFields.class);
                    for (String fieldName : ef.value()) {
                        JField fld = jEntityTarget.getField(fieldName);
                        if (fld == null) {
                            RuntimeException e = new RuntimeException("no such field in entity class '"
                                    + jEntityTarget.getName() + "': " + fieldName);
                            logger.log(TreeLogger.Type.ERROR, e.getMessage(), e);
                            throw e;
                        }

                        fieldsToMap.add(jEntityTarget.getField(fieldName));
                    }
                } else {
                    for (JField fld : jEntityTarget.getFields()) {
                        if (fld.getEnclosingType().equals(jEntityTarget)) {
                            fieldsToMap.add(fld);
                        }
                    }
                }

                List<String> generatedInitializations = new LinkedList<String>();
                List<String> generatedBindings = new LinkedList<String>();

                FieldMapperGenerator g = getFieldMapper(widgetField.getType().getQualifiedSourceName());

                generatedInitializations.add(
                        g.init(typeOracle, widgetField, jEntityTarget, currField, null, varName, fieldsToMap));

                if (g == null) {
                    throw new RuntimeException(
                            "Cannot generateGetField mapper for widget: " + jEntityTarget.getName());
                }

                for (JField fld : fieldsToMap) {
                    String fieldName = fld.getName();

                    JField targetField = jEntityTarget.getField(fieldName);
                    if (targetField == null) {
                        throw new RuntimeException(
                                "The field '" + fieldName + "' does not correspond with a field in the class: "
                                        + jEntityTarget.getQualifiedSourceName());
                    }

                    generatedBindings.add(g.generateFieldMapperGenerator(typeOracle, widgetField, jEntityTarget,
                            null, targetField));
                }

                Map<String, Object> vars = new HashMap<String, Object>();
                vars.put("typeOracle", typeOracle);
                vars.put("variableName", varName);
                vars.put("strTypeParms", strTypeParms);
                vars.put("targetWidget", widgetField.getType().getQualifiedSourceName());
                vars.put("targetType", jEntityTarget.getQualifiedSourceName());
                vars.put("initializers", generatedInitializations);
                vars.put("bindings", generatedBindings);
                vars.put("targetFieldName", widgetField.getName());

                String s = (String) TemplateRuntime.execute(mappingsGen, vars);
                sourceWriter.print(s);

                s = "widget." + currField.getName() + " = " + varName + ";";

                sourceWriter.println(s);

            } else if (currField.isAnnotationPresent(EntityMapped.class)) {
                EntityMapped entityMappedA = currField.getAnnotation(EntityMapped.class);

                JClassType entityType = currField.getType().isClassOrInterface();
                String varName = currField.getName() + "Mapper";

                String entityFieldName = currField.getName();
                String toEntityField;

                Map<String, List<JField>> toBeMapped = new HashMap<String, List<JField>>();

                for (JField fld : targetClass.getFields()) {
                    if (fld.isAnnotationPresent(MapField.class)) {
                        MapField mapFieldA = fld.getAnnotation(MapField.class);
                        toEntityField = "".equals(mapFieldA.value()) ? entityFieldName : mapFieldA.value();

                        if (!toBeMapped.containsKey(toEntityField)) {
                            toBeMapped.put(toEntityField, new LinkedList<JField>());
                        }

                        toBeMapped.get(toEntityField).add(fld);
                    }
                }

                /**
                 * Generate the field mappings.
                 */
                for (Map.Entry<String, List<JField>> entry : toBeMapped.entrySet()) {
                    List<String> generatedInitializations = new LinkedList<String>();
                    List<String> generatedBindings = new LinkedList<String>();
                    ArrayList<String[]> fieldIndexPositions = new ArrayList<String[]>();

                    for (JField fld : entry.getValue()) {
                        JClassType classType = fld.getType().isClassOrInterface();

                        String fieldName = fld.getAnnotation(MapField.class).value();
                        if ("".equals(fieldName)) {
                            fieldName = fld.getName();
                        }

                        JField targetField = entityType.getField(fieldName);
                        if (targetField == null) {
                            throw new RuntimeException("The field '" + fieldName
                                    + "' does not correspond with a field in the class: "
                                    + entityType.getQualifiedSourceName());
                        }

                        JClassType targetFieldType = targetField.getType().isClassOrInterface();

                        FieldMapperGenerator g = getFieldMapper(classType.getQualifiedSourceName());

                        if (g == null) {
                            throw new RuntimeException(
                                    "Cannot generateGetField mapper for widget: " + classType.getName());
                        }

                        generatedInitializations
                                .add(g.init(typeOracle, fld, entityType, targetField, currField, null, null));

                        generatedBindings.add(g.generateFieldMapperGenerator(typeOracle, fld, entityType,
                                targetField, currField));

                        if (getType(typeOracle, Widget.class).isAssignableFrom(classType)) {
                            fieldIndexPositions
                                    .add(new String[] { fieldName, g.generateValueExtractorStatement(typeOracle,
                                            fld, entityType, targetField, currField) });
                        }
                    }

                    Map<String, Object> vars = new HashMap<String, Object>();
                    vars.put("typeOracle", typeOracle);
                    vars.put("variableName", varName);
                    vars.put("initializers", generatedInitializations);
                    vars.put("targetFieldName", entityFieldName);
                    vars.put("bindings", generatedBindings);
                    vars.put("fieldIndexPositions", fieldIndexPositions);
                    vars.put("entityFieldName", entityFieldName);

                    String s = (String) TemplateRuntime.execute(entityMappingGen, vars);

                    sourceWriter.print(s);
                }
            } else if (currField.isAnnotationPresent(AddAllTo.class)) {
                String copyToField = currField.getAnnotation(AddAllTo.class).value();
                String copyFromField = currField.getName();

                Map<String, Object> vars = new HashMap<String, Object>();
                vars.put("copyToField", copyToField);
                vars.put("copyFromField", copyFromField);

                String s = (String) TemplateRuntime.execute(addAllToGen, vars);

                sourceWriter.print(s);
            }
        }
    } catch (Exception e) {
        logger.log(TreeLogger.Type.ERROR, "failed to map field (does not exist)", e);
        e.printStackTrace();
    }

    // end constructor source generation
    sourceWriter.outdent();
    sourceWriter.println("}");
}