Example usage for com.google.gwt.user.rebind SourceWriter print

List of usage examples for com.google.gwt.user.rebind SourceWriter print

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind SourceWriter print.

Prototype

void print(String s);

Source Link

Usage

From source file:net.sf.mmm.util.nls.impl.rebind.AbstractNlsBundleGenerator.java

License:Apache License

/**
 * Generates an the body of an {@link NlsBundle}-method.
 * // w  w w . ja v  a 2  s .  co  m
 * @param sourceWriter is the {@link SourceWriter}.
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 * @param method is the {@link NlsBundle}-method to generate an implementation for.
 */
protected void generateMethodBody(SourceWriter sourceWriter, TreeLogger logger, GeneratorContext context,
        JMethod method) {

    JParameter[] methodParameters = method.getParameters();
    if (methodParameters.length > 0) {

        sourceWriter.print("Map<String, Object> ");
        sourceWriter.print(VARIABLE_ARGUMENTS);
        sourceWriter.println(" = new HashMap<String, Object>();");

        // loop over parameters and generate code that puts the parameters into the arguments map
        for (JParameter parameter : methodParameters) {
            String name;
            Named namedAnnotation = parameter.getAnnotation(Named.class);
            if (namedAnnotation == null) {
                name = parameter.getName();
            } else {
                name = namedAnnotation.value();
            }
            sourceWriter.print(VARIABLE_ARGUMENTS);
            sourceWriter.print(".put(\"");
            sourceWriter.print(escape(name));
            sourceWriter.print("\", ");
            sourceWriter.print(parameter.getName());
            sourceWriter.println(");");
        }
    }

    sourceWriter.print("String ");
    generateMethodMessageBlock(sourceWriter, logger, context, method.getName());

    generateCreateMessageBlock(sourceWriter, methodParameters.length > 0);
}

From source file:net.sf.mmm.util.nls.impl.rebind.AbstractNlsBundleGenerator.java

License:Apache License

/**
 * Generates the source code block to create a new {@link NlsMessage}.
 * //from   w  w  w.  j av  a 2  s  .co  m
 * @param sourceWriter is the {@link SourceWriter}.
 * @param hasArguments - <code>true</code> if {@link NlsMessage#getArgument(String) arguments} are given,
 *        <code>false</code> otherwise.
 */
private void generateCreateMessageBlock(SourceWriter sourceWriter, boolean hasArguments) {

    // return NlsAccess.getFactory().create(message, arguments);
    if (hasArguments) {
        sourceWriter.print("return ");
        sourceWriter.print(NlsAccess.class.getSimpleName());
        sourceWriter.print(".getFactory().create(");
        sourceWriter.print(VARIABLE_MESSAGE);
        sourceWriter.print(", ");
        sourceWriter.print(VARIABLE_ARGUMENTS);
        sourceWriter.println(");");
    } else {
        sourceWriter.print("return new ");
        sourceWriter.print(NlsMessagePlain.class.getSimpleName());
        sourceWriter.print("(");
        sourceWriter.print(VARIABLE_MESSAGE);
        sourceWriter.println(");");
    }
}

From source file:net.sf.mmm.util.nls.impl.rebind.NlsBundleFactoryGenerator.java

License:Apache License

/**
 * Generates the <code>createBundle</code> method.
 * //from w w w  .  j a  v a  2 s.  c o m
 * @param sourceWriter is the {@link SourceWriter}.
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 */
protected void generateMethodCreateBundle(SourceWriter sourceWriter, TreeLogger logger,
        GeneratorContext context) {

    // method declaration
    sourceWriter.print("public <BUNDLE extends ");
    sourceWriter.print(NlsBundle.class.getSimpleName());
    sourceWriter.println("> BUNDLE createBundle(Class<BUNDLE> bundleInterface) {");
    sourceWriter.indent();

    // method body
    sourceWriter.println("BUNDLE bundle = getBundle(bundleInterface);");
    sourceWriter.println("if (bundle == null) {");
    sourceWriter.indent();
    // create and register
    generateBlockBundleCreation(sourceWriter, logger, context);
    sourceWriter.outdent();
    sourceWriter.println("}");

    sourceWriter.println("return bundle;");

    // end method...
    sourceWriter.outdent();
    sourceWriter.println("}");

}

From source file:net.sf.mmm.util.nls.impl.rebind.NlsBundleFactoryGenerator.java

License:Apache License

