Java Code Examples for com.google.gwt.core.ext.GeneratorContext#tryCreate()

The following examples show how to use com.google.gwt.core.ext.GeneratorContext#tryCreate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyViewCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String create(TreeLogger logger, GeneratorContext context) {
	PrintWriter printWriter = context.tryCreate(logger, this.packageName, this.viewProxySimpleName);
	if (printWriter == null) {
		return this.viewProxyQualifiedName;
	}

	SourceWriter srcWriter = this.getSourceWriter(printWriter, context);

	srcWriter.indent();
	this.generateProxy(logger, srcWriter);
	srcWriter.println();
	this.generateTokenPrefixes(logger, srcWriter);
	srcWriter.println();
	if (this.placeTokenizerClass == null) {
		this.generateInternalTokenizer(logger, srcWriter);
	} else {
		this.generateDelegateTokenizer(logger, srcWriter);
	}
	this.generateActivityFactory(logger, srcWriter);
	srcWriter.outdent();

	srcWriter.commit(logger);
	return this.viewProxyQualifiedName;
}
 
Example 2
Source File: AbstractInjectorCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 6 votes vote down vote up
public String create(TreeLogger logger, GeneratorContext context) {
	PrintWriter writer = context.tryCreate(logger, this.packageName, this.proxyName);
	if (writer == null) {
		return this.proxyQualifiedName;
	}

	SourceWriter srcWriter = this.getSourceWriter(writer, context);
	srcWriter.indent();

	this.doCreate(logger, context, srcWriter);

	srcWriter.outdent();
	srcWriter.commit(logger);

	return this.proxyQualifiedName;
}
 
Example 3
Source File: EventBinderGenerator.java    From gwteventbinder with Apache License 2.0 6 votes vote down vote up
private SourceWriter createSourceWriter(
    TreeLogger logger,
    GeneratorContext context,
    JClassType eventBinderType,
    JClassType targetType) {
  String simpleName = getSimpleGeneratedClassName(eventBinderType);
  String packageName = eventBinderType.getPackage().getName();
  ClassSourceFileComposerFactory composer =
      new ClassSourceFileComposerFactory(packageName, simpleName);

  composer.setSuperclass(AbstractEventBinder.class.getCanonicalName()
      + "<" + targetType.getQualifiedSourceName() + ">");
  composer.addImplementedInterface(eventBinderType.getName());

  composer.addImport(EventBinder.class.getCanonicalName());
  composer.addImport(EventBus.class.getCanonicalName());
  composer.addImport(GenericEvent.class.getCanonicalName());
  composer.addImport(GenericEventHandler.class.getCanonicalName());
  composer.addImport(HandlerRegistration.class.getCanonicalName());
  composer.addImport(LinkedList.class.getCanonicalName());
  composer.addImport(List.class.getCanonicalName());

  PrintWriter printWriter = context.tryCreate(logger, packageName, simpleName);
  return (printWriter != null) ? composer.createSourceWriter(context, printWriter) : null;
}
 
Example 4
Source File: Mvp4gGenerator.java    From mvp4g with Apache License 2.0 5 votes vote down vote up
private SourceWriter getSourceWriter(TreeLogger logger,
                                     GeneratorContext context,
                                     JClassType originalType,
                                     String packageName,
                                     String generatedClassName)
  throws UnableToCompleteException {

  logger.log(TreeLogger.INFO,
             "Generating writer for " + packageName + "." + generatedClassName,
             null);

  PrintWriter printWriter = context.tryCreate(logger,
                                              packageName,
                                              generatedClassName);
  if (printWriter == null) {
    return null;
  }

  ClassSourceFileComposerFactory classFactory = new ClassSourceFileComposerFactory(packageName,
                                                                                   generatedClassName);

  classFactory.addImplementedInterface(originalType.getName());
  String[] classesToImport = getClassesToImport();
  for (String classToImport : classesToImport) {
    classFactory.addImport(classToImport);
  }

  return classFactory.createSourceWriter(context,
                                         printWriter);
}
 
Example 5
Source File: ModelCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private PrintWriter getPrintWriter(TreeLogger logger, GeneratorContext ctx, String targetQualifiedName) {
	String packageName =
		this.proxyModelQualifiedName.indexOf('.') == -1 ? "" : this.proxyModelQualifiedName.substring(0,
			this.proxyModelQualifiedName.lastIndexOf('.'));
	int lastIndex = this.proxyModelQualifiedName.lastIndexOf('.');
	String className =
		lastIndex == -1 ? this.proxyModelQualifiedName : this.proxyModelQualifiedName.substring(lastIndex + 1,
			this.proxyModelQualifiedName.length());

	return ctx.tryCreate(logger, packageName, className);
}
 