/**
 * Generates the block that creates the {@link NlsBundle} instance lazily via {@link GWT#create(Class)}.
 * //from   w  w  w. j a  v a  2s .com
 * @param sourceWriter is the {@link SourceWriter}.
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 */
protected void generateBlockBundleCreation(SourceWriter sourceWriter, TreeLogger logger,
        GeneratorContext context) {

    // find all subclasses of NlsBundle
    TypeOracle typeOracle = context.getTypeOracle();
    JClassType bundleClass = typeOracle.findType(NlsBundle.class.getName());
    JClassType bundleWithLookupClass = typeOracle.findType(NlsBundleWithLookup.class.getName());
    JClassType[] types = typeOracle.getTypes();
    int bundleCount = 0;
    logger.log(Type.INFO, "Checking " + types.length + " types...");
    for (JClassType type : types) {
        if ((type.isAssignableTo(bundleClass))
                && (!type.equals(bundleClass) && (!type.equals(bundleWithLookupClass)))) {
            logger.log(Type.INFO, "Found NlsBundle interface: " + type);

            sourceWriter.print("if (");
            sourceWriter.print(type.getQualifiedSourceName());
            sourceWriter.println(".class == bundleInterface) {");
            sourceWriter.indent();

            sourceWriter.print("bundle = GWT.create(");
            sourceWriter.print(type.getQualifiedSourceName());
            sourceWriter.println(".class);");

            sourceWriter.println("register(bundleInterface, bundle);");

            sourceWriter.outdent();
            sourceWriter.print("} else ");

            bundleCount++;
        }
    }
    sourceWriter.println("{");
    sourceWriter.indent();

    sourceWriter.print("throw new ");
    sourceWriter.print(IllegalStateException.class.getSimpleName());
    sourceWriter.println("(\"Undefined NlsBundle \" + bundleInterface.getName());");

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

    logger.log(Type.INFO, "Found " + bundleCount + " NlsBundle interface(s).");
}

From source file:net.sf.mmm.util.nls.impl.rebind.NlsBundleGeneratorGwtI18n.java

License:Apache License

/**
 * {@inheritDoc}//w w  w  . j av  a2  s .com
 */
@Override
protected void generateFields(SourceWriter sourceWriter, TreeLogger logger, GeneratorContext context,
        JClassType bundleClass) {

    super.generateFields(sourceWriter, logger, context, bundleClass);

    // generate i18n interface singleton field
    sourceWriter.print("private static final ");
    String i18nInterface = generateBundleInterface(bundleClass, logger, context);
    sourceWriter.print(i18nInterface);
    sourceWriter.print(" ");
    sourceWriter.print(VARIABLE_GWT_I18N);
    sourceWriter.print(" = GWT.create(");
    sourceWriter.print(i18nInterface);
    sourceWriter.println(".class);");
    sourceWriter.println();
}

From source file:net.sf.mmm.util.nls.impl.rebind.NlsBundleGeneratorGwtI18n.java

License:Apache License

/**
 * {@inheritDoc}/*from  w w  w  . ja v  a  2  s.co m*/
 */
@Override
protected void generateMethodMessageBlock(SourceWriter sourceWriter, TreeLogger logger,
        GeneratorContext context, String methodName) {

    sourceWriter.print(VARIABLE_MESSAGE);
    sourceWriter.print(" = ");
    sourceWriter.print(VARIABLE_GWT_I18N);
    sourceWriter.print(".");
    sourceWriter.print(methodName);
    sourceWriter.println("();");
}

From source file:net.sf.mmm.util.nls.impl.rebind.NlsBundleGeneratorGwtI18n.java

License:Apache License

/**
 * This method generates the GWT-i18n-interface for the NLS-bundle.
 *
 * @param bundleClass is the {@link JClassType class} of the {@link net.sf.mmm.util.nls.api.NlsBundle} to
 *        generate./*from w  ww  . j a  v  a  2  s . c o m*/
 * @param logger is the {@link TreeLogger}.
 * @param context is the {@link GeneratorContext}.
 * @return the name of the generated class.
 */
@SuppressWarnings({ "rawtypes" })
private String generateBundleInterface(JClassType bundleClass, TreeLogger logger, GeneratorContext context) {

    Class bundleJavaClass;
    String bundleName = bundleClass.getQualifiedSourceName();
    try {
        bundleJavaClass = Class.forName(bundleName);
    } catch (ClassNotFoundException e) {
        throw new TypeNotFoundException(bundleName);
    }
    @SuppressWarnings("unchecked")
    ClassName bundleClassName = NlsBundleHelper.getInstance().getQualifiedLocation(bundleJavaClass);
    String packageName = bundleClassName.getPackageName();
    String simpleName = bundleClassName.getSimpleName();
    if (bundleClassName.getName().equals(bundleName)) {
        logger.log(TreeLogger.ERROR, getClass().getSimpleName() + ": Illegal NlsBundle '" + bundleName
                + "' - has to end with suffix 'Root'. Localization will not work!");
        simpleName = simpleName + "_Interface";
    }
    logger.log(TreeLogger.INFO, getClass().getSimpleName() + ": Generating " + simpleName);
    ClassSourceFileComposerFactory sourceComposerFactory = new ClassSourceFileComposerFactory(packageName,
            simpleName);
    sourceComposerFactory.makeInterface();
    // import statements
    sourceComposerFactory.addImport(Constants.class.getName());
    sourceComposerFactory.addImport(Generate.class.getCanonicalName());

    sourceComposerFactory.addImplementedInterface(Constants.class.getSimpleName());

    // @Generate annotation
    StringBuilder annotationBuffer = new StringBuilder();
    annotationBuffer.append("@");
    annotationBuffer.append(Generate.class.getSimpleName());
    annotationBuffer.append("(format = \"");
    annotationBuffer.append(PropertiesFormat.class.getName());
    annotationBuffer.append("\")");

    sourceComposerFactory.addAnnotationDeclaration(annotationBuffer.toString());

    PrintWriter writer = context.tryCreate(logger, packageName, simpleName);
    if (writer != null) {
        SourceWriter sourceWriter = sourceComposerFactory.createSourceWriter(context, writer);

        // generate methods for fields of bundle
        for (JMethod method : bundleClass.getOverridableMethods()) {
            JType returnType = method.getReturnType();
            if (!isLookupMethod(method)) {
                if (!NlsMessage.class.getName().equals(returnType.getQualifiedSourceName())) {
                    throw new IllegalCaseException(returnType.getQualifiedSourceName());
                }
                NlsBundleMessage messageAnnotation = method.getAnnotation(NlsBundleMessage.class);
                if (messageAnnotation != null) {
                    String message = messageAnnotation.value();
                    // generate message annotation
                    sourceWriter.print("@DefaultStringValue(\"");
                    sourceWriter.print(escape(message));
                    sourceWriter.println("\")");
                }

                NlsBundleKey keyAnnotation = method.getAnnotation(NlsBundleKey.class);
                if (keyAnnotation != null) {
                    // generate key annotation
                    sourceWriter.print("@Key(\"");
                    sourceWriter.print(escape(keyAnnotation.value()));
                    sourceWriter.println("\")");
                }
                // generate method
                sourceWriter.print("String ");
                sourceWriter.print(method.getName());
                sourceWriter.println("();");

                sourceWriter.println();
            }

        }
        sourceWriter.commit(logger);
    }
    return sourceComposerFactory.getCreatedClassName();
}

From source file:net.sf.mmm.util.pojo.descriptor.impl.rebind.PojoDescriptorBuilderGenerator.java

License:Apache License

/**
 * Generates the method {@link AbstractPojoDescriptorBuilderLimited#createDescriptor(Class)}.
 * //from  w  w w  .  jav a 2s  .c  o  m
 * @param sourceWriter is the {@link SourceWriter} where to {@link SourceWriter#print(String) write} the
 *        source code to.
 * @param logger is the {@link TreeLogger}.
 * @param simpleName is the {@link Class#getSimpleName() simple name} of the {@link Class} to generate.
 * @param inputType is the {@link JClassType} reflecting the input-type that triggered the generation via
 *        {@link GWT#create(Class)}.
 * @param context is the {@link GeneratorContext}.
 */