Example 6
Source File: ServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private PrintWriter getPrintWriter(TreeLogger logger, GeneratorContext ctx) {
	String packageName =
		this.proxyModelQualifiedName.indexOf('.') == -1 ? "" : this.proxyModelQualifiedName.substring(0,
			this.proxyModelQualifiedName.lastIndexOf('.'));
	String className = this.proxyModelQualifiedName.indexOf('.') == -1 ? this.proxyModelQualifiedName :
		this.proxyModelQualifiedName.substring(
			this.proxyModelQualifiedName.lastIndexOf('.') + 1, this.proxyModelQualifiedName.length());

	return ctx.tryCreate(logger, packageName, className);
}
 
Example 7
Source File: RestServiceBinderCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 5 votes vote down vote up
private PrintWriter getPrintWriter(TreeLogger logger, GeneratorContext ctx) {
	String packageName =
		this.proxyModelQualifiedName.indexOf('.') == -1 ? "" : this.proxyModelQualifiedName.substring(0,
			this.proxyModelQualifiedName.lastIndexOf('.'));
	String className = this.proxyModelQualifiedName.indexOf('.') == -1 ? this.proxyModelQualifiedName :
		this.proxyModelQualifiedName.substring(
			this.proxyModelQualifiedName.lastIndexOf('.') + 1, this.proxyModelQualifiedName.length());

	return ctx.tryCreate(logger, packageName, className);
}
 
Example 8
Source File: ProductConfigGenerator.java    From core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Generate source code for new class. Class extends
 * <code>HashMap</code>.
 *
 * @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);

    // ctor
    generateConstructor(sourceWriter);

    // Methods
    generateMethods(logger, sourceWriter, context);

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

    // commit generated class
    context.commit(logger, printWriter);
}
 
Example 9
Source File: TextBinderGenerator.java    From EasyML with Apache License 2.0 4 votes vote down vote up
@Override
public String generate(TreeLogger logger, GeneratorContext context,
		String requestedClass) throws UnableToCompleteException {

	TypeOracle typeOracle = context.getTypeOracle();

	JClassType objectType = typeOracle.findType(requestedClass);
	if (objectType == null) {
		logger.log(TreeLogger.ERROR, "Could not find type: " + requestedClass);
		throw new UnableToCompleteException();
	}

	implTypeName = objectType.getSimpleSourceName() + "Impl";

	implPackageName = objectType.getPackage().getName();

	JClassType[] implementedTypes = objectType.getImplementedInterfaces();

	// Can only implement one interface
	if (implementedTypes == null
			|| implementedTypes.length != 1
			|| !implementedTypes[0].getQualifiedSourceName().equals(
					TextBinder.class.getName())) {
		logger
		.log(
				TreeLogger.ERROR,
				"The type: " + requestedClass
				+ " Must implement only one interface: "
				+ TextBinder.class.getName());
		throw new UnableToCompleteException();
	}

	// Get parameterized type
	JParameterizedType parameterType = implementedTypes[0].isParameterized();
	if (parameterType == null) {
		logger.log(TreeLogger.ERROR, "The type: " + requestedClass
				+ " Must implement only one parameterized interface: "
				+ TextBinder.class.getName());
		throw new UnableToCompleteException();
	}

	if (parameterType.getTypeArgs() == null

			|| parameterType.getTypeArgs().length != 2) {
		logger.log(TreeLogger.ERROR,
				"The type: " + requestedClass
				+ " Must implement two parameterized interface: "
				+ TextBinder.class.getName() + " with two Parameter");
		throw new UnableToCompleteException();

	}

	parameterizedType1 = parameterType.getTypeArgs()[0];
	parameterizedType2 = parameterType.getTypeArgs()[1];
	// logger.log(TreeLogger.INFO ,
	// parameterizedType2.getParameterizedQualifiedSourceName() +"\n"
	// + parameterizedType2.getQualifiedSourceName());

	ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(
			implPackageName, implTypeName);

	composerFactory.addImport(Map.class.getCanonicalName());
	composerFactory.addImport(List.class.getCanonicalName());
	// composerFactory.addImport(Field.class.getCanonicalName());
	composerFactory
	.addImplementedInterface(objectType.getQualifiedSourceName());

	PrintWriter printWriter = context.tryCreate(logger, implPackageName,
			implTypeName);
	if (printWriter != null) {

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

		composeBindMethod(logger, sourceWriter);
		composeSyncMethod(logger, sourceWriter);
		sourceWriter.commit(logger);

	}
	return implPackageName + "." + implTypeName;
}
 