private void generateMethodCreateDescriptor(SourceWriter sourceWriter, TreeLogger logger, String simpleName,
        JClassType inputType, GeneratorContext context) {

    sourceWriter.print("public <POJO> ");
    sourceWriter.print(AbstractPojoDescriptorImpl.class.getSimpleName());
    sourceWriter.println("<POJO> createDescriptor(Class<POJO> pojoType) {");
    sourceWriter.indent();

    PojoDescriptorGeneratorConfiguration configuration = getConfiguration();
    TypeOracle typeOracle = context.getTypeOracle();
    int typeCount = 0;
    JClassType[] types = typeOracle.getTypes();
    for (JClassType type : types) {
        if (configuration.isPojoTypeSupported(type, typeOracle)) {
            typeCount++;
            sourceWriter.print("if (pojoType == ");
            sourceWriter.print(type.getQualifiedSourceName());
            sourceWriter.println(".class) {");
            sourceWriter.indent();
            sourceWriter.print("return GWT.create(");
            sourceWriter.print(type.getQualifiedSourceName());
            sourceWriter.println(".class);");
            sourceWriter.outdent();
            sourceWriter.print("} else ");
        }
    }
    if (typeCount > 0) {
        sourceWriter.println("{");
        sourceWriter.indent();
        if (typeCount <= 3) {
            logger.log(Type.WARN, "Found only " + typeCount + " supported type(s)");
        } else {
            logger.log(Type.INFO, "Found " + typeCount + " supported types.");
        }
    } else {
        logger.log(Type.ERROR, "No type found for criteria: " + configuration.getPojoTypeDescription());
    }
    sourceWriter.println("return super.createDescriptor(pojoType);");
    if (typeCount > 0) {
        sourceWriter.outdent();
        sourceWriter.println("}");
    }
    generateSourceCloseBlock(sourceWriter);
}

From source file:net.sf.mmm.util.pojo.descriptor.impl.rebind.PojoDescriptorGenerator.java

License:Apache License

/**
 * Generates the method {@link AbstractPojoDescriptorImpl#newInstance()}.
 *
 * @param inputType is the {@link JClassType} reflecting the input-type that triggered the generation via
 *        {@link com.google.gwt.core.client.GWT#create(Class)}.
 * @param sourceWriter is the {@link SourceWriter} where to {@link SourceWriter#print(String) write} the
 *        source code to.//from  w w  w.j av  a  2  s  . com
 * @param pojoDescriptor is the {@link PojoDescriptor}.
 */
private void generateMethodNewInstance(JClassType inputType, SourceWriter sourceWriter,
        PojoDescriptor<?> pojoDescriptor) {

    generateSourcePublicMethodDeclaration(sourceWriter, inputType.getQualifiedSourceName(), "newInstance", "",
            false);

    if (inputType.isInterface() != null) {
        sourceWriter.print("throw new ");
        sourceWriter.println(InstantiationFailedException.class.getName());
        sourceWriter.println("(getPojoClass());");
    } else {
        sourceWriter.print("return new ");
        sourceWriter.print(inputType.getQualifiedSourceName());
        sourceWriter.println("();");
    }

    generateSourceCloseBlock(sourceWriter);
}

From source file:net.sf.mmm.util.pojo.descriptor.impl.rebind.PojoDescriptorGenerator.java

License:Apache License

/**
 * Generates the constructor of the {@link Class} to generate.
 *
 * @param sourceWriter is the {@link SourceWriter} where to {@link SourceWriter#print(String) write} the
 *        source code to./*w  w  w. ja v a 2s.com*/
 * @param simpleName is the {@link Class#getSimpleName() simple name} of the {@link Class} to generate.
 * @param inputType is the {@link JClassType} reflecting the input-type that triggered the generation via
 *        {@link com.google.gwt.core.client.GWT#create(Class)}.
 * @param pojoDescriptor is the {@link PojoDescriptor}.
 * @param context is the {@link GeneratorContext}.
 */
protected void generateConstructor(SourceWriter sourceWriter, String simpleName, JClassType inputType,
        PojoDescriptor<?> pojoDescriptor, GeneratorContext context) {

    generateSourcePublicConstructorDeclaration(sourceWriter, simpleName);
    sourceWriter.print("super(new ");
    sourceWriter.print(SimpleGenericTypeLimited.class.getSimpleName());
    sourceWriter.print("(");
    sourceWriter.print(inputType.getName());
    sourceWriter.print(".class), ");
    sourceWriter.print(AbstractPojoDescriptorBuilderLimited.class.getSimpleName());
    sourceWriter.println(".getInstance());");

    // local variable for property descriptor
    sourceWriter.print(PojoPropertyDescriptorImpl.class.getSimpleName());
    sourceWriter.println(" propertyDescriptor;");

    JClassType superType = getConfiguration().getSupportedSuperType(inputType, context.getTypeOracle());
    StatefulPropertyGenerator state = new StatefulPropertyGenerator(sourceWriter, superType);

    for (PojoPropertyDescriptor propertyDescriptor : pojoDescriptor.getPropertyDescriptors()) {
        state.generatePropertyDescriptorBlock(propertyDescriptor);
    }
    generateSourceCloseBlock(sourceWriter);
}