Example 10
Source File: ExtendedServiceProxyGenerator.java    From appinventor-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a wrapper around the proxy generated by
 * {@link ServiceInterfaceProxyGenerator}.
 *
 * @param logger log interface
 * @param context generator context
 * @param typeName name of the interface that was passed to
 *        {@link com.google.gwt.core.client.GWT#create(Class)}
 * @param proxyTypeName the name of the wrapped proxy class
 * @return the name of the extended proxy class
 */
private String generateExtendedProxy(TreeLogger logger, GeneratorContext context,
    String typeName, String proxyTypeName) {
  JClassType type = context.getTypeOracle().findType(typeName);
  String packageName = type.getPackage().getName();
  String className = type.getSimpleSourceName() + PROXY_SUFFIX;
  String asyncName = typeName + ASYNC_SUFFIX;

  String classNameExtendedServiceProxy = "com.google.appinventor.client.ExtendedServiceProxy";

  // The generator can be invoked for the same class name more than once.
  // In this case the GeneratorContext.tryCreate method will return null to
  // indicate that the file already exists. This is not an error.
  PrintWriter out = context.tryCreate(logger, packageName, className);
  if (out != null) {
    out.println("package " + packageName + ";");
    out.println("class " + className);
    out.println("    extends " + classNameExtendedServiceProxy + "<" + typeName + ">");
    out.println("    implements " + ServiceDefTarget.class.getName() + ", " + asyncName + " {");
    out.println("  private " + proxyTypeName + " proxy = new " + proxyTypeName + "();");
    out.println("  public String getServiceEntryPoint() {");
    out.println("    return proxy.getServiceEntryPoint();");
    out.println("  }");
    out.println("  public void setRpcRequestBuilder(" + RpcRequestBuilder.class.getName() +
        " builder) {");
    out.println("    proxy.setRpcRequestBuilder(builder);");
    out.println("  }");
    out.println("  public void setServiceEntryPoint(String address) {");
    out.println("    proxy.setServiceEntryPoint(address);");
    out.println("  }");
    out.println("  public String getSerializationPolicyName() {");
    out.println("    return proxy.getSerializationPolicyName();");
    out.println("  }");

    for (JMethod method : type.getMethods()) {
      printMethod(out, method, typeName);
    }

    out.println("}");

    context.commit(logger, out);
  }

  return packageName + "." + className;
}
 
Example 11
Source File: Mvp4gRunAsyncGenerator.java    From mvp4g with Apache License 2.0 4 votes vote down vote up
private RebindResult create(JClassType originalType,
                            TreeLogger logger,
                            GeneratorContext context,
                            String typeName)
  throws UnableToCompleteException {

  Date start = new Date();

  String packageName = originalType.getPackage()
                                   .getName();
  String originalClassName           = originalType.getSimpleSourceName();
  String generatedClassName          = originalClassName + "Impl";
  String generatedClassQualifiedName = packageName + "." + generatedClassName;

  // check weather there is a usual version or not.
  if (checkAlreadyGenerated(logger,
                            context,
                            generatedClassQualifiedName)) {
    // Log
    logger.log(TreeLogger.INFO,
               "reuse already generated files",
               null);
    // stop generating
    return new RebindResult(RebindMode.USE_EXISTING,
                            generatedClassQualifiedName);
  }

  logger.log(TreeLogger.INFO,
             "Generating writer for " + packageName + "." + generatedClassName,
             null);

  PrintWriter printWriter = context.tryCreate(logger,
                                              packageName,
                                              generatedClassName);

  ClassSourceFileComposerFactory classFactory = new ClassSourceFileComposerFactory(packageName,
                                                                                   generatedClassName);

  classFactory.addImplementedInterface(originalType.getName());
  String[] classesToImport = getClassesToImport();
  for (String classToImport : classesToImport) {
    classFactory.addImport(classToImport);
  }

  if (printWriter != null) {
    SourceWriter sourceWriter = classFactory.createSourceWriter(context,
                                                                printWriter);
    logger.log(TreeLogger.INFO,
               "Generating source for " + generatedClassQualifiedName + " ",
               null);
    writeClass(sourceWriter,
               getRunAsync(originalType));
    sourceWriter.commit(logger);
  }

  Date end = new Date();

  logger.log(TreeLogger.INFO,
             "Mvp4g Module Cretor: " + (end.getTime() - start.getTime()) + "ms.");

  return new RebindResult(RebindMode.USE_ALL_NEW_WITH_NO_CACHING,
                          generatedClassQualifiedName);
}
 
Example 12
Source File: UiBinderLocalizedCreator.java    From putnami-web-toolkit with GNU Lesser General Public License v3.0 4 votes vote down vote up
private PrintWriter getPrintWriter(TreeLogger logger, GeneratorContext ctx, String targetQualifiedName) {
	String packageName = this.binderType.getPackage().getName();
	String className = this.binderProxySimpleName;
	return ctx.tryCreate(logger, packageName, className);
}
 
Example 13
Source File: JsonAutoBeanGenerator.java    From requestor with Apache License 2.0 4 votes vote down vote up
private SourceWriter getSourceWriter(TreeLogger logger, GeneratorContext ctx, JClassType intfType) {
    JPackage serviceIntfPkg = intfType.getPackage();
    String packageName = serviceIntfPkg == null ? "" : serviceIntfPkg.getName();
    PrintWriter printWriter = ctx.tryCreate(logger, packageName, getTypeSimpleName());
    if (printWriter == null) {
        return null;
    }

    ClassSourceFileComposerFactory composerFactory =
            new ClassSourceFileComposerFactory(packageName, getTypeSimpleName());

    String[] imports = new String[]{
            // java.util
            ArrayList.class.getCanonicalName(),
            Collection.class.getCanonicalName(),
            List.class.getCanonicalName(),
            Iterator.class.getCanonicalName(),
            Set.class.getCanonicalName(),
            // com.google.gwt.core.client
            GWT.class.getCanonicalName(),
            // com.google.web.bindery.autobean.shared
            AutoBean.class.getCanonicalName(),
            AutoBeanCodex.class.getCanonicalName(),
            AutoBeanFactory.class.getCanonicalName(),
            AutoBeanUtils.class.getCanonicalName(),
            // io.reinert.requestor.serialization
            DeserializationContext.class.getCanonicalName(),
            Deserializer.class.getCanonicalName(),
            HasImpl.class.getCanonicalName(),
            Serdes.class.getCanonicalName(),
            Serializer.class.getCanonicalName(),
            SerializationContext.class.getCanonicalName(),
            UnableToDeserializeException.class.getName(),
            UnableToSerializeException.class.getName(),
            // io.reinert.requestor.serialization.json
            JsonObjectSerdes.class.getCanonicalName(),
            JsonRecordReader.class.getCanonicalName(),
            JsonRecordWriter.class.getCanonicalName(),
    };

    for (String imp : imports) {
        composerFactory.addImport(imp);
    }

    composerFactory.addImplementedInterface(intfType.getErasedType().getQualifiedSourceName());

    return composerFactory.createSourceWriter(ctx, printWriter);
}
 
Example 14
Source File: JsonGwtJacksonGenerator.java    From requestor with Apache License 2.0 4 votes vote down vote up
private SourceWriter getSourceWriter(TreeLogger logger, GeneratorContext ctx, JClassType intfType) {
    JPackage serviceIntfPkg = intfType.getPackage();
    String packageName = serviceIntfPkg == null ? "" : serviceIntfPkg.getName();
    PrintWriter printWriter = ctx.tryCreate(logger, packageName, getTypeSimpleName());
    if (printWriter == null) {
        return null;
    }

    ClassSourceFileComposerFactory composerFactory =
            new ClassSourceFileComposerFactory(packageName, getTypeSimpleName());

    String[] imports = new String[]{
            // java
            ArrayList.class.getCanonicalName(),
            Collection.class.getCanonicalName(),
            HashSet.class.getCanonicalName(),
            Iterator.class.getCanonicalName(),
            LinkedHashSet.class.getCanonicalName(),
            LinkedList.class.getCanonicalName(),
            List.class.getCanonicalName(),
            Set.class.getCanonicalName(),
            TreeSet.class.getCanonicalName(),
            // com.github.nmorel.gwtjackson
            ObjectMapper.class.getCanonicalName(),
            ObjectReader.class.getCanonicalName(),
            ObjectWriter.class.getCanonicalName(),
            // com.google.gwt
            GWT.class.getCanonicalName(),
            // io.reinert.requestor
            DeserializationContext.class.getCanonicalName(),
            Deserializer.class.getCanonicalName(),
            JsonObjectSerdes.class.getCanonicalName(),
            JsonRecordReader.class.getCanonicalName(),
            JsonRecordWriter.class.getCanonicalName(),
            UnableToDeserializeException.class.getName(),
            UnableToSerializeException.class.getName(),
            Serdes.class.getCanonicalName(),
            Serializer.class.getCanonicalName(),
            SerializationContext.class.getCanonicalName()
    };

    for (String imp : imports) {
        composerFactory.addImport(imp);
    }

    composerFactory.addImplementedInterface(intfType.getErasedType().getQualifiedSourceName());

    return composerFactory.createSourceWriter(ctx, printWriter);
}
 
Example 15
Source File: ApplicationMetaDataGenerator.java    From core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Generate source code for new class. Class extends
 * <code>HashMap</code>.
 *
 * @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);
}